ADVANCED · LATTICES · 60 MIN
LWE and Module-LWE, with a toy instance
Terms in this lesson
- LWE sample
- secret
- error
- search-LWE
- decision-LWE
- Regev encryption
- decryption error
- Module-LWE
- rank
- structured matrix
What you’ll learn
- State the search and decision versions of the Learning With Errors (LWE) problem, and say what is assumed about each.
- Encrypt and decrypt one bit with Regev's LWE scheme by hand, on a toy instance with q = 97.
- Derive exactly when decryption is correct, and why the threshold sits at about q/4.
- Write a Module-LWE sample, show that it is an LWE sample with a structured matrix, and count what the structure saves.
- Break the toy instance two ways, and say which parameter stops each attack at real sizes.
Prerequisites. M-C (Maths for lattices and hashes): arithmetic mod q, centred representatives, the ring Rq = Zq[X]/(Xn + 1), vectors over Rq. Site: What is lattice-based cryptography? for the picture of a grid point nudged by a small error.
Notation. Vectors are columns of integers mod q. ⟨x, y⟩ = x0y0 + … + xn−1yn−1 mod q is the inner product (gloss: multiply entry by entry and add). "Centred" means the representative in −⌊q/2⌋ … ⌊q/2⌋ (M-C unit 1). ⌊q/2⌋ is q/2 rounded down: 48 for q = 97.
1. The LWE problem
Samples
Fix a modulus q, a dimension n and a secret s ∈ Zqn (gloss: a column of n integers mod q). One LWE sample is a pair
(a, b) with b = ⟨a, s⟩ + e mod q,
where a ∈ Zqn is chosen uniformly at random and e is a small integer, the error, drawn from an error distribution χ (gloss: a rule for picking small numbers, for example uniform on {−1, 0, 1}, or the centred binomial of M-C unit 4). Collect m samples as rows: A is the m × n matrix whose rows are the ai, and
b = A s + e mod q,
e = (e0, …, em−1).
Without the error this is a linear system, and Gaussian elimination solves it once m ≥ n and A has full rank (gloss: n of its rows are independent, so the system has exactly one solution; exercise E2). The error is the whole difficulty. Figure 1 shows the picture in two dimensions.
Two problems
- Search-LWE: given (A, b) with b = As + e, find s.
- Decision-LWE: given (A, b), decide whether b = As + e (for some s and small e) or b is uniformly random in Zqm, with success probability better than 1/2 by a margin that is not negligible (gloss: not shrinking faster than every power of 1/n).
A scheme's security proof usually needs the decision version: it argues that b "looks random". For prime q bounded by a polynomial in n the two are equally hard, each reducing to the other: Regev proved a search-to-decision reduction (Regev 2009). For Module-LWE, Langlois and Stehlé give a quantum reduction from worst-case problems on module lattices to both its search and its decision version (Langlois and Stehlé 2015, Theorems 4.7 and 4.8).
What is known about hardness, and what is not
Regev proved that, for a suitable Gaussian error with width αq > 2√n (gloss: α is the error's size as a fraction of q; the width is its standard deviation up to a constant), solving LWE on average is at least as hard as solving certain lattice problems (GapSVP and SIVP to within approximation factors of about n/α; gloss: approximate versions of deciding how short a lattice's shortest vector is, and of finding n short independent vectors) in the worst case by a quantum algorithm (Regev 2009, Theorem 1.1). Peikert (2009) gave a classical reduction, from GapSVP only and for some parameters (an exponentially large modulus q).
What this does and does not say:
- It says: an algorithm that breaks random LWE instances would give a quantum algorithm for every instance of those lattice problems (Peikert: a classical one for GapSVP, with an exponentially large modulus). No efficient algorithm, classical or quantum, is known for them.
- It does not say LWE is proven hard. It reduces one unproven assumption to another.
- The reduction's parameters are not the ones deployed schemes use. Deployed parameters are set by the estimated cost of the best known attacks: the Kyber specification (ML-KEM is "derived from the round-three version of the CRYSTALS-KYBER KEM", FIPS 203 §1.1) states its security as "core-SVP hardness", an attack-cost measure (Kyber specification v3.01, §1.5). The FrodoKEM designers write that they use the worst-case reduction "only for guidance" and choose concrete parameters by "a conservative analysis of the best known cryptanalytic attacks" (FrodoKEM specification, §1.2.2). P2 returns to the cost model.
Why the dimension must be large
The toy instance used in this lesson is far too small to be secure. Exercise H2 breaks it in two ways: by trying all 974 = 88,529,281 secrets, and much faster by guessing the errors on four rows (34 = 81 guesses) and solving a 4 × 4 linear system for each guess. The two attacks cost roughly qn and |χ|n respectively (|χ| is the number of values an error can take: 3 here). Real parameters push n into the hundreds so that both are hopeless. The best known attacks are then lattice reduction, whose cost also grows exponentially in n (P2).
Pause and recall optional
What does decision-LWE ask?
Show answer
Whether b came from As + e with a small e, or is uniformly random.
Does Regev's reduction prove that LWE is hard?
Show answer
No: it shows that an algorithm for LWE would give a quantum algorithm for worst-case lattice problems, which are themselves only assumed hard.
2. A toy instance
q = 97, n = 4, m = 8. The secret and errors (each error in {−1, 0, 1}):
s = (17, 72, 8, 32),
e = (1, −1, 1, −1, 0, 0, 1, −1).
| i | ai (row i of A) | ⟨ai, s⟩ mod 97 | ei | bi |
|---|---|---|---|---|
| 0 | (15, 63, 57, 60) | 86 | 1 | 87 |
| 1 | (83, 48, 26, 12) | 27 | −1 | 26 |
| 2 | (62, 3, 49, 55) | 27 | 1 | 28 |
| 3 | (77, 0, 89, 57) | 62 | −1 | 61 |
| 4 | (34, 92, 29, 75) | 37 | 0 | 37 |
| 5 | (13, 40, 3, 2) | 85 | 0 | 85 |
| 6 | (3, 83, 69, 1) | 15 | 1 | 16 |
| 7 | (48, 87, 27, 54) | 3 | −1 | 2 |
Row 0 by hand:
15 · 17 + 63 · 72 + 57 · 8 + 60 · 32 = 255 + 4,536 + 456 + 1,920
= 7,167
7,167 = 73 · 97 + 86,
so ⟨a0, s⟩ ≡ 86
b0 = 86 + 1 = 87
The public key is (A, b). The secret key is s. Someone who sees (A, b) sees eight noisy linear equations in four unknowns.
Pause and recall optional
In the toy, how many equations and how many unknowns does the public key give?
Show answer
Eight noisy equations in four unknowns.
Which values can each error take?
Show answer
−1, 0 or 1.
3. Regev's encryption of one bit
Regev's scheme (Regev 2009, Section 5). Every property used below is derived here.
Encrypt bit μ ∈ {0, 1}. Choose a random subset S of the rows. Send u, a vector in Zqn, and v, one number:
u = Σi∈S ai mod q
v = Σi∈S bi + μ · ⌊q/2⌋ mod q
Decrypt (u, v). Compute d = v − ⟨u, s⟩ mod q, taken in 0 … q − 1. Output 1 if q/4 < d < 3q/4, otherwise 0.
For q = 97: q/4 = 24.25 and 3q/4 = 72.75, so d ∈ {25, …, 72} decodes to 1 and d ∈ {0, …, 24} ∪ {73, …, 96} decodes to 0. The bit 1 is placed at ⌊97/2⌋ = 48, the middle of the circle of residues (figure 2).
Worked example: encrypt 0 with rows {0, 2, 5}
u = a0 + a2 + a5
= (15 + 62 + 13, 63 + 3 + 40, 57 + 49 + 3, 60 + 55 + 2)
= (90, 106, 109, 117)
≡ (90, 9, 12, 20)
v = b0 + b2 + b5
= 87 + 28 + 85
= 200
≡ 6
Decrypt:
⟨u, s⟩ = 90 · 17 + 9 · 72 + 12 · 8 + 20 · 32
= 1,530 + 648 + 96 + 640
= 2,914
≡ 4
d = 6 − 4
= 2, in the 0 zone: bit 0
The 2 is e0 + e2 + e5 = 1 + 1 + 0.
Worked example: encrypt 1 with rows {1, 3, 4, 7}
u = a1 + a3 + a4 + a7
= (242, 227, 171, 198)
≡ (48, 33, 74, 4)
v = b1 + b3 + b4 + b7 + 48
= 26 + 61 + 37 + 2 + 48
= 174
≡ 77
Decrypt:
⟨u, s⟩ = 48 · 17 + 33 · 72 + 74 · 8 + 4 · 32
= 816 + 2,376 + 592 + 128
= 3,912
≡ 32
d = 77 − 32
= 45, in the 1 zone: bit 1
45 = 48 − 3, and −3 = e1 + e3 + e4 + e7 = −1 − 1 + 0 − 1.
Derivation: what decryption actually computes
Write ES = Σi∈S ei, the accumulated error. Since bi = ⟨ai, s⟩ + ei,
v − ⟨u, s⟩ = Σi∈S bi + μ⌊q/2⌋ − ⟨Σi∈S ai, s⟩
= Σi∈S (bi − ⟨ai, s⟩) + μ⌊q/2⌋
= ES + μ⌊q/2⌋ (mod q).
So d is the bit's offset (0 or ⌊q/2⌋) moved by the accumulated error. The secret cancels exactly; only the error is left.
Derivation: when decryption is correct
Take q odd, so ⌊q/2⌋ = (q − 1)/2, and suppose |ES| < q/2 (always true below).
- μ = 0: d ≡ ES. It decodes to 0 exactly when ES lies within q/4 of 0, that is |ES| < q/4.
- μ = 1: d ≡ (q − 1)/2 + ES. It decodes to 1 exactly when q/4 < (q − 1)/2 + ES < 3q/4, that is −q/4 + 1/2 < ES < q/4 + 1/2.
Both hold whenever |ES| < q/4 − 1/2, and if |ES| ≥ q/4 + 1/2 at least one of the two bits fails. In between, the outcome depends on the bit and the sign of ES, because ⌊q/2⌋ is half a step below the true midpoint q/2. For q = 97 the exact windows are: μ = 0 correct iff −24 ≤ ES ≤ 24; μ = 1 correct iff −23 ≤ ES ≤ 24 (checked for every ES by this lesson's code).
In the toy, every one of the 28 = 256 subsets has −3 ≤ ES ≤ 3 (the extremes come from the three +1s or the three −1s; Σ|ei| = 6 is only an upper bound). That is far inside the window, so this key never fails to decrypt (checked by this lesson's code for every subset, including the empty one, and both bits).
This is the trade-off every lattice scheme makes: larger errors make the problem harder and decryption riskier. Real schemes choose the error distribution so that a failure is astronomically rare but not impossible; P4 computes such a failure probability exactly at toy size.
Why the ciphertext hides the bit, and why the toy's does not
Security rests on two steps (Regev 2009, Section 5):
- Under decision-LWE, the public key (A, b) cannot be told apart from a uniformly random (A, b′).
- If (A, b′) were uniform, a random subset sum (Σ ai, Σ b′i) is statistically close to uniform once m is large enough, about (1 + ε)(n + 1) log2 q samples for a small ε > 0 (a leftover-hash-lemma argument). Then v hides μ completely.
The toy violates step 2 badly: (n + 1) log2 q = 5 log2 97 ≈ 33.0, but m = 8. There are only 2 × 28 = 512 possible ciphertexts out of 975 = 8,587,340,257 pairs (u, v), all 512 distinct (checked by this lesson's code). Anyone holding the public key can list the 256 encryptions of 0 and the 256 encryptions of 1 and look the ciphertext up, without ever touching s.
Pause and recall optional
What is left of d = v − ⟨u, s⟩ once the secret cancels?
Show answer
ES + μ⌊q/2⌋, mod q: the bit's offset moved by the accumulated error.
For q = 97, which values of d decode to 1?
Show answer
25 to 72.
4. Module-LWE
From integers to ring elements
Module-LWE replaces each integer by an element of Rq = Zq[X]/(Xn + 1) (M-C units 2 and 3). The secret is a vector s ∈ Rqk of rank k (gloss: k ring elements; this is the module's rank, not the rank of a matrix as in section 1), the matrix is A ∈ Rqk × k, the error vector e ∈ Rqk has small coefficients, and
b = A s + e (in Rqk).
ML-KEM uses n = 256, q = 3329 and k = 2, 3 or 4 (FIPS 203 §2.3 and Table 2). Its key generation computes exactly this shape, written, in the NTT domain, NTT(t) = Â ∘ ŝ + ê (FIPS 203, Algorithm 13, K-PKE.KeyGen, which writes the left side as t with a hat; gloss: a hat marks a value in the NTT domain, so Â, ŝ and ê are A, s and e in that form, and ∘ is multiplication in NTT form; P3 explains both).
Worked example (k = 2, n = 4, q = 17)
Take s and A from M-C worked example 3:
s = (1 − X2, X3 + X)
A = 2X2 + 1XX3 + 3X + 1
so As = (2X2 + 2, 2X3 + 15X2 + 2X + 2) (M-C, computed there by hand). Add the small error e = (1 − X2, X2 − X), every coefficient in {−1, 0, 1}:
b0 = (2X2 + 2) + (1 − X2)
= X2 + 3
b1 = (2X3 + 15X2 + 2X + 2) + (X2 − X)
= 2X3 + 16X2 + X + 2 (centred: 2X3 − X2 + X + 2)
Derivation: a Module-LWE sample is an LWE sample with a structured matrix
Multiplying by a fixed a ∈ Rq is a linear map on the n coefficients of s. Its matrix Ma has as column j the coefficients of a · Xj: each column is the previous one shifted down by one place, with the entry that falls off the bottom coming back at the top with its sign flipped (the negacyclic rule, M-C unit 3). For a = X3 + 3 and n = 4, listing coefficients from X0 up (−1 ≡ 16 mod 17):
Ma = 3−10003−10003−11003
Replace each of the four entries of A by its 4 × 4 block and stack the coefficients of s0 and s1 into one column of length 8. Then b is exactly A′s′ + e′ for an 8 × 8 integer matrix A′: a plain LWE instance of dimension nk = 8 with m = 8 samples (checked by this lesson's code: the same b). The difference from plain LWE is that A′ is not uniform: it is fixed by k2n = 16 numbers instead of (nk)2 = 64.
What the structure buys, counted
At ML-KEM-768's size (n = 256, k = 3, 12 bits per coefficient since 211 < 3329 < 212):
| object | plain LWE, dimension 768, m = 768 | Module-LWE, k = 3 |
|---|---|---|
| numbers that define the matrix | 7682 = 589,824 | k2n = 2,304 |
| matrix sent in full | 884,736 bytes | 3,456 bytes |
| b | 1,152 bytes | 1,152 bytes |
The matrix shrinks by a factor n = 256. b does not shrink: at the same dimension it has the same number of coefficients. ML-KEM does not send the matrix at all, only a 32-byte seed from which both sides regenerate it, so its encapsulation key is 1,152 + 32 = 1,184 bytes (FIPS 203, Table 3). A plain-LWE scheme can also send its matrix as a seed: FrodoKEM generates "a fresh matrix A for every generated key" pseudorandomly "from a small seed" (FrodoKEM specification, §1.1 and §1.3). With seeds on both sides, the public keys are the same size at the same dimension. What the module structure still saves is work: the matrix takes k2 ring products to generate and multiply, each about n log n operations with the NTT (P3), instead of (nk)2 integer products.
What the structure might cost
The extra structure is extra information for an attacker. The worst-case reduction for Module-LWE is a quantum reduction from problems on module lattices, a special class, not from all lattices (Langlois and Stehlé 2015, Theorems 4.7 and 4.8). ML-KEM's security rests on the assumption that the module structure gives an attacker nothing beyond generic lattice reduction at its parameters. It is not a theorem.
Pause and recall optional
How many numbers define the module matrix at ML-KEM-768's size, against plain LWE of the same dimension?
Show answer
k2n = 2,304 against 7682 = 589,824.
Once both send the matrix as a seed, what does the module structure still save?
Show answer
Work: k2 ring products with the NTT instead of (nk)2 integer products.
Exercises
All use the toy key of section 2 unless stated. Try each before opening its solution; every answer is checked by this lesson's code.
Easy
E1. Decrypt the ciphertexts (u, v) = ((2, 81, 50, 34), 32) and ((49, 4, 13, 29), 18).
Show solution, E1
First ciphertext:
⟨u, s⟩ = 2 · 17 + 81 · 72 + 50 · 8 + 34 · 32
= 34 + 5,832 + 400 + 1,088
= 7,354
7,354 = 75 · 97 + 79,
so ⟨u, s⟩ ≡ 79
d = 32 − 79 = −47 ≡ 50,
and 25 ≤ 50 ≤ 72: bit 1
It was made from rows {2, 4, 6}: 50 = 48 + 2, and e2 + e4 + e6 = 1 + 0 + 1.
Second ciphertext:
⟨u, s⟩ = 49 · 17 + 4 · 72 + 13 · 8 + 29 · 32
= 833 + 288 + 104 + 928
= 2,153
2,153 = 22 · 97 + 19,
so ⟨u, s⟩ ≡ 19
d = 18 − 19
= −1
≡ 96, in the 0 zone: bit 0
Rows {0, 1, 7}: e0 + e1 + e7 = 1 − 1 − 1 = −1.
Easy
E2. Explain why an error of zero makes s recoverable by Gaussian elimination. Then solve: q = 97, A = 3572, b = (33, 13), e = 0. Find s.
Show solution, E2
With e = 0, b = As is a linear system mod q. Since q is prime, Zq is a field (M-C unit 1), so elimination works exactly as over the rationals and any n rows with an invertible n × n submatrix give s. Here:
det A = 3 · 2 − 5 · 7 = −29 ≡ 68
68−1 ≡ 10,
because 68 · 10 = 680
= 7 · 97 + 1
By Cramer's rule (or elimination):
s0 = 10 · (2 · 33 − 5 · 13)
= 10 · 1
= 10
s1 = 10 · (3 · 13 − 7 · 33)
= 10 · (−192)
≡ 10 · 2
= 20 (−192 + 2 · 97 = 2)
Check: 3 · 10 + 5 · 20 = 130 ≡ 33; 7 · 10 + 2 · 20 = 110 ≡ 13.
Medium
M1. Use the decision rule of section 3 (decode 1 iff q/4 < d < 3q/4) with q = 97 and m = 8. Find the largest E such that, for every choice of errors with |ei| ≤ E and every subset, both bits always decrypt correctly. First with the bit-1 offset ⌊q/2⌋ = 48, then with ⌈q/2⌉ = 49.
Show solution, M1
With every |ei| ≤ E, an adversarial choice of errors and subset can make ES any integer in −8E … 8E. With offset 48 the windows are −24 ≤ ES ≤ 24 (bit 0) and −23 ≤ ES ≤ 24 (bit 1), so we need 8E ≤ 23: E = 2 (range −16 … 16). E = 3 fails: all eight errors equal to −3 give ES = −24, and a 1 becomes d = 48 − 24 = 24 < 24.25, which decodes to 0.
With offset 49 the bit-1 window is −24 ≤ ES ≤ 23, and E = 3 fails the other way: ES = +24 gives d = 73 > 72.75. Again E = 2. The naive bound 8E < q/4 suggests 3; the half-step offset of ⌊q/2⌋ or ⌈q/2⌉ is what costs the last unit.
Medium
M2. n = 256, k = 2, q = 3329, 12 bits per coefficient. (a) Count the bytes of the public matrix and of b for plain LWE of dimension 512 with m = 512 samples, when the matrix is sent in full. (b) The same for Module-LWE of rank 2. (c) Both schemes now send the matrix as a 32-byte seed. Give both public-key sizes and say what the module structure still saves.
Show solution, M2
(a) Plain LWE: 512 × 512 = 262,144 entries × 12 bits = 393,216 bytes; b: 512 × 12 bits = 768 bytes.
(b) Module-LWE: 2 × 2 = 4 ring elements × 256 coefficients = 1,024 coefficients = 1,536 bytes; b: 2 × 256 = 512 coefficients = 768 bytes.
(c) Both: 768 + 32 = 800 bytes, which is ML-KEM-512's encapsulation key (FIPS 203, Table 3). The public keys are now equal. The module structure still saves work: generating and multiplying by 4 ring elements (each product about n log n operations with the NTT) instead of 262,144 independent entries, a factor n = 256 in the matrix alone.
Hard
H1. A designer picks q = 64 and errors in {−2, 0, 2}. You receive A (8 × 4) and one of two vectors b. Show how to tell, with certainty in one direction, which b is an LWE sample, and recover s mod 2. Then explain why the same choice of even errors is harmless when q is odd.
A = 154013285362563001014361257162402650324445489431137365818393
b(1) = (22, 27, 32, 3, 10, 30, 62, 3)
b(2) = (62, 50, 33, 53, 61, 3, 28, 16)
Show solution, H1
Because q = 64 is even, reduction mod 64 preserves parity, so b ≡ As + e ≡ As (mod 2): the error vanishes mod 2. Reduce everything mod 2:
A mod 2 = 10101000000001100000010111100011
b(1) mod 2 = (0, 1, 0, 1, 0, 0, 0, 1)
b(2) mod 2 = (0, 0, 1, 1, 1, 1, 0, 0)
Rows 2 and 4 of A are zero mod 2, so an LWE sample must have b2 and b4 even. b(2) has b2 = 33 and b4 = 61, both odd: it cannot be an LWE sample with even errors. b(1) passes.
Solve for s mod 2 from b(1): row 1 gives s0 = 1; row 0, s0 + s2 = 0, gives s2 = 1; row 3, s1 + s2 = 1, gives s1 = 0; row 7, s2 + s3 = 1, gives s3 = 0. Rows 5 and 6 check: s1 + s3 = 0 and s0 + s1 + s2 = 0. So s ≡ (1, 0, 1, 0) mod 2, the only solution (A mod 2 has rank 4).
In general the test is whether b mod 2 lies in the column space of A mod 2; here that space has 24 of the 28 parity patterns, so a uniform b passes with probability 1/16 and an LWE b always passes. A uniform b passes with probability 2rank − m, so once A mod 2 has full rank each extra sample halves it.
When q is odd, 2 has an inverse (2−1 ≡ 49 mod 97). Multiplying the whole sample by 49 gives b′ = A′s + e′ with A′ = 49A uniform and e′ = e/2 ∈ {−1, 0, 1}: still an LWE sample with small error, and parity carries no information because reduction mod an odd q scrambles it.
Hard
H2. Break the toy key of section 2 from (A, b) alone. (a) By brute force over all secrets: how many are there, and how many fit with every error in {−1, 0, 1}? Write and run the search. (b) Faster: guess the errors on rows 0–3, solve the 4 × 4 system for s, and test the guess on rows 4–7. How many guesses, and how many survive? Which parameter makes each attack infeasible at ML-KEM's size?
Show solution, H2
(a) There are 974 = 88,529,281 secrets. For each, compute b − As centred and keep s if every entry is in {−1, 0, 1}. Exactly one survives, (17, 72, 8, 32) (a numpy search takes a few seconds).
(b) Rows 0–3 of A form an invertible 4 × 4 matrix mod 97. For each of the 34 = 81 guesses g of (e0, e1, e2, e3), solve A0..3 s = b0..3 − g, then check that rows 4–7 leave errors in {−1, 0, 1}. Exactly one guess survives, g = (1, −1, 1, −1), giving s = (17, 72, 8, 32).
The first attack costs qn, the second |χ|n linear solves; both are exponential in n. With n = 256 coefficients per ring element and k = 3, the dimension is 768: even with only three possible values per error that is 3768 guesses (ML-KEM-768's errors take five values, −2 … 2), far beyond any computer, and the best known attacks are then lattice reduction (P2). The small q does not save the toy either; the dimension does.
Code for (b), pure Python 3, no libraries (output: [((1, -1, 1, -1), [17, 72, 8, 32])]):
import itertools
q = 97
A = [[15, 63, 57, 60],
[83, 48, 26, 12],
[62, 3, 49, 55],
[77, 0, 89, 57],
[34, 92, 29, 75],
[13, 40, 3, 2],
[ 3, 83, 69, 1],
[48, 87, 27, 54]]
b = [87,26,28,61,37,85,16,2]
def solve(M, v): # Gauss-Jordan mod q (q prime)
M = [row[:] + [x] for row, x in zip(M, v)]
for c in range(len(M)):
p = next(r for r in range(c, len(M)) if M[r][c] % q)
M[c], M[p] = M[p], M[c]
inv = pow(M[c][c], -1, q)
M[c] = [x * inv % q for x in M[c]]
for r in range(len(M)):
if r != c:
f = M[r][c]
M[r] = [(x - f * y) % q for x, y in zip(M[r], M[c])]
return [row[-1] for row in M]
def centred(x):
x %= q
return x - q if x > q // 2 else x
hits = []
for g in itertools.product([-1, 0, 1], repeat=4):
s = solve(A[:4], [(b[i] - g[i]) % q for i in range(4)])
if all(abs(centred(b[i] - sum(a * t for a, t in zip(A[i], s)))) <= 1
for i in range(4, 8)):
hits.append((g, s))
print(hits)
What this lesson does not cover
The proofs of the worst-case to average-case reductions (stated and cited, not proved); discrete Gaussian errors; Ring-LWE as a separate problem (it is Module-LWE with k = 1); lattice-reduction attacks and how they set n (P2 touches the cost model); the encryption scheme ML-KEM actually uses, which replaces the random subset by a small random vector and compresses the ciphertext (P3). Where the real difficulty lies: not in the algebra, which is a few lines, but in the choice of q, n and χ: the same equations are trivially broken (H1, H2), impossible to decrypt (errors too large), or secure, depending only on those three numbers.
Sources
- Sections 1 and 3. O. Regev, "On lattices, learning with errors, random linear codes, and cryptography", J. ACM 56(6), 2009 (conference version STOC 2005); author's version arXiv:2401.03703. Theorem 1.1 (the quantum worst-case reduction, αq > 2√n, approximation factor Õ(n/α)); Section 5 (the public-key cryptosystem); the search-to-decision reduction is in the same paper. C. Peikert, "Public-key cryptosystems from the worst-case shortest vector problem", STOC 2009, §1.1 (classical reduction from GapSVP, moduli q ≥ 2n/2).
- Section 1, parameter setting. CRYSTALS-Kyber specification, version 3.01, 31 January 2021, §1.5. FrodoKEM specification, 4 June 2021, §1.1, §1.2.2, §1.3.
- Section 4. A. Langlois and D. Stehlé, "Worst-case to average-case reductions for module lattices", Designs, Codes and Cryptography 75 (2015); IACR ePrint 2012/090. Theorems 4.7 and 4.8 (quantum reduction from worst-case problems on module lattices to Module-LWE, search and decision). NIST FIPS 203, Module-Lattice-Based Key-Encapsulation Mechanism Standard (13 August 2024): §1.1 (derived from CRYSTALS-KYBER), §2.3 (n = 256, q = 3329), Algorithm 13 (K-PKE.KeyGen), §8 Table 2 (k = 2, 3, 4) and Table 3 (800- and 1,184-byte encapsulation keys).
- Every number in this lesson, every worked example and every exercise answer is checked by this lesson's code.