Learning With Errors
Solving a linear system modulo is easy: Gaussian elimination. Add a little noise, with small random , and the problem is believed to be hard, even for quantum computers. That problem, Learning With Errors (LWE, Regev 2005), is the foundation of ML-KEM (Kyber), the post-quantum key exchange now deployed in browsers.
The problem
Fix a modulus and dimension . Pick a secret , a public random matrix and a small error vector (entries like ). Publish
Search-LWE: recover . Decision-LWE: distinguish from uniformly random pairs. Regev showed that solving LWE on average would solve worst-case lattice problems (a closest-vector problem in the lattice generated by ), which is why it is trusted.
Noise is everything
Without , linear algebra modulo a prime recovers from equations. With it, elimination amplifies the noise until it covers everything.
import numpy as np
rng = np.random.default_rng(42)
q, n = 97, 4
s = rng.integers(0, q, n, dtype=np.int64)
A = rng.integers(0, q, (n, n), dtype=np.int64)
e = rng.integers(-2, 3, n, dtype=np.int64)
def solve_mod(A, b, q): # Gauss-Jordan elimination modulo a prime q
M = np.concatenate([A % q, (b % q)[:, None]], axis=1).astype(np.int64)
k = len(b)
for c in range(k):
piv = next(r for r in range(c, k) if M[r, c] % q)
M[[c, piv]] = M[[piv, c]]
M[c] = M[c] * pow(int(M[c, c]), -1, q) % q
for r in range(k):
if r != c:
M[r] = (M[r] - M[r, c] * M[c]) % q
return M[:, -1]
print("secret: ", s.tolist())
print("solve without noise: ", solve_mod(A, A @ s, q).tolist())
print("solve with noise: ", solve_mod(A, A @ s + e, q).tolist())secret: [8, 75, 63, 42]
solve without noise: [8, 75, 63, 42]
solve with noise: [16, 26, 70, 65]
The error of size 2 turned into completely wrong coordinates.
Regev's encryption of one bit
Key generation: secret ; public key with rows.
Encrypt a bit : choose a random 0/1 vector (a random subset of the rows) and send
Decrypt: . The noise term is small, so the result is near 0 for and near for .
import numpy as np
rng = np.random.default_rng(7)
q, n, m = 3329, 16, 40 # q = 3329 is ML-KEM's modulus
s = rng.integers(0, q, n, dtype=np.int64)
A = rng.integers(0, q, (m, n), dtype=np.int64)
e = rng.integers(-2, 3, m, dtype=np.int64)
b = (A @ s + e) % q
def encrypt(bit):
r = rng.integers(0, 2, m, dtype=np.int64)
return (r @ A) % q, int((r @ b + bit * (q // 2)) % q)
def decrypt(u, v):
d = (v - int(u @ s)) % q
return int(q // 4 < d < 3 * q // 4), d
bits = [1, 0, 1, 1, 0, 0, 1, 0]
results = [decrypt(*encrypt(x)) for x in bits]
print("sent: ", bits)
print("decrypted:", [r[0] for r in results])
print("v - u.s: ", [r[1] for r in results])sent: [1, 0, 1, 1, 0, 0, 1, 0]
decrypted: [1, 0, 1, 1, 0, 0, 1, 0]
v - u.s: [1659, 3327, 1656, 1663, 0, 2, 1664, 3325]
The decrypted values cluster near 0 (or just below , which is ) and near . As long as the accumulated noise stays below , decryption is always correct. Parameter choice balances this against security: more noise is more secure but risks decryption failures.
From LWE to ML-KEM
Plain LWE needs an public matrix, which is large. ML-KEM uses Module-LWE: the entries of are polynomials in , so a small matrix of polynomials stands for a huge structured matrix, and multiplication is fast with the number-theoretic transform (an FFT modulo 3329). ML-KEM-768 has public keys of 1184 bytes and ciphertexts of 1088 bytes, larger than X25519's 32 bytes but fast to compute.
In software
- ML-KEM (FIPS 203) is in OpenSSL 3.5, BoringSSL and Go 1.24; Chrome, Firefox and Cloudflare negotiate the hybrid
X25519MLKEM768in TLS 1.3 by default. - The same LWE structure gives ML-DSA signatures (FIPS 204) and fully homomorphic encryption (BFV, CKKS), which computes on encrypted data.
Exercises
Exercise 1: Why noise?
Why is LWE without noise insecure?
Answer
is solved by Gaussian elimination in polynomial time.
Solution
With independent equations modulo a prime, elimination recovers in operations. The noise breaks exact linear algebra.
Exercise 2: Decryption threshold
With , which values of decrypt to 1?
Answer
Those strictly between and .
Solution
0 is encoded near 0 (equivalently near ), 1 near . The decision boundary is halfway, at and .
Exercise 3: Too much noise (code)
Repeat the Regev example with errors drawn from instead of and count decryption errors over 200 bits.
Answer
5 of the 200 bits decrypt wrongly, because can exceed .
Solution
import numpy as np
rng = np.random.default_rng(7)
q, n, m = 3329, 16, 40
s = rng.integers(0, q, n, dtype=np.int64)
A = rng.integers(0, q, (m, n), dtype=np.int64)
e = rng.integers(-150, 151, m, dtype=np.int64)
b = (A @ s + e) % q
errors = 0
for i in range(200):
bit = i % 2
r = rng.integers(0, 2, m, dtype=np.int64)
u, v = (r @ A) % q, int((r @ b + bit * (q // 2)) % q)
d = (v - int(u @ s)) % q
errors += int(q // 4 < d < 3 * q // 4) != bit
print("decryption errors:", errors, "of 200")decryption errors: 5 of 200
Each error has standard deviation about , so the sum of about 20 of them has a standard deviation near and sometimes crosses . (With errors up to 60 the same run has no failures: the sum stays more than 5 standard deviations below the threshold.): parameters must keep this probability negligible (ML-KEM targets below ).
Test yourself
LWE samples have the form…
- with small
In Regev encryption, a bit 1 is encoded near…
- 0
- 1
ML-KEM is based on…
- factoring
- elliptic curves
- Module-LWE over polynomial rings
- hash chains