Guide Cybersecurity 6 min read

Learning With Errors, the lattice puzzle behind post-quantum encryption, worked by hand

Learning With Errors, the puzzle under the post-quantum standard ML-KEM, hides a secret behind equations that are each slightly wrong. We worked one by hand, then measured how often a small Regev-style toy fails to decrypt, averaged over 2,000 keys, and how easily a textbook attack breaks it.

Kenji Tanaka
Developer Tools & Cloud Analyst · Singapore
Published 29 Nov 2026, 11:50 PM (SGT)
Share:
An orange pencil lies across an open spiral notebook of squared paper, with a crumpled sheet of the same paper behind it.
Photo by congerdesign on Pixabay
Advertisement

Key points

  • Learning With Errors asks for a secret hidden behind linear equations that are each slightly wrong. In our two-unknown example modulo 17, five noisy equations left exactly one of 289 candidate secrets.
  • Our Regev-style toy, simplified (16 unknowns, 64 equations, modulo 257), failed to decrypt about 2% of the time at noise bound 8, about 11% at 12 and about 23% at 16, averaged over 2,000 keys per bound.
  • The rate depends on the key. The first key we ran failed 3.6% of the time at bound 12, about a third of the average, so one key on its own understated the problem.
  • A textbook LLL attack we wrote in numpy recovered the secret for all 20 keys we tried at bound 8, for 51 of 70 at bound 12 and for none of 20 at bound 20. We did not run BKZ, a stronger attack.
  • NIST’s March 2025 report calls HQC’s stable decryption-failure analysis “the decisive factor” in choosing it over BIKE.

Learning With Errors (LWE) is a puzzle about linear equations that are each slightly wrong. A generalised form of it sits under ML-KEM, the NIST standard meant to replace key exchanges that Shor’s algorithm would break. We built a small version of the scheme Oded Regev introduced in 2005 and measured what the noise costs. Averaged over 2,000 keys, our toy failed to decrypt about 2% of the time when each error could reach ±8, and about 11% at ±12. A textbook attack we wrote still broke 51 of 70 keys at ±12.

Five noisy equations pin down a secret pair

Take two unknowns, s1 and s2, with arithmetic modulo 17, and the secret (3, 5). Each published row multiplies two coefficients by the secret, adds an error of −1, 0 or +1, and shows only the coefficients and the total. Row 1 is 2·3 + 7·5 + 1 = 42, which is 8 modulo 17. Without errors, rows 1 and 2 would give the secret by school-algebra elimination. With them, each row only narrows the field, so we tested all 289 possible pairs.

Row 1, 2·s1 + 7·s2 ≈ 8 modulo 17, leaves 51 of the 289 candidate pairs, shown as orange dots on a 17 by 17 grid.

Row 1 leaves 51

Any pair that comes within one of 8 still fits.

Adding row 2, 12·s1 + 1·s2 ≈ 7, cuts the candidates to 9 orange dots on the grid.

Row 2 leaves 9

Two noiseless rows would already give the answer. Two noisy ones leave nine.

Adding row 3, 13·s1 + 3·s2 ≈ 3, leaves 3 orange dots on the grid.

Row 3 leaves 3

Adding row 4, 7·s1 + 2·s2 ≈ 15, leaves two circled candidates, the real secret (3, 5) and an impostor (12, 0).

Row 4 leaves 2

The impostor (12, 0) is within one of every row so far.

Adding row 5, 16·s1 + 8·s2 ≈ 2, leaves one circled candidate, the secret (3, 5).

Row 5 leaves 1

For (12, 0), row 5 gives 192, which is 5 modulo 17, three away from the published 2.

Worked encryption of the bit 1 from rows 1 to 3. The sums give u = (10, 11) and v = 9. With the secret, u·s is 0 modulo 17, so d = 9, which is nearer 8 than 0 and decrypts to 1.

Encrypt and decrypt one bit

Three summed rows carry at most ±3 of error, so the bit always survives.

1 / 6

