Path: blob/main/frontier/08-lattices-post-quantum/sage/08f-kyber-overview.ipynb
483 views
Kyber / ML-KEM Overview
Module 08 | 08-lattices-post-quantum
Module-LWE, NIST FIPS 203, KEM flow, toy implementation
The Post-Quantum Moment
Motivating Question: Quantum computers will break RSA and elliptic curves. What replaces them?
The answer is not hypothetical. It is already standardized: NIST chose Kyber (ML-KEM) in August 2024 as FIPS 203. Chrome, Signal, and TLS 1.3 are already deploying it.
Shor's algorithm factors integers and computes discrete logarithms in polynomial time on a quantum computer. This breaks:
| Scheme | Hardness assumption | Broken by Shor? |
|---|---|---|
| RSA | Integer factorization | Yes |
| Diffie-Hellman | Discrete log in | Yes |
| ECDH / ECDSA | Discrete log on elliptic curves | Yes |
| ML-KEM (Kyber) | Module-LWE on lattices | No |
This notebook is the climax of Module 08. Everything you built in 08a through 08e—lattices, SVP, LLL, LWE, Ring-LWE—comes together here in the real-world scheme that is replacing ECDH as we speak.
Objectives
By the end of this notebook you will be able to:
Explain why Module-LWE offers a better efficiency/security tradeoff than plain LWE or Ring-LWE.
Describe the three phases of a KEM: KeyGen, Encapsulate, Decapsulate.
State the NIST parameter sets for ML-KEM-512, ML-KEM-768, and ML-KEM-1024.
Implement a toy Kyber-like KEM from scratch in SageMath.
Compare ML-KEM key/ciphertext sizes to classical schemes.
Prerequisites
Completion of Ring-LWE (the polynomial ring ).
Familiarity with LWE (08d): given , finding is hard.
Understanding of lattice hardness (08a-08c): SVP, LLL reduction.
Bridge from 08e: In Ring-LWE we moved from random matrices to a single polynomial . This gave us compact keys but concentrated trust in a single algebraic structure. Module-LWE is the sweet spot: it works with small matrices of polynomials, combining Ring-LWE's efficiency with LWE's flexibility.
1. From Ring-LWE to Module-LWE
Recall the progression of LWE variants:
| Variant | Public matrix | Key size | Security argument |
|---|---|---|---|
| LWE | Random | Strong (unstructured) | |
| Ring-LWE | Single | Relies on ring structure | |
| Module-LWE | (matrix of ring elements) | Tunable via |
Module-LWE works in the free module , where . The matrix is now a matrix whose entries are polynomials in . Increasing adds security without changing the underlying ring.
Why this matters: If a structural weakness is found in for a specific , we can increase to compensate—without redesigning the whole scheme. This modularity is exactly why NIST chose Module-LWE over Ring-LWE.
Checkpoint: Before continuing, make sure you understand the difference:
In LWE, is a matrix of integers mod .
In Ring-LWE, is a single polynomial in .
In Module-LWE, is a matrix of polynomials in .
Quick check: For Kyber-768 (, ), how many total coefficients are in the matrix ? Answer: . Compare this to plain LWE with the same security, which would need a matrix with entries.
2. What Is a KEM?
Misconception Alert: "Kyber is a public-key encryption scheme." Not quite! Kyber is a Key Encapsulation Mechanism (KEM). It does not encrypt arbitrary messages. Instead, it establishes a shared symmetric key between two parties.
A KEM has three algorithms:
KeyGen() (public key , secret key )
Encapsulate() (ciphertext , shared secret )
Decapsulate(, ) shared secret
The crucial property: the shared secret produced by Encapsulate and Decapsulate is the same value. Alice and Bob can then use as the key for AES-256 or ChaCha20.
Why KEM instead of PKE? KEMs are simpler to build securely. A KEM only needs to produce a random-looking key, not encrypt an arbitrary message. This makes the IND-CCA2 security proof cleaner via the Fujisaki-Okamoto transform.
3. Kyber KeyGen
KeyGen creates a Module-LWE instance:
Sample (public, random matrix of polynomials)
Sample (secret vector, small coefficients)
Sample (error vector, small coefficients)
Compute
Public key: Secret key:
This is exactly the LWE problem from 08d, but over the module instead of ! The hardness assumption is: given , it is computationally infeasible to recover .
Note: CBD stands for Centered Binomial Distribution. In Kyber, small coefficients are sampled from , which outputs integers in . For our toy version, we simply sample uniformly from .
Checkpoint: After KeyGen, what does a ciphertext need to contain?
Think about it: Bob wants to send Alice a shared secret. He has Alice's public key . He needs to create something that:
Encodes a message (or random seed) that Alice can recover.
Looks random to anyone without .
The answer: Bob creates his own Module-LWE instance using the same , and uses to "mix in" a message. The ciphertext will contain two parts: .
4. Kyber Encapsulation
To encapsulate (create a ciphertext and shared secret):
Choose a random message (in real Kyber, 32 random bytes)
Derive randomness from (we skip this in our toy version)
Sample (small random vector)
Sample and (small errors)
Compute:
(vector of polynomials)
(single polynomial)
Ciphertext:
Shared secret:
The term encodes the message bit into the "upper half" of . For our toy , this means . A message bit of 1 adds 9 to the coefficient; a bit of 0 adds nothing.
5. Kyber Decapsulation
Alice receives and uses her secret key to recover the message:
Compute
For each coefficient, round to the nearest value in to recover the message bit.
Why does this work? Let's expand:
Since , we have . Substituting:
The terms cancel! What remains is the encoded message plus a small noise term. Since all of have small coefficients, their products and sums are still small relative to . So we can round to recover exactly.
Misconception Callout: "Post-quantum cryptography is theoretical and not deployed anywhere yet."
Wrong. ML-KEM (Kyber) is NIST FIPS 203, standardized August 2024. It is already deployed in:
Google Chrome (hybrid X25519 + ML-KEM-768 since 2024)
Signal Protocol (PQXDH: X25519 + ML-KEM-1024 since September 2023)
TLS 1.3 (RFC 9180 hybrid key exchange)
Apple iMessage (PQ3 protocol since March 2024)
You are studying a scheme that is protecting billions of messages right now.
6. NIST Parameter Sets
Kyber (ML-KEM) defines three security levels by varying the module rank . All share and .
7. Complete Toy Kyber: End-to-End
Let's run the full KEM flow with slightly larger toy parameters to see everything work together. We will use , , (still tiny compared to real Kyber, but large enough to be interesting).
8. Why Decryption Works: Noise Analysis
The correctness of Kyber depends on the accumulated noise being small enough to not flip any message bits during decoding. Let's visualize this.
Checkpoint: The noise grows as a product of small polynomials. In real Kyber:
Coefficients of are in with .
After multiplication and accumulation, the noise per coefficient is bounded by roughly .
For Kyber-768: , , , so worst-case noise .
The threshold is .
Wait, ?! The worst case exceeds the threshold, but the probabilistic bound (using the fact that CBD noise is concentrated near 0) gives a failure probability of . This is why Kyber uses the Centered Binomial Distribution, not uniform sampling.
9. Performance Comparison
Post-quantum schemes must be practical. How does ML-KEM compare to what it replaces?
Exercises
Exercise 1: Verify the Noise Cancellation (Fully Worked)
Manually expand for our toy parameters and verify that the terms cancel.
Exercise 2: What Happens When Noise Is Too Large? (Guided)
Increase the noise bound in the small polynomial sampling from to . Run KeyGen, Encapsulate, and Decapsulate. Does decryption still succeed? Why or why not?
Hints:
Modify the
boundparameter insmall_poly()from 1 to 4.With and large noise, the accumulated error may exceed .
Run multiple trials (at least 20) and count how many fail.
Exercise 3: Implement Kyber with (Independent)
Modify the toy Kyber implementation to use module rank (corresponding to ML-KEM-768 security level). Use the toy parameters , .
Generate a matrix of polynomials for .
Run KeyGen, Encapsulate, and Decapsulate.
Verify that decryption succeeds.
Compare the number of polynomial elements in the public key for vs .
No hints. You have all the building blocks from this notebook.
What Comes Next
This notebook gave you the overview of Kyber / ML-KEM. The journey continues:
Module 08 Break notebooks: We will attack toy Kyber with deliberately weakened parameters. What happens if is too small? If the noise distribution is wrong? If the polynomial ring has a bad structure? These "break" exercises teach you why each design choice matters.
Module 08 Connect notebooks: ML-KEM in the wild. How does TLS 1.3 hybrid key exchange work? What is ML-DSA (Dilithium), the lattice-based signature scheme that complements ML-KEM? How do Signal and iMessage combine classical and post-quantum primitives?
Rust implementation (Module 08 project): Implement ML-KEM from scratch in Rust, including NTT-based polynomial multiplication, CBD sampling, and compression/decompression of ciphertext.
You now understand the core idea: Module-LWE gives us a trapdoor. Publishing hides , but knowing lets you strip away the randomness and recover a message. This is the same encrypt/decrypt paradigm as RSA and ElGamal, but built on a problem that quantum computers cannot solve.
Summary
| Concept | Key idea |
|---|---|
| Module-LWE | The sweet spot between LWE (general but slow) and Ring-LWE (fast but inflexible). Kyber works over , a matrix of polynomials. |
| KEM flow | KeyGen produces . Encapsulate creates encoding a random message. Decapsulate uses to recover the message, and both parties derive the same shared secret . |
| Noise cancellation | Correctness relies on , where the terms cancel algebraically |
| NIST parameters | ML-KEM-512/768/1024 use with , . Key sizes are roughly 1 KB (vs 32 bytes for X25519), but performance is competitive. |
| Already deployed | ML-KEM (FIPS 203) protects Chrome, Signal, iMessage, and TLS 1.3 traffic today |
Connection to the full module: Notebooks 08a-08c built your lattice intuition (bases, SVP, LLL). Notebook 08d introduced LWE as a hard problem on lattices. Notebook 08e moved to Ring-LWE for efficiency. This notebook (08f) assembled all the pieces into the real-world scheme. The progression was: geometry (08a-08c) hardness (08d) efficiency (08e) deployment (08f).