Blog

How Module Lattice Cryptography Works

A technical primer on ML-KEM for engineers: what module lattices are, why a little deliberate noise makes simple algebra hard to undo, and how that becomes a key exchange designed to resist quantum computers.

2026.12.03

A latticed roof Photo by Chris Linnett on Unsplash

Introduction

This week brought some new developments in cryptography. On December 2nd, the White House's Office of the National Cyber Director, the NSA and CISA issued a joint statement saying that AI companies had disclosed discoveries that affect the security of some of the cryptography in use today. The same day, Amazon, Anthropic, Apple, Cloudflare, Google, Microsoft, OpenAI and Oracle published a statement of their own, supporting the government's guidance to move to the algorithms in NIST's post-quantum standards, and to "accelerate the migration as soon as possible". The NSA also published an advisory this week, with a list of immediate steps towards post-quantum cryptography.

The part of that move which is furthest along is key exchange, and that's what this post is about.

In an earlier post, we took a close look at AES and ChaCha20, the two ciphers that do most of the heavy lifting when it comes to encrypting data on the internet. Both are symmetric ciphers, which means that both Alice and Bob need to hold the same secret key before they can start. A deep dive into key exchange, or how two computers that have never met agree on that key, was out of scope for that post, but a quick primer is useful here.

For the last few decades, key exchange has been handled by a small family of public-key algorithms: RSA, Diffie-Hellman, and the elliptic curve versions of Diffie-Hellman, like X25519. All of them would be broken by a large enough quantum computer.

That computer doesn't exist yet, but replacements for these algorithms do. In August 2024, NIST published FIPS 203, which standardizes the Module-Lattice-Based Key-Encapsulation Mechanism, or ML-KEM, previously known as CRYSTALS-Kyber. Most major browsers now prefer X25519MLKEM768, which combines ML-KEM with X25519. Cloudflare reports that, as of December 1st, 73% of the human traffic reaching its network is protected by post-quantum encryption.

So what is a "module lattice", why would a quantum computer struggle with one, and how do you build a key exchange out of it? This post is a primer for engineers who want to know what's behind the acronyms without reading through the standard. Let's take a closer look.

A Primer on Key Exchange

Symmetric ciphers like AES and ChaCha20 are fast, and they have one requirement: both parties must hold the same secret key. Key exchange is the problem of getting that key to both sides over a network that anyone can listen in on.

Public-key cryptography solves this with an asymmetry. Each party has a key pair: a public key that can be shared with anyone, and a private key that is kept secret. The two are linked by a mathematical problem that is easy to compute in one direction and practically impossible to reverse. For RSA, multiplying two large prime numbers together is easy, but working out which two primes went into the result is hard. Elliptic curves rely on a similar one-way street called the discrete logarithm problem.

The quantum problem

In 1994, Peter Shor published an algorithm that solves both of these problems efficiently on a quantum computer. The speedup is large enough that using bigger keys isn't a practical defence. We'll look at how it works in a moment.

No quantum computer today is anywhere near large enough to run Shor's algorithm against real keys, and nobody knows exactly when one will be. But the estimates of what it would take keep coming down, and an attacker doesn't need to have the machine today. They can record encrypted traffic now and decrypt it once a capable machine exists, an approach known as harvest now, decrypt later. This matters for any data that has to stay secret for many years and was protected by a key exchanged using RSA or X25519.

Symmetric ciphers are much less affected. The best known quantum attack on them, Grover's algorithm, effectively halves the length of the key, so AES-256 and ChaCha20 with their 256-bit keys keep a comfortable margin. The ciphers from the previous post are fine. It's the key exchange that needs replacing.

How Shor's algorithm works

Quantum computers are often described as trying every possible answer at once. That's only half true. A quantum computer can run a calculation on many inputs at the same time, in what's called a superposition. But when you measure the result, you only get to see one of the answers, picked at random. On its own, that's no better than guessing.

What makes quantum computers useful is interference. Before you measure anything, the possible results behave like waves: they can add up, or cancel out. A good quantum algorithm makes the useless results cancel and the useful ones add up, so that a useful one is what you are likely to see. This only works if the problem has some structure that makes the waves line up. A repeating pattern is exactly that kind of structure.

Factoring has a repeating pattern hidden inside it. Take the powers of 2, mod 15: they go 1, 2, 4, 8, 1, 2, 4, 8, and so on, repeating every 4 steps. Knowing that length is enough to factor 15. Since 24 is 1 (mod 15), we know that 24 − 1 is a multiple of 15. It also splits neatly in two: 24 − 1 = (22 − 1)(22 + 1) = 3 × 5. For other numbers, the two parts usually aren't the factors themselves, but each one shares a factor with the original number, which is easy to pull out. If that fails, you start again with a different number in place of 2. The same trick works for the huge numbers used in RSA. The catch is that their patterns are far too long for an ordinary computer to find the length of.

