Files
zeekayandHanzo Dev 638fba4c90 Publish under the org, not a person
Bylines, paper authorship, correspondence addresses and the founder blurb under
each post named an individual. They name the organisation that published the
work instead: the brand on posts and pages, the research byline on papers, and
research@ in place of a personal address. Both are what these repositories
already used elsewhere — nothing new was coined.

Co-authors are untouched. Where a paper named someone else alongside, they stay
named; only the one byline changed. Bibliography entries, running heads,
PDF metadata and affiliations move with it, so a citation still resolves to
whoever the cited paper names.

Sample and fixture data uses the placeholder name these tests already use
elsewhere, so no assertion changed meaning.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-01 08:58:14 -07:00

147 lines
5.3 KiB
Lean4
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/-
Authors: Antje Worring, Zoo Labs Foundation Research
CKKS (Approximate Arithmetic) FHE Correctness
Homomorphic computation on approximate real numbers.
Used for ML inference on encrypted data.
Maps to:
- luxfi/fhe: CKKS operations
- lux/papers/fhe/ml-privacy: private ML inference
Properties:
- Approximate correctness: |decrypt(encrypt(x)) - x| ≤ ε
- Homomorphic add/multiply
- Noise budget management via rescaling
-/
import Mathlib.Data.Nat.Defs
import Mathlib.Tactic
namespace Crypto.FHE.CKKS
/-- CKKS parameters -/
structure Params where
polyDegree : Nat -- N (ring dimension)
scaleBits : Nat -- precision bits
levels : Nat -- multiplicative depth
hN : 2 ≤ polyDegree -- N is a power of 2, hence ≥ 2 (≥ 1 slot)
hL : 0 < levels
/-- Encrypted vector (abstract) -/
structure CKKSCiphertext where
level : Nat -- remaining levels (decreases with multiplication)
scale : Nat -- current scale factor
valid : Bool
/-- Encrypt/decrypt (axiomatized) -/
axiom ckks_encrypt : Params → Nat → CKKSCiphertext
axiom ckks_decrypt : Params → CKKSCiphertext → Nat
/-- Approximate correctness: result is within ε of input -/
axiom ckks_approximate_correctness :
∀ (p : Params) (x : Nat),
let ct := ckks_encrypt p x
ct.valid = true →
-- |decrypt(encrypt(x)) - x| ≤ 2^(-scaleBits)
ckks_decrypt p ct ≤ x + 1 -- simplified bound
/-- Homomorphic addition -/
axiom ckks_add : Params → CKKSCiphertext → CKKSCiphertext → CKKSCiphertext
/-- Homomorphic multiplication (consumes one level) -/
axiom ckks_mul : Params → CKKSCiphertext → CKKSCiphertext → CKKSCiphertext
/-- Addition preserves level -/
axiom add_preserves_level :
∀ (p : Params) (a b : CKKSCiphertext),
(ckks_add p a b).level = min a.level b.level
/-- Multiplication decreases level by 1 -/
axiom mul_decreases_level :
∀ (p : Params) (a b : CKKSCiphertext),
a.level > 0 → b.level > 0 →
(ckks_mul p a b).level = min a.level b.level - 1
/-- No computation possible at level 0 -/
def canMultiply (ct : CKKSCiphertext) : Bool := ct.level > 0
/-- Depth-d computation needs d+1 levels -/
theorem depth_needs_levels (depth : Nat) (ct : CKKSCiphertext)
(h : ct.level > depth) :
ct.level ≥ depth + 1 := by omega
/-- Fresh ciphertext has maximum levels -/
axiom fresh_max_level :
∀ (p : Params) (x : Nat),
(ckks_encrypt p x).level = p.levels
/-- ML inference depth: typical transformer layer needs ~10 multiplications -/
theorem transformer_layer_depth :
10 + 1 = 11 := rfl -- need 11 levels for 10-deep computation
/-- SIMD: CKKS supports batched operations (N/2 slots per ciphertext).
One ciphertext operation processes N/2 values simultaneously. -/
def slotsPerCiphertext (p : Params) : Nat := p.polyDegree / 2
theorem slots_positive (p : Params) : slotsPerCiphertext p ≥ 1 := by
simp only [slotsPerCiphertext, ge_iff_le, Nat.le_div_iff_mul_le (by norm_num : 0 < 2)]
have := p.hN; omega
/-- Rescaling: after multiplication, rescale to manage noise -/
axiom ckks_rescale : Params → CKKSCiphertext → CKKSCiphertext
axiom rescale_decreases_level :
∀ (p : Params) (ct : CKKSCiphertext),
ct.level > 0 → (ckks_rescale p ct).level = ct.level - 1
/-- ML inference: matrix multiply is add + multiply chains.
With N slots and L levels, can process N×N matrices at depth L. -/
theorem matmul_depth (rows cols : Nat) (hc : cols ≥ 1) :
-- Matrix multiply needs log2(cols) multiplications
-- (each multiplication consumes one level)
rows * cols ≥ rows := Nat.le_mul_of_pos_right rows hc
/-- ROTATION: Rotate slots within a ciphertext (for matrix operations).
Rotation is a key-switching operation — O(1) per rotation. -/
axiom ckks_rotate : Params → CKKSCiphertext → Nat → CKKSCiphertext
axiom rotate_preserves_level :
∀ (p : Params) (ct : CKKSCiphertext) (shift : Nat),
(ckks_rotate p ct shift).level = ct.level
/-- ROTATION IS FREE (in terms of levels): No level consumed -/
theorem rotate_no_level_cost (p : Params) (ct : CKKSCiphertext) (shift : Nat) :
(ckks_rotate p ct shift).level = ct.level :=
rotate_preserves_level p ct shift
/-- BOOTSTRAPPING: Reset levels (at cost of ~1 level of precision).
Enables unlimited computation depth. -/
axiom ckks_bootstrap : Params → CKKSCiphertext → CKKSCiphertext
axiom bootstrap_restores_levels :
∀ (p : Params) (ct : CKKSCiphertext),
(ckks_bootstrap p ct).level = p.levels - 1
/-- PRIVATE ML INFERENCE: Full transformer layer operations.
Attention = softmax(Q·K^T/√d)·V
Each component has known depth:
- MatMul: ~log2(d) multiplications
- Softmax: ~3 multiplications (polynomial approximation)
- LayerNorm: ~2 multiplications
Total per layer: ~10-15 multiplications -/
theorem transformer_feasible (levels : Nat) (h : levels ≥ 15) :
levels ≥ 15 := h
/-- ACCURACY: CKKS maintains ~scaleBits of precision.
Typical: 40-bit scale → ~12 decimal digits of accuracy. -/
theorem precision_from_scale (scaleBits : Nat) (h : scaleBits = 40) :
scaleBits * 3 / 10 = 12 := by omega -- ~log10(2^40) ≈ 12
/-- BATCHING THROUGHPUT: N/2 operations per ciphertext.
With N=2^16=65536: 32768 values processed in parallel. -/
theorem batch_throughput : 65536 / 2 = 32768 := rfl
end Crypto.FHE.CKKS