Documentation

CslibFrontier.Algorithms.LatticeReduction.LLL

The LLL algorithm #

The basis reduction algorithm of [LLL82], (1.15)–(1.21), on the rows of an integer matrix B : Vector (Vector ℤ m) n, with exact rational arithmetic. Its rows, viewed in EuclideanSpace ℝ (Fin m), are Cslib.Algorithms.LatticeReduction.realRows B.

Each round of Cslib.Algorithms.LatticeReduction.lllLoop δ at stage 0 < k < n computes the Gram–Schmidt rows (Cslib.Algorithms.LatticeReduction.gramSchmidtRows), achieves |μₖ,ₖ₋₁| ≤ 1/2 by one rounding step (1.18), and then either swaps bₖ₋₁, bₖ and steps back when the exchange condition (1.5) fails (Case 1, (1.19)), or size-reduces the rest of row k (1.21) and advances (Case 2, (1.20)). The source indexes the basis from 1; stage k here is stage k + 1 of the source. Unlike (1.18) and (1.21), which reduce only when |μᵢⱼ| > 1/2, each size-reduction step subtracts ⌊μᵢⱼ⌉ bⱼ unconditionally; this is a no-op when |μᵢⱼ| < 1/2, and at μᵢⱼ = 1/2 it replaces μᵢⱼ by -1/2.

Cslib.Algorithms.LatticeReduction.lll runs the loop with δ = 3/4 for n + 2 (3 (⌊log₂ B⌋ + 1) · n(n-1)/2 + 1) rounds, where B = ∑ᵢ ‖bᵢ‖². The proof that this is enough is the potential argument of (1.23)–(1.26): every swap divides D = ∏ dᵢ by more than 4/3, every other round advances k, and 1 ≤ D ≤ B^{n(n-1)/2}.

The source notes after (1.5) that 3/4 "may be replaced by any fixed real number y with 1/4 < y < 1". Cslib.Algorithms.LatticeReduction.lllWith δ runs the same loop with parameter δ for n + 2 (⌈δ/(1-δ)⌉ (⌊log₂ B⌋ + 1) · n(n-1)/2 + 1) rounds: by Bernoulli's inequality (1/δ)^⌈δ/(1-δ)⌉ ≥ 2, so each swap still divides D by more than 1/δ. For δ = 3/4 this is the budget of lll (Cslib.Algorithms.LatticeReduction.lll_eq_lllWith).

Main definitions #

Main results #

The running time is not formalized: each round costs O(n² m) rational operations by construction, but there is no cost model here, and the bit-size bound of (1.26) is not proved.

References #

Integer bases #

def Cslib.Algorithms.LatticeReduction.rows {m n : ℕ} (B : Vector (Vector ℤ m) n) :
Fin n → Fin m → ℤ

The rows of an integer basis as a family of integer vectors.

@[simp]
theorem Cslib.Algorithms.LatticeReduction.rows_apply {m n : ℕ} (B : Vector (Vector ℤ m) n) (i : Fin n) (k : Fin m) :
rows B i k = B[i][k]

The coordinate embedding ℤᵐ → ℝᵐ.

noncomputable def Cslib.Algorithms.LatticeReduction.realRows {m n : ℕ} (B : Vector (Vector ℤ m) n) :

The rows of B as vectors of ℝᵐ.

theorem Cslib.Algorithms.LatticeReduction.realRows_apply {m n : ℕ} (B : Vector (Vector ℤ m) n) (i : Fin n) :
realRows B i = ratToEuclidean fun (k : Fin m) => ↑B[i][k]
theorem Cslib.Algorithms.LatticeReduction.inner_realRows {m n : ℕ} (B : Vector (Vector ℤ m) n) (i j : Fin n) :
inner ℝ (realRows B i) (realRows B j) = ↑(rows B i ⬝ᵥ rows B j)
theorem Cslib.Algorithms.LatticeReduction.mem_span_realRows_of_eq_sum {m n : ℕ} {B : Vector (Vector ℤ m) n} {v : Fin m → ℤ} {e : Fin n → ℤ} (h : v = ∑ i : Fin n, e i • rows B i) :

An integer combination of the rows of B, embedded in ℝᵐ, lies in the lattice spanned by realRows B.

The rows of B cast to ℚ.

The Gram–Schmidt rows of B, computed exactly in ℚ with O(n² m) operations.

G holds the Gram–Schmidt vectors of the rows of B.

