Skip to main content

๐Ÿ”“ Opening Boxes

Decapsulation in ML-KEMโ€‹


๐ŸŽฏ The Simple Storyโ€‹

Bob received a locked box from Alice.

Inside the box is a piece of paper with the secret code Alice sent, but Bob can't read it yet.

Bob's special key (s) can open the box:

  1. Bob takes the locked box (ciphertext C)
  2. Bob uses his secret key (s) to unlock it
  3. Bob "subtracts out" the fog Alice added
  4. Bob finds the piece of paper inside (message m)
  5. Bob does the same math Alice did to get the same secret (K)

Eve's problem: Eve sees the locked box (C) but doesn't have Bob's special key (s)!

Result: Bob recovers the secret K, Eve can't!

Why it works: Bob knows where Alice started (his secret), so he can unwind exactly what Alice did!


๐Ÿง  Mental Modelโ€‹

Hold this picture in your head:

Bob's Decapsulation:

Step 1: Bob receives ciphertext
C = (cโ‚, cโ‚‚) from Alice

Step 2: Bob decompresses
cโ‚' = Decompress(cโ‚)
cโ‚‚' = Decompress(cโ‚‚)

Step 3: Bob computes message recovery
m' = cโ‚‚' - tแต€ยทcโ‚' - cโ‚'แต€ยทs
Where s is Bob's secret!

Step 4: Bob checks message
Is m' reasonable (small coefficients)?

If yes: Compute u' = recovery function(m')
If no: Use implicit rejection

Step 5: Bob derives secret
K' = KDF(u', m', cโ‚', cโ‚‚')

Result: K' = K (same secret as Alice!)

Think of it like:

๐Ÿ”“ Mailbox (Find mail in locked box with your key)

๐Ÿ“ Reading (Decode what was written by following instructions)

๐ŸŽฏ Reversing (Bob knows where Alice started, can backtrack)


๐Ÿ“Š See It Happenโ€‹

Let's watch Bob decapsulate:


๐ŸŽฎ Try It Yourselfโ€‹

Question 1: Alice encapsulated K = [7, 22, ...] into C = (cโ‚, cโ‚‚). Bob has s from key generation. What does Bob compute for m'?

Show Answer

Bob computes message recovery:

m' = cโ‚‚ - tแต€ยทcโ‚ - cโ‚แต€ยทs

Where:

  • cโ‚‚ = from ciphertext (Alice's part 2)
  • t = Bob's t from key generation
  • cโ‚ = from ciphertext (Alice's part 1)
  • s = Bob's secret (from key generation)

Bob knows: s (keeps secret during key gen)

Bob computes: Result m' (same as Alice's m!)

Answer: m' = m (Bob can recover Alice's message)


Question 2: Why can Bob compute m' when Eve can't? What's Bob's advantage?

Show Answer

Both Bob and Eve see:

  • A, t (shared in public key)
  • C = (cโ‚, cโ‚‚) (ciphertext)

Bob advantage: Has s (secret from key generation)

Bob computes: m' = cโ‚‚ - tแต€ยทcโ‚ - cโ‚แต€ยทs

Bob knows s, so can solve!

Eve sees: Same cโ‚, cโ‚‚, A, t

But Eve doesn't know s, so can't compute the same thing.

She must: Find s AND e from the equation (impossible!)

Answer: Bob has s (knows secret), Eve doesn't!


Question 3: What if m' is "invalid" (coefficients not in range)? What does Bob do?

Show Answer this.

Bob checks: Is m' bounded (close to 0)?

If m' is reasonable (coeffs โ‰ˆ small), Bob continues:

  • Bob recovers: u' (from m')
  • Bob derives: K' (from u', m', C)

If m' is invalid (coeffs too big), Bob doesn't know if:

  • Alice truly made bad message
  • Attacker modified ciphertext
  • Decryption error

Solution: Implicit rejection

  • Bob still computes: K' (but from H(C) instead)
  • Attacker can't tell: Bob succeeded or failed!

Answer: Bob uses same K either way (implicit rejection).


๐Ÿ”ข The Mathโ€‹

Decapsulation Algorithmโ€‹

Decapsulate(sk = s, C = (cโ‚, cโ‚‚)):
1. Decompress ciphertext
cโ‚' = Decompress(cโ‚, dโ‚)
cโ‚‚' = Decompress(cโ‚‚, dโ‚‚)

2. Attempt message recovery
m' = cโ‚‚' - tแต€ยทcโ‚' - cโ‚'แต€ยทs

3. Verify message bounds
If m' is "reasonable" then
u' = compute(m')
Else
u' = implicit_rejection

4. Derive shared secret
K' = KDF(u', m', cโ‚', cโ‚‚')
or
K' = KDF(H(cโ‚', cโ‚‚'), 0ยณยฒ, H(cโ‚', cโ‚‚'))

Return K'

Message Recovery Derivationโ€‹

