Documentation

CslibFrontier.Crypto.Computational.LWE.KeyGen

Generic LWE key generation, sampling, and serialization #

Uniform power-of-two residue sampling, centered-binomial error sampling, LWE public matrix generation [B * t + e | B], flat uniform random tapes, serialized list/word codecs, exact distributional bridges to DecisionLWE, and uniform strict polynomial-time certificates.

Main definitions #

Main results #

References #

Secret vectors and LWE matrix identities #

def Cslib.Crypto.LWE.secretVector {n w : ℕ} (t : Fin n → ZMod (2 ^ w)) :
Fin (n + 1) → ZMod (2 ^ w)

The secret vector has leading coordinate one, followed by the negated secret entries.

theorem Cslib.Crypto.LWE.publicMatrix_mulVec_secretVector {n m w : ℕ} (B : Matrix (Fin m) (Fin n) (ZMod (2 ^ w))) (t : Fin n → ZMod (2 ^ w)) (error : Fin m → ZMod (2 ^ w)) :
(publicMatrix B t error).mulVec (secretVector t) = error

Multiplying [B * t + error | B] by (1, -t) recovers the modular error vector.

def Cslib.Crypto.LWE.publicMatrixInt {n m w : ℕ} (B : Matrix (Fin m) (Fin n) (ZMod (2 ^ w))) (t : Fin n → ZMod (2 ^ w)) (error : Fin m → ℤ) :
Matrix (Fin m) (Fin (n + 1)) (ZMod (2 ^ w))

The LWE public matrix [B * t + error | B], with integer errors cast modulo 2 ^ w.

theorem Cslib.Crypto.LWE.publicMatrixInt_mulVec_secretVector {n m w : ℕ} (B : Matrix (Fin m) (Fin n) (ZMod (2 ^ w))) (t : Fin n → ZMod (2 ^ w)) (error : Fin m → ℤ) :
(publicMatrixInt B t error).mulVec (secretVector t) = fun (i : Fin m) => ↑(error i)

Multiplying [B * t + error | B] by (1, -t) recovers the cast integer errors.

Typed LWE key sampling #

noncomputable def Cslib.Crypto.LWE.sampleResidue (w : ℕ) :
ProbComp (ZMod (2 ^ w))

Sample a uniformly random residue modulo 2 ^ w from w fair bits.

@[simp]

A uniform residue sample has the uniform finite-ring law modulo 2 ^ w.

noncomputable def Cslib.Crypto.LWE.sampleVector (count w : ℕ) :
ProbComp (Fin count → ZMod (2 ^ w))

Sample a uniformly random residue vector of length count modulo 2 ^ w.

@[simp]

A uniform residue vector sample has the uniform product law modulo 2 ^ w.

noncomputable def Cslib.Crypto.LWE.sampleMatrix (rows cols w : ℕ) :
ProbComp (Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w)))

Sample a uniformly random rows × cols matrix modulo 2 ^ w.

@[simp]
theorem Cslib.Crypto.LWE.eval_sampleMatrix (rows cols w : ℕ) :
(sampleMatrix rows cols w).eval = Probability.PMF.uniformOfFintype (Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w)))

A uniform matrix sample has the uniform finite-matrix law modulo 2 ^ w.

structure Cslib.Crypto.LWE.KeyInputs (n m w : ℕ) :

The separately sampled secret, public-matrix part, and integer errors.

  • secret : Fin n → ZMod (2 ^ w)

    Uniform secret entries.

  • publicPart : Matrix (Fin m) (Fin n) (ZMod (2 ^ w))

    Uniform public matrix entries.

  • error : Fin m → ℤ

    Independent bounded integer errors.

noncomputable def Cslib.Crypto.LWE.sampleKeyInputs (n m w η : ℕ) :

Draw the three inputs independently, using the specified finite centered-binomial error law.

