ADVANCED · ML-KEM · 75 MIN
ML-KEM I: K-PKE, the ring and the NTT
Terms in this lesson
- K-PKE
- ML-KEM
- encapsulation key
- decapsulation key
- compression
- NTT
- twiddle factor
- base-case multiplication
- BitRev7
What you’ll learn
- Write K-PKE key generation, encryption and decryption in ring-vector notation, and match each step to its line in FIPS 203.
- Derive exactly what decryption computes, including the two compression errors, and when it returns the right bit.
- Compress and decompress a coefficient, bound the error, and derive the sizes of keys and ciphertexts from k, du and dv.
- Explain why ζ = 17 works for q = 3329 and n = 256, why the NTT stops at quadratic factors, and compute one product the way FIPS 203 does, at toy size.
Prerequisites. P1 (LWE, Regev's one-bit scheme, Module-LWE). M-C: the ring Rq = Zq[X]/(Xn + 1) and its negacyclic product (unit 2), vectors and matrices over Rq (unit 3), the centred binomial distribution CBDη (unit 4). Site: What is ML-KEM? for what a KEM is for.
Notation. Rq = Zq[X]/(Xn + 1) with n = 256 and q = 3329 (FIPS 203 §2.3, §2.4). Letters: s, e, y, e1 are vectors in Rqk (k ring elements each); A is a k × k matrix over Rq; t, u are vectors; v, e2, w, μ are single ring elements. T is transpose: tTy = t0y0 + … + tk−1yk−1, a ring element. Because e1 already names a vector, the entries of e and e1 are written e[i] and e1[i]. A hat (t̂) means "in NTT form" (section 4). "Centred" means the representative in −⌊q/2⌋ … ⌊q/2⌋. ⌈x⌋ is rounding to the nearest integer with halves rounded up: ⌈y + ½⌋ = y + 1 (FIPS 203 §2.3).
1. From Regev's bit to K-PKE
P1's scheme encrypted one bit with a random subset of public rows. K-PKE, the public-key encryption scheme inside ML-KEM, makes three changes:
- Module-LWE instead of LWE. The secret s and error e are vectors of k ring elements with small coefficients, A is a k × k matrix over Rq, and t = As + e.
- A small random vector instead of a subset. The sender picks a fresh small y ∈ Rqk (and small e1, e2) and sends u = ATy + e1 and v = tTy + e2 + μ. A subset sum is y with entries in {0, 1}; a small y with fresh errors is what makes the ciphertext a Module-LWE sample in its own right.
- 256 bits at once, then compression. The message m (32 bytes = 256 bits) becomes the polynomial μ whose coefficient i is ⌈q/2⌋ = 1665 if bit i is 1 and 0 if it is 0. The ciphertext is rounded to fewer bits per coefficient before it is sent (section 3).
The three algorithms, in ring-vector form
Small means: every coefficient drawn from CBDη (η1 for s, e, y; η2 for e1, e2).
- KeyGen. A uniform in Rqk×k; s, e small. t = As + e. Public: (t, A); secret: s.
- Encrypt(m). y, e1 small, e2 small. u = ATy + e1; v = tTy + e2 + μ. Send (Compressdu(u), Compressdv(v)).
- Decrypt. Decompress to u′, v′. w = v′ − sTu′. Bit i = Compress1(wi).
The same, line by line in FIPS 203
FIPS 203 §5 gives K-PKE as Algorithms 13–15. The arithmetic is done in NTT form, which section 4 explains; ignore the hats for now.
| Step | FIPS 203 line (verbatim; hats as reconstructed from the extracted text) |
|---|---|
| Seeds | Alg. 13 line 1: (ρ, σ) ← G(d‖k); "expand 32+1 bytes to two pseudorandom 32-byte seeds" |
| A | Alg. 13 line 5: Â[i, j] ← SampleNTT(ρ‖j‖i) |
| s, e | Alg. 13 lines 9, 13: SamplePolyCBDη1(PRFη1(σ, N)) |
| t | Alg. 13 line 18: t̂ ← Â ∘ ŝ + ê, "noisy linear system in NTT domain" |
| Keys | Alg. 13 line 19: ekPKE ← ByteEncode12(t̂)‖ρ; line 20: dkPKE ← ByteEncode12(ŝ) |
| y, e1, e2 | Alg. 14 lines 10, 14, 17: y from CBDη1; e1, e2 from CBDη2 |
| u | Alg. 14 line 19: u ← NTT−1(ÂT ∘ ŷ) + e1 |
| μ | Alg. 14 line 20: μ ← Decompress1(ByteDecode1(m)) |
| v | Alg. 14 line 21: v ← NTT−1(t̂T ∘ ŷ) + e2 + μ |
| Ciphertext | Alg. 14 lines 22–23: c1 ← ByteEncodedu(Compressdu(u)); c2 ← ByteEncodedv(Compressdv(v)) |
| Decrypt | Alg. 15 lines 3–4: u′, v′ by Decompress; line 6: w ← v′ − NTT−1(ŝT ∘ NTT(u′)); line 7: m ← ByteEncode1(Compress1(w)) |
In these lines ‖ joins byte strings, ∘ is multiplication in NTT form (section 4), and 𝔹ℓ is a string of ℓ bytes. G, H and PRF are FIPS 203's hash functions and SampleNTT and SamplePolyCBD its samplers; this lesson uses only what they output. In G(d‖k), d is a 32-byte random seed and k is the parameter k written as one byte; everywhere else in this lesson d is a number of bits per coefficient.
Two consequences you can read off the table. A is generated directly in NTT form from the 32-byte seed ρ, and the public matrix is never sent: the encryptor regenerates  from ρ (Alg. 14 lines 4–8), which is why P1's M2 found that a seed removes the matrix from the key size. And the keys hold t̂ and ŝ, not t and s: the NTT form is what is stored. (ek, the encapsulation key, is ML-KEM's public key; dk, the decapsulation key, is kept secret.) Figure 1 shows what crosses between the two sides.
The parameter sets (FIPS 203 §8, Table 2):
| k | η1 | η2 | du | dv | Random bit generator (RBG) strength, bits | |
|---|---|---|---|---|---|---|
| ML-KEM-512 | 2 | 3 | 2 | 10 | 4 | 128 |
| ML-KEM-768 | 3 | 2 | 2 | 10 | 4 | 192 |
| ML-KEM-1024 | 4 | 2 | 2 | 11 | 5 | 256 |
Pause and recall optional
Which value of K-PKE is never sent, and how does the encryptor get it?
Show answer
The matrix Â: it is regenerated from the 32-byte seed ρ.
What does dkPKE hold?
Show answer
ŝ, the secret in NTT form.
2. Why decryption works
Derivation: what w is
Start without compression (u′ = u, v′ = v). Rq is commutative, so (As)Ty = sTATy, and
tTy = (As + e)Ty = sTATy + eTy,
sTu = sT(ATy + e1)
= sTATy + sTe1.
Subtract:
w = v − sTu
= sTATy + eTy + e2 + μ − sTATy − sTe1
= μ + (eTy − sTe1 + e2).
The secret-times-matrix term cancels exactly, as in P1. What is left is the message plus a noise polynomial made of small things multiplied together.
Now add compression. Write u′ = u + cu and v′ = v + cv, where cu and cv are the rounding errors of section 3. Then
w = μ + eTy − sTe1 + e2 + cv − sTcu.
Every term except μ is small, but each coefficient of eTy, sTe1 and sTcu is a sum of k · n products (exercise M3).
Derivation: the decoding window at q = 3329
Decoding uses Compress1 (FIPS 203 eq. 4.7 with d = 1): x ↦ ⌈2x/q⌋ mod 2. For q = 3329 this is 1 exactly when 833 ≤ x ≤ 2,496 (checked for every x by this lesson's code): 2x/q ≥ ½ iff x ≥ 832.25, and 2x/q < 3/2 iff x < 2,496.75. The bit-1 offset is Decompress1(1) = ⌈q/2⌋ = ⌈1,664.5⌋ = 1,665. Let N be one centred coefficient of the noise:
- bit 0: Compress1(N mod q) = 0 iff −832 ≤ N ≤ 832;
- bit 1: Compress1(1,665 + N) = 1 iff 833 ≤ 1,665 + N ≤ 2,496, iff −832 ≤ N ≤ 831.
So a coefficient decrypts correctly, whatever its bit, iff its noise lies in −832 … 831 (checked for every N), a margin of 832 either side (831 on the positive side for bit 1). This is P1's "about q/4" rule (q/4 = 832.25), made exact. ML-KEM does not guarantee it: a decryption failure is possible, with probability FIPS 203 gives as 2−138.8, 2−164.8 and 2−174.8 for the three parameter sets (FIPS 203 §3.2, Table 1, which refers to Theorem 1 of Bos et al., "CRYSTALS-Kyber: a CCA-secure module-lattice-based KEM", 2018; NIST's figures, not recomputed in this lesson). P4 shows how such a number is computed.
Pause and recall optional
What is left of w once the secret-times-matrix term cancels?
Show answer
μ plus the noise eTy − sTe1 + e2, and cv − sTcu with compression.
What is the margin at q = 3329?
Show answer
832 either side; 831 on the positive side for a 1.
3. Compression and sizes
Definitions (FIPS 203 §4.2.1)
For d < 12 (12 is the bit length of q):
Compressd(x) = ⌈(2d / q) · x⌋ mod 2d (eq. 4.7),
Decompressd(y) = ⌈(q / 2d) · y⌋ (eq. 4.8).
"Division and rounding in the computation of these functions are performed in the set of rational numbers"; "Floating-point computations shall not be used" (§4.2.1).
Worked example, d = 4, x = 1,000. 16 · 1,000 / 3,329 = 4.806…, so Compress4(1,000) = 5. Decompress4(5) = ⌈5 · 3,329 / 16⌋ = ⌈1,040.3125⌋ = 1,040. The round trip moved 1,000 to 1,040: error +40.
Derivation: the error bound
Let y′ = ⌈2d x / q⌋ (before "mod 2d"). Then |2d x / q − y′| ≤ ½, so |q y′ / 2d − x| ≤ q / 2d+1. Decompress rounds q y′ / 2d to an integer, adding at most ½. The error is an integer, so
|Decompressd(Compressd(x)) − x| ≤ ⌈q / 2d+1⌋
(as a centred value mod q).
The "mod 2d" only matters when y′ = 2d, which happens for x near q; then Decompress gives 0 ≡ q, the same residue. The bound is reached; the worst cases over all x are: d = 1: 832; d = 4: 104; d = 5: 52; d = 10: 2; d = 11: 1.
Why u keeps 10 bits and v only 4
cv enters w directly: at dv = 4 it is at most 104 per coefficient, against the margin of 832. cu enters as sTcu, a sum of k · 256 products per coefficient. Its size depends on the spread of cu, not its maximum. Treating u's coefficients as uniform mod q (a model, not a theorem), the variance of the compression error is 0.924 at d = 10 and 3,607.7 at d = 4 (computed exactly over all x). With s from CBD2 (variance η/2 = 1) and k = 3, sTcu has variance 768 × 0.924 ≈ 710 at du = 10, but 768 × 3,607.7 ≈ 2.77 million at du = 4: a standard deviation of about 1,664, twice the margin. Exercise M3 works this through, and figure 2 shows it.
Sizes, derived
- A polynomial with 256 coefficients of 12 bits takes 256 · 12 / 8 = 384 bytes; of d bits, 32d bytes.
- ek = ByteEncode12(t̂)‖ρ: k polynomials and a 32-byte seed, 384k + 32 bytes (Alg. 13 output 𝔹384k+32).
- ciphertext = c1‖c2: k polynomials at du bits and one at dv bits, 32(du k + dv) bytes (Alg. 14 output 𝔹32(du k + dv)).
- dk of ML-KEM (not of K-PKE) = dkPKE‖ek‖H(ek)‖z: 384k + (384k + 32) + 32 + 32 = 768k + 96 bytes (Alg. 16 line 3 and output 𝔹768k+96). Why ek and H(ek) are inside dk is P4's subject (decapsulation re-encrypts).
| ek | dk | ciphertext | shared secret | |
|---|---|---|---|---|
| ML-KEM-512 (k = 2, du = 10, dv = 4) | 800 | 1,632 | 768 | 32 |
| ML-KEM-768 (k = 3, 10, 4) | 1,184 | 2,400 | 1,088 | 32 |
| ML-KEM-1024 (k = 4, 11, 5) | 1,568 | 3,168 | 1,568 | 32 |
Every entry follows from the formulas and equals FIPS 203 Table 3 (§8). Uncompressed, an ML-KEM-768 ciphertext would be (3 + 1) · 384 = 1,536 bytes; compression saves 448.
Pause and recall optional
Why can v be cut to 4 bits but u only to 10?
Show answer
v's rounding error is added once; u's is multiplied by s and summed k · 256 times.
How many bytes is an ML-KEM-768 ciphertext?
Show answer
32 · (10 · 3 + 4) = 1,088.
4. The ring and the NTT
Why multiplication needs a trick
A product in Rq done the schoolbook way, every coefficient of one times every coefficient of the other (M-C unit 2), takes 2562 = 65,536 multiplications. Key generation alone needs k2 of them. The number-theoretic transform (NTT) is the fast Fourier transform over Zq: it turns one product in Rq into 128 tiny products.
The factorisation that makes it work
The idea is the Chinese remainder theorem for polynomials: if X256 + 1 factors into coprime pieces, a polynomial is fully described by its remainders modulo the pieces, and products can be computed piece by piece.
- ζ = 17. FIPS 203 §4.3: "ζ = 17 ∈ Zq is a primitive 256-th root of unity modulo q. Thus, ζ128 ≡ −1." (17128 ≡ 3,328 ≡ −1, 17256 ≡ 1, and no smaller power is 1.)
- Linear factors would need 512th roots of unity: a root r of X256 + 1 has r256 = −1 and r512 = 1, so it has order 512. The multiplicative group mod 3,329 has q − 1 = 3,328 = 28 · 13 elements, and 512 does not divide 3,328. So there are none: "There are 128 primitive 256-th roots of unity and no primitive 512-th roots of unity in Zq" (§4.3).
- The best available is quadratic factors. Let γi = ζ2·BitRev7(i)+1 for i = 0 … 127, where BitRev7(i) reverses the 7 bits of i (§4.3). Each γi is an odd power of 17, so γi128 = 17128·odd = (−1)odd = −1. Modulo X2 − γi, X256 = (X2)128 ≡ γi128 = −1, so X2 − γi divides X256 + 1. The 128 values γi are distinct (checked by this lesson's code), so these 128 factors are pairwise coprime; their degrees add up to 256 and both sides are monic, so
X256 + 1 = ∏i=0127 (X2 − γi) (FIPS 203 eq. 4.10).
- The NTT of f is the list of its 128 remainders f mod (X2 − γi) = f̂[2i] + f̂[2i+1]X (eq. 4.13): 256 numbers again. Rq "is isomorphic to another ring Tq, which is a direct sum of 128 quadratic extensions of Zq" (§4.3).
The first values: γ0 = 17, γ1 = −17, γ2 = 2,761, γ3 = −2,761, 583, −583, 2,649, −2,649 (FIPS 203 Appendix A, as printed there, residues mod q: in centred form 2,761 ≡ −568 and 2,649 ≡ −680). They come in pairs γ, −γ: X2 − γ and X2 + γ are the two halves of one split of X4 − γ2.
Base-case multiplication
Multiplying two remainders modulo X2 − γ: (a0 + a1X)(b0 + b1X) = a0b0 + (a0b1 + a1b0)X + a1b1X2, and X2 = γ, so
c0 = a0b0 + a1b1γ,
c1 = a0b1 + a1b0
(Algorithm 12, BaseCaseMultiply). Algorithm 11 (MultiplyNTTs) applies it to the 128 pairs with γ = ζ2BitRev7(i)+1. Cost: 5 multiplications per pair, 640 in all.
The butterfly
How are the 128 remainders computed fast? Split f = flo + Xn/2 fhi. If c2 = −1 then Xn + 1 = (Xn/2 − c)(Xn/2 + c), and
f mod (Xn/2 − c) = flo + c·fhi,
f mod (Xn/2 + c) = flo − c·fhi.
One multiplication by c gives both halves. Repeat on each half: Xn/2 − c splits the same way with a square root of c in place of c, and in Z3329 such roots exist at every layer down to X2 − γi (they are powers of 17). Seven layers take 256 coefficients down to 128 pairs. Algorithm 9 (NTT) is exactly this: for len = 128, 64, …, 2, each butterfly does t ← zeta · f̂[j + len]; f̂[j + len] ← f̂[j] − t; f̂[j] ← f̂[j] + t, with zeta = ζBitRev7(i), the twiddle factors, taken in order from a 128-entry table (Appendix A: 1, 1,729, 2,580, 3,289, 2,642, 630, 1,897, 848, …). The first zeta used is ζBitRev7(1) = 1764 = 1,729, a square root of −1 (1,7292 ≡ −1), which is the c of the first split. Cost: 7 layers × 128 butterflies = 896 multiplications. Algorithm 10 (NTT−1) undoes the layers in reverse order, using entries of the same table. Each inverse butterfly leaves out the division by 2, so it returns twice the true value; after 7 layers the result is 27 = 128 times too big, and one multiplication at the end corrects it: "f ← f · 3303 mod q", 3,303 ≡ 128−1 = 2−7 (3,303 · 128 = 422,784 = 127 · 3,329 + 1).
A product in Rq is then NTT−1(NTT(f) × NTT(g)): three transforms of 896 multiplications, 640 for the base cases and 256 for the final scaling, 3,584 in all, against 65,536. In ML-KEM many transforms are never needed: Â is sampled directly in NTT form, and ŝ and t̂ are stored in NTT form, so only the fresh small vectors and the results are transformed.
Worked example: the whole method at n = 4, q = 17
At toy size the same shape appears with c = 4: 42 = 16 ≡ −1 (mod 17), so 4 plays the role of 1,729, and
X4 + 1 = (X2 − 4)(X2 + 4)
= (X2 − 4)(X2 − 13) (check: X4 − 17X2 + 52 ≡ X4 + 1).
Multiply f = 10 + 4X + 12X2 + X3 by g = −1 + X − X3 (these are A00 and s0 of section 5).
- Forward butterfly (flo = 10 + 4X, fhi = 12 + X): - f mod (X2 − 4) = (10 + 4·12) + (4 + 4·1)X = 58 + 8X ≡ 7 + 8X; - f mod (X2 + 4) = (10 − 48) + (4 − 4)X ≡ 13 + 0X. - g (glo = −1 + X, ghi = 0 − X): g mod (X2 − 4) = −1 + (1 − 4)X ≡ 16 + 14X; g mod (X2 + 4) = −1 + 5X ≡ 16 + 5X.
- Base case with γ = 4: c0 = 7·16 + 8·14·4 = 560 ≡ 16, c1 = 7·14 + 8·16 = 226 ≡ 5 → 16 + 5X. - With γ = 13: c0 = 13·16 + 0 = 208 ≡ 4, c1 = 13·5 + 0·16 = 65 ≡ 14 → 4 + 14X.
- Inverse butterfly: flo = (r1 + r2)/2, fhi = (r1 − r2)/(2·4), with 2−1 ≡ 9 and 8−1 ≡ 15 (mod 17): - lo = (16 + 4, 5 + 14) · 9 = (180, 171) ≡ (10, 1); hi = (16 − 4, 5 − 14) · 15 = (180, −135) ≡ (10, 1).
Product: 10 + X + 10X2 + X3. Schoolbook check: the unreduced product has coefficients (X0 … X6) −10, 6, −8, 1, −3, −12, −1; folding X4 = −1 gives (−10 + 3, 6 + 12, −8 + 1, 1) = (−7, 18, −7, 1) ≡ (10, 1, 10, 1). Same.
Pause and recall optional
Why does the NTT stop at quadratic factors?
Show answer
Linear factors need elements of order 512, and 512 does not divide 3,328.
How many multiplications does one base-case product take?
Show answer
5.
5. Worked example: baby Kyber, end to end
Toy parameters: n = 4, q = 17, k = 2, all small values in {−1, 0, 1} (the support of CBD1), no compression, no NTT, A written in the ordinary domain. The values were drawn once at random; 16 ≡ −1 (mod 17).
KeyGen.
A = A00A01A10A11 with A00 = (10, 4, 12, 1), A01 = (2, 3, 11, 1), A10 = (16, 6, 1, 2), A11 = (13, 13, 2, 7) (coefficients of X0 … X3).
s = (s0, s1) = ((−1, 1, 0, −1), (1, −1, −1, 1)); e = (e[0], e[1]) = ((1, 1, −1, 1), (1, 0, −1, −1)).
t0 = A00s0 + A01s1 + e[0] = (10, 1, 10, 1) + (11, 8, 5, 6) + (1, 1, −1, 1) = (22, 10, 14, 8) ≡ (5, 10, 14, 8).
(A00s0 is section 4's product. A01s1: unreduced (2, 1, 6, −11, −9, 10, 1), folded (2 + 9, 1 − 10, 6 − 1, −11) ≡ (11, 8, 5, 6).)
t1 = A10s0 + A11s1 + e[1] = (5, 11, 7, 0) + (9, 5, 3, 5) + (1, 0, −1, −1) ≡ (15, 16, 9, 4).
Encrypt m = (1, 0, 1, 1). μ = (9, 0, 9, 9), since Decompress1(1) = ⌈17/2⌋ = ⌈8.5⌋ = 9.
y = ((−1, 1, −1, 0), (0, −1, 1, −1)); e1 = ((1, 0, 1, 1), (−1, −1, 1, 1)); e2 = (1, −1, 0, −1).
u0 = A00y0 + A10y1 + e1[0] = (1, 7, 16, 7) + (7, 0, 12, 6) + (1, 0, 1, 1) ≡ (9, 7, 12, 14).
u1 = A01y0 + A11y1 + e1[1] = (8, 0, 7, 7) + (1, 16, 7, 15) + (−1, −1, 1, 1) ≡ (8, 15, 15, 6).
(u = ATy + e1: u0 uses the first column of A.)
v = t0y0 + t1y1 + e2 + μ = (1, 3, 8, 13) + (11, 7, 3, 9) + (1, −1, 0, −1) + (9, 0, 9, 9) ≡ (5, 9, 3, 13).
Decrypt. sTu = s0u0 + s1u1 = (1, 14, 9, 6) + (14, 15, 3, 1) ≡ (15, 12, 12, 7).
w = v − sTu = (5 − 15, 9 − 12, 3 − 12, 13 − 7) ≡ (7, 14, 8, 6).
Compress1 at q = 17 returns 1 for 5 … 12 (⌈2x/17⌋ = 1 iff 4.25 ≤ x < 12.75): 7 → 1, 14 → 0, 8 → 1, 6 → 1. m = (1, 0, 1, 1).
The noise, as derived in section 2: w − μ = (7 − 9, 14, 8 − 9, 6 − 9) ≡ (−2, −3, −1, −3), and eTy − sTe1 + e2 gives the same polynomial. The window at q = 17 is −4 … 3 for bit 1 and −4 … 4 for bit 0, so the largest noise here, −3, is two steps from failing (−5 would fail). P4 measures how often this toy fails.
The same instance, as code (Python 3.13, standard library only):
q, n = 17, 4
def mul(a, b): # product in Z_q[X]/(X^n + 1)
r = [0] * n
for i in range(n):
for j in range(n):
if i + j < n: r[i + j] += a[i] * b[j]
else: r[i + j - n] -= a[i] * b[j]
return [x % q for x in r]
add = lambda *p: [sum(c) % q for c in zip(*p)]
A = [[[10, 4, 12, 1], [2, 3, 11, 1]], [[16, 6, 1, 2], [13, 13, 2, 7]]]
s = [[-1, 1, 0, -1], [1, -1, -1, 1]]; e = [[1, 1, -1, 1], [1, 0, -1, -1]]
t = [add(mul(A[i][0], s[0]), mul(A[i][1], s[1]), e[i]) for i in range(2)]
y = [[-1, 1, -1, 0], [0, -1, 1, -1]]; e1 = [[1, 0, 1, 1], [-1, -1, 1, 1]]; e2 = [1, -1, 0, -1]
m = [1, 0, 1, 1]; mu = [9 * b for b in m]
u = [add(mul(A[0][j], y[0]), mul(A[1][j], y[1]), e1[j]) for j in range(2)]
v = add(mul(t[0], y[0]), mul(t[1], y[1]), e2, mu)
w = [(a - b) % q for a, b in zip(v, add(mul(s[0], u[0]), mul(s[1], u[1])))]
print("w =", w, "decoded =", [1 if 5 <= x <= 12 else 0 for x in w])
Output: w = [7, 14, 8, 6] decoded = [1, 0, 1, 1].
Pause and recall optional
In baby Kyber, which window must each noise coefficient stay in?
Show answer
−4 … 4 for a 0, −4 … 3 for a 1, at q = 17.
What does the sum eTy − sTe1 + e2 equal?
Show answer
w − μ.
Exercises
Sizes use FIPS 203's formulas from section 3. Try each before opening its solution.
Easy
E1. A hypothetical parameter set has k = 5, du = 11, dv = 5. Give the sizes of ek, dk and the ciphertext.
Show solution, E1
ek = 384 · 5 + 32 = 1,952 bytes; dk = 768 · 5 + 96 = 3,936; ciphertext = 32 · (11 · 5 + 5) = 32 · 60 = 1,920.
Easy
E2. Compute Compress4(3,000) and Decompress4 of the result for q = 3,329. What is the rounding error, and what is the largest it could be for d = 4?
Show solution, E2
24 · 3,000 / 3,329 = 48,000 / 3,329 = 14.418…, rounded: 14. Decompress4(14) = ⌈14 · 3,329 / 16⌋ = ⌈2,912.875⌋ = 2,913. Error −87. The bound of section 3 gives at most ⌈3,329 / 32⌋ = ⌈104.03⌋ = 104, and some x reaches it.
Medium
M1. For n = 4 and q = 17, X4 + 1 does split into linear factors. (a) Find an element of order 8 mod 17 and list the four roots of X4 + 1. (b) Multiply f = 10 + 4X + 12X2 + X3 by g = −1 + X − X3 by evaluating both at the four roots, multiplying, and interpolating back. Check against section 4. (c) Why is the same not possible for q = 3,329, n = 256?
Show solution, M1
(a) The powers of 2 mod 17 are 2, 4, 8, 16, 15, 13, 9, 1: order 8. The roots of X4 + 1 are the elements of order 8, the odd powers 21, 23, 25, 27, that is, in increasing order, 2, 8, 9, 15 (each satisfies r4 ≡ −1).
(b) f at (2, 8, 9, 15) = (6, 13, 13, 8); g at the same points = (10, 5, 10, 5). Products: (60, 65, 130, 40) ≡ (9, 14, 11, 6). The unique cubic through those four values (solve the 4 × 4 Vandermonde system mod 17) is 10 + X + 10X2 + X3, as in section 4. Evaluating at a non-root breaks the method: at X = 3 the product polynomial's value differs from f(3)·g(3) mod 17, because the product was reduced mod X4 + 1 and 3 is not a root of it.
(c) For n = 256 the roots of X256 + 1 have order 512, and 512 does not divide 3,328 = 28 · 13, so Z3329 has none. ML-KEM stops one level early, at quadratic factors, and pays with base-case multiplication.
Medium
M2. With the baby-Kyber secret key of section 5, decrypt u0 = (4, 1, 15, 11), u1 = (2, 2, 12, 11), v = (10, 6, 0, 14).
Show solution, M2
s0u0 = (3, 1, 14, 0), s1u1 = (6, 16, 14, 16), sum (9, 17, 28, 16) ≡ (9, 0, 11, 16). w = v − sTu = (10 − 9, 6 − 0, 0 − 11, 14 − 16) ≡ (1, 6, 6, 15). Decode with 5 … 12 → 1: m = (0, 1, 1, 0). (Centred noise: 1, 6 − 9 = −3, −3, −2.)
Medium
M3. For ML-KEM-768 (k = 3, η1 = η2 = 2, dv = 4), estimate the standard deviation of one coefficient of the noise eTy − sTe1 + e2 + cv − sTcu, first with du = 10 and then with du = 4. Treat every coefficient as independent, CBD2 as having variance 1, and the compression errors as those of a uniform x. Which term grows, and what happens to decryption?
Show solution, M3
One coefficient of eTy is a sum of k · 256 = 768 products of independent CBD2 values: variance 768 × 1 × 1 = 768. The same for sTe1. e2: variance 1. cv at dv = 4: 3,607.7. sTcu: 768 × 1 × Var(cu).
- du = 10: Var(cu) = 0.924, so sTcu contributes 710; total 5,854.3, standard deviation 76.5, about a tenth of the 832 margin.
- du = 4: Var(cu) = 3,607.7, so sTcu contributes 2,770,687; total 2,775,831, standard deviation 1,666.1, twice the margin. The term that grows is sTcu: the rounding error of u is multiplied by the secret and summed 768 times, while the error of v is added once. A Monte Carlo run of 20,000 coefficients under the same model gives standard deviations 76.8 and 1,688.8. Decryption reads the noise mod q, so what matters is where it lands on the circle: with du = 4 the noise is spread over the whole circle, and about half of all bits come out wrong (48.8% of the 20,000, reduced mod q and centred, fall outside −832 … 831; 49.6% under the normal model): decryption is no better than guessing. (The independence and uniformity assumptions are a model; P4 does the exact computation for the real scheme.)
Hard
H1. Implement K-PKE with compression at q = 3,329, n = 16, k = 2, η = 2 (uniform A from any random generator; schoolbook products). Encrypt 300 random 16-bit messages with (du, dv) = (10, 4), (4, 4) and (3, 3), and count bit failures. What does a count of 0 out of 4,800 let you say?
Show solution, H1
With (du, dv) = (10, 4): 0 bit failures in 4,800. With (4, 4): 113 (2.4%). With (3, 3): 1,109 (23%). (A seeded run of this lesson's code; your counts will differ with your generator, but not in kind.) Zero failures in N = 4,800 trials bounds the failure rate only weakly: at 95% confidence it is below about 3/N ≈ 6 × 10−4 (the "rule of three", treating the 4,800 bits as independent trials, which they are not quite: the 16 bits of one message share s, y and e1). ML-KEM-768's 2−164.8 cannot be checked by sampling at all; it has to be computed (P4).
Hard
H2. ML-KEM's dk is dkPKE‖ek‖H(ek)‖z. Verify 768k + 96 = 384k + (384k + 32) + 32 + 32, and show from FIPS 203's Algorithm 18 slices where each part sits. Why does K-PKE's own decryption need only the first 384k bytes?
Show solution, H2
384k + (384k + 32) + 32 + 32 = 768k + 96 for every k. Algorithm 18 slices dk as dkPKE = dk[0 : 384k], ekPKE = dk[384k : 768k + 32], h = dk[768k + 32 : 768k + 64], z = dk[768k + 64 : 768k + 96]. K-PKE.Decrypt (Algorithm 15) reads only ŝ = ByteDecode12(dkPKE), so it needs only the first 384k bytes. The rest is there for ML-KEM's decapsulation, which re-encrypts the decrypted message under ek and compares ciphertexts, and uses z when they differ (stated here; derived in P4).
What this lesson does not cover
How SampleNTT, SamplePolyCBD, PRF, G and H work (named and cited; the details of the extendable-output function, XOF, and of the hashes are in M-C unit 4 and FIPS 203 §4.1 and §4.2); ByteEncode bit packing; Montgomery and Barrett reduction and other implementation speed-ups (P10 mentions them); the Fujisaki-Okamoto transform that turns K-PKE into ML-KEM, implicit rejection, and the failure-probability computation (P4); why K-PKE alone is only secure against passive attackers (P4). Where the real difficulty lies: the algebra above is complete, but the parameters are a balance that this lesson only shows from one side: du, dv and η set the noise against the 832 margin (and the size of every message), while k and η set the cost of lattice attacks (P2). Implementations then add their own difficulty: all of this must run in constant time (P10).
Sources
All from NIST FIPS 203, Module-Lattice-Based Key-Encapsulation Mechanism Standard, published and effective 13 August 2024, https://doi.org/10.6028/NIST.FIPS.203. Full text read twice, 26 Nov 2026. VERIFIED unless noted.
- Notation. §2.3 (n, q, ζ, BitRev7, rounding ⌈·⌋), §2.4.
- Section 1. §5.1–5.3, Algorithms 13–15 (K-PKE.KeyGen, Encrypt, Decrypt); §8 Table 2 (parameter sets).
- Section 2. Algorithms 14–15 for w; §4.2.1 eq. (4.7) for Compress1; §3.2 Table 1 (failure rates; NIST's figures, not recomputed here).
- Section 3. §4.2.1 eq. (4.7), (4.8) and the rational-arithmetic rule; Algorithm 13 and 14 output lengths; §6.1 Algorithm 16 (dk layout); §8 Table 3 (sizes). The ML-KEM-768 and ML-KEM-1024 sizes also appear in RFC 10024 §4.1–4.3 (August 2026), inside the hybrid shares X25519MLKEM768 (1,216 = 1,184 + 32 and 1,120 = 1,088 + 32) and SecP384r1MLKEM1024 (1,665 = 1,568 + 97); VERIFIED by the science checker, 26 Nov 2026.
- Section 4. §4.3 (ζ = 17, no 512-th roots, eq. 4.10, Tq, eq. 4.13); Algorithms 9–12; Appendix A (zeta and gamma tables).
- Exercise H2. §6 Algorithm 18 (slices of dk). The reason decapsulation needs ek and H(ek) is stated here and derived in P4.
- Every number, every hand step and every exercise answer is computed by this lesson's code (Python 3.13); the code block in section 5 is part of it and runs as printed.