Shor's algorithm finds that length in three steps:

  1. It computes the powers for a huge range of inputs at once, in superposition.
  2. It measures the output. Say the output is 2. The only inputs left in the superposition are those that give 2, which are 1, 5, 9, 13 and so on. These are evenly spaced, exactly 4 apart.
  3. It applies a quantum Fourier transform. An ordinary Fourier transform can pick out the pitch of a sound from its repeating wave. This one turns the even spacing into sharp peaks, and measuring gives us one of those peaks. A few runs are usually enough to work out the spacing.

Shor's algorithm turns a repeating pattern into sharp peaks

This works because the pattern repeats exactly. Every repeat lines up with all the others, so their waves add up at the peaks and cancel out everywhere else.

Elliptic curves have the same weakness. There, the public key Q is a fixed point G multiplied by the private key d, so Q = d·G. An attacker can combine the two public values into a function of two numbers:

f(x, y) = x·G + y·Q

Now add d to x, and take 1 away from y. We gain d·G and lose one Q. Those are the same thing, so nothing changes:

f(x + d, y − 1) = f(x, y)

This function has a repeating pattern too, and the step from one repeat to the next is the private key. Finding that step is exactly what Shor's algorithm does.

So we need a new one-way problem, and ideally one that doesn't give Shor's algorithm an exact pattern to find. This is where lattices come in.

Why now?

Until recently, quantum computers were the main reason to migrate, on a timeline measured in years. NIST's transition plan, finalized in October, deprecates RSA and elliptic curve cryptography after 2030 and disallows them after 2035.

The government's statement adds a second reason: AI. It says that the latest AI models have "demonstrated the capability to solve some of the most complex and previously unsolved problems" in mathematics, and that the companies building them have disclosed discoveries that "impact the security of some cryptography currently in use". Its current assessment is that these advances "do not present an immediate and widespread threat to that cryptography".

A timeline of the post-quantum migration

The details of which cryptography is affected haven't been made public, so we won't speculate here. Some new research on lattices was published a few days earlier, though, and we'll look at what it does and doesn't say later in the post.

Key encapsulation

Before we get to the maths, it's worth being clear about what we're trying to build. ML-KEM has the same goal as X25519: to give Alice and Bob a shared secret. It just gets there in a different way.

X25519 is a key agreement. Alice and Bob each generate a key pair, and send each other their public keys. Alice's private key is a number a, and her public key is a·G. Bob's are b and b·G. Each of them multiplies their own private key with the public key they received:

Alice:  a · (b·G)
Bob:    b · (a·G)

Both are the same point on the curve, and the shared secret comes from that point. Neither side picked it. Alice and Bob worked it out together, and each of them supplied half of what was needed.

ML-KEM is a Key Encapsulation Mechanism (KEM), and only Alice makes a key pair. Think of her public key as an open padlock. She can hand it out to anyone, and anyone can snap it shut, but only she has the key that opens it. Alice sends her padlock to Bob. Bob comes up with a secret, locks it in a box with her padlock, and sends the box back. Alice opens it, and now they both have the same secret.

So in a key agreement, both sides work out a secret together. In a KEM like this one, one side comes up with a secret and locks it, so that only the other side can open it. Here's how the two compare:

Key agreement (X25519)KEM (ML-KEM)
Who makes a key pairAlice and BobOnly Alice
What Alice sendsHer public keyHer public key
What Bob sendsHis public keyThe locked box, called a ciphertext
Where the secret comes fromBoth key pairs, combinedIt starts from random bytes picked by Bob
Who can go firstEither sideAlice, because Bob needs her public key

Either way, each side sends one message, and both end up with a shared secret. In TLS, the client already sends its key first, so a KEM can take the place of X25519 without an extra round trip.

A KEM is made up of three operations, which match the steps above: making the padlock, locking the box, and opening it.

  • KeyGen – Alice generates a key pair and sends the public half to Bob.
  • Encaps – Bob uses Alice's public key to produce two things: a shared secret, which he keeps, and a ciphertext which "encapsulates" that secret, which he sends back to Alice.
  • Decaps – Alice uses her private key to recover the same shared secret from the ciphertext.

Both sides now hold the same secret, and can use it as the key for a symmetric cipher like ChaCha20-Poly1305. Everything after that point is the territory of the previous post.

KEM overview

Before we continue, let's set out what we will and won't cover. An exhaustive technical description of ML-KEM is out of scope for a single blog post. The goal here is to build up the core idea step by step, and then see how ML-KEM puts it to use:

  • We will cover the problem that ML-KEM is built on, and how it generates keys, and then encapsulates and decapsulates a shared secret.
  • We won't cover ML-DSA, its sibling for digital signatures. It's built on the same module lattice foundations but works quite differently.
  • We won't cover the Number Theoretic Transform, which real implementations use to multiply polynomials quickly. It matters a great deal for performance, but not for understanding why the scheme works.
  • We won't cover the formal security proofs.
  • The worked examples use two unknowns and a small modulus, so that we can follow them by hand. Real implementations use hundreds of unknowns and a larger modulus.

