Skip to content
Trapdoor·Lab

ML-KEM: from LWE to post-quantum key exchange

1Toy

Noise that hides a secret

Take the equations bᵢ = ⟨aᵢ, s⟩ mod q: public vectors aᵢ, public results bᵢ and a secret vector s. That is school algebra — Gaussian elimination recovers s at once. But add a small random noise eᵢ to each result, and recovering s from the samples (aᵢ, bᵢ = ⟨aᵢ, s⟩ + eᵢ) becomes the learning with errors problem (LWE). Oded Regev defined it in 2005 and built encryption of a bit on it: the sender adds up a random subset of the samples and adds the bit times ⌊q/2⌋ to the sum of the bᵢ. The owner of s subtracts ⟨u, s⟩ and gets bit · ⌊q/2⌋ plus the sum of the noise — a point on the circle of numbers modulo q, which only has to be assigned to the nearer zone. In the toy s is two numbers, and q = 3329 is the same modulus as in ML-KEM. Move the noise slider: with little noise the point always lands in the right zone; with a lot, the noise sum passes a quarter of the circle and the bit comes out flipped.

Toy implementation — not for productionLWE in two dimensions falls in a fraction of a second, and this code does not run in constant time. For real: ML-KEM-768 from WebCrypto or an audited library.

LWE in two dimensions

100

Drawing a key…

ML-KEM-768 (FIPS 203, August 2024) is the same idea at scale, in its Module-LWE form: numbers are replaced by polynomials, which shortens keys and speeds up multiplication. The secret is 3 polynomials of 256 coefficients, 768 numbers modulo 3329, each in −2 … 2. Regev-style encryption carries 256 bits at once: a random message m from which both sides derive the shared secret K with a hash. After decrypting m, the receiver encrypts it again and compares the result with the ciphertext it got; if they differ, it returns a pseudorandom secret instead of an error (implicit rejection). The noise is chosen so that a decapsulation failure has probability 2−164.8 (FIPS 203, Table 1). Altogether this is a key encapsulation mechanism (KEM): KeyGen, Encaps and Decaps — computed below by your browser’s WebCrypto.

ML-KEM-768 live

Real ML-KEM-768 (FIPS 203) from your browser’s WebCrypto. You generate the key pair; the sender knows only your encapsulation key ek.

Checking whether this browser has ML-KEM in WebCrypto…

2Break it

What you cannot break here

ML-KEM has one attack in the graph — Shor’s algorithm — and resists it. First see on the toy what protects LWE. Without noise the samples are a plain system of equations: two are enough for Gaussian elimination modulo 3329 to hand over s in a few steps — that is why the noise is necessary. With noise the solution of the system is wrong, but in two dimensions that changes nothing: there are only 3329² ≈ 11 million candidates for s, and your browser checks them all in a fraction of a second. Security comes from the dimension. The toy scales the rate measured here up to ML-KEM-768’s 768 dimensions — an extrapolation, not a measurement — and quotes the cost of the best known lattice attacks from the Kyber 3.02 specification (Table 4), the scheme ML-KEM came from.

Toy implementation — not for productionLWE in two dimensions falls in a fraction of a second, and this code does not run in constant time. For real: ML-KEM-768 from WebCrypto or an audited library.

Recover the secret from the samples

You are on the attacker’s side. You know only the samples (aᵢ, bᵢ) and the noise bound η — recover the secret s.
0

Drawing a secret…

Run the search, and from your browser’s rate I will work out how long it would take at ML-KEM-768’s size.

A quantum computer does not help here the way it does against RSA and ECDH. Shor’s algorithm finds the period of a function: aˣ mod N repeats every r steps, the quantum Fourier transform reveals r, and r yields the factors of N. The discrete logarithm falls the same way, on elliptic curves too (the Shor’s algorithm station). In LWE there is no hidden period to find: the periodic structure of the lattice is public — the public aᵢ and q define it — and the secret is which lattice point lies closest to the vector b. The noise makes this a question of distance, not of period. Nobody knows a polynomial-time quantum algorithm for LWE: in 2024 Yilei Chen announced one, and on 18 April he wrote himself that step 9 contains a bug he does not know how to fix. The known quantum speed-ups concern the sieve inside the lattice attack and are built into the parameters: 2¹⁶⁶ instead of 2¹⁸³ operations for ML-KEM-768. That is why its status in the graph is “resistant”. In TLS, ML-KEM does not replace the curve yet but works next to it (X25519MLKEM768) — see the Hybrid schemes station.

