Documentation

CslibFrontier.Crypto.Computational.MLDSA.FIPS204Signing

Definitions of FIPS 204 signing and the strong chosen-message forgery experiment #

FIPS 204, Algorithms 2, 6 and 7. The external signer accepts the encoded private key, a bit-string message, a byte context and 32 bytes of randomness. A zero randomness string selects deterministic signing. The bounded loop uses the 814-attempt option of Appendix C. The adaptive strong-unforgeability experiment draws fresh hedging randomness for each signature and records the context, message and exact bytes of every successful reply. Its sole external service is signing. The sufCmaGame specialization evaluates FIPS 202 SHAKE in both signing and verification. Its zero-signing-query lemma recovers the byte-level key-only experiment.

verify_of_sign proves that every successful signature under a generated key passes Algorithm 3. On a fresh verifying triple whose digest occurs in the log, the extractor finds a previous reply and computes two colliding SHAKE256 inputs or an integer MSIS vector.

refreshFirstRequest_sufCmaGame gives a classical resampling identity for the first request and its first internal attempt. The requester may use the known SHAKE256 prefix at every raw address. The signer consumes the refreshed Algorithm 29 bytes and uses the resulting table through its retries, later signing requests and final verification.

@[reducible, inline]

Bytes consumed by Algorithm 34 for one mask polynomial, 576 at ML-DSA-44 and 640 at ML-DSA-65 and ML-DSA-87.

The Algorithm 34 mask at counter κ from private bytes indexed by its two-byte nonce.

def Cslib.Crypto.MLDSA.FIPS204.Xofs.expandMask (H : Xofs) (s : Level) (seed : List Byte) (κ : ℕ) :
Vector (Vector (ZMod 8380417) 256) s.params.ℓ

ExpandMask (Algorithm 34). Each mask polynomial absorbs the same seed and its own two-byte little-endian counter, and decodes 256 coefficients in [1 − γ₁, γ₁].

@[reducible, inline]
abbrev Cslib.Crypto.MLDSA.FIPS204.expandMask (s : Level) (seed : List Byte) (κ : ℕ) :
Vector (Vector (ZMod 8380417) 256) s.params.ℓ

FIPS 202 specialization of Xofs.expandMask.

def Cslib.Crypto.MLDSA.FIPS204.finishSigningAttempt (s : Level) (sk : PrivateKey s) (y : Fin s.params.ℓ → Poly) (w : Fin s.params.k → Poly) (digest : List Byte) (challenge : Option Poly) :

Complete Algorithm 7 using the sampled challenge and digest. XOF exhaustion is fatal, a failed norm or hint check rejects the mask, and success returns the encoded signature.

theorem Cslib.Crypto.MLDSA.FIPS204.length_finishSigningAttempt {s : Level} {sk : PrivateKey s} {y : Fin s.params.ℓ → Poly} {w : Fin s.params.k → Poly} {digest : List Byte} {challenge : Option Poly} {sig : List Byte} (hd : digest.length = digestBytes s) (h : finishSigningAttempt s sk y w digest challenge = some (some sig)) :

The length of every successful reply, including for decoded keys outside KeyGen's image.

def Cslib.Crypto.MLDSA.FIPS204.Xofs.signingRetries (H : Xofs) (s : Level) (sk : PrivateKey s) (A : PublicMatrix s) (μ : List Bool) (mask : ℕ → Vector (Vector (ZMod 8380417) 256) s.params.ℓ) :

Algorithm 7's 814-attempt loop with an explicit mask at each retry counter. The ambient XOF supplies the commitment digests and sparse challenges.

Raw SHAKE256 input for the private mask seed in Algorithm 7, line 7.

theorem Cslib.Crypto.MLDSA.FIPS204.length_maskSeedInput (key : List Byte) (rnd : FixedBytes 32) (μ : List Bool) :
(maskSeedInput key rnd μ).length = 8 * (key.length + 32) + μ.length

The 32-byte hedge contributes 256 bits between the key and representative.

def Cslib.Crypto.MLDSA.FIPS204.Xofs.signInternal (H : Xofs) (s : Level) (bytes : List Byte) (formatted : List Bool) (rnd : FixedBytes 32) :

Algorithm 7 on encoded secret keys, formatted bit-string messages and 32 hedging bytes. The signer distinguishes a rejected attempt (advance κ by ℓ) from XOF exhaustion (abort). After 814 rejected attempts it returns the single error of Appendix C.