Alice computes: c1=ATu+e1cโ‚ = Aแต€ u + eโ‚ c2=tTu+e2+mcโ‚‚ = tแต€ u + eโ‚‚ + m

Bob knows: t=As+et = A s + e

Bob computes message recovery: mโ€ฒ=c2โˆ’tTc1โˆ’c1Tsm' = cโ‚‚ - tแต€ cโ‚ - cโ‚แต€ s

Expanding (with algebra): =(tTu+e2+m)โˆ’(As+e)Tc1โˆ’c1Ts= (tแต€ u + eโ‚‚ + m) - (A s + e)แต€ cโ‚ - cโ‚แต€ s =tTu+e2+mโˆ’sTATc1โˆ’eTc1โˆ’c1Ts= tแต€ u + eโ‚‚ + m - sแต€ Aแต€ cโ‚ - eแต€ cโ‚ - cโ‚แต€ s =tTu+e2+mโˆ’tTc1โˆ’eTc1โˆ’sTe1= tแต€ u + eโ‚‚ + m - tแต€ cโ‚ - eแต€ cโ‚ - sแต€ eโ‚ =e2+mโˆ’eTc1โˆ’sTe1= eโ‚‚ + m - eแต€ cโ‚ - sแต€ eโ‚ =mโˆ’(eTc1โˆ’sTe1โˆ’e2)= m - (eแต€ cโ‚ - sแต€ eโ‚ - eโ‚‚)

Where the error terms cancel out (they're small!)

Result: mโ€ฒโ‰ˆmm' โ‰ˆ m (recover Alice's message)

Implicit Rejectionโ€‹

If Bob can't recover valid m, he uses implicit rejection:

Kโ€ฒ=KDF(H(c1โˆฃโˆฃc2),032,H(c1โˆฃโˆฃc2))K' = KDF(H(cโ‚ || cโ‚‚), \text{0}ยณยฒ, H(cโ‚ || cโ‚‚))

Where 0ยณยฒ = 32 bytes of zeros.

Property: Attacker can't distinguish between:

  • Bob successfully decapsulating
  • Bob failing decapsulation

So attacker doesn't learn if modified ciphertext!


๐Ÿ’ก Why We Careโ€‹

Decryption Failure Probabilityโ€‹

When Bob computes m', error terms accumulate:

e2+eTc1โˆ’sTe1e_2 + e^T c_1 - s^T e_1

All terms are small polynomials:

  • e2e_2 sampled from Binomial(ฮทโ‚‚)
  • eTc1e^T c_1 and sTe1s^T e_1 are small (multiplication results small + small)

Probability of too large: ~2^-139

Meaning: One in 10^42 decapsulations fail. Basically never!

Bob's Decryption Advantageโ€‹

Bob knows: s (his secret from key generation)

Eve doesn't: s (only sees A, t, C)

Result:

  • Bob can: Compute the subtraction (cancel errors)
  • Eve can't: Doesn't know s to cancel with

Security Propertyโ€‹

If Bob implicitly rejects (can't recover m):

Eve can't tell if:

  • Alice sent bad message
  • Someone modified ciphertext
  • Bob's error terms too big

Benefit: Attacker learns nothing from failed attempts!


โœ… Quick Checkโ€‹

Can you explain decapsulation to a 5-year-old?

Try saying this out loud:

"Bob gets a locked box from Alice. He uses his secret key to open it. Inside is a paper with instructions. Bob follows them backward to find what Alice wrote. He gets the same secret number Alice put in the box!"

Can you trace decapsulation?

Try this examples:

Bob decapsulates:

  1. Bob receives: C = (cโ‚, cโ‚‚)
  2. Bob decompresses: cโ‚', cโ‚‚'
  3. Bob computes: m' = cโ‚‚' - tแต€ยทcโ‚' - cโ‚'แต€ยทs
  4. Bob checks: Is m' bounded?
  5. If yes: Compute u' = f(m')
  6. Bob derives: K' = KDF(u', m', cโ‚', cโ‚‚')
  7. Result: K' = K (same secret Alice sent)

Answer: Bob uses s (secret) to recover K, Eve can't!


๐Ÿ“‹ Key Takeawaysโ€‹

โœ… Decapsulation steps: Decompress C, recover m', derive K' โœ… Bob's advantage: Has s (can cancel error terms) โœ… Message recovery: m' = cโ‚‚ - tแต€ยทcโ‚ - cโ‚แต€ยทs โœ… Error cancellation: Errors are small, can still decode โœ… Failure probability: ~2^-139 (basically never fails) โœ… Implicit rejection: If fails, can't tell (attacker learns nothing) โœ… Shared secret: K' = K (Bob gets same secret!)


๐ŸŽ‰ What You'll Learn Nextโ€‹

Now you understand decapsulation! Next, we'll see the full example:

๐Ÿ’ป Practical Implementation โ†’ Code Examples

Try ML-KEM in real Python/JavaScript code!