Library · |post-quantum⟩ The quantum-safe fix

Lattice cryptography, explained

Lattices, learning with errors and module lattices; how ML-KEM and ML-DSA work at a high level; and why no quantum shortcut is known.

Almost every system that adopts post-quantum cryptography in the next few years will use a lattice scheme. The TLS handshake in your browser, the SSH session to your server, the signature on your next firmware update and the key service in your cloud will most likely rest on ML-KEM or ML-DSA. A security architect who signs off on that choice should understand what it rests on.

This article builds the idea from the ground up, using a grid on a page and a calculation small enough to do by hand. It then shows, at a high level, how ML-KEM and ML-DSA use it. You do not need linear algebra. For the standards themselves and their parameter sets, see The NIST post-quantum standards.

What a lattice is

Picture graph paper with a dot at every crossing of two lines. That is a lattice in two dimensions. Every dot can be reached from the origin by taking whole steps right or left and whole steps up or down. In more dimensions the idea is the same. A lattice is the set of all points you can reach by adding and subtracting whole-number multiples of some chosen vectors.

Those chosen vectors are a basis. In the graph-paper example, one basis is a step of one unit right and a step of one unit up. The basis is a recipe for the grid, not the grid itself. The same lattice has many different bases, and that is where the difficulty comes from.

L = { a₁·b₁ + a₂·b₂ + … + aₙ·bₙ  |  a₁, …, aₙ whole numbers }

Here b₁ to bₙ are the basis vectors. Real cryptographic lattices have hundreds of dimensions, so nobody can draw them, but the two-dimensional picture carries over.

The short-vector and closest-vector problems

Two questions about a lattice are believed to be hard when the dimension is large. The shortest vector problem asks for the nonzero lattice point nearest the origin. The closest vector problem gives you a point that is not on the lattice and asks for the lattice point nearest to it.

Both are easy in two dimensions, where you can see the answer. In hundreds of dimensions, the best known algorithms, classical and quantum, take time that grows exponentially with the dimension. Cryptographers choose dimensions so that this cost exceeds the targets of NIST's security categories.

Good and bad bases

The same lattice can be described by short, nearly perpendicular vectors, which is a good basis, or by long, nearly parallel vectors, which is a bad basis. With a good basis, closest-vector questions are easy. You round your target point to the nearest whole-number combination and you are done. With a bad basis the same rounding lands far from the true answer.

A tiny example shows it. The grid of whole-number points in two dimensions has the obvious basis (1, 0) and (0, 1). The vectors (7, 1) and (6, 1) generate exactly the same grid, because their determinant is 7 × 1 − 6 × 1 = 1. The second pair is a bad basis for a grid that has a perfectly good one.

Found it. With the short basis, rounding lands on the secret point.

An intuition, not the exact ML-KEM construction. Score so far: private basis 0 of 0, public basis 0 of 0.

Try it: find the lattice point nearest the target using a short basis, then a long basis, and compare how easy each is.

This asymmetry is the basis of lattice cryptography. The public key is a bad basis, or something equivalent to it. The private key is a good basis, or a secret that lets you act as if you had one. Anyone can describe the lattice, but only the key holder can solve problems in it quickly.

Learning with errors

Modern schemes do not use bases directly. They use a problem called learning with errors, or LWE, which is easier to build schemes from and has a security argument connecting it to lattice problems. In words: take a secret list of numbers, compute several linear combinations of it, add a small random error to each result, and publish the blurred results. Recovering the secret from them is believed to be hard.

Without the errors, this would be ordinary linear algebra, and a laptop could solve it with Gaussian elimination. The errors destroy that method, because a small mistake in one equation spreads to everything after it.

A worked example with modulus 17

Use a small modulus q = 17 and a single secret number s = 6. The public data are pairs (a, b) where b = a·s + e mod 17 and e is a small error.

Four LWE samples with secret s = 6 and modulus 17
aa·s mod 17Error eb published
31+12
814−113
513+114
1115015

An attacker sees only a and b. To test a guess for s, compute b − a·s mod 17 for each pair and see whether the results are all small. For the guess s = 6 they are 1, −1, 1 and 0, which are all small, so the guess is plausible. For the wrong guess s = 2 they are −4, −3, 4 and −7, and one is not small, so the guess fails.

