Documentation

CslibFrontier.Foundations.Data.Rat.Reconstruction

Rational reconstruction, height bounds, and Chinese-remainder recovery #

Implements Wang's extended-Euclidean rational reconstruction Rat.reconstruct over ℤ / Mℤ, rational height bounds (Rat.height), uniqueness modulo M below the 2 * N * D < M threshold, completeness of Euclidean reconstruction under height/denominator bounds, and multi-modulus Chinese-remainder rational recovery (Rat.reconstructPi) across pairwise coprime moduli (such as finite-field primes ZMod pᵢ).

Main definitions #

Main results #

References #

def Rat.height (q : ℚ) :

Multiplicative height max |num| den of a rational number q.

@[simp]
theorem Rat.height_zero :
height 0 = 1
@[simp]
theorem Rat.height_one :
height 1 = 1
@[simp]
theorem Rat.height_neg (q : ℚ) :
theorem Rat.height_inv {q : ℚ} (hq : q ≠ 0) :
theorem Rat.height_divInt_le (a b : ℤ) (hb : b ≠ 0) :
theorem Rat.height_mul_le (p q : ℚ) :
theorem Rat.height_add_le (p q : ℚ) :
(p + q).height ≤ 2 * p.height * q.height
theorem Rat.eq_of_modEq_of_bounds {M N D : ℕ} {a₁ a₂ : ℤ} {b₁ b₂ : ℕ} (hb₁ : 0 < b₁) (hb₂ : 0 < b₂) (ha₁ : a₁.natAbs ≤ N) (ha₂ : a₂.natAbs ≤ N) (hbd₁ : b₁ ≤ D) (hbd₂ : b₂ ≤ D) (hM : 2 * N * D < M) (hmod : a₁ * ↑b₂ ≡ a₂ * ↑b₁ [ZMOD ↑M]) :
↑a₁ / ↑b₁ = ↑a₂ / ↑b₂

Uniqueness of modular rational recovery: if a₁ * b₂ ≡ a₂ * b₁ [ZMOD M] with |aᵢ| ≤ N, 0 < bᵢ ≤ D, and 2 * N * D < M, then (a₁ : ℚ) / b₁ = (a₂ : ℚ) / b₂.

theorem Rat.eq_of_modEq_of_height_mul_lt {M : ℕ} {p q : ℚ} (hM : 2 * p.height * q.height < M) (hmod : p.num * ↑q.den ≡ q.num * ↑p.den [ZMOD ↑M]) :
p = q

Modular uniqueness stated directly in terms of rational heights: if p, q : ℚ satisfy (p.num : ZMod M) * (q.den : ZMod M) = (q.num : ZMod M) * (p.den : ZMod M) and 2 * height p * height q < M, then p = q.

def Rat.reconstruct (M : ℕ) (u : ℤ) (N D : ℕ) :

Wang's extended-Euclidean rational reconstruction modulo M. Returns some q when the Euclidean sequence first reaches remainder r ≤ N with cofactor t satisfying |t| ≤ D and Nat.Coprime t.natAbs M.

theorem Rat.reconstruct_eq_some {M N D : ℕ} {u : ℤ} {q : ℚ} (hq : reconstruct M u N D = some q) :
0 < M ∧ q.num.natAbs ≤ N ∧ q.den ≤ D ∧ q.den.Coprime M ∧ q.num ≡ u * ↑q.den [ZMOD ↑M]

Soundness of Rat.reconstruct: every output q satisfies |q.num| ≤ N, q.den ≤ D, Nat.Coprime q.den M, and q.num ≡ u * q.den [ZMOD M].

theorem Rat.reconstruct_of_eq {M N D : ℕ} {u : ℤ} {q : ℚ} (hN : q.num.natAbs ≤ N) (hD : q.den ≤ D) (hM : 2 * N * D < M) (hcop : q.den.Coprime M) (hmod : q.num ≡ u * ↑q.den [ZMOD ↑M]) :
reconstruct M u N D = some q

Completeness of Rat.reconstruct: if q : ℚ satisfies |q.num| ≤ N, q.den ≤ D, 2 * N * D < M, Nat.Coprime q.den M, and (u : ZMod M) represents q, then reconstruct returns some q.

noncomputable def Rat.reconstructPi {k : ℕ} (m : Fin k → ℕ) (hcop : Pairwise (Function.onFun Nat.Coprime m)) (res : (i : Fin k) → ZMod (m i)) (N D : ℕ) :

Chinese-remainder rational reconstruction from residues in ∀ i, ZMod (m i) across pairwise coprime moduli m : Fin k → ℕ.

theorem Rat.reconstructPi_of_eq {k : ℕ} {m : Fin k → ℕ} (hcop : Pairwise (Function.onFun Nat.Coprime m)) {res : (i : Fin k) → ZMod (m i)} {N D : ℕ} {q : ℚ} (hN : q.num.natAbs ≤ N) (hD : q.den ≤ D) (hM : 2 * N * D < ∏ i : Fin k, m i) (hcop_den : ∀ (i : Fin k), q.den.Coprime (m i)) (hres : ∀ (i : Fin k), ↑q.num = res i * ↑q.den) :
reconstructPi m hcop res N D = some q

Completeness of Rat.reconstructPi: if ∏ i, m i > 2 * N * D, each m i is coprime to q.den, and res i equals (q.num : ZMod (m i)) * (q.den : ZMod (m i))⁻¹ for every i, then Rat.reconstructPi recovers some q.