Learning With Errors (LWE) asks you to recover a secret vector from modular linear equations whose answers each contain a small random error, or to distinguish those noisy samples from uniform data. For the polynomial version and a worked multiplication, start with the Ring-LWE and Module-LWE guide.

Suppose you want to hide a secret number. One approach: multiply your secret by random numbers and give out the products. That’s essentially what public-key cryptography has done for decades. The problem is that if the math is too clean, a quantum computer can unravel it (that’s Shor’s algorithm).

Learning With Errors (LWE), introduced by Oded Regev in 2005 (for which he received the 2018 Gödel Prize), takes a different approach: deliberately introduce random noise into the equations. The equations become just slightly wrong — a small random error is added to each one. This tiny perturbation, it turns out, makes the whole system hard to solve with the best classical and quantum methods known today when parameters are chosen carefully. And yet, someone who knows the secret can still extract information from the noisy equations, because they know which direction to look.

Intuition

Imagine guessing someone’s bank balance by asking them, where every answer is off by a small random amount, like $1 or $2. Ask 100 times and the average gives you the exact number.

Learning With Errors (LWE) is a harder version of this game. The secret is a vector, each answer is a random linear equation in it plus a small error, and all arithmetic wraps around modulo q, so averaging no longer helps. Without the errors, Gaussian elimination would recover the secret from a handful of equations. With them, no efficient algorithm is known, classical or quantum, which is why LWE is used for post-quantum encryption.

Try the noisy equations

LWE playground: how noise breaks algebra

Drag the noise slider to 0 and watch elimination recover the secret exactly. Then turn the noise back up and add samples.

Secret s = (?, ?) · modulus q = 97 · each row is b = a₁s₁ + a₂s₂ + e (mod 97)

#a = (a₁, a₂)bhidden error e
1(27, 52)40?
2(61, 48)95?
3(75, 11)42?
samples m
3
noise bound
±3
elimination on rows 1–2 gives
(68, 55)
secrets consistent with data
6 / 9409

With errors up to ±3, the same elimination returns (68, 55), which is wrong. 6 of 9409 secrets fit all 3 samples within ±3; add samples to narrow them down.

What to notice: Without noise, recovering s is high-school linear algebra: n equations, n unknowns, done. A few units of noise make elimination useless, and the attacker is left searching for the secret that best explains every noisy equation — a bounded-distance decoding (closest-vector) problem on a lattice. Here brute force over q² = 9,409 secrets still works; ML-KEM uses n = 256·k (k = 2, 3, 4) with q = 3329, so the secret has at least 512 unknown coordinates, guessing is hopeless, and the best known attacks are lattice reduction.

Decrypt one bit before reading the reductions

Work modulo 17. Suppose the transmitted pair is u = (6, 4), v = 13 and the secret is (3, 5). The dot product is 38, so subtracting it leaves residue 9. Of the two bit encodings 0 and 8, residue 9 is closest to 8 on the modular circle. The result is bit 1. Work through how the sender gets these numbers or see the lattice behind the noisy equation.

The LWE Distribution

The formal definition of LWE pins down every parameter: the dimension, the modulus, and the error distribution. All three must be chosen together — they determine both security and correctness.

Definition 15 — The LWE Distribution

Fix parameters: dimension n≥1n \geq 1, modulus q≥2q \geq 2, and an error distribution χ\chi over Zq\mathbb{Z}_q (typically a discrete Gaussian DZ,σD_{\mathbb{Z}, \sigma} with standard deviation σ≪q\sigma \ll q).

For a secret s∈Zqns \in \mathbb{Z}_q^n, the LWE distribution As,χA_{s,\chi} is the distribution over pairs (a,b)∈Zqn×Zq(a, b) \in \mathbb{Z}_q^n \times \mathbb{Z}_q obtained by:

1. Sample a←Zqna \leftarrow \mathbb{Z}_q^n uniformly at random.
2. Sample error e←χe \leftarrow \chi.
3. Output the pair (a,  b=a⊤s+e(modq))(a,\; b = a^\top s + e \pmod{q}).