def Cslib.Algorithms.LatticeReduction.gsCoeff {m n : ℕ} (G : Vector (Vector ℚ m) n) (B : Vector (Vector ℤ m) n) (i j : Fin n) :

μᵢⱼ = ⟨b*ⱼ, bᵢ⟩ / ‖b*ⱼ‖², computed from the Gram–Schmidt rows G.

Size reduction #

def Cslib.Algorithms.LatticeReduction.subRow {m n : ℕ} (B : Vector (Vector ℤ m) n) (i j : Fin n) (r : ℤ) :

Replace row i by bᵢ - r bⱼ.

theorem Cslib.Algorithms.LatticeReduction.rows_subRow {m n : ℕ} (B : Vector (Vector ℤ m) n) (i j : Fin n) (r : ℤ) :
rows (subRow B i j r) = Function.update (rows B) i (rows B i - r • rows B j)
def Cslib.Algorithms.LatticeReduction.reduceStep {m n : ℕ} (G : Vector (Vector ℚ m) n) (B : Vector (Vector ℤ m) n) (i j : Fin n) :

One size-reduction step bᵢ ← bᵢ - ⌊μᵢⱼ⌉ bⱼ ([LLL82], (1.18)), performed unconditionally (the source performs it only when |μᵢⱼ| > 1/2).

theorem Cslib.Algorithms.LatticeReduction.isSizeStep_reduceStep {m n : ℕ} (G : Vector (Vector ℚ m) n) (B : Vector (Vector ℤ m) n) {i j : Fin n} (hji : j < i) :
theorem Cslib.Algorithms.LatticeReduction.rows_reduceStep_of_ne {m n : ℕ} (G : Vector (Vector ℚ m) n) (B : Vector (Vector ℤ m) n) {i j i' : Fin n} (h : i' ≠ i) :
rows (reduceStep G B i j) i' = rows B i'

After the step bᵢ ← bᵢ - ⌊μᵢⱼ⌉ bⱼ, |μᵢⱼ| ≤ 1/2.

The step bᵢ ← bᵢ - r bⱼ leaves μᵢₗ unchanged for l > j.

def Cslib.Algorithms.LatticeReduction.reduceRow {m n : ℕ} (G : Vector (Vector ℚ m) n) (i : Fin n) :
ℕ → Vector (Vector ℤ m) n → Vector (Vector ℤ m) n

Size-reduce row i against rows t - 1, …, 0, in this order, using the Gram–Schmidt rows G (which size reduction does not change).

