Lattice Reduction
Lattice reduction turns a bad basis into a good one: short, nearly orthogonal vectors. In two dimensions Lagrange and Gauss solved it exactly; in higher dimensions the LLL algorithm (Lenstra, Lenstra, Lovász, 1982) finds a basis that is good enough for an astonishing range of attacks, in polynomial time.
Two dimensions: Lagrange-Gauss reduction
The 2-D algorithm is Euclid's algorithm for vectors: repeatedly subtract the best integer multiple of the shorter vector from the longer one, and swap.
def dot(u, v):
return u[0] * v[0] + u[1] * v[1]
def gauss_reduce(u, v):
if dot(u, u) > dot(v, v):
u, v = v, u
while True:
m = round(dot(u, v) / dot(u, u))
v = (v[0] - m * u[0], v[1] - m * u[1])
if dot(v, v) >= dot(u, u):
return u, v
u, v = v, u
bad = ((13, 15), (8, 9)) # a bad basis of some lattice
u, v = gauss_reduce(*bad)
print("reduced:", u, v, " det before:", 13 * 9 - 15 * 8, " det after:", u[0] * v[1] - u[1] * v[0])reduced: (-1, 0) (0, -3) det before: -3 det after: 3
The reduced basis spans the same lattice (the determinant is unchanged up to sign) and its first vector is a shortest nonzero vector, exactly.
LLL in higher dimensions
LLL combines Gram-Schmidt orthogonalization with two operations:
- Size reduction: subtract integer multiples of earlier vectors so that each Gram-Schmidt coefficient satisfies .
- Lovász condition: if (usually ), swap and and step back.
The first vector of the result is at most times longer than the shortest vector. That exponential factor sounds weak, but in practice LLL often finds the true shortest vector in small dimensions.
from fractions import Fraction
def lll(B, delta=Fraction(3, 4)):
B = [list(map(Fraction, b)) for b in B]
n = len(B)
dot = lambda x, y: sum(a * b for a, b in zip(x, y))
def gram_schmidt():
Bs, mu = [], [[Fraction(0)] * n for _ in range(n)]
for i in range(n):
v = B[i][:]
for j in range(i):
mu[i][j] = dot(B[i], Bs[j]) / dot(Bs[j], Bs[j])
v = [a - mu[i][j] * b for a, b in zip(v, Bs[j])]
Bs.append(v)
return Bs, mu
Bs, mu = gram_schmidt()
k = 1
while k < n:
for j in range(k - 1, -1, -1): # size reduction
q = round(mu[k][j])
if q:
B[k] = [a - q * b for a, b in zip(B[k], B[j])]
Bs, mu = gram_schmidt()
if dot(Bs[k], Bs[k]) >= (delta - mu[k][k - 1] ** 2) * dot(Bs[k - 1], Bs[k - 1]):
k += 1
else: # Lovasz condition failed: swap
B[k], B[k - 1] = B[k - 1], B[k]
Bs, mu = gram_schmidt()
k = max(k - 1, 1)
return [[int(a) for a in b] for b in B]
bad = [[201, 37, 15], [1648, 297, 120], [1, 2, 3]]
good = lll(bad)
print("reduced basis:", good)
print("lengths before:", [round(sum(a * a for a in b) ** 0.5, 2) for b in bad])
print("lengths after: ", [round(sum(a * a for a in b) ** 0.5, 2) for b in good])reduced basis: [[1, 2, 3], [-7, 16, -9], [30, 11, -18]]
lengths before: [204.93, 1678.84, 3.74]
lengths after: [3.74, 19.65, 36.67]
(This direct version recomputes Gram-Schmidt after every change, which is slow but easy to follow. Real implementations such as fplll update incrementally and use floating point carefully.)
An application: finding hidden small relations
Suppose you know (that is ) and want integers , not all 0, with . Put the problem in a lattice where a short vector means a small relation, and let LLL find it:
from fractions import Fraction
def lll(B, delta=Fraction(3, 4)):
B = [list(map(Fraction, b)) for b in B]; n = len(B)
dot = lambda x, y: sum(a * b for a, b in zip(x, y))
def gs():
Bs, mu = [], [[Fraction(0)] * n for _ in range(n)]
for i in range(n):
v = B[i][:]
for j in range(i):
mu[i][j] = dot(B[i], Bs[j]) / dot(Bs[j], Bs[j])
v = [a - mu[i][j] * b for a, b in zip(v, Bs[j])]
Bs.append(v)
return Bs, mu
Bs, mu = gs(); k = 1
while k < n:
for j in range(k - 1, -1, -1):
q = round(mu[k][j])
if q:
B[k] = [a - q * b for a, b in zip(B[k], B[j])]; Bs, mu = gs()
if dot(Bs[k], Bs[k]) >= (delta - mu[k][k - 1] ** 2) * dot(Bs[k - 1], Bs[k - 1]):
k += 1
else:
B[k], B[k - 1] = B[k - 1], B[k]; Bs, mu = gs(); k = max(k - 1, 1)
return [[int(a) for a in b] for b in B]
x = 1.4142135623730951
W = 10**8 # weight on the "error" coordinate
basis = [[1, 0, 0, round(W)], [0, 1, 0, round(W * x)], [0, 0, 1, round(W * x * x)]]
a, b, c, err = lll(basis)[0]
print("relation:", (a, b, c), " residual:", a + b * x + c * x * x)relation: (-2, 0, 1) residual: 4.440892098500626e-16
LLL rediscovers (up to sign) from 16 decimal digits. The same idea, a short vector exposing hidden small numbers, breaks ECDSA when a few bits of each nonce leak, and breaks RSA when the private exponent is too small (Boneh-Durfee).
In software
fpylll/fplll, Sage'smatrix.LLL(), and NTL implement LLL and the stronger BKZ algorithm, which is what lattice-crypto parameter estimates are measured against.- Lattice reduction also solves knapsack cryptosystems (Merkle-Hellman, broken in 1982) and integer-relation problems in computer algebra.
Exercises
Exercise 1: Gauss reduction by hand
Reduce the basis , .
Answer
and .
Solution
, so , which is not shorter than , so we stop: the reduced basis is the standard basis of .
Exercise 2: What does control?
What happens to LLL's output quality and running time as approaches 1?
Answer
Better (shorter) bases, more swaps and longer running time.
Solution
A larger makes the Lovász condition stricter, so more swaps are made and the basis becomes closer to optimal; at polynomial running time is no longer guaranteed.
Exercise 3: A golden relation (code)
Use the relation-finding lattice to find a quadratic relation for .
Answer
(up to sign).
Solution
Replace x in the code above by 1.618033988749895; LLL returns relation: (-1, -1, 1) residual: 0.0, that is . A direct check of the relation:
x = 1.618033988749895
print(x * x - x - 1)0.0
Test yourself
Lagrange-Gauss reduction in 2-D is analogous to…
- the sieve of Eratosthenes
- Euclid's algorithm
- fast exponentiation
- the CRT
The LLL algorithm runs in…
- exponential time
- polynomial time, with an approximation factor exponential in the dimension
- constant time
- time proportional to the determinant
Which attack uses lattice reduction?
- Pollard rho
- recovering ECDSA keys from slightly biased nonces
- birthday collisions
- padding oracles