Some terminology

  • Encapsulation key: the public key in a KEM. This is what FIPS 203 calls it, and it can be shared with anyone.
  • Decapsulation key: the private key in a KEM. It must be kept secret.
  • Shared secret: the 32 bytes that both parties end up with, used as a key for symmetric encryption.
  • Modular arithmetic: arithmetic that wraps around at a fixed number called the modulus, like the hours on a clock. We write "mod 97" to mean that after 96 we go back to 0. This is the same wrap-around we saw in ChaCha's Add operation, just with a different limit.
  • Matrix-vector product: each row of a matrix is multiplied entry by entry with a vector, and the results are added up, giving one number per row.
  • Polynomial: an expression like 3 + 2x + 5x2. For our purposes, a polynomial is just a list of numbers called coefficients: here, 3, 2 and 5.
  • Noise: small random numbers that are deliberately added to a calculation. Also called the error.
  • Post-quantum cryptography: algorithms that run on ordinary computers but are designed to resist attacks from quantum computers. Not to be confused with quantum cryptography, which needs special hardware.
  • Hybrid: using a classical algorithm and a post-quantum algorithm together, so that an attacker needs to break both.

Lattices

A lattice is a grid of points that repeats forever in every direction. To make one, take a couple of arrows, which we'll call basis vectors, and mark every point that you can reach by following whole-number multiples of them: two steps along the first arrow and one along the second, three steps back along the first and five along the second, and so on. The set of all the points you can reach is a lattice.

The same lattice can be described by many different bases. Some are "good": short arrows, close to perpendicular. Others are "bad": long arrows that point in almost the same direction. Both of the bases below produce exactly the same set of points.

The same lattice with a good and a bad basis

Hard problems on lattices

Lattices are interesting to cryptographers because some very simple questions about them turn out to be extremely hard to answer. The one we care about is the Closest Vector Problem: given a point that is not on the lattice, find the lattice point nearest to it.

Closest vector problem

In two dimensions, you can solve this by looking at the picture. With a good basis it's also easy for a computer, which can more or less round to the nearest grid line. With a bad basis, that rounding trick tends to land on the wrong point. In two dimensions that isn't much of an obstacle, because it's easy to turn a bad basis back into a good one.

Now imagine the same problem in several hundred dimensions. The number of nearby candidates grows exponentially as dimensions are added, and nobody knows an efficient way to turn a bad basis into a good enough one. At the sizes used in cryptography, the best known algorithms would take far too long to be practical. After about thirty years of study, that holds for the best known quantum algorithms too.

Hold on to the idea of a point that is close to, but not exactly on, a lattice point. We're about to arrive at it again from a completely different direction.


Learning With Errors

Lattices give us a hard problem, but pictures of dots are awkward to compute with. In 2005, Oded Regev introduced a way to state the same kind of problem using nothing more than basic algebra. It's called Learning With Errors (LWE), and all of ML-KEM is built on top of it.

Why do we need noise?

To understand LWE, let's see what happens when we try to hide a secret behind an ordinary system of linear equations. After all, the secret is never sent directly, so this seems like it should work. Let's try it.

We'll do all of our arithmetic mod 97. Alice picks two small secret numbers, s1 and s2. She then picks some random public numbers, multiplies them with her secrets, and publishes the results:

21·s₁ + 55·s₂ = 84   (mod 97)
83·s₁ +  6·s₂ = 63   (mod 97)

Can an eavesdropper work out Alice's secret numbers? Easily. This is two equations with two unknowns, and a method called Gaussian elimination solves it almost instantly: the answer is s1 = 2 and s2 = −1. Adding more unknowns doesn't help either. A laptop can solve a system like this with thousands of unknowns in seconds.

Now let's make one tiny change. Before publishing her results, Alice nudges each one by a small random amount. She subtracts 1 from the first and adds 1 to the second:

21·s₁ + 55·s₂ ≈ 83   (mod 97)
83·s₁ +  6·s₂ ≈ 64   (mod 97)

Each answer is only off by one, but this is enough to break Gaussian elimination. To see why, let's follow those errors through it.

Elimination gets rid of one unknown at a time. To cancel out s2, the eavesdropper multiplies the first equation by 6 and the second by 55, so that both contain 330·s2, and then subtracts one from the other. Everything in each equation gets multiplied, including its error:

How a small error grows during Gaussian elimination

The error of −1 becomes −6, the error of +1 becomes +55, and subtracting them gives −61. What's left is 23·s1 ≈ 82 (mod 97). Undoing the 23 means multiplying by 38, because 23 × 38 = 874, which is 1 mod 97. That scales the error up to −2318, which wraps around the modulus 24 times and lands on +10.

The eavesdropper ends up with s1 = 12 and s2 = −26, instead of 2 and −1. The wrap-around is what makes this so effective. With ordinary numbers, small errors can be averaged out using a method like least squares. Mod 97, there's no way to tell that the 12 came from a huge error. In fact, the wrong answer fits the published equations perfectly.

What sets the real answer apart is that its numbers are small: both the secret and the noise. With two unknowns, you could find it by trying every small pair. With several hundred, there are far too many combinations to try, and every known algorithm for the job takes an impractically long time. That is the Learning With Errors problem:

t = A·s + e   (mod q)
  • A is a matrix of public random numbers
  • s is the secret, made up of small numbers
  • e is the noise: small, random, and also secret
  • t is the published result

Given A and t, find s. Without the noise, this is easy. With it, nobody knows how to solve it efficiently.