Equivalently, in matrix form: given mm samples, the distribution produces (A,b)=(A,As+e)(A, b) = (A, As + e) where A∈Zqm×nA \in \mathbb{Z}_q^{m \times n} is a random matrix, s∈Zqns \in \mathbb{Z}_q^n is the fixed secret, and e∈Zqme \in \mathbb{Z}_q^m is a vector of independent errors from χ\chi.

The parameters (n,q,χ)(n, q, \chi) fully specify the hardness of the LWE problem: larger nn and smaller σ/q\sigma/q increase security; larger qq increases correctness margin.

Decision vs Search LWE and the Equivalence

LWE has two natural formulations — one asks you to find the secret, the other asks you to detect whether a given sample is an LWE sample at all. Remarkably, these two problems are computationally equivalent.

Search-LWE

Given polynomially many samples (ai,bi)←As,χ(a_i, b_i) \leftarrow A_{s,\chi}, find the secret s∈Zqns \in \mathbb{Z}_q^n.

This is directly analogous to solving a noisy linear system. Without noise, it’s trivial via Gaussian elimination. With noise — believed infeasible in high dimension, even for quantum computers.

Decision-LWE

Given mm pairs (ai,bi)(a_i, b_i), decide: are they from As,χA_{s, \chi} for some hidden ss, or are they uniformly random in Zqn×Zq\mathbb{Z}_q^n \times \mathbb{Z}_q?

Security of encryption requires Decision-LWE to be hard: if an attacker can distinguish LWE ciphertexts from random strings, they can recover information about the plaintext.

Theorem — Search-LWE and Decision-LWE Are Equivalent

For a prime modulus qq bounded by a polynomial in the dimension, Regev’s Lemma 4.2 applies to an error distribution χ\chi on the residues modulo qq:

If there exists a polynomial-time algorithm solving Decision-LWE, then there exists a polynomial-time algorithm solving Search-LWE (Regev 2005, refined by Micciancio and Mol 2011).

The reduction works by a hybrid argument: given a Decision-LWE oracle, one can recover each coordinate of ss independently by querying samples where the sample is randomized using the guessed coordinate. Regev’s proof adds rr to the chosen coordinate of the public vector and rgrg to the response, for uniformrr and a guess gg. The correct guess preserves the sample distribution. For an incorrect guess, the response becomes uniform because the nonzero difference is invertible modulo the prime.

Consequence: to prove a scheme is semantically secure (indistinguishable under chosen-plaintext attack), one assumes Decision-LWE is hard. That is the stronger assumption: Decision-hard trivially implies Search-hard (anyone who can recover ss can also tell LWE samples from random). The equivalence says the extra strength costs nothing, because Decision-LWE is hard whenever Search-LWE is.

Suggest a correction to this section

LWE Hardness and Regev’s Reduction

Regev’s theorem gives a quantum reduction from worst-case GapSVP and SIVP at specified approximation factors to average-case plain LWE with appropriate modulus and error parameters. An efficient solver for that LWE distribution would give an efficient quantum algorithm for those worst-case problems.

ML-KEM and ML-DSA use structured module assumptions. See the Ring-LWE and Module-LWE guide for the distinction between plain, ring, and module settings.

Module-LWE (MLWE) — Used in Kyber

The version of LWE used in Kyber (ML-KEM, FIPS 203) is neither plain LWE nor Ring-LWE but a middle ground called Module-LWE. It combines the efficiency of Ring-LWE with a stronger hardness assumption, and adds a tunable security parameter.

Definition — Module-LWE (MLWE)

Let Rq=Zq[x]/(xn+1)R_q = \mathbb{Z}_q[x]/(x^n + 1) be the NTRU-style polynomial ring. Fix a module rank k≥1k \geq 1.

The Module-LWE distribution over Rqk×RqR_q^k \times R_q is defined as follows: sample secret s∈Rqks \in R_q^k (a vector of kk ring elements with small coefficients), sample a←Rqka \leftarrow R_q^k uniformly, sample error e←χe \leftarrow \chi over RqR_q, and output (a,  b=a⊤s+e)∈Rqk×Rq(a,\; b = a^\top s + e) \in R_q^k \times R_q.

In matrix form: the public key matrix is A∈Rqk×kA \in R_q^{k \times k} and the LWE relation is b=As+eb = As + e for vectors in RqkR_q^k. Each ring element encodes nn scalar LWE equations, so the full system has knkn equations in knkn unknowns.