Four rows still leave an impostor, (12, 0), within one of every total. Row 5 removes it, and a sixth row, kept as a check, agrees. At 16 unknowns modulo 257, the size of the toy below, trying every candidate stops being a pencil job.

Summing rows hides a single bit

FIPS 203, the NIST standard that defines ML-KEM, describes the trick in its §3.2. A zero is sent as “an approximately correct linear equation” and a one as “a far-from-correct linear equation”.

“Encryption can then be delegated to another party by publishing a large collection of noisy linear equations, which can be combined appropriately by the encrypting party.”

Our sender adds up a random subset of rows and, for a 1, adds 8, half of 17 rounded down. The receiver subtracts what the secret predicts, leaving the summed error plus 8 for a 1, and reads a 1 if that remainder sits more than 4 from 0. In the worked example, sending a 1 by summing rows 1 to 3 leaves 9, one step from 8, so the receiver reads a 1.

Three summed rows are always safe here, because their errors total at most ±3. Four rows whose errors are all −1 put an encrypted 1 on 4, exactly as far from 0 as from 8. Regev’s correctness condition gives the same limit, a summed error smaller than 4.

Failure rates swing from key to key

The toy we measured has 16 unknowns, 64 rows and arithmetic modulo 257, with each error drawn evenly from −B to +B. It is a Regev-style toy, simplified, after Oded Regev’s “On lattices, learning with errors, random linear codes, and cryptography” (Journal of the ACM, 2009). Regev draws errors from a rounded normal distribution where we use a flat one, and by our arithmetic his parameters need more than 94 rows where we use 64.

Advertisement

FIPS 203 counts ML-KEM’s failure rate over the random choice of keys (“uniformly random seeds d, z”) as well as over the message, and Regev’s 2009 analysis also counts the choice of keys. So we averaged over 2,000 keys per bound (seed 20261126), computing each key’s rate exactly in Python 3.9.6 with numpy 2.0.2 on an Apple M4 under macOS 26.7.2.

Noise bound BMean over 2,000 keysMedian keyMiddle 90% of keys
10%0%0%
4about 1 in 100,000 (0.0005% to 0.0015% across our estimates)0%0 to under 0.001%
82.1%0.49%0.05–9.8%
12about 11%6.2%1.9–37.8%
1623.2%16.9%8.0–60.0%
Line chart of the toy’s decryption failure rate against noise bound B. The mean over 2,000 keys rises from near 0 at B = 4 to about 2% at 8, 11% at 12 and 23% at 16, inside a band from p5 to p95 that reaches 60%. One key, shown dashed, stays well below the mean.
Each key’s rate computed exactly; n = 16, m = 64, q = 257. Python 3.9.6, numpy 2.0.2, Apple M4, 29 November 2026. RECATOOLS, measured 29 November 2026

Bound 4 rests on a handful of rare keys, so we give only its order of magnitude. Bound 12 moved between 11.1% and 11.5% across four runs, one of them a million full runs of the scheme with a fresh key each time.

The first key we ran, seeded 2026, failed 3.6% of the time at bound 12, a third of the average. In our analysis, most of the spread comes from how far a key’s errors lean to one side; at bound 12 the correlation between that lean and the failure rate was 0.94. Decryption failures are a mature research topic, and a 2018 paper by D’Anvers, Vercauteren and Verbauwhede showed that attackers can use them against schemes “that have a relatively high failure rate”.

Download

Our toy LWE failure-rate and attack measurements

Failure rates averaged over keys for two seeds, the million-run full-scheme check with its 95% interval, and the LLL attack result for every key batch, with parameters, software, machine and date in each row. Measured by RECATOOLS on 29 November 2026.

CSV · 6.5 KB

Download the CSV — Our toy LWE failure-rate and attack measurements (CSV)

Our textbook attack broke the toy at low noise