t = A·s + e

Where is the lattice?

Suppose there are more equations than unknowns. Take every possible s, using any whole numbers and not only small ones. Work out the exact, noise-free answer A·s for each one, plot all of those answers as points, and repeat the whole pattern every q steps in each direction. The points form a regular grid: a lattice. Alice's published result t is one of those points, nudged slightly off the grid by the noise.

Recovering her secret means finding the grid point nearest to t. That's the closest vector problem from earlier, in a lattice with hundreds of dimensions, and with only the public matrix A to describe it, which gives a "bad" basis. Strictly speaking, it's a slightly easier version called bounded distance decoding, because we know that t is unusually close to a grid point.

In Regev's original version, only the noise has to be small. ML-KEM, like our example, has exactly as many equations as unknowns, and makes up for this by requiring the secret to be small too. This leads to the same kind of nearest-point problem, in a larger lattice that has dimensions for both the secret and the noise.

Regev also proved that, with enough noise, solving random instances of LWE is at least as hard as solving the worst cases of certain lattice problems on a quantum computer. Similar proofs exist for the more structured version of LWE that ML-KEM uses, which we'll get to later. ML-KEM uses less noise than these proofs need, so they support its design, but they don't prove that its exact numbers are safe. Its sizes were chosen by estimating the cost of the best known attacks.

Why doesn't Shor's algorithm work here?

Let's try the trick that worked on elliptic curves, this time on Alice's equations. Her public values are A and t, so we combine those instead. Here x is a vector and y is a number:

f(x, y) = A·x + y·t   (mod q)

If Alice hadn't added any noise, t would be exactly A·s. Stepping by the secret would change nothing, and Shor's algorithm would find it:

f(x + s, y − 1) = f(x, y)         without noise

With noise, t = A·s + e. The two parts no longer cancel completely, and each step leaves a bit of noise behind:

f(x + s, y − 1) = f(x, y) − e     with noise

The function now almost repeats. That is a problem for Shor's algorithm, for two reasons.

The exact pattern is the wrong one. Shor's algorithm finds patterns that repeat exactly. Our example still has one, but its step is (12, −26). That's the same wrong answer that Gaussian elimination gave us.

The near-repeats drift apart. What if the attacker treats outputs that are close as if they were the same? The trouble is that the noise adds up with every step. With Alice's numbers, stepping along the real secret from (0, 0) gives:

(0, 0)   (1, −1)   (2, −2)   (3, −3)   (4, −4)   ...

Each output is further from where we started, and soon it's too far away to count as a repeat. Elliptic curves give Shor's algorithm a huge number of perfect repeats. Here, it gets a few rough ones at best, which isn't enough for the waves to add up into sharp peaks:

Exact repeats give sharp peaks, and drifting repeats give blurred ones

A blurred peak still says something about the secret, so an attacker could run this many times and collect a lot of faint hints. But with hundreds of unknowns, nobody knows an efficient way to put those hints together, even on a quantum computer. Researchers have studied this closely: LWE has been shown to be about as hard as a problem of just this kind, about finding a secret step from a limited number of repeats.

So the noise does two jobs. It stops an ordinary computer from solving the equations with elimination, and it stops a quantum computer from finding the secret with Shor's algorithm.

A toy encryption scheme

A hard problem is only useful if it comes with a trapdoor: something that makes it easy for the person holding the secret. Let's build a tiny encryption scheme out of LWE, and use it to send a single bit, a 0 or a 1, from Bob to Alice. We'll keep working mod 97. With only two unknowns it's far too small to be secure, but it works in the same way as the real thing.

Two rules will help us keep track of all the numbers:

  • Secrets and noise are small. Alice and Bob pick them at random from numbers close to 0, and 0 itself is allowed. We'll see why they have to be small once Alice decrypts the message.
  • Every number worked out from a secret gets its own noise before it's shared. As we saw earlier, exact equations give too much away.

Step 1: Alice makes her keys

Alice starts with A, a grid of random public numbers. These are the same four numbers that we used in the equations earlier:

A =  21  55
     83   6

Her secret is s = (2, −1). For each row of A, she multiplies the first number by 2 and the second by −1, adds the results together, and then adds some noise. A has two rows, so she gets two numbers, t1 and t2. That means she needs two noise numbers: e = (−1, 1).

t₁ = 21·2 + 55·(−1) − 1 = −14, which is 83   (mod 97)
t₂ = 83·2 +  6·(−1) + 1 = 161, which is 64   (mod 97)

Alice makes her key pair

Her public key is A and t = (83, 64), which she sends to Bob. Her private key is s.

Step 2: Bob encrypts a bit

Bob wants to send Alice the bit 1. His plan is to hide it behind a mask: a number that he adds to the message, and that only Alice can take away again. He sends her two things. u helps her work out the mask, and v is the masked message.

He starts by picking a small secret of his own, y = (1, 2).

To make u, he does what Alice did, but he works down the columns of A instead of along the rows. We'll see why in a moment. There are two columns, so u has two numbers, and needs two noise numbers. We'll call them e1 = (0, 1).