3In the real world

Where ML-KEM runs

  • Chrome and TLSSince Chrome 131 the browser offers the ML-KEM-768 + X25519 hybrid in TLS (codepoint 0x11EC). It replaced the earlier hybrid with Kyber (0x6399), which the final ML-KEM is incompatible with.Source (Chrome and TLS)
  • OpenSSH 10.0Since the release of 9 April 2025, ssh agrees on keys by default with the hybrid mlkem768x25519-sha256 — ML-KEM-768 together with X25519.Source (OpenSSH 10.0)
  • Signal — SPQRIn October 2025 Signal added the post-quantum ratchet SPQR to its protocol; it refreshes keys during a conversation with an incremental version of ML-KEM 768.Source (Signal — SPQR)
  • NIST IR 8547NIST’s transition plan (October 2026): RSA, DH and ECDH key establishment is deprecated after 2030 and disallowed after 2035. ML-KEM is today the only approved post-quantum KEM, and ML-KEM-768 the recommended default parameter set.Source (NIST IR 8547)
  • WebCryptoThe WICG draft “Modern Algorithms in the Web Cryptography API” adds ML-KEM to WebCrypto: generateKey, encapsulateBits and decapsulateBits. The toy above uses it — in a browser that has implemented it.Source (WebCrypto)
4Take it to your project

ML-KEM from the platform, paired with a curve

Good practice

Take ML-KEM-768 (or ML-KEM-1024) from the platform: WebCrypto, .NET 10 and later (MLKem), OpenSSL 3.5 or an audited library. Treat the shared secret K as key material — run it through HKDF with a context and use it in an AEAD. In protocols, combine ML-KEM with X25519, as TLS and OpenSSH do. Start with data that must stay secret for years: a ciphertext collected today can be decrypted once a quantum computer exists (“harvest now, decrypt later”, NIST IR 8547).

JavaScript
const alg = { name: 'ML-KEM-768' };
const { publicKey, privateKey } = await crypto.subtle.generateKey(
  alg, false, ['encapsulateBits', 'decapsulateBits'],
);
// the sender knows only publicKey (ek)
const { ciphertext, sharedKey } =
  await crypto.subtle.encapsulateBits(alg, publicKey);
// the receiver: the same secret → HKDF → AEAD key
const secret = await crypto.subtle.decapsulateBits(alg, privateKey, ciphertext);
C#
using MLKem receiver = MLKem.GenerateKey(MLKemAlgorithm.MLKem768);
byte[] ek = receiver.ExportEncapsulationKey(); // 1184 B

using MLKem sender = MLKem.ImportEncapsulationKey(MLKemAlgorithm.MLKem768, ek);
sender.Encapsulate(out byte[] ciphertext, out byte[] sharedSecret);

byte[] secret = receiver.Decapsulate(ciphertext); // == sharedSecret
Anti-pattern

Do not write your own ML-KEM or LWE from a paper: parameters, noise sampling and constant time are separate traps — in KyberSlash several Kyber libraries divided a secret number by q, and the timing of the division gave the secret away. Do not take toy parameters either: in two dimensions the secret falls in a fraction of a second. No error from decapsulation does not tell you who sent the ciphertext — a KEM does not authenticate the sender, since anyone who knows ek can encapsulate a secret. Authentication comes from a signature or a protocol such as TLS.

JavaScript
// ✕ no error ≠ trusted sender: a modified ciphertext also gives 32 B
const key = await crypto.subtle.decapsulateBits(alg, privateKey, ciphertext);
if (key.byteLength === 32) trust(sender); // ek is public