@[reducible, inline]
abbrev Cslib.Crypto.MLDSA.FIPS204.signInternal (s : Level) (bytes : List Byte) (formatted : List Bool) (rnd : FixedBytes 32) :

FIPS 202 specialization of Xofs.signInternal.

def Cslib.Crypto.MLDSA.FIPS204.Xofs.sign (H : Xofs) (s : Level) (sk : List Byte) (message : List Bool) (ctx : List Byte) (rnd : FixedBytes 32) :

Pure ML-DSA.Sign (Algorithm 2) with explicit hedging randomness. The deterministic variant uses 32 zero bytes. Contexts longer than 255 bytes return an error.

@[reducible, inline]
abbrev Cslib.Crypto.MLDSA.FIPS204.sign (s : Level) (sk : List Byte) (message : List Bool) (ctx : List Byte) (rnd : FixedBytes 32) :

FIPS 202 specialization of Xofs.sign.

ExpandMask (Algorithm 34). Each of the ℓ rows queries the same seed with its own two-byte little-endian counter. All rows share their raw XOF with every other signing role.

@[simp]

Evaluating expandMaskReads against a stream answerer computes Xofs.expandMask.

def Cslib.Crypto.MLDSA.FIPS204.signingQueries {R : Type} (s : Level) (sk : PrivateKey s) (A : PublicMatrix s) (μ : List Bool) (seed : List Byte) (reply : Option (List Byte) → R) :

The capped signing loop. Each attempt reads all ExpandMask rows, the commitment digest and the 221 SampleInBall bytes. Every request goes to one raw SHAKE256 interface.

def Cslib.Crypto.MLDSA.FIPS204.Xofs.signInternalReads {R : Type} (H : Xofs) (s : Level) (key : List Byte) (formatted : List Bool) (rnd : FixedBytes 32) (reply : Option (List Byte) → R) :

The full internal signer. Matrix expansion uses SHAKE128; every use of SHAKE256, from the message representative and masking seed through the rejection loop, is an oracle call.

theorem Cslib.Crypto.MLDSA.FIPS204.signingQueries_bounded {R : Type} (s : Level) (sk : PrivateKey s) (A : PublicMatrix s) (μ : List Bool) (seed : List Byte) (reply : Option (List Byte) → R) (fuel κ : ℕ) :
OracleComp.IsQueryBounded (signingQueries s sk A μ seed reply fuel κ) ((s.params.ℓ + 2) * fuel)

Each FIPS trial uses ℓ mask-row reads, one commitment read and one challenge-expansion read. Rejection and fatal expansion consume these reads before the result is examined.

theorem Cslib.Crypto.MLDSA.FIPS204.Xofs.signInternalReads_bounded {R : Type} (H : Xofs) (s : Level) (key : List Byte) (formatted : List Bool) (rnd : FixedBytes 32) (reply : Option (List Byte) → R) :
OracleComp.IsQueryBounded (H.signInternalReads s key formatted rnd reply) ((s.params.ℓ + 2) * 814 + 2)

Two prelude reads for the representative and hedging seed, followed by at most 814 trials.

theorem Cslib.Crypto.MLDSA.FIPS204.evalPure_signingQueries {R : Type} (H : Xofs) (s : Level) (sk : PrivateKey s) (A : PublicMatrix s) (μ : List Bool) (seed : List Byte) (reply : Option (List Byte) → R) (fuel κ : ℕ) :

Evaluating signingQueries against a stream answerer applies reply to the id evaluation of the same loop.

theorem Cslib.Crypto.MLDSA.FIPS204.evalPure_signingQueries_succ {R : Type} (H : Xofs) (s : Level) (sk : PrivateKey s) (A : PublicMatrix s) (μ : List Bool) (seed : List Byte) (reply : Option (List Byte) → R) (fuel κ : ℕ) :
OracleComp.evalPure (ExtendableOutput.Request.answer H.h256) (signingQueries s sk A μ seed reply (fuel + 1) κ) = have y := ringVectorCoeffs (H.expandMask s seed κ); have w := moduleSISMap A y; have d := H.commitmentDigest s (highBitsVec s.params w) μ; match finishSigningAttempt s sk y w d (H.sampleInBall s d) with | none => reply none | some (some sig) => reply (some sig) | some none => OracleComp.evalPure (ExtendableOutput.Request.answer H.h256) (signingQueries s sk A μ seed reply fuel (κ + s.params.ℓ))

Evaluating one trial of signingQueries executes finishSigningAttempt on the ambient XOF's mask, commitment digest, and challenge bytes, terminating on failure or acceptance and advancing the counter by ℓ on mask rejection.