u₁ = 21·1 + 83·2 + 0 = 187, which is 90   (mod 97)
u₂ = 55·1 +  6·2 + 1 =  68

To make the mask, he multiplies Alice's two public numbers in t by his own secret, and adds the results together. This gives a single number, so it only needs a single noise number, e2 = 1.

mask = 83·1 + 64·2 + 1 = 212, which is 18   (mod 97)

Finally, he adds his message. A 0 is written as 0, and a 1 is written as 49. That's roughly half of 97, which keeps the two as far apart as possible.

v = 18 + 49 = 67

Bob sends u = (90, 68) and v = 67 to Alice. Together, they are the ciphertext.

Bob encrypts a bit

Alice has one set of noise and Bob has two because of what each of them shares. Alice shares two new numbers, so she needs two noise numbers, which fit in e. Bob shares three: two in u, which use e1, and one in v, which uses e2.

Step 3: Alice decrypts it

Alice works out the mask for herself, by multiplying the two numbers in u by her secret. Then she takes it away from v:

mask = 90·2 + 68·(−1) = 112, which is 15   (mod 97)

v − mask = 67 − 15 = 52

Alice decrypts the bit

She gets 52. That's not exactly 49, but it's a lot closer to 49 than it is to 0, so Alice reads the bit as 1. Had Bob sent a 0, she would have ended up with 3, which is right next to 0.

Decoding by rounding

Why do the masks almost match?

Bob's mask was 18, and Alice's was 15. They got there in different ways, so why are they so close?

Underneath, both are adding up the same four products. Each product is one number from A, times one of Alice's secret numbers, times one of Bob's. Alice's secret goes with the columns of A, and Bob's goes with the rows. That's why she worked along the rows, and he worked down the columns. Each of them did their half of the multiplication, and passed the result to the other to finish.

Both masks come from the same four products

Without any noise, both masks would be exactly 16. The noise moves Bob's up by 2 and Alice's down by 1, so they end up 3 apart. That is why Alice got 52 instead of 49. As long as this difference stays smaller than about a quarter of the modulus, she will always read the right bit.

This is why the secrets and the noise all have to be small. The noise in Alice's t gets multiplied by Bob's secret, and the noise in Bob's u gets multiplied by Alice's. If these numbers were too large, the masks could end up too far apart, and Alice would read the wrong bit. For example, if Bob's secret was (30, 60), the masks would be 32 apart, and she would read his 1 as a 0.

This is a lot like the key agreement we saw earlier, where Alice and Bob arrive at the same value from two directions. It's also one reason that ML-KEM is a KEM and not a key agreement. Two values that are only close to each other can't be used directly as a shared key, since a key has to match exactly. But close is good enough for Alice to read a message from Bob.

An eavesdropper sees A, t, u and v. As far as anyone knows, the only way to work out the mask is to find Alice's secret or Bob's, or to solve an equally hard problem, and that means dealing with noisy equations. With two unknowns, they could just try every small pair. With several hundred, nobody knows how to do it in a practical amount of time. The noise that gets in the attacker's way is the same noise that Alice simply rounds away.

The core of ML-KEM is this idea, scaled up, and these three steps will become KeyGen, Encaps and Decaps. Going back to the padlock: A and t are the padlock, u and v are the locked box, and Bob's bit is the secret inside. ML-KEM sends 256 random bits this way instead of one, and both sides then turn those bits into their shared secret.


From numbers to polynomials

The toy scheme has a problem: size. To be secure, plain LWE needs several hundred unknowns, which makes A a grid of hundreds of thousands of numbers. Schemes built this way do exist, FrodoKEM being the best known, but their public keys run from about 9.6 to 21.5 kilobytes, and every operation involves a lot of multiplication.

The fix is to swap each number in the scheme for a polynomial.

In ML-KEM, a polynomial is a row of 256 numbers, each between 0 and 3328 (that is, mod 3329). Adding two polynomials is done slot by slot. Multiplying them works as usual, with one extra rule: there is no slot 257. Anything that overflows off the end wraps around to the start, and has its sign flipped on the way.

Here's what that looks like with 4 slots instead of 256, multiplying a polynomial by x:

(1 + 2x + 3x² + 4x³) · x  =  −4 + 1x + 2x² + 3x³

Multiplying a polynomial by x

If this reminds you of the Rotate operation from ChaCha's quarter round, it should. Multiplying by x rotates the coefficients by one position, and multiplying by a full polynomial is a blend of many such rotations.

This buys us two things. First, a single polynomial multiplication mixes every coefficient of one input with every coefficient of the other, so one polynomial does the job of a 256×256 grid of numbers while taking only 256 numbers to write down. Second, a polynomial with 256 slots can carry 256 message bits at once, one per slot, which is exactly the size of the shared secret we're trying to exchange. This variant is known as Ring-LWE.

As for the odd-looking 3329, it's a prime that is just large enough to keep the noise manageable, small enough for every coefficient to fit in 12 bits, and exactly one more than a multiple of 256, which is what makes the fast multiplication trick we're skipping over possible. There's nothing up this sleeve either.

The "module" in module lattice