With one secret number you could simply try all 17 guesses. The real problem uses a secret vector with hundreds of entries, so the number of guesses is astronomical, and there is no known shortcut that avoids search. The same arithmetic, scaled up, gives the security of ML-KEM.

Module lattices

Plain LWE needs large public keys, because the public matrix has hundreds of rows and columns of full-sized numbers. Cryptographers reduced that cost by using polynomials instead of single numbers. The polynomials in ML-KEM have degree 256 and coefficients modulo q = 3,329.[1] One polynomial packs 256 numbers and multiplies them with one fast operation.

A module lattice is a lattice built from a small matrix of such polynomials. ML-KEM uses matrices of size k × k with k = 2, 3 or 4 for ML-KEM-512, ML-KEM-768 and ML-KEM-1024.[1] The problem is called module learning with errors, or MLWE. It keeps most of the compactness of ring-based schemes while avoiding some of their extra algebraic structure. Raising k raises the security and the byte sizes together, which is why the parameter sets scale in clean steps.

How ML-KEM works

ML-KEM turns MLWE into a key-encapsulation mechanism in three steps.[1]

  1. Key generation. Choose a public matrix A, built from a seed. Choose a secret vector s and an error vector e, both with small coefficients. Compute t = A·s + e. The public key is A (as a seed) and t. The private key is s.
  2. Encapsulation. The sender picks a random 32-byte message m and derives all further randomness by hashing m together with the public key. The sender chooses small vectors and computes two values: u, a blurred multiple of A, and v, a blurred multiple of t with the message m added in, scaled up so that it occupies the high part of each coefficient. The ciphertext is (u, v). The shared secret is derived from m.
  3. Decapsulation. The receiver uses s to compute v minus a multiple of u. Because t = A·s + e, nearly all the large terms cancel, leaving the message plus a small residue. Rounding removes the residue and recovers m. The shared secret is 32 bytes.
v − sᵀ·u ≈ m·(q/2) + small error → round → m

There is one more ingredient. A bare scheme like this is secure against an attacker who only listens, but not against one who sends crafted ciphertexts and watches how the receiver reacts. The standard fixes this with the Fujisaki–Okamoto transform. In words, the receiver decrypts, then re-encrypts the result with the same derived randomness and checks that it gets exactly the ciphertext it received. If it does, the ciphertext was honestly made and the receiver outputs the shared secret. If it does not, the receiver outputs a pseudo-random value derived from a secret key, so a forged ciphertext yields a useless key and no useful error message.[1]

Because the error terms are small, there is a tiny chance that decryption rounds incorrectly. The parameters make that probability negligible, but the check above also means such a failure would produce a mismatched key and a failed session, not a leak.

How ML-DSA works

ML-DSA is a signature scheme, so it works differently. It follows the Fiat–Shamir approach, which turns an interactive proof of knowledge into a signature. In an interactive proof, the prover commits to a random value, the verifier replies with a random challenge, and the prover responds. Fiat and Shamir noticed that you can replace the verifier by a hash function that takes the commitment and the message and outputs the challenge.[2]

With lattices there is a snag. The response is a combination of the secret and the random commitment. If the response ever falls in a certain range, it leaks information about the secret over many signatures. ML-DSA solves this with aborts. The signer computes a candidate signature and checks whether it lies in a safe range. If not, it throws the attempt away and starts again with fresh randomness. Only safe attempts are released. On average the signer needs a few tries, which is why signing time varies from one signature to the next.[2]

ML-DSA uses the modulus q = 8,380,417 and relies on MLWE together with a second problem, module short integer solution, or MSIS.[2] The verifier rebuilds the commitment from the signature and the public key, hashes it with the message and checks that the challenge matches and that the response is short.

Why no quantum shortcut is known

Shor's algorithm breaks RSA and elliptic curves because their hidden structure is a repeating pattern that a quantum computer can detect.[4] Lattice problems do not have that kind of periodic structure for Shor's algorithm to exploit. The best known quantum algorithms for the lattice problems behind ML-KEM and ML-DSA improve on classical ones by a modest amount in the exponent. Grover-style search gives the usual square-root speed-up on search steps, and cryptographers include it in their cost estimates.[5]

