Path: blob/main/frontier/08-lattices-post-quantum/sage/08e-ring-lwe.ipynb
483 views
Ring-LWE: Structured Lattices for Efficient Cryptography
Module 08e | Lattices and Post-Quantum Cryptography
From random matrices to polynomial rings, shrinking keys from megabytes to kilobytes.
Objectives
By the end of this notebook you will be able to:
Explain why plain LWE has impractically large keys and how algebraic structure fixes this.
Work confidently in the polynomial quotient ring .
Generate and verify a Ring-LWE instance in SageMath.
Visualize the negacyclic matrix that underlies polynomial multiplication in .
Compare LWE, Ring-LWE, and Module-LWE in terms of key size and security assumptions.
Use the Number Theoretic Transform (NTT) for fast polynomial multiplication.
Prerequisites
08d. Learning With Errors: you know that LWE gives us where is a random matrix, is a secret vector, and is a small noise vector.
02c. Polynomial Rings and 02f. Quotient Rings: you have seen how to form quotient rings and do arithmetic in them.
Bridge from 08d: In the LWE notebook, we saw that the public matrix is completely random, every entry is independent. This gives strong security guarantees, but it also means the public key contains ring elements. For practical parameters (), that is over a million integers just for the matrix. Can we do better?
1. The Key-Size Problem in Plain LWE
Motivating Question: LWE keys are megabytes large. Can we add mathematical STRUCTURE to shrink them to kilobytes, without making the problem easy to solve?
Let us make the problem concrete. In standard LWE with security parameter , the public matrix has dimensions roughly (we often take samples). Each entry is an element of , requiring bits.
For NIST-level security (, ), the matrix alone requires:
That is enormous for a public key. Compare: RSA-2048 has a 256-byte key, and ECC uses about 32 bytes. We need a way to compress while preserving the hardness of the underlying problem.
A factor of 1024x reduction in key size. That is the power of structure. But the central question is: does this structure make the problem easier to break?
The answer, after over a decade of cryptanalysis, is: for carefully chosen rings, no known attacks exploit the structure. Let us now understand exactly what ring we use and why.
2. The Ring
Bridge from Module 02: In 02f. Quotient Rings, you learned that we can form a quotient ring by taking polynomials modulo . Now we apply that same construction with a specific choice: where is a power of 2.
Elements of are polynomials of degree at most with coefficients in :
Addition is coefficient-wise modulo . Multiplication is polynomial multiplication followed by reduction modulo (and coefficients modulo ).
The key reduction rule is: . This means any power wraps around with a sign flip: .
Why specifically? When is a power of 2, the polynomial is the -th cyclotomic polynomial , which is irreducible over . This means has no zero divisors when is chosen appropriately, the ring has good algebraic properties for both security and efficiency.
Checkpoint, do this by hand first!
Before running the next cell, compute in with , by hand.
Hint: Multiply normally to get . Since the degree is less than , no reduction modulo is needed. All coefficients are already less than , so the answer is .
3. Ring-LWE: The Core Construction
Now we can state Ring-LWE. It is beautifully simple, just LWE, but with polynomials instead of vectors and matrices:
| LWE | Ring-LWE | |
|---|---|---|
| Public randomness | Matrix | Polynomial |
| Secret | Vector | Polynomial |
| Error | Vector (small) | Polynomial (small coefficients) |
| Public output |
The Ring-LWE problem: given , find , or even just distinguish from a uniformly random polynomial.
"Small coefficients" for and means each coefficient is drawn from a narrow distribution centered at 0 (e.g., or a small discrete Gaussian).
4. The Negacyclic Matrix: Why Polynomial Multiplication IS Matrix Multiplication
Here is the deep connection: multiplying by a fixed polynomial in is equivalent to multiplying a vector of coefficients by a special negacyclic matrix.
If , then the map corresponds to multiplying the coefficient vector of by the matrix:
Each row is a negacyclic shift of the first row: shift right by one, and the element that wraps around gets negated.
This is why Ring-LWE is a special case of LWE: we are restricting the random matrix to this structured family. One polynomial ( coefficients) determines the entire matrix. That is the source of the -factor compression.
Misconception Callout: "Adding structure always makes crypto weaker."
This is a natural intuition, and it is not entirely wrong, structure does reduce the problem's generality, and some structured variants of lattice problems are easier. But Ring-LWE specifically has been studied intensively since Lyubashevsky, Peikert, and Regev introduced it in 2010. No known attacks on Ring-LWE exploit the ring structure when parameters are chosen correctly (i.e., is a power of 2, is chosen appropriately, and errors are large enough).
The security of Ring-LWE is supported by a worst-case to average-case reduction: solving a random Ring-LWE instance is at least as hard as solving the Shortest Vector Problem (SVP) on ideal lattices in the worst case. This is a weaker assumption than plain LWE's reduction to general lattices, but it is still considered strong enough for cryptographic use.
5. Module-LWE: The Middle Ground (and What Kyber Actually Uses)
Crypto Foreshadowing: Kyber (now standardized as ML-KEM in FIPS 203) does not use Ring-LWE directly. It uses Module-LWE, a clever middle ground.
The spectrum from less structure to more structure:
| Variant | Public matrix shape | Structure | Key size |
|---|---|---|---|
| LWE | None (fully random) | ||
| Module-LWE | matrix of ring elements | ||
| Ring-LWE | (single element) | Maximally structured |
Module-LWE uses a small matrix (e.g., or ) where each entry is a polynomial in . Kyber uses and for its three security levels.
Why not just use Ring-LWE? Module-LWE gives a more conservative security assumption (closer to plain LWE) while still being efficient. It also lets you adjust the security level by changing without changing the ring dimension .
6. The Number Theoretic Transform (NTT): Fast Multiplication
Naive polynomial multiplication in costs coefficient operations. The Number Theoretic Transform (NTT) brings this down to , exactly like the FFT, but over instead of .
The NTT requires a primitive -th root of unity in , i.e., an element such that and for . This exists when .
The NTT evaluates a polynomial at the powers of , turning multiplication into pointwise operations:
where denotes component-wise multiplication.
Exercises
Exercise 1: Build a Ring-LWE Instance (Fully Worked)
Task: Construct a Ring-LWE instance in . Use secret and error . Choose a random and compute . Verify by recovering from .
Solution:
Exercise 2: Negacyclic Matrix and Key Size (Guided)
Task: For the Ring-LWE instance from Exercise 1 (with , ):
Build the negacyclic matrix corresponding to .
Verify that where are coefficient vectors.
Compute the ratio of key sizes: plain LWE ( entries) vs Ring-LWE ( entries).
Hints:
Use the
negacyclic_matrix()function defined earlier.Extract coefficient vectors using
.lift()followed by indexing.The key size ratio should be exactly .
Exercise 3: NTT Multiplication (Independent)
Task: Work in .
Find a primitive 16th root of unity modulo 17 (use
find_primitive_root).Define and .
Compute in using:
(a) Direct ring multiplication in SageMath.
(b) NTT-based multiplication using
ntt_negacyclicandintt_negacyclic.
Verify both methods give the same result.
Bonus: What is conceptually? Note that and think about what happens when you multiply by modulo .
No hints for this one, you have all the tools you need from the notebook above.
Summary
| Concept | Key idea |
|---|---|
| Key-size bottleneck | Plain LWE requires elements for the public matrix, meaning megabytes of key material at cryptographic parameters |
| The ring | Working in (with a power of 2) replaces the random matrix with a single polynomial, compressing keys by a factor of |
| Negacyclic structure | Multiplying by in is equivalent to multiplying by a negacyclic matrix. One polynomial ( coefficients) determines the entire matrix. |
| Module-LWE | The middle ground used by Kyber/ML-KEM: a small matrix of ring elements, balancing efficiency with conservative security |
| NTT acceleration | The Number Theoretic Transform enables polynomial multiplication, making Ring-LWE-based schemes competitive with classical cryptography in speed |
Crypto Foreshadowing: The NIST standard ML-KEM (formerly Kyber, FIPS 203) uses Module-LWE over the ring with module ranks . In the next notebook, we will see how Ring-LWE becomes a complete key encapsulation mechanism.
Next: Kyber / ML-KEM Overview