Path: blob/main/frontier/08-lattices-post-quantum/sage/08a-lattices-and-bases.ipynb
483 views
Lattices and Bases
Module 08a | Lattices and Post-Quantum Cryptography
The geometry of integer grids, and the surprising difficulty of finding short vectors.
Question: Imagine you're standing at the origin with two arrows (basis vectors). You can only move by adding or subtracting whole copies of these arrows. What points can you reach? And does it matter which arrows you start with?
By the end of this notebook, you'll see that the set of reachable points is a lattice, that the same lattice can arise from many different pairs of arrows, and that telling a "good" pair from a "bad" pair is the heart of post-quantum cryptography.
Objectives
By the end of this notebook you will be able to:
Define a lattice as the set of all integer linear combinations of basis vectors
Visualize 2D lattices and their basis vectors in SageMath
Demonstrate that different bases can generate the same lattice via unimodular matrices
Compute the fundamental domain and its volume (= |det(B)|)
Distinguish "good" (short, orthogonal) from "bad" (long, nearly parallel) bases
Prerequisites
Modules 01–06 (modular arithmetic, groups, rings, fields, number theory, elliptic curves)
Basic linear algebra: vectors, matrices, determinants, linear independence
A working SageMath installation (or CoCalc/SageMathCell)
Bridge from Modules 01–06: In those modules, cryptographic security came from number-theoretic problems — factoring (RSA), the discrete logarithm (Diffie-Hellman), and ECDLP (elliptic curves). Shor's algorithm can solve all of these on a quantum computer. We now turn to a fundamentally different source of hardness: the geometry of high-dimensional integer lattices. These problems are believed to resist even quantum attacks.
What Is a Lattice?
A lattice is the set of all integer linear combinations of a set of linearly independent vectors :
The matrix whose rows are is called a basis for the lattice. The lattice has rank and lives in ambient dimension .
The key word is integer. Unlike a vector space (where coefficients can be any real number), lattice points are a discrete, countably infinite set — a regular grid of isolated points.
Visualizing a 2D Lattice
Let's plot some lattice points. For each integer combination with , we get a lattice point.
Notice how the points form a regular pattern, but the grid is tilted and stretched compared to . Each basis vector is an arrow from the origin, and every lattice point is an integer combination of these arrows.
Checkpoint: Look at the plot above. Is the point in this lattice? Before reading on, try to find integers such that .
Common mistake: "A lattice is just a grid of evenly spaced points." Only if the basis is orthogonal! A general lattice has points at integer combinations of arbitrary linearly independent vectors. The spacing between points varies by direction — some directions are "dense," others are "sparse." This anisotropy is exactly what makes lattice problems hard.
Different Bases, Same Lattice
Here is the key insight that makes lattice cryptography work:
The same lattice can be described by many different bases. Some bases are "good" (short, nearly orthogonal vectors) and some are "bad" (long, nearly parallel vectors). Lattice cryptography hides secrets by publishing a "bad" basis while keeping a "good" one private.
When do two bases and generate the same lattice? Precisely when for some unimodular matrix — an integer matrix with .
Look carefully at the two plots. The dots are in exactly the same positions. Only the arrows (basis vectors) changed. The "good" basis has short, nearly perpendicular arrows that make the lattice structure obvious. The "bad" basis has long, nearly parallel arrows that obscure it.
Checkpoint: Can you verify that the bad basis generates the same points? Pick any lattice point from the first plot and express it using the bad basis vectors.
Unimodular Equivalence
Let's formalize this. Two bases and generate the same lattice if and only if there exists an integer matrix with (a unimodular matrix) such that:
Why ? Because:
must be an integer matrix (so has integer entries when does)
must be invertible over the integers (so we can go back: )
An integer matrix has an integer inverse iff
The set of all unimodular matrices forms a group called .
The Fundamental Domain
The fundamental domain (or fundamental parallelepiped) of a lattice basis is the set:
It's the parallelogram (in 2D) spanned by the basis vectors. A crucial fact:
The volume of the fundamental domain = , and this is the same for ALL bases of the same lattice.
This makes an invariant of the lattice itself, called the lattice determinant or covolume. Geometrically, it tells you the "density" of lattice points: larger determinant = sparser lattice.
Both parallelograms have the same area (), even though they look very different. The good-basis parallelogram is compact and fat; the bad-basis parallelogram is long and thin. But they tile the plane in the same way — one copy per lattice point, no gaps, no overlaps.
Crypto foreshadowing: The security of post-quantum schemes like Kyber (ML-KEM) and Dilithium (ML-DSA) reduces to lattice problems. An attacker is given a "bad" basis and must find short lattice vectors — essentially, recover a "good" basis. In 2D this is easy (the LLL algorithm, coming in notebook 08c). In 500+ dimensions, it is believed to be computationally infeasible even for quantum computers.
Measuring Basis Quality
How do we quantify whether a basis is "good" or "bad"? Two natural measures:
Vector lengths: and . Shorter is better.
Orthogonality: the angle between and . Closer to is better.
The Hadamard ratio combines both:
This ratio is always between 0 and 1. A value of 1 means the basis is perfectly orthogonal. Values close to 0 indicate a "bad" basis with long, nearly parallel vectors.
The good basis has shorter vectors, a wider angle, and a Hadamard ratio closer to 1. The bad basis has longer vectors, a narrower angle, and a Hadamard ratio closer to 0.
Checkpoint: If the Hadamard ratio is 1.0, what does the fundamental domain look like? (Answer: a rectangle — the basis vectors are perpendicular, and the parallelepiped degenerates to a rectangle.)
Beyond 2D: The Algebra Still Works
We can't visualize a 100-dimensional lattice, but the definitions are identical:
A basis is an matrix with linearly independent rows
The lattice
Two bases span the same lattice iff they differ by a unimodular matrix
gives the covolume (generalizing for square bases)
Cryptographic lattices typically have dimension or . The intuitions from 2D — good vs. bad bases, finding short vectors, fundamental domain volume — all carry over, but the computational difficulty explodes with dimension.
Exercises
Exercise 1 (Worked)
Given the basis :
Compute the determinant and the Hadamard ratio.
Apply the unimodular matrix to get .
Verify that both bases generate the same lattice by checking that the point has integer coordinates in both.
Exercise 2 (Guided)
Given the basis :
Plot the lattice and its fundamental domain.
Find a unimodular matrix such that has a higher Hadamard ratio than .
Plot the new lattice to verify the points are the same.
Hint: Try simple shear matrices like for small values of .
Exercise 3 (Independent)
Create your own 2D lattice with a "deliberately bad" basis:
Start with the standard basis and apply 3–4 different unimodular transforms (compose them: ) to create a basis with Hadamard ratio below 0.2.
Plot your bad basis alongside the original to verify they generate the same lattice.
Compute the basis quality metrics and explain geometrically why the Hadamard ratio is so low.
Reflection: If someone gave you only the bad basis, could you easily recover the original? This is essentially the problem an attacker faces in lattice-based cryptography.
Summary
| Concept | Key idea |
|---|---|
| Lattice | The set of all integer linear combinations of basis vectors: |
| Multiple bases | Two bases and generate the same lattice iff for a unimodular matrix () |
| Fundamental domain | Its volume $ |
| Good vs. bad bases | Good bases have short, nearly orthogonal vectors (high Hadamard ratio). Bad bases have long, nearly parallel vectors (low Hadamard ratio). Both generate the same points. |
| Crypto relevance | Lattice-based schemes publish a bad basis and keep a good one private. Finding a good basis from a bad one is computationally hard in high dimensions, and this hardness is believed to survive quantum computers. |
Crypto connection: The NIST post-quantum standards ML-KEM (Kyber) and ML-DSA (Dilithium) rely on the hardness of lattice problems. In the next notebooks, we'll formalize these problems (SVP, CVP) and see how the LLL algorithm provides a partial solution — enough to break small lattices but not the large ones used in practice.
Next: The Shortest Vector Problem — why finding the shortest nonzero lattice vector is hard, and how SVP/CVP connect to cryptographic security.