Skip to main content

๐Ÿ”’ Putting Secrets in Boxes

Encapsulation in ML-KEMโ€‹


๐ŸŽฏ The Simple Storyโ€‹

Alice wants to send a piece of paper with a secret code to Bob.

She can't just mail it (Eve reads it!). She needs a magic box!

Alice's magic box:

  1. Alice grabs a random piece of paper (secret message K)
  2. Alice creates instructions "how to send K"
  3. Alice writes instructions with Bob's map (matrix A)
  4. Alice adds fog to the instructions (error eโ‚)
  5. Alice locks the instructions in a box (ciphertext C)
  6. Alice sends the locked box to Bob

Bob's box opening:

  1. Bob receives the locked box (ciphertext C)
  2. Bob uses his secret key (s) to open it
  3. Bob recovers Alice's instructions
  4. Bob uses his secret location to decode them
  5. Bob gets the same piece of paper (K)!

Eve watches everything but:

  • Eve sees Bob's map (matrix A) that was shared earlier
  • Eve sees the locked box (ciphertext C) sent over network
  • But Eve can't: open the box without Bob's secret key (s)!

That's how encapsulation works - Alice puts a random secret K into a locked box using the public map (A, t), Bob opens it with his secret key (s), and both have the same secret K!


๐Ÿง  Mental Modelโ€‹

Hold this picture in your head:

Alice's Encapsulation:

Step 1: Alice generates random secret
K = 32 random bytes

Step 2: Alice picks random message vector
u = (polynomials)

Step 3: Alice uses Bob's public map
A = matrix (shared with Alice)

Step 4: Alice computes lockbox parts
cโ‚ = Aแต€ยทu + eโ‚ โ† part 1
cโ‚‚ = tยทu + eโ‚‚ + m โ† part 2 (m from u)

Step 5: Alice locks the box
C = (cโ‚, cโ‚‚) ciphertext

Step 6: Alice sends box
Send C to Bob

Step 7: Both have K
Alice: K from step 1
Bob: Gets K when opening box (decapsulation)

Eve sees: A (shared earlier), C (sent)
Eve can't: Open box without s (Bob's secret!)

Think of it like:

๐Ÿ“ฆ Package shipping (Alice packs item, sends box, Bob opens)

๐Ÿ” Envelope (Write letter, seal envelope, send, Bob opens with his key)

๐ŸŽ Puzzle present (Mystery gift, wrapped, sent, opened by Bob)


๐Ÿ“Š See It Happenโ€‹

Let's watch Alice encapsulate:


๐ŸŽฎ Try It Yourselfโ€‹

Question 1: Alice generates K = [42, 17, ...]. She encapsulates to get C = (cโ‚, cโ‚‚). What does Bob recover when he decapsulates?

Show Answer

Bob receives: Ciphertext C = (cโ‚, cโ‚‚)

Bob decapsulates: K' = Decapsulate(sk, C)

Result: K' = [42, 17, ...] (same as Alice's K!)

Why? Because Bob uses his secret s (from key generation) to compute the same ciphertext parts and recover K.

Answer: Bob gets K' = K (same secret as Alice generated)


Question 2: Why doesn't Eve just capture C and extract K herself?

Show Answer

Eve sees: Ciphertext C (encapsulated K)

Eve wants: Open box to get K

Problem: Eve doesn't have sk (Bob's secret)

Without sk, Eve can't compute what K is, even though she sees C!

Eve must solve: Given C, find encapsulated K

This is the MLWE problem: Aยทs + e = t (with encapsulation modifications)

Answer: Eve sees C but can't get K without Bob's sk.


Question 3: What makes ciphertext C different for each encapsulation? If Alice encapsulates twice, does C change?

Show Answer this.

Alice encapsulates (first time):

  1. Alice generates: uโ‚ (random vector)
  2. Alice computes: cโ‚โ‚ = Aแต€ยทuโ‚ + eโ‚โ‚
  3. Alice computes: cโ‚‚โ‚ = tยทuโ‚ + eโ‚‚โ‚ + mโ‚
  4. Ciphertext: Cโ‚ = (cโ‚โ‚, cโ‚‚โ‚)
  5. Secret: Kโ‚

Alice encapsulates (second time):

  1. Alice generates: uโ‚‚ (different random!)
  2. Alice computes: cโ‚โ‚‚ = Aแต€ยทuโ‚‚ + eโ‚โ‚‚ (different!)
  3. Alice computes: cโ‚‚โ‚‚ = tยทuโ‚‚ + eโ‚‚โ‚‚ + mโ‚‚ (different!)
  4. Ciphertext: Cโ‚‚ = (cโ‚โ‚‚, cโ‚‚โ‚‚) (different!)
  5. Secret: Kโ‚‚ (different!)