theorem Cslib.Crypto.LWE.eval_sampleKeyInputs (n m w η : ℕ) :
(sampleKeyInputs n m w η).eval = do let secret ← Probability.PMF.uniformOfFintype (Fin n → ZMod (2 ^ w)) let publicPart ← Probability.PMF.uniformOfFintype (Matrix (Fin m) (Fin n) (ZMod (2 ^ w))) let error ← ProbComp.eval (OracleComp.replicateFin m (ProbComp.sampleCenteredBinomial η)) pure { secret := secret, publicPart := publicPart, error := error }

The complete joint law retains the independence of the secret, public part, and errors.

theorem Cslib.Crypto.LWE.mem_support_sampleKeyInputs_iff (n m w η : ℕ) (inputs : KeyInputs n m w) :
inputs ∈ (sampleKeyInputs n m w η).eval.support ↔ ∀ (i : Fin m), (inputs.error i).natAbs ≤ η

Every secret and public part is possible; the exact error restriction is coordinatewise.

noncomputable def Cslib.Crypto.LWE.keyGen (n m w η : ℕ) :
ProbComp (Matrix (Fin m) (Fin (n + 1)) (ZMod (2 ^ w)) × (Fin n → ZMod (2 ^ w)))

Generate an LWE public matrix [B * t + e | B] and retain the sampled secret t.

Flat uniform tapes for key sampling #

A residue uses a uniform word of exactly its binary width.

theorem Cslib.Crypto.LWE.exists_uniform_seed_sampleVector (count w : ℕ) :
∃ (evaluate : List Bool → Fin count → ZMod (2 ^ w)), (sampleVector count w).eval = PMF.map evaluate (BitString.uniformList (count * w))

Independent residues use disjoint width-sized blocks.

theorem Cslib.Crypto.LWE.exists_uniform_seed_sampleMatrix (rows cols w : ℕ) :
∃ (evaluate : List Bool → Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w))), (sampleMatrix rows cols w).eval = PMF.map evaluate (BitString.uniformList (rows * (cols * w)))

Independent rows use disjoint row-sized blocks.

theorem Cslib.Crypto.LWE.exists_uniform_seed_sampleKeyInputs (n m w η : ℕ) :
∃ (evaluate : List Bool → KeyInputs n m w), (sampleKeyInputs n m w η).eval = PMF.map evaluate (BitString.uniformList (n * w + m * (n * w) + m * (2 * η)))

All key-generation inputs are independent functions of disjoint parts of one finite tape.

Serialized random inputs, key serialization, and Decision-LWE bridges #

Secret entries, public-matrix rows, and signed errors, without dependent dimensions.

  • secret : List ℕ

    List of secret vector residue representatives.

  • publicPart : List (List ℕ)

    List of public-matrix rows (m rows of n residue representatives).

  • error : List ℤ

    List of signed integer errors.