Ring-LWE is compact, but it has two drawbacks. All of that extra structure makes some cryptographers nervous, since structure is exactly what attacks tend to exploit. And the main way to make it stronger is to use a bigger polynomial, which means re-engineering the arithmetic for every security level.

Module-LWE is the middle ground. We keep the polynomial fixed at 256 coefficients, and go back to using a small matrix and small vectors like the toy scheme did. The only difference is that every entry is now a polynomial instead of a number.

The structure of ML-KEM-768

A module is like the vectors you may know from school, but with polynomials like these in place of ordinary numbers. A module lattice is a lattice built from them.

To increase security, we make the matrix bigger and leave the polynomials alone. ML-KEM comes in three sizes, where k is the number of polynomials in each vector:

kSecret numbers (k × 256)Comparable to
ML-KEM-5122512AES-128
ML-KEM-7683768AES-192
ML-KEM-102441024AES-256

That's where the numbers in the names come from. ML-KEM-768 is the one you're most likely to run into, and is the parameter set that NIST recommends as the default.

The core operation, multiplying two 256-coefficient polynomials, is identical at every security level. What mainly changes is the number of times it's performed, along with small adjustments to the noise and rounding. Like ChaCha and its quarter round, the whole scheme comes down to one small building block, repeated.


ML-KEM

We now have everything we need. Let's walk through the three operations of ML-KEM-768, which will look very familiar after the toy scheme.

Key generation

Alice starts by generating the public matrix A: a 3×3 grid of polynomials. Rather than picking and transmitting all 2,304 numbers, she starts from a random 32-byte seed and expands it using the hash function SHAKE. Anyone with the seed can regenerate exactly the same matrix, so the seed is all she needs to share.

Next, she picks her secret s and her noise e at random. Each of them is a vector of 3 polynomials, and each polynomial is a list of 256 coefficients. That makes 768 coefficients in s and another 768 in e, and all of them have to be small numbers. The reason is the same as in the toy scheme: if they were too large, Alice would read some of Bob's bits wrong when she decrypts.

To pick each coefficient, she flips four coins (in practice, these are random-looking bits made from a second seed, which she keeps secret). She counts the heads in the first pair, then subtracts the heads in the second pair. For example, if the first pair has two heads and the second pair has one, the coefficient is 2 − 1 = 1. The result is always between −2 and 2, and is usually 0 or ±1.

Picking a small number by flipping four coins

Finally, she computes the now-familiar:

t = A·s + e

Her encapsulation key is t plus the seed for A. That's 3 polynomials of 256 coefficients at 12 bits each, plus 32 bytes: 1,184 bytes in total. At its core, her decapsulation key is s.

Encapsulation

Bob receives the encapsulation key and regenerates A from the seed. Then:

  1. He picks a random 32-byte message m. That's 256 bits, one for each slot of a polynomial. Each 0 bit is encoded as 0 and each 1 bit as 1665, which is half of 3329, rounded.

  2. He picks his own small secret y, and small noise e1 and e2, using the same coin-flipping method. Here, y and e1 are vectors of 3 polynomials, and e2 is a single polynomial.

  3. He computes the same two values as in the toy scheme:

    u = Aᵀ·y + e₁
    v = t·y  + e₂ + encode(m)

    Aᵀ is A with its rows and columns swapped, which is how we write working down the columns. t·y multiplies the two vectors entry by entry and adds up the results, like Bob's mask.

  4. He compresses u and v by rounding every coefficient to fewer bits: 10 bits for each coefficient of u and just 4 for v. This shrinks the ciphertext to 1,088 bytes. It also adds more noise, roughly doubling the typical error, which the scheme is designed to absorb.

  5. He hashes m together with Alice's encapsulation key to produce the 32-byte shared secret K.

Decapsulation

Alice receives the ciphertext, scales the rounded numbers back up to get values close to the original u and v, and computes:

v − s·u

Just as before, everything cancels except the encoded message and some leftover noise. She rounds each of the 256 coefficients to whichever of 0 or 1665 is nearer, reads off the 256 bits of m, and hashes it in the same way that Bob did to arrive at the same K.

ML-KEM key generation, encapsulation and decapsulation

There is a chance that the leftover noise in some slot is large enough to make Alice round the wrong way, giving her a different key to Bob. The parameters are chosen so that for ML-KEM-768 this happens with a probability of about 2−164.8 (FIPS 203, Table 1). In practice, it can be ignored.

Keeping active attackers out

There is one more piece. The scheme as described so far is safe against an eavesdropper who only watches. An active attacker can try something sneakier: send Alice ciphertexts that have been deliberately tampered with, nudging a coefficient here and there, and observe whether she still ends up with a working key. Each answer leaks a little about where the noise in her key sits, and with enough of them, her secret, if she keeps using the same key.

ML-KEM closes this hole with a technique called the Fujisaki-Okamoto transform, which has three parts:

  • No free choices for Bob. Bob's secret and noise in step 2 aren't picked freely. They are generated from a hash of the message m and Alice's encapsulation key. This means that for any given message and key, there is exactly one valid ciphertext.
  • Alice checks Bob's work. After recovering m, Alice runs Bob's encryption steps herself, and checks that her result matches the ciphertext she received byte for byte.
  • Fail silently. If it doesn't match, Alice doesn't raise an error. She quietly returns a random-looking key derived from the ciphertext and a secret value of her own. The attacker only sees that the connection doesn't work, and learns nothing about why.