theorem Cslib.Crypto.MLDSA.FIPS204.Xofs.evalPure_signInternalReads_of_h128 {R : Type} (B H : Xofs) (h128 : B.h128 = H.h128) (s : Level) (key : List Byte) (formatted : List Bool) (rnd : FixedBytes 32) (reply : Option (List Byte) → R) :
OracleComp.evalPure (ExtendableOutput.Request.answer H.h256) (B.signInternalReads s key formatted rnd reply) = reply (H.signInternal s key formatted rnd)

Evaluating signInternalReads against a stream answerer applies reply to Xofs.signInternal whenever both XOF pairs agree on SHAKE128.

Every accepted output of signingQueries at id has length signatureBytes s.

theorem Cslib.Crypto.MLDSA.FIPS204.Xofs.length_signInternal {H : Xofs} {s : Level} {key : List Byte} {formatted : List Bool} {rnd : FixedBytes 32} {sig : List Byte} (h : H.signInternal s key formatted rnd = some sig) :

Every signature returned by Xofs.signInternal has length signatureBytes s, including for decoded keys outside keyGen's image.

theorem Cslib.Crypto.MLDSA.FIPS204.verify_of_sign {H : Xofs} {s : Level} {seed : FixedBytes 32} {keys : KeyPair} {f : Forgery} {rnd : FixedBytes 32} (hg : H.keyGen s seed = some keys) (hs : H.sign s keys.secretKey f.message f.context rnd = some f.signature) :

For any fixed pair of XOFs, Algorithm 3 accepts every successful signature produced under a key generated with those same XOFs. This includes deterministic and hedged randomness.

A context and bit-string message chosen by an external signing caller.

  • context : List Byte

    The external context bytes.

  • message : List Bool

    The original message bits.

@[reducible, inline]

An adaptive randomized adversary whose signing calls may return the FIPS error.

Keep each successful signature with the exact context and message presented to the signer.

FIPS verifies the output triple and no signing call returned that triple.

Sign with fresh hedging randomness and log the context and message of a successful reply.

Run the forger against the actual signed responses with 32 independent byte draws per request. The program returns the adversary's output and the full successful signing log.

The closed program produces the same joint transcript as the signing oracle in the strong chosen-message experiment.

@[reducible, inline]

Signing service with the FIPS 202 XOFs.

theorem Cslib.Crypto.MLDSA.FIPS204.signingOracle_preserves_verified {H : Xofs} {s : Level} {seed : FixedBytes 32} {keys : KeyPair} (hg : H.keyGen s seed = some keys) (q : SigningQuery) (hist : List Forgery) (ih : ∀ g ∈ hist, H.verify s keys.publicKey g.message g.context g.signature = some true) {reply : Option (List Byte)} {next : List Forgery} (hs : (reply, next) ∈ (H.signingOracle s keys.secretKey q hist).support) (g : Forgery) :
g ∈ next → H.verify s keys.publicKey g.message g.context g.signature = some true

A hedged signing call under a generated key preserves verification of every logged triple.

theorem Cslib.Crypto.MLDSA.FIPS204.verify_of_mem_signingTranscript {H : Xofs} {s : Level} {seed : FixedBytes 32} {keys : KeyPair} {adv : SigningForger} {out f : Forgery} {log : List Forgery} (hg : H.keyGen s seed = some keys) (hrun : (out, log) ∈ (ProbOracleComp.runWith (H.signingOracle s keys.secretKey) (adv keys.publicKey) []).support) (hf : f ∈ log) :

Every signature in the actual adaptive signing transcript passes the FIPS byte verifier under the public key returned by the same seed.

Search the actual signing log for a reply sharing the forgery's digest, then extract from the two triples using the public bytes and the matrix supplied to the solver.

theorem Cslib.Crypto.MLDSA.FIPS204.extractLoggedDigest_of_verifiedLog {H : Xofs} {s : Level} {pk : List Byte} {A : PublicMatrix s} {out : Forgery} {log : List Forgery} (matrix : H.expandA s (List.take 32 pk) = some A) (logged : ∀ g ∈ log, H.verify s pk g.message g.context g.signature = some true) (fresh : out ∉ log) (repeated : ∃ g ∈ log, List.take (digestBytes s) g.signature = List.take (digestBytes s) out.signature) (valid : H.verify s pk out.message out.context out.signature = some true) :

A fresh verifying triple and a verified signing log at the same public matrix give a computed witness whenever the full challenge digest occurs in the log.

