← Back to section · Post-Quantum Cryptography

Encapsulation and Decapsulation, End to End: How ML-KEM's Three Algorithms Work Inside

From expanding the matrix A out of a seed and centred binomial sampling to hash-based derivation of the shared secret and implicit rejection: a step-by-step walk through KeyGen, Encaps and Decaps following the data flow of FIPS 203.

Published 22 November 2026 · 5 min read · PQ Frontier

Encapsulation and Decapsulation, End to End: How ML-KEM's Three Algorithms Work Inside
AI-generated site artwork
Video explainer · ML-KEM: one complete key exchange

The big picture: K-PKE plus one layer of wrapping

FIPS 203 specifies ML-KEM in two layers. The inner layer is a public-key encryption scheme, K-PKE, which offers security only against passive attacks (IND-CPA): it encrypts a 32-byte message under a public key. The outer layer is ML-KEM itself, which uses hashing and a re-encryption check to wrap K-PKE into a KEM that resists active attacks (IND-CCA). The skeletons of the three external algorithms — KeyGen, Encaps, Decaps — all rest on one set of hash functions: SHA3-256 (written H, used for the public-key hash), SHA3-512 (written G, outputting 64 bytes at once and serving both as key derivation and as a randomness expander), SHAKE256 (written J and PRF), and SHAKE128, the extendable-output function that generates the matrix.

The shortcut to understanding ML-KEM is to track how three pieces of data flow between the algorithms: the random seeds, the message m, and the shared secret K.

KeyGen: growing a key pair out of 64 bytes of seed

Key generation consumes two 32-byte random inputs: the seed d and the implicit-rejection value z. First, (ρ, σ) = G(d): ρ is the public matrix seed and σ the private sampling seed. The matrix  is expanded element by element from ρ using SHAKE128 — note that the public key stores only the 32-byte ρ, and the bulky matrix is regenerated on demand for every operation, which is the key trick that keeps ML-KEM public keys at the kilobyte scale. Every coefficient of the secret vector s and the noise vector e is sampled from a centred binomial distribution by the PRF keyed with σ, counter by counter. Then, in the NTT domain, t̂ =  ∘ ŝ + ê is computed; the public key (encapsulation key ek) is the encoding of t̂ together with ρ, while the private key (decapsulation key dk) packs four things: the K-PKE decryption key, a full copy of ek, the public-key hash H(ek), and z. This explains why dk is much larger than ek — it embeds everything the checks will need.

Encaps: what actually gets encrypted is the message m

Encapsulation takes three steps. First, draw a 32-byte random message m and compute the public-key hash H(ek). Second, (K, r) = G(m ‖ H(ek)): the single hash output is split in half — the first 32 bytes are the shared secret K, and the last 32 bytes, r, serve as the randomness tape for the K-PKE encryption. Third, c = K-PKE.Encrypt(ek, m, r), and the outputs are the ciphertext c and K.

This construction carries two deeper points. First, K is bound to m and stirred with the public-key hash: the same m derives completely different K values under different public keys, ruling out cross-key relations. Second, the encryption randomness r is not independently random but deterministically derived from m — which lets the decapsulator replay the entire encryption verbatim and compare the ciphertext byte for byte. Stirring H(ek) into the derivation also buys domain separation: the shared secret is bound to this specific public key, so even if an attacker assembles the same m elsewhere, they cannot carry or correlate sessions across different key pairs; it is also why the decapsulation key must preserve the copy of ek and its hash exactly — the derivation formula needs it on both sides, and without it the decapsulator cannot even assemble the inputs to recompute K′. The K-PKE encryption itself follows the standard LWE-style structure: expand the matrix from ρ, sample a temporary random vector and noise, compute the two components (u, v), and compress them to du and dv bits respectively for the ciphertext encoding.

One layer deeper: how K-PKE turns m into the ciphertext (u, v)

With the outer flow clear, look inside K-PKE.Encrypt. It likewise expands the matrix  from the ρ in the public key, then uses the encryption randomness r as the PRF key to sample, counter by counter, a temporary random vector y (parameter η1) and two noise terms e1 and e2 (parameters η2 and η1). It then computes u = A^T ∘ y + e1 and v = t^T ∘ y + e2 + μ, where μ is the encoding polynomial of the message m: expand m’s 32 bytes bit by bit, mapping each 0 bit to the coefficient 0 and each 1 bit to ⌈q/2⌋ — the information is written into “closer to 0 or closer to q/2”. Decryption computes an approximation of that polynomial as v − s^T ∘ u and reads each bit back by checking whether every coefficient lies closer to 0 or to q/2; as long as the noise and compression error do not push a coefficient past the q/4 decision line, the bit recovers exactly — and this is precisely where the failure probability δ comes from. Finally u and v are compressed to du and dv bits respectively and concatenated into the ciphertext c. Grasping this layer pays off practically: it explains why an ML-KEM ciphertext has two components of different sizes (u is a vector of k polynomials, v a single polynomial), and why du and dv in the parameter table are always one larger, one smaller — their error budgets are not symmetric.

Decaps: decrypt, re-encrypt, compare — accept, or implicitly reject

Decapsulation is four moves. Decrypt c with the K-PKE key inside dk to obtain m′; compute (K′, r′) = G(m′ ‖ H(ek)) (using the public-key hash stored in dk — which is why the copy of ek and its hash inside dk must match the real ek, and why FIPS 203 mandates key-validity checks); re-encrypt m′ under r′ to get c′; and finally compare c′ with c in constant time: if they are equal, output K′; if not, output J(z ‖ c) — a “pseudo shared secret” derived from the secret value z and the received ciphertext.

The beauty of implicit rejection is that an attacker who submits a tampered ciphertext receives only a K that looks perfectly random: there is no error signal and no information about the private key, while honest parties necessarily compute the same K whenever c is untouched. The whole of Decaps throws no exceptions and leaks nothing through branches; its output is always a well-formed 32-byte secret, so the protocol layer needs no special path for “decapsulation failure”. It also means the upper protocol simply feeds the shared secret into key derivation as usual, without needing (or attempting) to tell whether the secret in hand is genuine or fake — that distinction is deliberately kept inside the KEM.

Summary

Compressed into one sentence: KeyGen expands a lattice key pair out of seeds, Encaps encrypts a random message and derives the shared secret from it, and Decaps verifies — by decrypting and re-encrypting — that the ciphertext was not tampered with, faking a secret with z whenever verification fails. The next layer down — why re-encryption and comparison lift CPA security to CCA security — is the work of the Fujisaki–Okamoto transform, which has its own article on this site.

References

  1. NIST, FIPS 203: Module-Lattice-Based Key-Encapsulation Mechanism Standard, 2024. https://doi.org/10.6028/NIST.FIPS.203
  2. CRYSTALS Team, CRYSTALS-Kyber Algorithm Specifications and Supporting Documentation, Round 3 submission, 2021. https://pq-crystals.org/kyber/

Tags: ML-KEMK-PKEKey EncapsulationFIPS 203