NIST used these estimates to place each parameter set in a security category.[3] The accurate statement is that the problems have been studied for decades and no efficient quantum attack is known. It is not a proof that none exists. SIKE, an isogeny scheme that looked equally solid, fell to a classical attack in 2022.[6] Lattices have had longer and broader scrutiny than SIKE had, but the lesson stands.

Implementation concerns

A secure algorithm in an insecure implementation is not secure. Lattice schemes have particular hazards.

  • Constant-time code. Any step that branches or indexes memory on secret data can leak the secret through timing. Reference implementations avoid secret-dependent branches, and a library should be tested for it.
  • Side channels. Power, electromagnetic emissions and cache behaviour can leak secrets from smart cards, HSMs and shared cloud hosts. Masking and other countermeasures add cost, and hardware vendors work on them now.
  • Rejection sampling and aborts. ML-DSA signing loops a variable number of times. Implementations must make sure the loop count reveals nothing secret.
  • Decryption failure. The standard sets parameters so that the chance of a failure is negligible. Do not alter parameters or encodings.
  • Randomness. Both schemes need a good random number generator. A weak one undermines everything else.

In practice, use a maintained library from a reputable source, and prefer validated modules where regulation asks for them. Do not write your own. Many libraries and platforms now ship ML-KEM and ML-DSA, and the readiness of each changes quickly, so check the readiness tracker.

Sizes

Lattice schemes are fast, but their keys and signatures are larger than elliptic curves. Sizes in bytes:

Lattice scheme sizes compared with classical algorithms (bytes)
SchemePublic keyCiphertext or signature
ML-KEM-512800768
ML-KEM-7681,1841,088
ML-KEM-10241,5681,568
ML-DSA-441,3122,420
ML-DSA-651,9523,309
ML-DSA-872,5924,627
RSA-2048256256
ECDSA P-25664about 64 to 72
X25519 / Ed255193232 (X25519 share) / 64 (signature)

The figures are from the standards.[1][2] The practical effect is on anything that carries keys or signatures in fixed-size fields or on constrained links: certificate chains, UDP-based protocols, smart cards and embedded devices. A chain of several ML-DSA certificates adds several kilobytes to a TLS handshake. The CNSA 2.0 suite requires the largest of these sets, ML-KEM-1024 and ML-DSA-87, for national security systems.[7]

What to do now

  • Treat ML-KEM-768 and ML-DSA-65 as the default starting points, and plan for ML-KEM-1024 and ML-DSA-87 where a mandate or very long data life requires them.
  • Check message-size limits in your protocols, load balancers, HSM interfaces and message queues. Test with ML-DSA-sized certificates before production.
  • Run qsc scan to find where RSA, ECDH and ECDSA are used today. Each finding shows the replacement, and qsc fix lists the plan.
  • Choose a library that is maintained, tested for constant-time behaviour and supported by your platform vendor.
  • Keep the algorithm name in configuration, not in code, so you can change it later.

Related reading. The NIST post-quantum standards, Hash-based signatures, Hybrid key exchange in TLS, SSH and VPNs and How Shor's algorithm breaks RSA.

Sources

  1. NIST. FIPS 203: Module-Lattice-Based Key-Encapsulation Mechanism Standard. 2024-08-13. https://csrc.nist.gov/pubs/fips/203/final
  2. NIST. FIPS 204: Module-Lattice-Based Digital Signature Standard. 2024-08-13. https://csrc.nist.gov/pubs/fips/204/final
  3. NIST Computer Security Resource Center. Post-Quantum Cryptography project. https://csrc.nist.gov/projects/post-quantum-cryptography
  4. Peter Shor. Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer. 1994 (FOCS); SIAM J. Computing 26(5), 1997. https://arxiv.org/abs/quant-ph/9508027
  5. Lov Grover. A fast quantum mechanical algorithm for database search. STOC 1996. https://arxiv.org/abs/quant-ph/9605043
  6. Wouter Castryck and Thomas Decru. An efficient key recovery attack on SIDH (SIKE). July 2022. https://eprint.iacr.org/2022/975
  7. NSA. Announcing the Commercial National Security Algorithm Suite 2.0. September 2022. https://media.defense.gov/2022/Sep/07/2003071834/-1/-1/0/CSA_CNSA_2.0_ALGORITHMS_.PDF