Instances For
    theorem Cslib.Crypto.LWE.SerializedKeyInputs.ext {x y : SerializedKeyInputs} (secret : x.secret = y.secret) (publicPart : x.publicPart = y.publicPart) (error : x.error = y.error) :
    x = y
    theorem Cslib.Crypto.LWE.encSerializedKeyInputs_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 : γ → List ℕ} {T0 S0 : γ → ℕ} {x1 : γ → List (List ℕ)} {T1 S1 : γ → ℕ} {x2 : γ → List ℤ} {T2 S2 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInO x0 encIn (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encNat) T0 S0) (h1 : Turing.MultiTapeTM.ComputableInO x1 encIn (Turing.MultiTapeTM.encList (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encNat)) T1 S1) (h2 : Turing.MultiTapeTM.ComputableInO x2 encIn (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encIntBin) T2 S2) :
    Turing.MultiTapeTM.ComputableInO (fun (x : γ) => { secret := x0 x, publicPart := x1 x, error := x2 x }) encIn encSerializedKeyInputs (fun (x : γ) => T0 x + (T1 x + T2 x)) fun (x : γ) => S0 x + (S1 x + S2 x)

    Binary residues and signed binary errors, with all list and pair framing.

    Independently sample every key-input entry using binary residues and signed errors.

    List every residue's canonical representative and every signed error in coordinate order.

    Read fixed dimensions from arbitrary lists, padding absent entries with zero and reducing natural representatives modulo 2 ^ w. Excess entries are ignored.

    @[simp]
    theorem Cslib.Crypto.LWE.decode_serializeKeyInputs {n m w : ℕ} (inputs : KeyInputs n m w) :
    decodeKeyInputs n m w (serializeKeyInputs inputs) = inputs

    Canonical lists decode to their original typed key inputs, including empty dimensions.

    The efficient key-input sampler has exactly the typed sampler's joint law, including the independence of the secret, public-matrix part, and signed errors.

    Decoding the efficient sampled lists recovers the mathematical key-input sampler.

    def Cslib.Crypto.LWE.serializePublicMatrix {n m w : ℕ} (A : Matrix (Fin m) (Fin (n + 1)) (ZMod (2 ^ w))) :

    Canonical public matrices use the generic fixed-width row-major representation.

    def Cslib.Crypto.LWE.serializeSecret {n w : ℕ} (secret : Fin n → ZMod (2 ^ w)) :

    Canonical secret residues are consecutive low-order-first words.

    def Cslib.Crypto.LWE.decodeSecret (n w : ℕ) (word : List Bool) :
    Fin n → ZMod (2 ^ w)

    Total secret decoding reads the declared number of residue words, padding with zero bits.

    @[simp]
    theorem Cslib.Crypto.LWE.decode_serializeSecret {n w : ℕ} (secret : Fin n → ZMod (2 ^ w)) :
    decodeSecret n w (serializeSecret secret) = secret

    Canonical secret words retain every residue.

    def Cslib.Crypto.LWE.secretProductResidue (n w row : ℕ) (secretWord ciphertextWord : List Bool) :

    Compute the canonical residue modulo 2 ^ w of row row of an (n + 1)-column binary matrix ciphertextWord multiplied by secretVector (decodeSecret n w secretWord) using signed binary subtraction.

    @[simp]
    theorem Cslib.Crypto.LWE.secretProductResidue_zero (n row : ℕ) (secretWord ciphertextWord : List Bool) :
    secretProductResidue n 0 row secretWord ciphertextWord = 0

    At width zero, secretProductResidue reduces modulo 1 and returns 0.

    theorem Cslib.Crypto.LWE.secretProductResidue_eq (n w row : ℕ) (secretWord ciphertextWord : List Bool) :
    secretProductResidue n w row secretWord ciphertextWord = (↑(Matrix.binaryEntry (n + 1) w ciphertextWord row 0) - ∑ j : Fin n, ↑(Matrix.binaryEntry (n + 1) w ciphertextWord row (↑j + 1)) * decodeSecret n w secretWord j).val

    Signed word arithmetic computes the canonical residue of row row paired with (1, -secret).

    theorem Cslib.Crypto.LWE.secretProductResidue_eq_dotProduct {r : ℕ} (n w : ℕ) (row : Fin r) (secretWord ciphertextWord : List Bool) :
    secretProductResidue n w (↑row) secretWord ciphertextWord = (Matrix.ofBinary r (n + 1) w ciphertextWord row ⬝ᵥ secretVector (decodeSecret n w secretWord)).val

    For row : Fin r, secretProductResidue computes the canonical residue of row row of Matrix.ofBinary r (n + 1) w ciphertextWord dotted with secretVector (decodeSecret n w secretWord).

    @[simp]
    theorem Cslib.Crypto.LWE.length_serializePublicMatrix {n m w : ℕ} (A : Matrix (Fin m) (Fin (n + 1)) (ZMod (2 ^ w))) :

    A serialized public matrix has exact bit length m * (n + 1) * w.

    @[simp]
    theorem Cslib.Crypto.LWE.length_serializeSecret {n w : ℕ} (secret : Fin n → ZMod (2 ^ w)) :
    (serializeSecret secret).length = n * w

    A serialized secret vector has exact bit length n * w.

    Compute the leading entry B * secret + error of a public row in binary arithmetic.

    Compute the public word from arbitrary serialized sampled inputs.

    Normalize and serialize an arbitrary secret list to the declared dimensions.

    Sample random key inputs and run the actual serialized public-key computation.

    theorem Cslib.Crypto.LWE.publicRowHead_eq {n m w : ℕ} (inputs : SerializedKeyInputs) (i : Fin m) :
    publicRowHead n w inputs ↑i = (publicMatrixInt (decodeKeyInputs n m w inputs).publicPart (decodeKeyInputs n m w inputs).secret (decodeKeyInputs n m w inputs).error i 0).val

    The binary signed-remainder computation returns the canonical leading public residue.

    The total public-key computation is exactly the typed public matrix of the decoded inputs.

    theorem Cslib.Crypto.LWE.secretSerialized_eq (n w : ℕ) (secret : List ℕ) :
    secretSerialized n w secret = serializeSecret fun (i : Fin n) => ↑(secret.getD (↑i) 0)

    The total secret computation agrees with the same padded and modularly normalized inputs.

    Canonical sampled inputs produce exactly the original typed public key.

    Canonical sampled inputs produce exactly the original typed secret key.

    Every arbitrary input produces the complete declared public word.

    theorem Cslib.Crypto.LWE.length_secretSerialized (n w : ℕ) (secret : List ℕ) :
    (secretSerialized n w secret).length = n * w

    Secret normalization retains exactly the declared number of residue bits.

    theorem Cslib.Crypto.LWE.eval_keyGenSerialized (n m w η : ℕ) :
    (keyGenSerialized n m w η).eval = PMF.map (fun (keys : Matrix (Fin m) (Fin (n + 1)) (ZMod (2 ^ w)) × (Fin n → ZMod (2 ^ w))) => (serializePublicMatrix keys.1, serializeSecret keys.2)) (keyGen n m w η).eval

    The efficient key sampler retains the typed construction's complete public/secret joint law.

    Decision-LWE error law and public-word ensembles #

    noncomputable def Cslib.Crypto.LWE.noiseLaw (w η : ℕ) :
    PMF (ZMod (2 ^ w))

    Reduce the difference of two independent η-bit counts modulo 2 ^ w.

    theorem Cslib.Crypto.LWE.eval_errorVector_mod (m w η : ℕ) :
    PMF.map (fun (error : Fin m → ℤ) (i : Fin m) => ↑(error i)) (ProbComp.eval (OracleComp.replicateFin m (ProbComp.sampleCenteredBinomial η))) = Probability.PMF.pi fun (x : Fin m) => noiseLaw w η

    Reducing independent integer errors preserves the complete independent modular error law.

    The generated public matrix is an exact decision-LWE sample.

    theorem Cslib.Crypto.LWE.eval_keyGen_publicWord (n m w η : ℕ → ℕ) (security : ℕ) :
    PMF.map (fun (keys : Matrix (Fin (m security)) (Fin (n security + 1)) (ZMod (2 ^ w security)) × (Fin (n security) → ZMod (2 ^ w security))) => Matrix.toBinary keys.1) (keyGen (n security) (m security) (w security) (η security)).eval = realEnsemble n m w (fun (k : ℕ) => noiseLaw (w k) (η k)) security

    Encoding the actual public marginal gives the encoded real matrix realEnsemble of DecisionLWE (decisionLWE_iff_encoded).

    theorem Cslib.Crypto.LWE.eval_keyGenSerialized_publicWord (n m w η : ℕ → ℕ) (security : ℕ) :
    PMF.map Prod.fst (keyGenSerialized (n security) (m security) (w security) (η security)).eval = realEnsemble n m w (fun (k : ℕ) => noiseLaw (w k) (η k)) security

    The actual serialized key generator has precisely the public marginal realEnsemble of DecisionLWE.

    Uniform polynomial-time certificates for LWE key generation #

    Compute a public row's leading residue from arbitrary binary inputs and signed errors.

    Compute the modular row inner product secretProductResidue in polynomial time.