Module rank kk controls security:
— k=2k = 2: ML-KEM-512 (NIST security category 1)
— k=3k = 3: ML-KEM-768 (NIST security category 3)
— k=4k = 4: ML-KEM-1024 (NIST security category 5)

NIST categories compare against reference attack costs. Concrete cost estimates depend on the algorithm and resource model. See FIPS 203. Compare public-key layouts.

Hardness: Langlois and Stehlé give reductions from worst-case problems on module lattices to average-case Module-LWE for specified parameters. Rank one gives the ring-shaped sample. Increasing rank at fixed ring degree makes the module larger while retaining the ring arithmetic in each entry.

Suggest a correction to this section

LWE Parameter Selection for 128-Bit Security

Choosing LWE parameters is a precise engineering task, not a guess. The standard methodology uses the Lattice Estimator tool (Albrecht et al.) to model the cost of all known attacks — primal, dual, and hybrid — and find the parameter set that minimizes key/ciphertext sizes while keeping all attack costs above the target security level.

Parameter Selection for 128-Bit Classical Security

The three parameters (n,q,σ)(n, q, \sigma) must satisfy several constraints simultaneously:

Dimension nn: Must be large enough that the best BKZ attack on the primal lattice costs ≥2128\geq 2^{128} operations. For Kyber with k=3k=3, the effective LWE dimension is n⋅k=768n \cdot k = 768. The BKZ block size needed to break this (round-3 Kyber specification) is β≈623\beta \approx 623, with Core-SVP cost 20.292⋅623≈21822^{0.292 \cdot 623} \approx 2^{182} classically. For comparison, Kyber-512 needs β≈406\beta \approx 406 (≈2118\approx 2^{118}) and Kyber-1024 β≈873\beta \approx 873 (≈2256\approx 2^{256}). Core-SVP counts only a single SVP call, so it is a conservative lower estimate: Kyber-512’s 21182^{118} is still argued to meet NIST Level 1 once the full attack cost is counted.

Modulus qq: Must be large enough that the message encoding (⌊q/2⌉⋅m)(\lfloor q/2 \rceil \cdot m) does not get overwhelmed by the accumulated error after decryption. For Kyber: q=3329q = 3329 (a prime chosen so that q≡1(mod256)q \equiv 1 \pmod{256} for efficient NTT).

Error parameter σ\sigma: In Kyber, the error distribution is a centered binomial distribution BηB_\eta with η=2\eta = 2 or 33, giving σ≈η/2≈1\sigma \approx \sqrt{\eta/2} \approx 1. This is much smaller than the Gaussian used in theoretical constructions, optimized for efficiency.

Security vs correctness tradeoff: Larger σ\sigma makes the problem harder (better security) but increases decryption failure probability. The failure probability must be kept below 2−1282^{-128} to prevent attacks that exploit decryption failures. This pins σ\sigma in a narrow window.

Quantum security factor: Parameters are chosen with a security margin: classical Core-SVP security ~182 bits, quantum ~165 bits for ML-KEM-768 (a Level 3 parameter set). Both are far above the 128-bit baseline, providing a buffer against algorithmic improvements.

LWE Parameters: The Three Knobs

Three numbers control the security and performance of any LWE-based scheme. Getting them right is the difference between a secure system and one that can be broken in hours.

Definition 16 — LWE Parameter Set (n, q, σ)

Dimension n — the number of unknowns. Bigger nn means more security (exponentially harder to solve) but larger keys and slower operations. Typical values: n=512n = 512 to 20482048.

Modulus q — all arithmetic is done mod qq. Must be large enough that the signal (the encrypted message) is not overwhelmed by noise, but not so large that security decreases. Often a prime near n2n^2 to n2.5n^{2.5}.

Noise width σ — the standard deviation of the error distribution. Smaller σ\sigma makes decryption more accurate; larger σ\sigma makes the problem harder. The ratio σ/q≈1/n\sigma/q \approx 1/n is the sweet spot for most schemes.

LWE-Based Encryption: The Basic Construction

Here is the simplest possible LWE encryption scheme, due to Regev. It encrypts one bit at a time and illustrates the core idea behind all LWE-based schemes.

Construction 1 — Regev Encryption

