← Back to section · Post-Quantum Cryptography

ML-KEM Implementation Notes: The Traps Are All in the Details, Past the Specification

The algorithm can be right while the implementation still loses everything: randomness, input validation, constant-time behaviour and key management — the five places production code most often comes to grief when landing FIPS 203.

Published 22 November 2026 · 5 min read · PQ Frontier

ML-KEM Implementation Notes: The Traps Are All in the Details, Past the Specification
AI-generated site artwork
Video explainer · ML-KEM: one complete key exchange

Trap one: randomness quality — the dullest and the deadliest

All of ML-KEM’s security rests on three random inputs: the seed d and implicit-rejection value z at key generation, and the random message m at encapsulation. If they are predictable, an attacker can replay the sampling process and compute the private key or the shared secret, and no amount of beautiful lattice mathematics will save the system. FIPS 203 explicitly requires these random bits to come from an approved random bit generator (RBG); the recurring engineering accidents include virtual-machine snapshot rollbacks replaying random state, embedded devices generating long-term keys before first-boot entropy is available, and “simplifying” m into a counter or a timestamp. Mind the API shape, too: the internal interface of FIPS 203 takes d and z explicitly as inputs (to make test vectors reproducible), and a production wrapper must wire the system random source in above that layer — test hooks shipping to production, such as all-zero or fixed seeds left in a code path, are the accident pattern this class of system replays again and again.

Trap two: skipping the input checks

The standard prescribes input-validity checks for all three algorithms. They look like bureaucratic ceremony but each has a defensive purpose. Before encapsulation, the public key undergoes a modulus check (decode it at 12 bits per coefficient and re-encode, comparing for equality), ensuring every coefficient lies in the legal range and keeping malformed keys from driving the arithmetic into unanalysed territory. During decapsulation, the public-key hash embedded in dk is checked against the copy of the public key stored alongside it, so swapped or corrupted key material cannot keep “working while sick” — these checks are the first gate against fault injection and key-confusion attacks. Implementers often trim them “for performance”, but on a modern CPU their cost is negligible next to the operation as a whole; every check you save has to be explained twice over in a security audit.

Trap three: non-constant-time decapsulation

Decaps is the main side-channel battleground. Inside it sit a judgement — “does the re-encrypted ciphertext equal the received one” — and a selection — “use K′ if equal, the pseudo-secret if not”. If the comparison returns early at the first differing byte, an attacker can probe byte by byte with timing; if the selection is a plain conditional branch, power draw and electromagnetic emanations can leak the same information. The correct approach is constant time everywhere: the comparison scans all bytes before reducing, the selection is done with mask arithmetic, and at higher compiler optimisation levels you must additionally defend against the classic problem of the optimiser turning constant-time code back into branches — use assembly barriers or audited primitive libraries. Subtler still is decapsulation-failure timing: the honest failure probability is at the 2^-139 scale, but if an implementation makes the “failure path” measurably faster or slower than the normal path, an attacker who can submit ciphertexts at will may still assemble a usable oracle. The principle is single: Decaps’s externally observable behaviour — time, power, output format — must depend neither on secrets nor on the outcome of the checks.

Trap four: storage and reuse of key material

A KEM private key is long-term material, and its management should observe at least three rules. First, the serialisation format is the one the standard defines: the 3168 bytes of dk (at the 1024 tier) include the public-key copy and its hash, so store and transport it as a whole rather than inventing a “store only the seed” compressed format — seed-form private keys, where only d and z are kept and the rest re-expanded, are indeed permitted by the standard, but the expansion path and its checks must not be bypassable. Second, be precise about reuse: an ML-KEM private key is meant to be reused across many decapsulations (that is how a KEM is used), but mixing the same key pair across protocols and security domains breaks the protocol layers’ security arguments; separate uses by key derivation and purpose isolation. Third, zeroise private keys and intermediate secrets in memory after use (both the standard and the FIPS 140 series of module requirements point this way), taking care that the compiler does not optimise the memset away.

Trap five: half-right interoperability

The last class of trap loses interoperability rather than security: bit order in byte encoding, rounding direction in compression, one wrong digit in an NTT constant table, or using a legacy “Kyber” implementation as if it were ML-KEM (standardisation changed the hashing and seed usage, so the two are not byte-compatible) — the symptom is always “all my own tests pass, and I cannot talk to anyone else”. There is no clever fix: run the NIST ACVP test vectors and compare byte for byte, cross-test against at least one independent implementation, and put both steps in CI rather than scrambling for them before launch.

Trap six: test suites that only test the happy path

The last trap lives in process rather than code: many teams’ test suites contain only positive cases — valid keys, valid ciphertexts, everyone happy. But ML-KEM’s security boundary is defined by the negative cases: a tampered ciphertext must take the implicit-rejection path with no timing difference, a malformed public key must be caught by the modulus check, a mismatched private-key hash must halt with an error, and inputs of every wrong length must be refused. Leave those untested and you have treated the FO transform and the input checks as if they never existed. A responsible test matrix includes at least: the official ACVP vectors (positive cases matched byte for byte), mutation testing that flips ciphertext bytes one at a time (the output must change unpredictably and the timing distribution must stay indistinguishable), robustness tests with truncated and elongated inputs, and the same vector set rerun across optimisation levels and compilers (to catch constant-time code being optimised into shape-shifting branches). Keeping that matrix in continuous integration for the long term is what it means to maintain an implementation as a security product rather than a mathematics exercise.

Summary

ML-KEM’s security is a product of “specification × implementation”, and if either factor is zero the product is zero: randomness must be approved-grade, the input checks must all be present, Decaps must be constant-time, key material must be managed across its lifecycle, and interoperability must be proven against vectors. When writing ML-KEM code, treat every seemingly fussy “shall” in the standard as the marker of a real attack class or a real incident.

References

  1. NIST, FIPS 203: Module-Lattice-Based Key-Encapsulation Mechanism Standard, 2024 (Chapter 7 specifies the input checks). https://doi.org/10.6028/NIST.FIPS.203
  2. NIST, ACVP ML-KEM test vectors. https://github.com/usnistgov/ACVP-Server

Tags: ML-KEMImplementation SecuritySide ChannelsFIPS 203