ADVANCED · MATHS MODULE · 50 MIN
Maths for lattices and hashes
Terms in this lesson
- modulus
- Zq
- centred representative
- field
- inverse
- order
- cyclic group
- Zq*
- polynomial ring
- negacyclic
- module
- rank
- inner product
- centred binomial distribution
- variance
- tail
- preimage resistance
- collision resistance
- birthday bound
What you’ll learn
- Reduce integers modulo q, pick the centred representative, and find inverses modulo 3329 with Euclid's algorithm.
- Multiply polynomials in Zq[X]/(Xn + 1) by hand, including the sign flip when a power wraps past Xn−1.
- Compute matrix–vector products and inner products whose entries are ring elements (modules).
- Write down the centred binomial distribution, add independent small errors exactly, and bound a tail by counting.
- State the three security properties of a hash function and the generic cost of breaking each.
Prerequisites. School algebra. Site: Public-key cryptography, Hash functions. If you have done Q0 unit M4 (modular arithmetic and continued fractions), unit 1 below will be quick: it adds only centred representatives and the facts lattice schemes rely on.
Unit 1. Arithmetic modulo q
Zq and reduction
Fix a positive integer q, the modulus. Two integers are congruent modulo q, written a ≡ b (mod q), when q divides a − b. Zq ("the integers mod q") is the set {0, 1, …, q − 1} with addition and multiplication followed by reduction: a mod q is the remainder r with 0 ≤ r < q and q | a − r (read "q divides a − r").
Examples (q = 17): −5 mod 17 = 12, because −5 = (−1)·17 + 12. 40 mod 17 = 6.
Examples (q = 3329, the ML-KEM modulus): −1 mod 3329 = 3328; 5000 mod 3329 = 1671.
Centred representatives
Lattice schemes care whether a number is small, and "small" means close to 0 on the circle of residues, in either direction. 3328 is not small as a remainder, but 3328 ≡ −1, which is. So we also use the centred representative: the unique r ≡ a (mod q) with
−q/2 < r ≤ q/2.
For odd q this is the range −(q − 1)/2, …, (q − 1)/2:
| q | centred range | examples |
|---|---|---|
| 17 | −8 … 8 | 9 → −8; 12 → −5; 6 → 6 |
| 3329 | −1664 … 1664 | 3328 → −1; 1671 → −1658; 1665 → −1664; 1664 → 1664 |
For even q the interval is lopsided: for q = 16 it is −7 … 8.
We write this centred reduction as r mod± q. (FIPS 203 §2.3 defines only the ordinary reduction, to 0, …, q − 1; P3 uses the centred one to measure the noise in decryption.)
Inverses, and why the modulus is prime
An inverse of a modulo q is a number a−1 with a · a−1 ≡ 1 (mod q). It exists exactly when gcd(a, q) = 1: if a·x + q·y = 1 for some integers x, y, then a·x ≡ 1; conversely a·x ≡ 1 means a·x − 1 is a multiple of q, so any common divisor of a and q divides 1.
So when q is prime, every non-zero element has an inverse, and Zq is a field (you can divide by anything except 0). 3329 is prime (no divisor from 2 up to √3329 ≈ 57.7). By contrast, mod 16 only the odd numbers are invertible; 4 has no inverse because gcd(4, 16) = 4.
Worked example: 17−1 mod 3329 by the extended Euclidean algorithm.
Divide repeatedly:
3329 = 195·17 + 14
17 = 1·14 + 3
14 = 4·3 + 2
3 = 1·2 + 1
2 = 2·1 + 0 → gcd = 1
Back-substitute, keeping 1 as a combination of the two most recent remainders:
1 = 3 − 1·2
= 3 − (14 − 4·3) = 5·3 − 14
= 5·(17 − 14) − 14 = 5·17 − 6·14
= 5·17 − 6·(3329 − 195·17)
= 1175·17 − 6·3329
So 17−1 ≡ 1175 (mod 3329). Check: 17 · 1175 = 19975 = 6 · 3329 + 1.
A second route, valid because 3329 is prime: Fermat's little theorem (a special case of Euler's theorem, derived in Q0 unit M4) gives aq−1 ≡ 1, so a−1 ≡ aq−2. Computing 173327 mod 3329 gives 1175 again.
Orders, and the number 17
The order of a modulo q is the smallest k ≥ 1 with ak ≡ 1. For prime q, the non-zero residues under multiplication, written Zq*, form a cyclic group of size q − 1: every one of them is a power of a single element. So every order divides q − 1, and for every divisor d of q − 1 there are elements of order exactly d.
For q = 3329: q − 1 = 3328 = 28 · 13. Repeated squaring gives 17128 ≡ 3328 ≡ −1, so 17256 ≡ 1: the order of 17 divides 256 but not 128, so it is exactly 256. No element has order 512, because 512 does not divide 3328. FIPS 203 §2.3 calls ζ = 17 a primitive 256-th root of unity modulo q; you will use exactly this fact in the hard exercise and again in P3.
Pause and recall optional
What is the centred representative of 3000 mod 3329?
Show answer
3000 − 3329 = −329.
Does 6 have an inverse mod 3330?
Show answer
No: 3330 = 6 · 555, so gcd(6, 3330) = 6.
Unit 2. Polynomial rings with wrap-around
The ring Zq[X]/(Xn + 1)
Zq[X] is the set of polynomials with coefficients in Zq. Rq = Zq[X]/(Xn + 1) keeps only polynomials of degree below n, with one rule:
Xn = −1.
Every element is a list of n coefficients (a0, a1, …, an−1) meaning a0 + a1X + … + an−1Xn−1. Addition is coefficient by coefficient, mod q. Multiplication is ordinary polynomial multiplication, then every term Xn+j is replaced by −Xj, then coefficients are reduced mod q.
In ML-KEM and ML-DSA, n = 256. Here we use n = 4 and q = 17 so that everything fits on a line.
The wrap-around rule
Multiplying by X shifts every coefficient up by one place; the top coefficient leaves the window and comes back at the bottom with its sign flipped. With n = 4, q = 17:
X · (4X3 + 3X2 + 2X + 1) = 4X4 + 3X3 + 2X2 + X
= 3X3 + 2X2 + X − 4
≡ 3X3 + 2X2 + X + 13.
The powers of X go 1, X, X2, X3, −1, −X, −X2, −X3, 1: X has order 8 = 2n. A cyclic shift without the sign flip would be the ring Zq[X]/(Xn − 1); the sign flip is why this ring is called negacyclic.
In formulas: if c = a · b in Rq, then
ck = Σi + j = k ai bj − Σi + j = k + n ai bj (mod q),
for k = 0, …, n − 1.
Worked example 1
(X3 + 2)(X2 + 1) in Z17[X]/(X4 + 1).
Ordinary product: X5 + X3 + 2X2 + 2.
Replace X5 = X · X4 = −X: the product is X3 + 2X2 − X + 2 ≡ X3 + 2X2 + 16X + 2.
Worked example 2
a = −X3 + 2X + 1 (stored as 16X3 + 2X + 1), b = X3 + X2 + 3.
Ordinary product (mod 17): 16X6 + 16X5 + 2X4 + X2 + 6X + 3. (The X3 terms cancel: −3X3 + 2X3 + X3 = 0.)
Wrap: 16X6 = 16X2 · X4 → −16X2 ≡ X2; 16X5 → −16X ≡ X; 2X4 → −2.
Collect: X2: 1 + 1 = 2. X: 6 + 1 = 7. Constant: 3 − 2 = 1.
a · b = 2X2 + 7X + 1. Multiplication in Rq is commutative; b · a gives the same.
Try it
Runs on Python 3 (tested on 3.13.18), standard library only.
def negmul(a, b, q):
"""a, b: coefficient lists, lowest degree first, both of length n.
Returns a*b in Z_q[X]/(X^n + 1)."""
n = len(a)
c = [0] * n
for i in range(n):
for j in range(n):
k = i + j
if k < n:
c[k] += a[i] * b[j]
else:
c[k - n] -= a[i] * b[j] # X^(k) = -X^(k-n)
return [x % q for x in c]
print(negmul([1, 2, 0, 16], [3, 0, 1, 1], 17)) # worked example 2
Output:
[1, 7, 2, 0]
(Lowest degree first: 1 + 7X + 2X2, as above.) This loop costs n2 multiplications; for n = 256 that is 65,536. P3 shows how the number-theoretic transform does it in about n log n.
Why Xn + 1, and why it matters that q is prime
Xn + 1 with n a power of 2 is the 2n-th cyclotomic polynomial (its roots, wherever they exist, are exactly the elements of order 2n), the choice made by ML-KEM and ML-DSA. Whether it factors modulo q decides how fast multiplication can be. For q = 17, n = 4, it splits completely:
X4 + 1 ≡ (X − 2)(X − 8)(X − 9)(X − 15) (mod 17),
the four roots being the elements of order 8 (2 has order 8 mod 17). The hard exercise works out exactly when this happens and what it means for q = 3329.
Pause and recall optional
What is X · X3 in Z17[X]/(X4 + 1)?
Show answer
X4 = −1 ≡ 16.
How many multiplications does the schoolbook product take for n = 256?
Show answer
n2 = 65,536.
Unit 3. Vectors and matrices over Rq (modules)
A vector of k elements of Rq is an element of Rqk, a module of rank k. It holds k · n coefficients. A k × k matrix over Rq multiplies such a vector in the usual row-times-column way, except that each product of entries is a product in Rq. The inner product of u, v ∈ Rqk is
⟨u, v⟩ = u0v0 + u1v1 + … + uk−1vk−1,
a single element of Rq.
Worked example 3 (k = 2, n = 4, q = 17)
s = (s0, s1) = (1 − X2, X3 + X).
A = 2X2 + 1XX3 + 3X + 1.
Row 0: (2X2 + 1)(1 − X2) = 2X2 − 2X4 + 1 − X2 = X2 + 3 (since −2X4 = +2). X · (X3 + X) = X4 + X2 = X2 − 1.
Sum: (As)0 = 2X2 + 2.
Row 1: (X3 + 3)(1 − X2) = X3 − X5 + 3 − 3X2 = X3 − 3X2 + X + 3 ≡ X3 + 14X2 + X + 3 (since −X5 = +X). (X + 1)(X3 + X) = X4 + X3 + X2 + X = X3 + X2 + X − 1.
Sum: (As)1 = 2X3 + 15X2 + 2X + 2, centred 2X3 − 2X2 + 2X + 2.
What rank buys
With n = 256 and k = 3, a vector in Rqk has 768 coefficients. At 12 bits each (3329 < 212 = 4096) that is 1,152 bytes. The structure is what makes keys small. Multiplying by one ring element a is a linear map on n coefficients, so one element of Rq stands in for an n × n matrix over Zq (each column is the previous one shifted by the wrap-around rule) described by only n numbers. A k × k matrix over Rq therefore has k2n = 2,304 coefficients where the matching plain-LWE matrix (P1) would have (kn)2 = 589,824 independent entries. (ML-KEM goes further and does not send the matrix at all, only a 32-byte seed it is expanded from: P3.)
| k | coefficients in Rqk (n = 256) | bytes at 12 bits |
|---|---|---|
| 2 | 512 | 768 |
| 3 | 768 | 1,152 |
| 4 | 1,024 | 1,536 |
Pause and recall optional
How many coefficients does a vector in Rqk hold for k = 3 and n = 256?
Show answer
768, which is 1,152 bytes at 12 bits each.
Is the inner product of two such vectors a number or a ring element?
Show answer
A single element of Rq.
Unit 4. Small errors, and hashes
The centred binomial distribution
Lattice schemes add small random errors. ML-KEM draws each error coefficient from the centred binomial distribution with parameter η, written CBDη: flip 2η fair coins (bits 0 or 1) and output
x = (b1 + … + bη) − (bη+1 + … + b2η).
The value x lies in −η … η, and by counting coin patterns
P(x) = C(2η, η + x) / 4η,
where C(m, r) is the binomial coefficient ("m choose r"). Exact tables:
| x | η = 1 | η = 2 | η = 3 |
|---|---|---|---|
| −3 | 0 | 0 | 1/64 |
| −2 | 0 | 1/16 | 3/32 |
| −1 | 1/4 | 1/4 | 15/64 |
| 0 | 1/2 | 3/8 | 5/16 |
| 1 | 1/4 | 1/4 | 15/64 |
| 2 | 0 | 1/16 | 3/32 |
| 3 | 0 | 0 | 1/64 |
| variance | 1/2 | 1 | 3/2 |
The mean is 0 and the variance (the average squared value) is η/2, because each coin contributes variance 1/4 and there are 2η of them.
Adding errors. Sums of independent CBD samples are again CBD: CBDη1 + CBDη2 = CBDη1+η2, because you are just flipping more coins. In particular the sum of two independent CBD1 samples is CBD2 (exercise M3), and the sum of eight is CBD8, with variance 4.
A tail bound by direct counting
Decryption in P1 and P4 fails when an accumulated error is too large. How likely is that? Take S = the sum of 8 independent CBD1 errors, so S is CBD8 and P(S = x) = C(16, 8 + x)/216. Then
P(|S| ≥ 7) = 2 · [C(16, 15) + C(16, 16)] / 216
= 2 · 17 / 65,536
= 17/32,768
≈ 5.2 × 10−4.
Compare the general-purpose Chebyshev bound, P(|S| ≥ t) ≤ Var(S)/t2 = 4/49 ≈ 8.2 × 10−2: true, but about 160 times too pessimistic here.
| t | exact P(|S| ≥ t) | Chebyshev 4/t2 |
|---|---|---|
| 4 | 2,517/32,768 ≈ 7.7 × 10−2 | 2.5 × 10−1 |
| 5 | 697/32,768 ≈ 2.1 × 10−2 | 1.6 × 10−1 |
| 6 | 137/32,768 ≈ 4.2 × 10−3 | 1.1 × 10−1 |
| 7 | 17/32,768 ≈ 5.2 × 10−4 | 8.2 × 10−2 |
| 8 | 1/32,768 ≈ 3.1 × 10−5 | 6.3 × 10−2 |
P4 computes a failure probability exactly in this way, at toy size, rather than from a loose bound. The shape of the error distribution matters: eight errors drawn uniformly from {−1, 0, 1} (variance 2/3 each, not 1/2) reach |S| ≥ 7 with probability 2/729 ≈ 2.7 × 10−3, about five times more often.
Many coefficients at once. A polynomial has n coefficients, and a failure in any one of them counts. If each coefficient independently has probability p of a bad event, the chance that none of n does is (1 − p)n. Example: CBD4 gives |x| = 4 with probability 2/256 = 1/128; across 256 coefficients, the chance that at least one has |x| = 4 is 1 − (127/128)256 ≈ 0.87. Rare per coefficient is not rare per polynomial.
Hash functions: three properties
A hash function H maps inputs of any length to an output of fixed length, say ℓ bits. Three properties, in order of how much the attacker is allowed to choose (least to most):
- Preimage resistance (one-way): given an output y, it is infeasible to find any x with H(x) = y.
- Second-preimage resistance: given x, it is infeasible to find x′ ≠ x with H(x′) = H(x).
- Collision resistance: it is infeasible to find any pair x ≠ x′ with H(x) = H(x′).
Generic attacks, which work against any function with ℓ-bit output: trying inputs until one hits a given y takes about 2ℓ evaluations (preimage and second preimage). Finding some collision takes only about 2ℓ/2, by the birthday bound: among k random outputs from N = 2ℓ possibilities,
P(no collision) = (1 − 1/N)(1 − 2/N)…(1 − (k − 1)/N)
≈ e−k(k−1)/(2N),
which drops to about 1/2 when k ≈ 1.18 √N. For N = 365 birthdays, k = 23 gives a collision probability of 0.507. For N = 232, k = 216 outputs give 1 − e−1/2 ≈ 0.39. So, against classical search, finding a collision costs about 2128 evaluations of a 256-bit hash, and a preimage about 2256. What quantum search changes is the subject of P2.
ML-KEM and ML-DSA use hashes to expand short seeds into matrices and error vectors and to bind ciphertexts to keys; SLH-DSA (P7) is built from nothing but a hash function. ML-KEM and ML-DSA instantiate them with SHA-3 and SHAKE (FIPS 202); SLH-DSA has parameter sets on SHAKE and on SHA-2. This lesson uses only the definitions above.
Pause and recall optional
What is the variance of CBDη?
Show answer
η/2: each of the 2η coins contributes 1/4.
About how many evaluations does a generic collision search on a 256-bit hash take?
Show answer
About 2128, by the birthday bound.
Exercises
Answers below each one; every number was checked by running this lesson's code.
Easy
E1. Give a mod q and the centred representative for: (a) −5, q = 17; (b) 9, q = 17; (c) 5000, q = 3329; (d) 1665, q = 3329.
Show solution, E1
(a) 12, centred −5. (b) 9, centred 9 − 17 = −8 (since 9 > 17/2). (c) 5000 − 3329 = 1671; 1671 > 1664.5, so centred 1671 − 3329 = −1658. (d) 1665 > 1664.5, so centred 1665 − 3329 = −1664. Note that 1664 itself stays 1664: the range is −1664 … 1664.
Easy
E2. Find 1000−1 mod 3329 with the extended Euclidean algorithm, and give 2−1 and 3−1 mod 3329.
Show solution, E2
3329 = 3·1000 + 329; 1000 = 3·329 + 13; 329 = 25·13 + 4; 13 = 3·4 + 1. Back-substitute: 1 = 13 − 3·4 = 13 − 3(329 − 25·13) = 76·13 − 3·329 = 76(1000 − 3·329) − 3·329 = 76·1000 − 231·329 = 76·1000 − 231(3329 − 3·1000) = 769·1000 − 231·3329. So 1000−1 ≡ 769. Check: 769,000 = 231·3329 + 1. 2−1 = (3329 + 1)/2 = 1665. 3−1: 3329 = 3·1109 + 2 and 3 = 2 + 1, so 1 = 3 − (3329 − 1109·3) = 1110·3 − 3329, giving 1110; check 3·1110 = 3330 = 3329 + 1.
Medium
M1. In Z17[X]/(X4 + 1), compute (5X3 + X + 3)(4X3 + 2X). Give the result with coefficients in 0 … 16 and centred.
Show solution, M1
Ordinary product: 20X6 + 10X4 + 4X4 + 12X3 + 2X2 + 6X = 20X6 + 14X4 + 12X3 + 2X2 + 6X, i.e. 3X6 + 14X4 + 12X3 + 2X2 + 6X mod 17. Wrap: 3X6 → −3X2; 14X4 → −14. Collect: X3: 12; X2: 2 − 3 = −1 ≡ 16; X: 6; constant: −14 ≡ 3. 12X3 + 16X2 + 6X + 3, centred −5X3 − X2 + 6X + 3.
Medium
M2. In R17 with n = 4, let u = (X + 1, 2X3) and v = (X3 + 2, X2 + 1). Compute ⟨u, v⟩.
Show solution, M2
u0v0 = X4 + X3 + 2X + 2 = X3 + 2X + 1. u1v1 = 2X5 + 2X3 = 2X3 − 2X ≡ 2X3 + 15X. Sum: 3X3 + 17X + 1 ≡ 3X3 + 1.
Medium
M3. Let x and y be independent CBD1 samples. Find the exact distribution of x + y, and show it equals CBD2.
Show solution, M3
CBD1: P(−1) = 1/4, P(0) = 1/2, P(1) = 1/4. Convolve: P(±2) = (1/4)(1/4) = 1/16 each; P(±1) = 2 · (1/4)(1/2) = 1/4 each; P(0) = (1/2)2 + 2 · (1/4)2 = 1/4 + 1/8 = 3/8. These are C(4, 2 + x)/16 = 1/16, 4/16, 6/16, 4/16, 1/16: exactly CBD2. (Directly: x + y is "two coins minus one coin, plus another coin minus another", which is two coins minus two coins.)
Hard
H1. Let q be prime and n a power of 2.
(a) Show that Xn + 1 splits into n distinct linear factors modulo q if and only if q ≡ 1 (mod 2n).
(b) For q = 3329 and n = 256, show X256 + 1 has no root mod q, but factors as the product of the 128 quadratics X2 − 17j for odd j = 1, 3, …, 255, none of which factors further.
Show solution, H1
(a) A root x satisfies xn = −1, so x2n = 1 while xn ≠ 1 (q > 2). The order of x divides 2n, a power of 2, but does not divide n, so the order is exactly 2n. Orders divide q − 1, so a root exists only if 2n | q − 1. Conversely, if 2n | q − 1, the cyclic group Zq* has an element ψ of order 2n. Then ψj for odd j = 1, 3, …, 2n − 1 are n distinct elements (distinct powers below the order), and each satisfies (ψj)n = (ψn)j = (−1)j = −1. A degree-n polynomial with n distinct roots is the product of the n linear factors: Xn + 1 = Πj odd (X − ψj).
(b) 2n = 512, and 3328 = 28 · 13 is not divisible by 512, so by (a) there is no root, hence no linear factor. But 256 | 3328, and 17 has order 256. Put Y = X2: X256 + 1 = Y128 + 1, and by the argument of (a) with n = 128 and ψ = 17, Y128 + 1 = Πj odd, 1 ≤ j ≤ 255 (Y − 17j). Substitute back: X256 + 1 = Π (X2 − 17j), 128 factors. Each X2 − 17j is irreducible, because a root would be a square root of 17j, an element of order 512, which does not exist. (Checked in code: the product of the 128 quadratics equals X256 + 1 mod 3329, and no 17j with j odd is a square.) This is why FIPS 203 multiplies in Rq by working on 128 pairs of coefficients rather than 256 single values: P3 builds the transform on this factorisation.
What this module leaves out
- Ideal lattices and algebraic number theory (why cyclotomic rings, the canonical embedding). The security proofs that use them are stated and cited in P1, not proved.
- Fast multiplication (the NTT): P3.
- Formal security definitions for hashes (games, advantage) and the random-oracle model: P4 uses them where it needs them.
- Discrete Gaussians, used by some lattice schemes (FN-DSA, P8) in place of the centred binomial.
Where the real difficulty lies: not in any single step here, but in keeping three levels apart later: an integer mod q, a polynomial of 256 such integers, and a vector of k polynomials. Every ML-KEM formula in P3 mixes all three.
Sources
- Unit 1. NIST FIPS 203, Module-Lattice-Based Key-Encapsulation Mechanism Standard (13 August 2024, DOI 10.6028/NIST.FIPS.203), §2.3: the definition of r mod m (0, …, m − 1) and ζ = 17 as a primitive 256-th root of unity mod q = 3329. Inverses, Euclid, the order of 17 and the field property are derived in the text.
- Unit 2. FIPS 203 §2.3 (the ring Rq, n = 256, q = 3329). The factorisation of X256 + 1 (exercise H1) is proved in the text; FIPS 203's NTT (P3) is built on it.
- Unit 3. FIPS 203, K-PKE.KeyGen (the matrix A generated from a 32-byte seed). Modules and the inner product are defined in the text.
- Unit 4. FIPS 203 §4.2.2, Algorithm 8 SamplePolyCBDη (the centred binomial distribution); FIPS 203 §4.1 and NIST FIPS 202, SHA-3 Standard (August 2015, DOI 10.6028/NIST.FIPS.202): SHA3-256, SHA3-512, SHAKE128, SHAKE256. NIST FIPS 205 (SLH-DSA; SHAKE and SHA-2 parameter sets). Every distribution, tail, birthday and failure number is derived in the text and checked by this lesson's code.