Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

How does ML-KEM produce the same shared secret on both sides?

Alice wants to send Bob an invoice. How can they establish a key that only they know, even if someone can see everything they send? The arrows show what travels between them.

AliceSender BobRecipient
  1. Bob — Generate a public key and a private key. Keep the private key.
  2. From Bob to Alice — Send the public key
  3. Alice — Use Bob’s public key to create a ciphertext and a shared secret. Keep the shared secret.
  4. From Alice to Bob — Send the ciphertext
  5. Bob — Use the private key to decode the expected-length ciphertext. Derive a candidate shared secret and re-encrypt the decoded value.
  6. Bob — Compare the received and regenerated ciphertexts in constant time. Select the candidate if they match, or a fallback derived from a private value and the received ciphertext otherwise. The result of the check stays with Bob.
  7. With an unaltered ciphertext and the corresponding key, Alice and Bob obtain the same 32-byte shared secret.

Alice can now encrypt the invoice with a key derived from their shared secret. Envelope example

Why introduce errors on purpose?

Given enough exact linear equations, you can solve for the unknowns by elimination. Suppose you add a small secret error to the result of each multiplication and publish only the remainder modulo a fixed number. Producing these equations is easy, but recovering the original secret is believed to be hard at suitable parameter sizes, even with many equations. This is the idea behind Learning with Errors (LWE). To keep keys and ciphertexts practical in size while accounting for quantum computers, Kyber’s designers used Module-LWE, which performs these calculations on vectors of polynomials. ML-KEM is the standardized scheme based on Kyber. Kyber design background · FIPS 203

Why can a slightly different value decode to the same bit?

Imagine a clock face numbered from 0 to 15. Place bit 0 at mark 0 and bit 1 at mark 8, on the opposite side of the clock. A reading of 7 or 9 is closest to 8, so it decodes to 1. A reading of 15 is one step around the clock from 0, so it decodes to 0. The two parties can compute different intermediate values as long as they round to the same mark at the end.

What cancels out when Bob uses the private key?

Set aside rounding from compression and call the internal message encoded on the clock face E. A is a public matrix, s is Bob’s secret, and r is a value Alice uses just this once. The superscript ᵀ denotes transposition, which swaps rows and columns.

OperationMain equation
Bob creates his public keyt = A·s + e, where e is a small error
Alice computes the two ciphertext componentsu = Aᵀ·r + e₁ and v = tᵀ·r + e₂ + E
Bob computes the value to decodev − sᵀ·u = E + eᵀ·r + e₂ − sᵀ·e₁

Try that subtraction on the clock face. Use a single number in place of the matrix and let all values wrap around at 16. With A = 5, Bob’s secret s = 2, and error e = 1, the public value t is 5 × 2 + 1 = 11. Alice puts a 1 bit at mark 8 and chooses r = 1, e₁ = 1, and e₂ = 0. She gets u = 6. For v, 11 + 8 = 19 wraps around to 3. Bob receives 6 and 3. He starts at mark 3, takes 2 × 6 steps backward, and lands at 7, which decodes to the original bit 1. This small example shows how cancellation and rounding work together.

In the last row, (A·s)ᵀ·r from v equals sᵀ·Aᵀ·r from the term being subtracted, so the two cancel. If the remaining error is small, Bob can recover the bits encoded in E. ML-KEM uses polynomials with 256 coefficients and works modulo 3329. The number-theoretic transform (NTT) changes the representation to speed up polynomial multiplication. ML-KEM-512, ML-KEM-768, and ML-KEM-1024 use vectors of 2, 3, and 4 polynomials respectively, with error distributions and compression tuned to each parameter set.

Why encrypt again after decoding?

Alice derives the shared secret and the randomness for the internal encryption from a fresh random value and the hash of Bob’s public key. Bob repeats the derivation using the value he decoded. He also derives a fallback from a separate secret value stored in the private key and the received ciphertext. He regenerates the ciphertext and compares it with the one he received in constant time. For a ciphertext of the expected length, a match selects the candidate shared secret, and a mismatch selects the fallback. In the usage example, decaps returns only the selected 32-byte value in either case. The comparison result stays private. Key generation and encapsulation example · FIPS 203 §§4–7