TExcelTAD
Home › Number Theory & Cryptography › Lattice Reduction

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.

python · try it yourself
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:

  1. Size reduction: subtract integer multiples of earlier vectors so that each Gram-Schmidt coefficient satisfies ∣μij∣≤12\lvert \mu_{ij} \rvert \le \frac{1}{2}.
  2. Lovász condition: if ∥bk∗∥2<(δ−μk,k−12)∥bk−1∗∥2\lVert \mathbf{b}_k^* \rVert^2 < \left(\delta - \mu_{k,k-1}^2\right) \lVert \mathbf{b}_{k-1}^* \rVert^2 (usually δ=3/4\delta = 3/4), swap bk\mathbf{b}_k and bk−1\mathbf{b}_{k-1} and step back.

The first vector of the result is at most 2(n−1)/22^{(n-1)/2} times longer than the shortest vector. That exponential factor sounds weak, but in practice LLL often finds the true shortest vector in small dimensions.

python · try it yourself
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 x≈1.4142135623730951x \approx 1.4142135623730951 (that is 2\sqrt{2}) and want integers a,b,ca, b, c, not all 0, with a+bx+cx2≈0a + bx + cx^2 \approx 0. Put the problem in a lattice where a short vector means a small relation, and let LLL find it:

python · try it yourself
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 x2−2=0x^2 - 2 = 0 (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's matrix.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 u=(1,0)u = (1, 0), v=(7,1)v = (7, 1).

Answer

(1,0)(1, 0) and (0,1)(0, 1).

Solution

m=round⁡(u⋅v/u⋅u)=7m = \operatorname{round}(u \cdot v / u \cdot u) = 7, so v−7u=(0,1)v - 7u = (0, 1), which is not shorter than uu, so we stop: the reduced basis is the standard basis of Z2\mathbb{Z}^2.

Exercise 2: What does δ\delta control?

What happens to LLL's output quality and running time as δ\delta approaches 1?

Answer

Better (shorter) bases, more swaps and longer running time.

Solution

A larger δ\delta makes the Lovász condition stricter, so more swaps are made and the basis becomes closer to optimal; at δ=1\delta = 1 polynomial running time is no longer guaranteed.

Exercise 3: A golden relation (code)

Use the relation-finding lattice to find a quadratic relation for φ=(1+5)/2≈1.618033988749895\varphi = (1 + \sqrt{5})/2 \approx 1.618033988749895.

Answer

φ2−φ−1=0\varphi^2 - \varphi - 1 = 0 (up to sign).

Solution

Replace x in the code above by 1.618033988749895; LLL returns relation: (-1, -1, 1) residual: 0.0, that is −1−x+x2=0-1 - x + x^2 = 0. A direct check of the relation:

python · try it yourself
x = 1.618033988749895
print(x * x - x - 1)
0.0

Test yourself

Lagrange-Gauss reduction in 2-D is analogous to…

  1. the sieve of Eratosthenes
  2. Euclid's algorithm
  3. fast exponentiation
  4. the CRT

The LLL algorithm runs in…

  1. exponential time
  2. polynomial time, with an approximation factor exponential in the dimension
  3. constant time
  4. time proportional to the determinant

Which attack uses lattice reduction?

  1. Pollard rho
  2. recovering ECDSA keys from slightly biased nonces
  3. birthday collisions
  4. padding oracles