๐ 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:
- Bob takes the locked box (ciphertext C)
- Bob uses his secret key (s) to unlock it
- Bob "subtracts out" the fog Alice added
- Bob finds the piece of paper inside (message m)
- 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:
Bob knows:
Bob computes message recovery:
Expanding (with algebra):
Where the error terms cancel out (they're small!)
Result: (recover Alice's message)
Implicit Rejectionโ
If Bob can't recover valid m, he uses implicit rejection:
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:
All terms are small polynomials:
- sampled from Binomial(ฮทโ)
- and 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:
- Bob receives: C = (cโ, cโ)
- Bob decompresses: cโ', cโ'
- Bob computes: m' = cโ' - tแตยทcโ' - cโ'แตยทs
- Bob checks: Is m' bounded?
- If yes: Compute u' = f(m')
- Bob derives: K' = KDF(u', m', cโ', cโ')
- 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!