TExcelTAD
Home › Number Theory & Cryptography › Learning With Errors

Learning With Errors

Solving a linear system As=bA\mathbf{s} = \mathbf{b} modulo qq is easy: Gaussian elimination. Add a little noise, As+e=bA\mathbf{s} + \mathbf{e} = \mathbf{b} with small random e\mathbf{e}, 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 qq and dimension nn. Pick a secret s∈Zqn\mathbf{s} \in \mathbb{Z}_q^n, a public random matrix A∈Zqm×nA \in \mathbb{Z}_q^{m \times n} and a small error vector e\mathbf{e} (entries like −2,…,2-2, \ldots, 2). Publish

(A, b=As+e mod q)(A, \ \mathbf{b} = A\mathbf{s} + \mathbf{e} \bmod q)

Search-LWE: recover s\mathbf{s}. Decision-LWE: distinguish (A,b)(A, \mathbf{b}) 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 AA), which is why it is trusted.

Noise is everything

Without e\mathbf{e}, linear algebra modulo a prime recovers s\mathbf{s} from nn equations. With it, elimination amplifies the noise until it covers everything.

python · try it yourself
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 s\mathbf{s}; public key (A,b=As+e)(A, \mathbf{b} = A\mathbf{s} + \mathbf{e}) with mm rows.

Encrypt a bit μ\mu: choose a random 0/1 vector r\mathbf{r} (a random subset of the rows) and send

u=rTA,v=rTb+μ⌊q2⌋\mathbf{u} = \mathbf{r}^T A, \qquad v = \mathbf{r}^T \mathbf{b} + \mu \left\lfloor \tfrac{q}{2} \right\rfloor

Decrypt: v−u⋅s=rTe+μ⌊q/2⌋v - \mathbf{u} \cdot \mathbf{s} = \mathbf{r}^T\mathbf{e} + \mu \lfloor q/2 \rfloor. The noise term rTe\mathbf{r}^T\mathbf{e} is small, so the result is near 0 for μ=0\mu = 0 and near q/2q/2 for μ=1\mu = 1.

python · try it yourself
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 qq, which is −small-\text{small}) and near q/2=1664q/2 = 1664. As long as the accumulated noise stays below q/4q/4, 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 m×nm \times n public matrix, which is large. ML-KEM uses Module-LWE: the entries of AA are polynomials in Zq[x]/(x256+1)\mathbb{Z}_q[x]/(x^{256} + 1), 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 X25519MLKEM768 in 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

As=bA\mathbf{s} = \mathbf{b} is solved by Gaussian elimination in polynomial time.

Solution

With nn independent equations modulo a prime, elimination recovers s\mathbf{s} in O(n3)O(n^3) operations. The noise breaks exact linear algebra.

Exercise 2: Decryption threshold

With q=3329q = 3329, which values of v−u⋅sv - \mathbf{u} \cdot \mathbf{s} decrypt to 1?

Answer

Those strictly between q/4≈832q/4 \approx 832 and 3q/4≈24963q/4 \approx 2496.

Solution

0 is encoded near 0 (equivalently near qq), 1 near ⌊q/2⌋=1664\lfloor q/2 \rfloor = 1664. The decision boundary is halfway, at q/4q/4 and 3q/43q/4.

Exercise 3: Too much noise (code)

Repeat the Regev example with errors drawn from [−150,150][-150, 150] instead of [−2,2][-2, 2] and count decryption errors over 200 bits.

Answer

5 of the 200 bits decrypt wrongly, because rTe\mathbf{r}^T\mathbf{e} can exceed q/4q/4.

Solution
python · try it yourself
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 150/3≈87150/\sqrt{3} \approx 87, so the sum of about 20 of them has a standard deviation near 8720≈39087\sqrt{20} \approx 390 and sometimes crosses q/4=832q/4 = 832. (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 2−1382^{-138}).

Test yourself

LWE samples have the form…

  1. AsA\mathbf{s}
  2. As+e mod qA\mathbf{s} + \mathbf{e} \bmod q with small e\mathbf{e}
  3. gs mod pg^s \bmod p
  4. H(s)H(s)

In Regev encryption, a bit 1 is encoded near…

  1. 0
  2. q/2q/2
  3. qq
  4. 1

ML-KEM is based on…

  1. factoring
  2. elliptic curves
  3. Module-LWE over polynomial rings
  4. hash chains