A lattice is a regular grid of points that repeats in every direction, here in 65 dimensions. The standard attack on LWE arranges the 64 published equations as such a grid, in which the secret errors form an unusually short arrow from one point to another, and then searches for that short arrow. We wrote the textbook search method, LLL, ourselves in numpy (with its usual setting δ = 0.99). A key counted as broken when the exact errors and secret came out. Each attack took 1.3 to 1.9 seconds on the same Mac, at a one-minute load average of 2.7 to 3.7.

Bar chart of the share of fresh keys whose secret our LLL attack recovered. It broke 20 of 20 keys at bounds 4, 6, 8, 10 and 11, 19 of 20 at 9, 51 of 70 at 12, 26 of 50 at 13, 18 of 50 at 14, 9 of 70 at 16 and none of 20 at 20.
Fresh keys from two seeded batches (99 and 100). A key counts as broken when its exact errors and secret were recovered. RECATOOLS, measured 29 November 2026

At bound 8, where the toy almost always decrypts, it broke all 20 keys. At bound 12 it broke 51 of 70, and at 13 it broke 26 of 50. From 14 it lost most keys, breaking 18 of 50 there and 9 of 70 at 16, where the toy garbles about 23% of its bits, and at 20 it broke none of 20. The seed-2026 key resisted at bound 12, so one key misleads in this direction too. A stronger method called BKZ, which we did not run, might break more. Nothing here shows any setting of this toy to be secure.

NIST weighed failure rates when it chose HQC

Table 1 of FIPS 203, “Decapsulation failure rates for ML-KEM”, gives 2⁻¹³⁸·⁸ for ML-KEM-512, the smallest set, and lower rates for the other two. Our toy says nothing about how those parameters were chosen.

When NIST picked HQC as a backup key-encapsulation mechanism, its report, NIST IR 8545, gave the reason. “The decisive factor in favor of HQC relative to BIKE is HQC’s stable DFR analysis.” DFR is the decryption failure rate, and a low one, it adds, “is required to achieve IND-CCA2 security”. The same report notes that HQC “is similar in structure to Learning with Errors (LWE)-based cryptosystems, like Regev”.

Where the other families stand in NIST’s words

Quoted cells come from FIPS 203, FIPS 205, NIST IR 8413-upd1, IR 8545 and NIST IR 8610.

FamilyWhat it rests onWhere NIST stands
Lattice“the presumed hardness of the so-called Module Learning with Errors (MLWE) problem”Standardised as ML-KEM (FIPS 203) and ML-DSA (FIPS 204)
Hash-based“the presumed difficulty of finding preimages for hash functions”Standardised as SLH-DSA (FIPS 205), beside SP 800-208 for stateful schemes
Code-based“the general- and syndrome-decoding problems”“NIST has only selected HQC for standardization”; Classic McEliece “is no longer under consideration for standardization as part of the current NIST PQC Standardization Process”, pending ISO’s work on it
Multivariate“the MQ problem and the MinRank problem”UOV, MAYO, QR-UOV and SNOVA in round 3 of NIST’s additional-signature process; NIST “anticipates a longer timeline”
IsogenyFor SQIsign, “finding isogenies between supersingular elliptic curves”SIKE “eliminated”; SQIsign in round 3

Rainbow, a multivariate scheme, fell to “private key recovery … in a little over two days of computation time on a single laptop”, in IR 8413-upd1’s words, and IR 8545 says SIKE’s secret isogeny “can be efficiently recovered from the public key”. Both are told in our guide to two post-quantum breaks and a timing leak, and the standards’ key and signature sizes in our guide to the NIST standards, read in the text.

Advertisement
Kenji Tanaka
Developer Tools & Cloud Analyst · Singapore

Kenji Tanaka covers developer tools, cloud platforms, DevOps, CI/CD, and software supply-chain topics for RECATOOLS.

View author profile → · Editorial policy

About this byline Kenji Tanaka is a RECATOOLS editorial persona for developer tools, cloud, DevOps, and software supply-chain coverage. Articles are produced and reviewed under RECATOOLS editorial supervision.

Corrections policy

Go further

More from Cybersecurity

Advertisement
Advertisement