Path: blob/main/frontier/08-lattices-post-quantum/sage/08c-lll-algorithm.ipynb
483 views
The LLL Algorithm
Module 08c | Lattices and Post-Quantum
The most important algorithm in lattice theory finds short vectors in polynomial time, but not short enough to break modern cryptography.
Motivating Question: Given a terrible basis with long, nearly-parallel vectors, can we algorithmically find a "nice" basis with short, nearly-orthogonal vectors? And can we do it in polynomial time?
In notebook 08b we saw that finding the shortest vector in a lattice (SVP) is hard, believed to require exponential time in general. But what if we settle for an approximately short vector? In 1982, Arjen Lenstra, Hendrik Lenstra, and László Lovász discovered that a clever combination of Gram-Schmidt orthogonalization and a simple swap-and-reduce loop can transform any lattice basis into a "reduced" one with provably short vectors, all in polynomial time. Their algorithm, LLL, remains the single most important tool in computational lattice theory.
Objectives
By the end of this notebook you will be able to:
Compute the Gram-Schmidt orthogonalization (GSO) of a lattice basis by hand and in SageMath
State and check the two LLL conditions (size-reduction and the Lovász condition)
Trace the LLL algorithm step-by-step on a small example
Measure basis quality using orthogonality defect and Hermite factor
Explain why LLL is powerful enough to break early lattice schemes but not modern ones like Kyber
Prerequisites
Completion of The Shortest Vector Problem
Familiarity with lattices, bases, and the concept of SVP
Basic linear algebra: dot products, vector norms, projections
Bridge from 08b: We ended notebook 08b knowing that SVP is hard in the worst case. But "hard in the worst case" doesn't mean "impossible to approximate." LLL gives us an efficient algorithm that approximately solves SVP, and that approximation is good enough to break many cryptographic schemes, though not all.
1. Gram-Schmidt Orthogonalization: The Geometric Backbone
Before we can understand LLL, we need the Gram-Schmidt orthogonalization (GSO). Given a basis , GSO produces an orthogonal basis for the same vector space (but not the same lattice!) using the formulas:
The coefficients measure how much of we need to subtract from to make it orthogonal to all previous vectors.
Key insight: The GSO vectors are the "orthogonal components" of the basis. Their norms tell us how much "new" length each basis vector contributes in a direction orthogonal to all previous vectors.
Let's start with the simplest case: two vectors in .
GSO in 3D
Now let's see the pattern with three vectors. Each new vector gets projected onto all previous GSO vectors.
Important: The GSO vectors are not lattice vectors in general (they have rational coordinates). GSO tells us about the geometry of the basis, but we cannot simply use the GSO basis as our lattice basis. LLL works with the original integer basis while using the GSO vectors as a guide.
2. The LLL Conditions
A basis is LLL-reduced (with parameter , typically ) if two conditions hold:
Condition 1: Size-Reduced
This means each basis vector has been "cleaned up" — its projections onto earlier GSO vectors are at most half. If , we can size-reduce by replacing (where is rounding to the nearest integer).
Condition 2: Lovász Condition
This prevents the GSO norms from decreasing too quickly. If the condition fails for index , we swap and .
The standard choice gives a good balance between reduction quality and speed. Increasing toward 1 gives better reduction but slower convergence.
Checkpoint: Look at the output above. Which condition failed — size-reduction, Lovász, or both? What does the failure of each condition tell you geometrically about the basis?
3. Step-by-Step LLL on a 2D Example
Let's trace every single step of the LLL algorithm on a small example. This is the best way to build intuition.
Input basis:
This is a "bad" basis: the vectors are long and nearly parallel.
Checkpoint: In the step-by-step trace above, find the swap step. Before the swap, which vector was longer — or ? After the swap and re-reduction, are the vectors more nearly orthogonal? (Hint: compute the angle between them.)
4. SageMath's Built-in LLL
In practice, you will use M.LLL() on an integer matrix. Let's see the dramatic improvement it produces on larger, more badly-conditioned bases.
5. Quality Metrics: How Good Is Our Reduced Basis?
We need quantitative ways to measure how "nice" a basis is. Two key metrics:
Orthogonality Defect
By Hadamard's inequality, , with equality only when the basis is perfectly orthogonal. The closer to 1, the better.
Hermite Factor
This measures how short the first (shortest) vector is relative to the lattice determinant. LLL guarantees . Smaller is better.
Common mistake: "LLL finds the shortest vector." No! LLL finds an approximately shortest vector. The first vector of the LLL-reduced basis satisfies:
where is the true shortest vector length. This approximation factor grows exponentially with dimension. In 2D it's harmless (), but in 500D it's astronomically large (). LLL does not solve SVP — it solves an exponential approximation of SVP, and it does so in polynomial time.
6. LLL Guarantees and Approximation Factor
Let's make the guarantees concrete. The LLL algorithm (with ) guarantees:
Running time: Polynomial in and the bit-length of the input — specifically where is the dimension and bounds the entries.
Output quality: The first vector satisfies .
All vectors bounded: For each , where is the -th successive minimum.
Let's visualize how the approximation factor grows with dimension:
The approximation factor grows linearly on a log scale, meaning it's exponential in the dimension. This is the fundamental limitation of LLL:
In low dimensions (say ), LLL often finds vectors much shorter than the worst-case guarantee — sometimes close to the actual shortest vector.
In high dimensions (say ), the approximation factor becomes so large that LLL-reduced vectors can be far from optimal.
This gap is precisely what modern lattice-based cryptography exploits.
7. Applications: Breaking Things with LLL
LLL is an incredibly versatile tool. Here we demonstrate two classic applications.
Application 1: Breaking Low-Dimensional Knapsack Cryptography
The subset-sum (knapsack) problem was once proposed as a basis for public-key encryption (Merkle-Hellman, 1978). The idea: public key is a set of weights and a target sum . Finding the subset is the hard problem.
LLL can break this by embedding it as a lattice problem!
LLL found the secret binary message by reducing the lattice! The key idea: the solution vector is a short vector in the constructed lattice (its entries are just 0s and 1s), so LLL can find it.
This is essentially how Shamir broke the Merkle-Hellman knapsack cryptosystem in 1982 — the same year LLL was published.
Application 2: Finding Integer Relations
Given real numbers , an integer relation is a vector such that . LLL can find these! Classic application: recover minimal polynomials of algebraic numbers.
Crypto Foreshadowing: LLL breaks many early lattice-based schemes (knapsack crypto, certain NTRU parameters, low-exponent RSA attacks) but NOT modern ones like Kyber/ML-KEM. Why? Modern schemes are designed in dimensions with carefully chosen parameters so that the best known lattice algorithms (even ones better than LLL, like BKZ) cannot find short enough vectors in reasonable time. In notebook 08f, we'll see exactly how Kyber's parameter selection defeats lattice reduction.
8. Limitations of LLL
Let's see empirically how LLL's quality degrades with dimension.
Notice that:
The actual Hermite factor is well below the theoretical bound (LLL performs better in practice than worst-case theory suggests).
Nevertheless, the Hermite factor grows with dimension — LLL produces increasingly "loose" approximations as increases.
For cryptographic applications like Kyber (), even running LLL (or its more powerful variant BKZ) cannot find vectors short enough to break the scheme.
This is the core insight of modern lattice-based cryptography: lattice reduction algorithms exist and are powerful, but their approximation quality degrades fast enough that we can design secure schemes by choosing dimensions and parameters carefully.
Exercises
Exercise 1 (Worked)
Problem: Apply LLL to the basis . Trace each step by hand, then verify with SageMath. Compute the Hermite factor before and after.
Exercise 2 (Guided)
Problem: Generate a random integer matrix with entries in . Apply LLL. Then:
Compute the orthogonality defect before and after reduction.
Verify that all GSO coefficients in the reduced basis.
Verify the Lovász condition holds for all consecutive pairs.
Hints:
Use
random_matrix(ZZ, 4, 4, x=-50, y=51)to generate the matrix.Use the
check_lll_conditions()function defined earlier.Use the
basis_quality()function for orthogonality defect.
Exercise 3 (Independent)
Problem: Use LLL to find a small integer linear combination of the columns of:
that gives a short vector. Specifically:
Form the lattice generated by the columns of (i.e., ).
Apply LLL to find a reduced basis.
What is the shortest vector you find? What integer combination produces it?
Can you beat LLL by trying random combinations? How many random attempts does it take to find something as short?
Summary
In this notebook we explored the LLL algorithm. Key takeaways:
Gram-Schmidt orthogonalization provides the geometric backbone: it decomposes basis vectors into orthogonal components whose norms reveal the basis quality.
LLL reduction requires two conditions: size-reduction () and the Lovász condition ().
The algorithm alternates between size-reducing (cleaning up projections) and swapping (fixing Lovász violations), converging in polynomial time.
Quality metrics like orthogonality defect and Hermite factor quantify how "nice" a basis is.
LLL guarantees — polynomial time, but exponential approximation factor.
Applications include breaking knapsack crypto and finding integer relations — LLL is a universal tool for problems that can be cast as "find a short vector."
Limitations: The approximation factor degrades exponentially with dimension, which is exactly what makes modern lattice-based cryptography possible.
Looking ahead: In notebook 08d, we introduce the Learning With Errors (LWE) problem — the computational hardness assumption behind Kyber/ML-KEM. The key insight: LWE problems live in dimensions large enough that even the best lattice reduction algorithms (LLL and its successors like BKZ) cannot find short enough vectors to break them.
Next: Learning With Errors