Key generation: Sample a secret s∈Zqns \in \mathbb{Z}_q^n. Sample a matrix AA of random LWE rows and publish (A,  b=As+e)(A,\; b = As + e) as the public key.

Encrypt a bit μ∈{0,1}\mu \in \{0,1\}: Choose a random subset of the published LWE rows, sum them up, and add μ⋅⌊q/2⌋\mu \cdot \lfloor q/2 \rfloor to the right side. This hides the bit in a random-looking sum.

Decrypt: Compute c2−c1⊤sc_2 - c_1^\top s. The errors cancel approximately, leaving something close to 0 (if μ=0\mu = 0) or close to q/2q/2 (if μ=1\mu = 1). Round to recover the bit.

Ring-LWE: The Same Idea, Much More Efficient

Plain LWE has a problem: the public key is a matrix with n2n^2 entries — that’s megabytes of data for cryptographic parameters. Ring-LWE fixes this by doing all the arithmetic inside a polynomial ring instead of plain integers. The key insight: a single ring element encodes the same information as an entire LWE matrix, thanks to the structure of polynomial multiplication.

Definition 17 — Ring-LWE

Replace Zqn\mathbb{Z}_q^n with the polynomial ring Rq=Zq[x]/(xn+1)R_q = \mathbb{Z}_q[x]/(x^n + 1) — polynomials of degree less than nn, with coefficients reduced mod qq, and the polynomial itself reduced mod xn+1x^n + 1.

A Ring-LWE sample is a pair (a,b=a⋅s+e)∈Rq×Rq(a, b = a \cdot s + e) \in R_q \times R_q where multiplication is polynomial multiplication in RqR_q. One ring element encodes nn scalar LWE equations simultaneously. Key sizes drop from O(n2)O(n^2) to O(nlog⁡q)O(n \log q).

The tradeoff: Ring-LWE operates on ideal lattices (lattices with ring structure), which have more algebraic symmetry than general lattices. Theoretically, this could be exploited. In practice, no attack has broken Ring-LWE at standard parameters, but it’s considered a slight weakening of the hardness assumption compared to plain LWE.

Module-LWE: The Best of Both Worlds

A small worked example of both variants, with the matrix that one polynomial replaces, is in 6.7 Ring-LWE and Module-LWE.

Module-LWE is the version used by Kyber and Dilithium. It’s Ring-LWE but over a module of rank kk — think of it as a k×kk \times k matrix of ring elements instead of a single ring element. This lets you tune security by adjusting kk without changing the ring structure.

LWE (plain)

Uses unstructured modular matrices. A dense square matrix stores quadratically many residues before compression. Its classical formulation occurs in theoretical constructions and FHE, not practical key exchange.

Ring-LWE

Works over one polynomial ring element. Keys: O(nlog⁡q)O(n \log q) bits. Fast (NTT multiplication). Slight algebraic structure concern. Used in early PQC schemes like NewHope.

Module-LWE (Kyber)

Works over a k×kk \times k matrix of ring elements. Keys: O(k2nlog⁡q)O(k^2 n \log q) bits. Rank kk is one parameter alongside the ring, modulus, and error distribution. The NIST-selected standard for key encapsulation.

The NTT: Why Ring-LWE Is Fast

Multiplying two degree-nn polynomials naively takes O(n2)O(n^2) multiplications. Ring-LWE schemes would be impractically slow at large nn without a trick: the Number Theoretic Transform (NTT). The NTT is like a Fourier transform but for integers modulo a prime. It converts a polynomial into its “frequency domain” in O(nlog⁡n)O(n \log n) operations. Multiplication in the NTT domain is element-wise — just multiply corresponding frequencies — then convert back. Kyber uses the NTT with n=256n = 256 and q=3329q = 3329, chosen so that the NTT is especially efficient. This is why Kyber key exchange is faster than X25519 despite using larger objects.

Check Your Understanding

  1. Q1. Without the error term, b = As mod q could be solved with Gaussian elimination. What does adding a small random error e change?

  2. Q2. What happens if a designer makes the error σ too large relative to q?

  3. Q3. In Regev encryption, why can the key holder decrypt while an eavesdropper cannot?

  4. Q4. Why does Kyber use Module-LWE instead of plain LWE or Ring-LWE?