The decapsulation check

One thing ML-KEM doesn't do is prove who Alice is. An attacker in the middle could swap her padlock for their own, and Bob wouldn't notice. The same is true of X25519. In TLS, certificates and signatures take care of this.


Key takeaways

We've gone from dots on a grid, to noisy equations, to polynomials, and finally to a complete key exchange. Let's step back and look at what this design gets us, and what it costs.

How confident can we be?

We've seen why Shor's algorithm doesn't carry over to LWE. It isn't the only quantum algorithm, though. At practical sizes, the best quantum attacks on lattices take an ordinary attack and speed up the searching inside it, in the same spirit as Grover's algorithm. On paper this makes them modestly faster, and it isn't clear that they would be any faster in practice. Neither kind of attack comes close to being practical at the sizes ML-KEM uses.

This is a statement about the best known attacks, and not a proof. Nobody has proven that lattice problems are hard, for quantum computers or for ordinary ones. The confidence comes from decades of people trying to break them. We trust RSA for the same reason. Newer schemes have had less time to earn that trust: in 2022, the smallest version of SIKE, another candidate in NIST's process, was broken in about ten minutes on a single CPU core.

Lattices continue to get this kind of scrutiny. On November 30th, Anthropic published four papers on lattice problems. The results were found by one of its AI models, and come with formalizations in Lean, a language for writing proofs that a computer can check. One of the papers describes new quantum algorithms for lattice problems that include Module-LWE, the problem that ML-KEM is built on.

In theory, the improvement is a big one. If the matrix stays the same size, the cost of the attack grows much more slowly than exponentially as the polynomials get bigger. But this result is asymptotic: it describes how the cost grows, and it only pays off for problems vastly larger than anything in use. The paper's title calls the algorithms "galactic", a term for algorithms that are faster in theory but too costly to ever run. The authors state that they are not practical for ML-KEM's parameters.

Cloudflare's analysis agrees. It points out that as ML-KEM's security level goes up, which is done by making the matrix bigger, the cost still grows exponentially. A straightforward version of the attack would need at least billions of error-corrected qubits, and would be far slower than the best known attacks on ordinary computers.

So this research doesn't change the practical picture for ML-KEM. It does show why its parameters include a safety margin, and why most deployments pair it with an older algorithm, as we'll see below.

Why ML-KEM was chosen

Lattices aren't the only problem that resists Shor's algorithm. NIST started its search for post-quantum algorithms in 2016 and received 82 submissions, built on several different kinds of mathematics. ML-KEM, then called Kyber, was selected in 2022. NIST pointed to its performance across software and hardware, and found the Module-LWE problem slightly more convincing than the ones behind similar lattice schemes.

In practice, choosing a scheme means balancing trust, speed and size. Classic McEliece, for example, is based on a much older problem, and has tiny ciphertexts. But its smallest public key is about 261 kilobytes, which is a lot to send at the start of every connection. SIKE had very small keys, and turned out to be broken. ML-KEM is fast, its keys are around a kilobyte, and the problem behind it has held up well so far.

Small numbers, big keys

ML-KEM is fast. There is no arithmetic on very large numbers as in RSA. It's mostly additions and multiplications of 12-bit numbers, which modern CPUs can do many at a time, along with a good amount of hashing. Performance varies by platform, but Cloudflare finds that ML-KEM is typically faster than X25519, even at its largest size.

The cost is size, compared to the elliptic curves it replaces:

Alice sendsBob sendsShared secret
X2551932 bytes32 bytes32 bytes
ML-KEM-7681,184 bytes1,088 bytes32 bytes

Key and ciphertext sizes

A kilobyte or so each way is manageable, but it does have side effects. The opening message of a TLS connection no longer fits in a single network packet. When browsers first rolled this out, it broke connections to some servers that had assumed it always would. Some network equipment made the same assumption.

Belt and braces

Module lattices are well studied, but they're also young compared to RSA and elliptic curves. So in practice, most deployments don't use ML-KEM alone. The usual approach is a hybrid: run X25519 and ML-KEM-768 side by side, and derive the session key from both shared secrets. The session key is designed to stay safe as long as at least one of them does.

Hybrid key exchange

This might seem odd. If a quantum computer can break X25519, why keep it? Because the two protect against different problems:

  • ML-KEM protects against a future quantum computer. If someone records a connection today and builds a quantum computer later, they can break the X25519 half. They still can't get the session key without breaking ML-KEM too.
  • X25519 protects against a flaw in ML-KEM. Someone could find a weakness in lattices, or a bug that leaks keys from one of the newer ML-KEM implementations. An attacker might be able to use it straight away, with an ordinary computer. X25519 would still stop them, because breaking it takes a quantum computer that doesn't exist yet.

A hybrid has already paid off in this way, with a different algorithm. In 2019, Google and Cloudflare tested a hybrid of X25519 and SIKE on real connections. When SIKE was broken three years later, those connections were still protected by X25519.