theorem Cslib.Algorithms.LatticeReduction.reduceRow_spec {m n : ℕ} (G : Vector (Vector ℚ m) n) (i : Fin n) (t : ℕ) (B : Vector (Vector ℤ m) n) :
t ≤ ↑i → IsGramSchmidtRows G B → LinearIndependent ℝ (realRows B) → Relation.ReflTransGen IsSizeStep (realRows B) (realRows (reduceRow G i t B)) ∧ (∀ (i' : Fin n), i' ≠ i → rows (reduceRow G i t B) i' = rows B i') ∧ (∀ (l : Fin n), ↑l < t → |InnerProductSpace.gramSchmidtCoeff ℝ (realRows (reduceRow G i t B)) i l| ≤ 1 / 2) ∧ ∀ (l : Fin n), t ≤ ↑l → l < i → InnerProductSpace.gramSchmidtCoeff ℝ (realRows (reduceRow G i t B)) i l = InnerProductSpace.gramSchmidtCoeff ℝ (realRows B) i l

Exchange #

theorem Cslib.Algorithms.LatticeReduction.rows_swap {m n : ℕ} (B : Vector (Vector ℤ m) n) (i j : Fin n) :
rows (B.swap ↑i ↑j ⋯ ⋯) = rows B ∘ ⇑(Equiv.swap i j)
theorem Cslib.Algorithms.LatticeReduction.realRows_swap {m n : ℕ} (B : Vector (Vector ℤ m) n) (i j : Fin n) :
realRows (B.swap ↑i ↑j ⋯ ⋯) = realRows B ∘ ⇑(Equiv.swap i j)
def Cslib.Algorithms.LatticeReduction.exchangeCondQ {m n : ℕ} (δ : ℚ) (G : Vector (Vector ℚ m) n) (B : Vector (Vector ℤ m) n) (i j : Fin n) :

The exchange condition (1.5) at (i, j), computed from the Gram–Schmidt rows G.

Instances For
    @[instance_reducible]
    instance Cslib.Algorithms.LatticeReduction.instDecidableExchangeCondQ {m n : ℕ} (δ : ℚ) (G : Vector (Vector ℚ m) n) (B : Vector (Vector ℤ m) n) (i j : Fin n) :
    theorem Cslib.Algorithms.LatticeReduction.IsGramSchmidtRows.exchangeCondQ_iff {m n : ℕ} {G : Vector (Vector ℚ m) n} {B : Vector (Vector ℤ m) n} (hG : IsGramSchmidtRows G B) (δ : ℚ) {i j : Fin n} (hij : i < j) :
    exchangeCondQ δ G B i j ↔ ExchangeCondAt (↑δ) (realRows B) i j

    Swap rows i and j.

    The loop #

    The first k vectors are reduced: size reduced in rows < k and the exchange condition at consecutive pairs below k.

    theorem Cslib.Algorithms.LatticeReduction.PrefixReduced.isReduced {n : ℕ} {E : Type u_1} [NormedAddCommGroup E] [InnerProductSpace ℝ E] {δ : ℝ} {b : Fin n → E} {k : ℕ} (hb : PrefixReduced δ b k) (hk : n ≤ k) :
    theorem Cslib.Algorithms.LatticeReduction.PrefixReduced.mono {n : ℕ} {E : Type u_1} [NormedAddCommGroup E] [InnerProductSpace ℝ E] {δ : ℝ} {b : Fin n → E} {k k' : ℕ} (hb : PrefixReduced δ b k) (h : k' ≤ k) :
    theorem Cslib.Algorithms.LatticeReduction.PrefixReduced.congr {n : ℕ} {E : Type u_1} [NormedAddCommGroup E] [InnerProductSpace ℝ E] {δ : ℝ} {b c : Fin n → E} {k : ℕ} (hb : PrefixReduced δ b k) (h : ∀ (i : Fin n), ↑i < k → c i = b i) :
    theorem Cslib.Algorithms.LatticeReduction.PrefixReduced.succ {n : ℕ} {E : Type u_1} [NormedAddCommGroup E] [InnerProductSpace ℝ E] {δ : ℝ} {b : Fin n → E} {k : ℕ} (hb : PrefixReduced δ b k) (hk : k < n) (hsize : ∀ (l : Fin n), ↑l < k → |InnerProductSpace.gramSchmidtCoeff ℝ b ⟨k, hk⟩ l| ≤ 1 / 2) (hex : ∀ (h : 0 < k), ExchangeCondAt δ b ⟨k - 1, ⋯⟩ ⟨k, hk⟩) :
    PrefixReduced δ b (k + 1)

    The LLL loop ([LLL82], (1.15)–(1.21)) with parameter δ in place of 3/4, for at most fuel rounds, at stage k (stage k + 1 of the source). For 0 < k < n it computes the Gram–Schmidt rows, achieves |μₖ,ₖ₋₁| ≤ 1/2 (1.18), and then either swaps bₖ₋₁, bₖ and steps back (Case 1, when (1.5) fails at (k - 1, k)) or size-reduces the rest of row k (1.21) and advances (Case 2). Each round costs O(n² m) rational operations.

    theorem Cslib.Algorithms.LatticeReduction.lllLoop_spec {m n : ℕ} {δ : ℚ} (hδ0 : 0 < δ) (fuel : ℕ) (B : Vector (Vector ℤ m) n) (k s : ℕ) :
    LinearIndependent ℝ (realRows B) → PrefixReduced (↑δ) (realRows B) k → potential (realRows B) < (↑δ)⁻¹ ^ s → n - k + 2 * s ≤ fuel → IsReduced (↑δ) (realRows (lllLoop δ fuel B k)) ∧ LinearIndependent ℝ (realRows (lllLoop δ fuel B k))

    Invariant of the LLL loop: with 0 < δ, the potential below δ⁻¹ ^ s and enough fuel, the loop returns a δ-reduced linearly independent family. Since the potential of linearly independent integer rows is at least 1, the hypotheses can hold only for δ < 1.

    The algorithm #

    B = ∑ᵢ ‖bᵢ‖², a bound on the squared length of every input vector.

    The number of exchange steps allowed by the potential argument: 3 (⌊log₂ B⌋ + 1) · n(n-1)/2 bounds log_{4/3} B^{n(n-1)/2}.

    The number of exchange steps per doubling of the potential allowed for the parameter δ: ⌈δ / (1 - δ)⌉, so that δ⁻¹ ^ swapFactor δ ≥ 2 for 0 < δ < 1 (two_le_inv_pow_swapFactor). For δ = 3/4 it is 3.

    The number of exchange steps allowed by the potential argument for the parameter δ: ⌈δ / (1 - δ)⌉ (⌊log₂ B⌋ + 1) · n(n-1)/2 bounds log_{1/δ} B^{n(n-1)/2}.

    theorem Cslib.Algorithms.LatticeReduction.two_le_inv_pow_swapFactor {δ : ℚ} (hδ0 : 0 < δ) (hδ1 : δ < 1) :
    2 ≤ (↑δ)⁻¹ ^ swapFactor δ

    Bernoulli's inequality gives δ⁻¹ ^ ⌈δ / (1 - δ)⌉ ≥ 2 for 0 < δ < 1.

    theorem Cslib.Algorithms.LatticeReduction.potential_realRows_lt_inv_pow {m n : ℕ} {δ : ℚ} (hδ0 : 0 < δ) (hδ1 : δ < 1) (B : Vector (Vector ℤ m) n) :
    potential (realRows B) < (↑δ)⁻¹ ^ (swapBudgetOf δ n (sqNormSum B) + 1)

    The potential of an integer basis is below δ⁻¹ ^ (swapBudgetOf δ n B + 1), where B = ∑ᵢ ‖bᵢ‖², for 0 < δ < 1.

    The LLL algorithm ([LLL82], (1.15)–(1.21)) with parameter δ in place of 3/4 on the rows of an integer matrix, run for at most n + 2 (swapBudgetOf δ n B + 1) rounds. The source allows any fixed δ with 1/4 < δ < 1 (remark after (1.5)); termination and reducedness need only 0 < δ < 1 (isReduced_lllWith), and 1/4 < δ is used for the approximation bound (dotProduct_lllWith_zero_le).

    theorem Cslib.Algorithms.LatticeReduction.isReduced_lllWith {m n : ℕ} {δ : ℚ} (hδ0 : 0 < δ) (hδ1 : δ < 1) (B : Vector (Vector ℤ m) n) (hB : LinearIndependent ℝ (realRows B)) :

    Correctness of LLL with parameter 0 < δ < 1: on linearly independent rows, the output is δ-reduced and linearly independent.

    LLL with any parameter preserves the lattice.

    The LLL algorithm ([LLL82], (1.15)–(1.21)) with δ = 3/4 on the rows of an integer matrix, run for at most n + 2 (swapBudget n B + 1) rounds.

    Correctness of LLL: on linearly independent rows, the output is 3/4-reduced and linearly independent.

    theorem Cslib.Algorithms.LatticeReduction.dotProduct_lllWith_zero_le {m n : ℕ} {δ : ℚ} (hδ : 1 / 4 < δ) (hδ1 : δ < 1) (B : Vector (Vector ℤ m) (n + 1)) (hB : LinearIndependent ℝ (realRows B)) {x : Fin m → ℤ} (hx : x ∈ Submodule.span ℤ (Set.range (rows B))) (hx0 : x ≠ 0) :
    ↑(rows (lllWith δ B) 0 ⬝ᵥ rows (lllWith δ B) 0) ≤ (4 / (4 * δ - 1)) ^ n * ↑(x ⬝ᵥ x)

    Proposition (1.11) of [LLL82] with the constant of the remark after (1.9): for 1/4 < δ < 1 and n + 1 linearly independent integer rows, the first row of lllWith δ B satisfies |b₀|² ≤ (4/(4δ - 1))ⁿ |x|² for every nonzero x in the lattice. As δ → 1 the factor tends to (4/3)ⁿ.

    theorem Cslib.Algorithms.LatticeReduction.dotProduct_lll_zero_le {m n : ℕ} (B : Vector (Vector ℤ m) (n + 1)) (hB : LinearIndependent ℝ (realRows B)) {x : Fin m → ℤ} (hx : x ∈ Submodule.span ℤ (Set.range (rows B))) (hx0 : x ≠ 0) :
    rows (lll B) 0 ⬝ᵥ rows (lll B) 0 ≤ 2 ^ n * x ⬝ᵥ x

    For n + 1 linearly independent integer rows, the first output row satisfies |b₀|² ≤ 2ⁿ |x|² for every nonzero x in the lattice. This is (1.11) of [LLL82], |b₁|² ≤ 2ⁿ⁻¹ |x|² for n vectors in the source's indexing.

    A decidable criterion for the hypothesis of isReduced_lll: the integer Gram determinant is nonzero.