Answer: C changes Alice encapsulates! Each time produces different (C, K) but decapsulation still recovers K.


๐Ÿ”ข The Mathโ€‹

Encapsulation Algorithmโ€‹

Encapsulate(pk = (A, t)):
1. Generate random values
u โ† Binomial(ฮทโ‚‚)^k (message vector)
eโ‚, eโ‚‚ โ† Binomial(ฮทโ‚‚)^k (error vectors)
m โ† H(encode(u)) (message from u)

2. Compute ciphertext parts
cโ‚ โ† Aแต€ยทu + eโ‚ (mod q)
cโ‚‚ โ† tแต€ยทu + eโ‚‚ + m (mod q)

3. Compress ciphertext
c โ† Compress'(cโ‚) || Compress''(cโ‚‚)

4. Derive shared secret
K โ† KDF(encode(u), encode(m), H(c))

Return (K, c)

Notation Breakdownโ€‹

SymbolMeaningDetails
AShared matrixBob's matrix (3ร—3)
tShared vectorBob's t = Aยทs + e
uAlice's vectorRandom (each call!)
eโ‚, eโ‚‚Error vectorsSmall polynomials
mMessageDerived from u
cโ‚, cโ‚‚CiphertextEncapsulated box
KShared secret32-byte symmetric key

Compression Levelsโ€‹

ML-KEM compresses ciphertext parts:

ComponentRaw SizeCompressionFinal Size
cโ‚3072 bits10 bits/coeff120 bytes
cโ‚‚3072 bits4 bits/coeff48 bytes
Total C6144 bits-1088 bytes

Trade-off: More bits = smaller C, smaller bits = decryption failures


๐Ÿ’ก Why We Careโ€‹

Encapsulation is Random per Callโ€‹

Alice generates random u each time she encapsulates:

Result: Even if she encapsulates the same K repeatedly, C changes each time!

Why? Randomness from u makes each C unpredictable.

Benefit: Attacker learning from one encapsulation doesn't help with subsequent ones.

Key Indistinguishabilityโ€‹

Eve seeing: C = Encapsulate(pk)

Eve can't tell: Which K (of 2^256 possibilities) is in the box!

Even if Eve: Computes many encapsulations, learns nothing about future ones.

Size vs. Speed Trade-offโ€‹

Ciphertext C: 1,088 bytes (fixed size)

Compared to alternatives:

  • RSA: 2,048-4,096 bytes (larger!)
  • ECC: 64 bytes (smaller!)

ML-KEM middle ground: Small enough (1 KB), fast enough (10-25 ms), quantum-resistant!


โœ… Quick Checkโ€‹

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

Try saying this out loud:

"Alice wants to send Bob a secret code. She puts the code in a magic lockbox by following instructions on Bob's map, then adds fog. She sends the locked box. Bob can open it with his secret key and get the same code. Eve sees the box but can't open it!"

Can you trace encapsulation?

Try this examples:

Alice encapsulates:

  1. Alice gets: pk = (A, t) from Bob
  2. Alice generates: u (random vector)
  3. Alice samples: eโ‚, eโ‚‚ (small polynomials)
  4. Alice computes: m = H(u)
  5. Alice computes: cโ‚ = Aแต€ยทu + eโ‚, cโ‚‚ = tยทu + eโ‚‚ + m
  6. Alice derives: K = KDF(u, m, cโ‚, cโ‚‚)
  7. Alice sends: C = (cโ‚, cโ‚‚)

Bob later decapsulates: Gets same K.

Answer: Alice encapsulates K โ†’ C, sends C, Bob opens C โ†’ K.


๐Ÿ“‹ Key Takeawaysโ€‹

โœ… Encapsulation steps: Generate u, eโ‚, eโ‚‚, compute cโ‚, cโ‚‚, derive K โœ… Ciphertext: C = (cโ‚, cโ‚‚) compressed to 1,088 bytes โœ… Random per call: Different u each time = different C โœ… Key indistinguishable: Eve can't tell which K is in C โœ… Ciphertext size: 1,088 bytes (RSA larger, ECC smaller, ML-KEM middle ground) โœ… Bob's advantage: Has sk = s, can compute needed values โœ… Shared secret: K from Alice, K' from Bob, equals same!


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

Now you understand encapsulation! Next, we'll learn about:

๐Ÿ”“ Opening Boxes โ†’ Decapsulation

How Bob uses his secret key to open the box and recover the same secret!