A hybrid does have a limit. Against an attacker with a quantum computer, X25519 adds nothing, and the hybrid is only as strong as ML-KEM. So X25519 is mainly useful in the years before that computer exists, while ML-KEM is still being tested in the real world. It's cheap to keep, since it only adds 32 bytes each way, but it also adds some complexity. NIST says that hybrids are typically expected to be a temporary step before moving to post-quantum algorithms alone.

The pairing of X25519 and ML-KEM-768 is the X25519MLKEM768 key exchange that you'll find in TLS, standardized this year as RFC 10024 and enabled by default in Chrome, OpenSSL and Go. OpenSSH uses the same pairing by default since version 10.0, and both Signal and iMessage have combined elliptic curves with ML-KEM's predecessor Kyber for some time.

NIST is hedging too. In 2025 it selected a second KEM, HQC, which is built on entirely different mathematics, as a backup in case lattices ever turn out to have a weakness.

Simple maths, sharp edges

If you read the previous post, some of this may be giving you déjà vu. ML-KEM's building blocks are simple: integer arithmetic on small numbers, with no lookup tables indexed by secret data, and noise that is picked by counting coin flips. The more traditional choice, picking the noise from a bell curve (a Gaussian distribution), is difficult to implement without leaking timing information. Like ChaCha, it was designed to be straightforward to implement in constant time.

That doesn't make it foolproof. In late 2023, researchers found that the official reference implementation of Kyber divided a secret-dependent number by 3329 in a couple of places. On many CPUs, the time that a division takes depends on the numbers involved, and that tiny variation was enough to recover an entire secret key, in some cases within minutes. The attack was named KyberSlash, and one of the people behind it was Daniel Bernstein, the same researcher who designed ChaCha and demonstrated the cache-timing attack on AES that we covered last time.

The fix was a few lines of code: replace the division with a multiplication and a shift. The lesson is one we've seen before. The maths can be sound and an implementation can still leak, so use a well-vetted library rather than writing your own.

Where things stand

The move to post-quantum cryptography is still in progress, and some parts are further along than others:

  • Standards. ML-KEM is FIPS 203, and its signature sibling ML-DSA is FIPS 204. NIST recommends ML-KEM-768 as the default.
  • Deployment. Key exchange is furthest along. Most of the human traffic reaching Cloudflare already uses post-quantum encryption, and it's on by default in recent versions of many common tools. Signatures and certificates are at an earlier stage, partly because post-quantum signatures are much larger than the ones they replace.
  • Deadlines. NIST will deprecate RSA and elliptic curve key exchange after 2030 and disallow them after 2035. Separately, a White House order and the memo that puts it into practice require the most sensitive US government systems to move to post-quantum key exchange by the end of 2030, and the NSA's CNSA 2.0 suite specifies ML-KEM-1024 for national security systems.

For key exchange, much of the migration comes down to keeping software up to date. If your TLS library, SSH client and browser are recent, there's a good chance that you're already using ML-KEM.

For everything else, the NSA's advisory is a useful reference. Its post-quantum guidance for national security systems is CNSA 2.0, with older approved algorithms as a stopgap, but its six actions are useful more widely. They are ordered to put quick wins with the broadest reach first:

  1. Take an inventory of where cryptography is used, and which algorithms each system relies on.
  2. Move TLS services to TLS 1.3, with post-quantum settings enabled wherever they're available.
  3. Stand up certificate authorities and roots of trust that can handle post-quantum algorithms.
  4. Replace certificates, and re-sign software and firmware, that rely on deprecated algorithms.
  5. Apply the software and firmware updates that enable post-quantum cryptography.
  6. Isolate whatever can't be upgraded yet, and monitor live traffic for weak cryptography that is still in use.

The second step is where ML-KEM comes in. For anyone who needs a hybrid key exchange, the advisory points to RFC 10024. That standard defines several pairings, including X25519MLKEM768. But CNSA 2.0 asks for ML-KEM-1024, and the pairing that uses it is SecP384r1MLKEM1024.


Conclusion

At its core, ML-KEM multiplies small secret numbers by large public ones, adds them up, wraps around at 3329, and then deliberately gets the answer slightly wrong. Exact equations are easy to solve. Slightly wrong ones, with enough unknowns, are out of reach of every attack we know of, quantum or otherwise. The rest of the design, from polynomials to modules to the re-encryption check, is there to make that idea compact, fast, and safe against tampered ciphertexts.

It comes with trade-offs. The keys are larger, and the problem it relies on has been studied for less time than the ones it replaces. That's why it's mostly being deployed alongside elliptic curves for now, and why NIST is standardizing a backup. The details will keep changing as the transition continues, but the core idea should help you make sense of it the next time ML-KEM shows up in a changelog or a TLS config.

Sometimes the best way to keep a secret is to be carefully, deliberately, a little bit wrong.

Deployment
Get started

Keep your secrets

Self-host or start on fully managed Cloud in under a minute.

Self-hosted

Run Phase on your own infrastructure. Free and open source.

AI agent deployment skill
$ npx skills add phasehq/ai
Cloud

Fully managed. From signup to your first secret in 60 seconds.

Sign up with