← Back to section · Post-Quantum Cryptography
ML-DSA's Three Algorithms Dissected: Key Generation, Signing, and Verification End to End
Walk through FIPS 204 in the standard's own order: how a seed expands into a key pair, how the commitment–challenge–response signing flow is built, and why verification convinces you the signer holds the private key.
The ML-DSA specification (FIPS 204) defines three algorithms: KeyGen, Sign, and Verify. They share a set of building blocks — the NTT, samplers that expand seeds into matrices and secret vectors, and the Power2Round/Decompose machinery that splits vectors into high and low parts. This article walks through all three algorithms in the standard’s order; afterwards you should be able to read any ML-DSA implementation and say what each step is doing.
Key generation: everything starts with 32 bytes of seed
KeyGen’s only input is a 32-byte random seed ξ. Hashing it with SHAKE256 (absorbing the dimensions k and l as well, to prevent cross-parameter-set reuse) expands it into three things: a matrix seed ρ, a sampling seed ρ′, and a signing key K, 32 bytes each.
The matrix A (k×l polynomials) is then generated on the fly from ρ via ExpandA — A is never stored and never transmitted, which is the key to compressing the public key to 1.3 KB. The secret vectors s1 (l polynomials) and s2 (k polynomials) are sampled from ρ′ via ExpandS, every coefficient drawn from the tiny range {−η, …, η}. Then compute t = A·s1 + s2: s1 is the real secret, and s2 plays the role of “error,” making it a Module-LWE problem to recover s1 from t.
Finally, split t at 2^13: the high part t1 goes into the public key, the low part t0 into the private key — discarding the low bits adds another layer of rounding noise that further hardens inversion. The public key pk is the encoding of (ρ, t1); the private key sk packages ρ, K, the private-key digest tr (tr = H(pk), 64 bytes, needed at the very start of signing), s1, s2, and t0. Note that K and tr are not “secret vectors,” but they are functional parts of the signing procedure: K derives the per-signature randomness source, and tr binds signatures to this specific public key.
Signing: commitment, challenge, response
What gets signed is not the message M itself but a domain-separated representation M′: in the pure mode, M′ is a zero byte, the length of a context string ctx, ctx itself, and M concatenated. The ctx is chosen by the application (at most 255 bytes) to stop a signature produced under one protocol from being transplanted into another; there is also a pre-hash mode, HashML-DSA, which hashes the message first and writes the hash algorithm’s OID into the representation, suiting streaming or large-message scenarios.
The flow itself has four steps:
- Digest. μ = H(tr ‖ M′), 64 bytes. With tr inside, μ is bound to this public key.
- Mask. Derive ρ″ from K, μ, and a 32-byte random value rnd, then generate the mask polynomials y one by one with ExpandMask. Taking rnd as real randomness gives the hedged variant; all-zero rnd gives the deterministic variant (this column has a dedicated article on the choice). The commitment is w = A·y; take its high part w1.
- Challenge. c̃ = H(μ ‖ encoding of w1), and SampleInBall turns c̃ into a sparse polynomial c with exactly τ coefficients equal to ±1 and the rest zero. This is where the Fiat–Shamir transform lands: the challenge that a verifier would have sent randomly in an interactive protocol is, in a non-interactive signature, produced by the hash function “asking itself.”
- Response and checks. z = y + c·s1. If z’s coefficients are too large (which would leak information about s1), abort, advance the counter, and start over — this is rejection sampling. Also check that the low part of w − c·s2 stays within bounds and that c·t0 is not too large; once all pass, use MakeHint to produce the hint h, which supplies, in the fewest bytes possible, the low-order information the verifier is missing. The final signature is σ = (c̃, z, h).
The whole loop needs only a few iterations on average — but that word “average” is exactly where implementations must be careful: the iteration count correlates with the secret, and side-channel protection gets its own article.
The supporting cast: samplers, the NTT, and high/low splitting
Four recurring components make the three algorithms run, and they are worth meeting individually. First, the expanders: ExpandA unfolds the 32-byte ρ position by position into all of matrix A, ExpandS unfolds ρ′ into small-coefficient secrets, and ExpandMask unfolds masks by round counter — all driven by SHAKE hash streams, essentially “deterministically generating large amounts of seemingly random structured material from a short seed.” Second, SampleInBall: it samples a sparse polynomial with exactly τ nonzero coefficients from the challenge seed, and it sets the size of the challenge space — the probability a forger must guess. Third, the NTT: naive polynomial multiplication is a 256² convolution; the Number Theoretic Transform brings it close to linear, and every matrix–vector product like A·y and A·z depends on it; implementations keep polynomials resident in NTT form, converting back to plain coefficients only for sampling and encoding. Fourth, the high/low splitting family: Power2Round splits t into t1 and t0 at key generation, and Decompose/HighBits/LowBits do the same to the commitment during signing and verification — the correctness of the whole algorithm is, at heart, the single idea “the high parts must line up, and the low parts must not get in the way,” applied at different stages. Understand these four components and the dozens of pages of pseudocode in FIPS 204 reduce to orchestration.
Verification: rebuilding the commitment from public data alone
The verifier holds pk, M, and σ. It recomputes μ, unpacks ρ and t1 from pk, rebuilds the matrix A, and computes w′ = A·z − c·t1·2^13. The reasoning: A·z contains both A·y and A·c·s1, while c·t1·2^13 approximates c·(t − t0) ≈ c·A·s1 + c·s2; subtracting leaves w′ close to the original commitment w, up to low-order noise. The hint h in the signature then corrects w′ via UseHint to recover w1′, and the verifier finally checks that H(μ ‖ encoding of w1′) equals the c̃ in the signature, and that z’s norm is in bounds. Both pass, and the signature is valid.
Verification needs no secret; it relies on an asymmetry of direction: computing forward (with s1) is easy, while peeling s1 out of t is a hard lattice problem. Read end to end, ML-DSA’s design philosophy is clear — compress everything with seeds, replace interaction with hashing, and use rejection sampling plus hints to strike a fine three-way balance among security, correctness, and size.
Two decisions at the interface layer
Finally, two decisions that application integration cannot dodge. First, what to put in the context string ctx: FIPS 204 leaves it to protocol designers, and the principle is “a short label that distinguishes the purpose” — if one key serves two protocols, their ctx values must differ, or you have dismantled domain separation by hand; for a key used in a single protocol, an empty ctx is legal and common. Second, pure mode versus pre-hash mode: pure mode digests the whole message inside the algorithm, so large messages must be buffered entirely into the signing device; boundary devices such as HSMs and smart cards are better served by pre-hash mode (hash outside, send the digest in to be signed), but pre-hash pushes “choice of hash algorithm” onto the application, and a wrong hash or a wrong OID makes signatures fail at the verifier for no obvious reason. Neither decision is cryptographically deep, yet both are among the most common integration-stage failures — they deserve their own pass in an interface design review.
Tags: ML-DSAalgorithm walkthroughFIPS 204Fiat-Shamir