theorem Cslib.Crypto.MLDSA.FIPS204.extractLoggedDigest_of_signingTranscript {H : Xofs} {s : Level} {seed : FixedBytes 32} {keys : KeyPair} {adv : SigningForger} {out : Forgery} {log : List Forgery} (hg : H.keyGen s seed = some keys) (hrun : (out, log) ∈ (ProbOracleComp.runWith (H.signingOracle s keys.secretKey) (adv keys.publicKey) []).support) (fresh : out ∉ log) (repeated : ∃ g ∈ log, List.take (digestBytes s) g.signature = List.take (digestBytes s) out.signature) (valid : H.verify s keys.publicKey out.message out.context out.signature = some true) :

Every fresh accepted forgery with a previously signed digest yields a valid computed witness. Key generation, the adaptive hedged signing service, verification and extraction use one XOF pair. The public matrix is the one obtained from the seed in the generated public key.

The strong chosen-message forgery experiment for FIPS 204's finite parameter sets: generate both keys from one uniform seed, answer adaptive signing calls with the secret key, and accept a verifying triple precisely when the signer never returned it.

@[reducible, inline]

FIPS 202 specialization of the strong chosen-message game.

Restricting the strong chosen-message experiment to a forger that makes no signing calls recovers the byte-verifier key-only experiment exactly, including key-generation failure.

The finite set of actual Algorithm 29 raw inputs at the parameter set's digest width.

def Cslib.Crypto.MLDSA.FIPS204.Xofs.withExpansionRows (H : Xofs) (s : Level) (seen : List Bool → ℕ) (table : ↥(digestRows s) → FixedBytes 221) :

A 221-byte suffix window after the previous read prefix at each SHAKE256 digest input. SHAKE128 and all other SHAKE256 words retain their background streams.

noncomputable def Cslib.Crypto.MLDSA.FIPS204.Xofs.refreshFirstSign (H : Xofs) (s : Level) (seen : List Bool → ℕ) (bytes : List Byte) (q : SigningQuery) (rnd : FixedBytes 32) (table : ↥(digestRows s) → FixedBytes 221) :
PMF ((↥(digestRows s) → FixedBytes 221) × Option (List Byte))

Answer the distinguished first request by feeding fresh bytes into its actual first Algorithm 29 expansion. The returned table is used by all later signing calls and verification. Malformed requests return the FIPS error. A rejected response retries with the next counter.

@[reducible, inline]

The continuation of an actual signing request. It receives the byte reply or failure and can make more randomized, adaptive signing requests before returning its forgery.

Deliver the first signing reply, log its successful triple, then run the remaining chosen-message program and check freshness and verification under the same XOFs.

theorem Cslib.Crypto.MLDSA.FIPS204.refreshFirstRequest_sufCmaGame {State : Type} (H : Xofs) (s : Level) (seen : List Bool → ℕ) (choose : (List Bool → List Byte) → List Byte → ProbComp (SigningQuery × State)) (next : State → SigningContinuation) :
(H.keyExperiment s fun (keys : KeyPair) => do let __x ← (choose (fun (x : List Bool) => H.h256.read x (seen x)) keys.publicKey).eval match __x with | (q, st) => do let rnd ← Probability.PMF.uniformOfFintype (FixedBytes 32) let table ← Probability.PMF.uniformOfFintype (↥(digestRows s) → FixedBytes 221) let __x ← H.refreshFirstSign s seen keys.secretKey q rnd table match __x with | (updated, ans) => (H.withExpansionRows s seen updated).continueFirstSigning s keys q ans (next st)) = (Probability.PMF.uniformOfFintype (↥(digestRows s) → FixedBytes 221)).bind fun (table : ↥(digestRows s) → FixedBytes 221) => have K := H.withExpansionRows s seen table; K.sufCmaGame s fun (pk : List Byte) => do let __x ← ProbOracleComp.sample (choose (fun (x : List Bool) => K.h256.read x (seen x)) pk) match __x with | (q, st) => do let reply ← ProbOracleComp.query q next st K pk reply

Refresh the Algorithm 29 row in the first signing request of a chosen-message run. The adversary chooses the query from the generated public key and earlier raw SHAKE256 prefixes. The oracle draws a uniform hedge. The refreshed bytes drive the first attempt with counter zero. Rejection executes the remaining 813 attempts; success or failure is delivered to the continuation with the actual signing log. Subsequent signing and final verification use the updated table. The raw-prefix view is classical.