Documentation

CslibFrontier.Algorithms.PolynomialArithmetic.FastHalfGCD

Knuth–Schönhage Half-GCD and Modular Polynomial Inversion over ZMod p #

Implements the divide-and-conquer Half-GCD recursion (hgcd), extended Euclidean algorithm (fastExtGCDCore), and modular polynomial inversion (fastInvMod) for univariate polynomials over prime fields ZMod p, proving exact algebraic correctness, unimodularity, degree reduction below d / 2, and matrix entry-degree invariants.

Scope and Provenance #

This module is an algebraic correctness milestone for the Half-GCD recursion following Knuth (1970), Schönhage (1971), and [vzGG13], Chapter 11 (fast Euclidean algorithm). Polynomial division, remainder, and multiplication use Mathlib's abstract (ZMod p)[X] operations without a machine-level Word-RAM or multi-tape running-time certificate; certifying the Õ(d * log p) bit complexity in a concrete machine model is left as future work.

Main definitions #

Main results #

2 × 2 Polynomial Matrices and Shift-Quotient Helpers #

A 2 × 2 matrix over F[X] tracking Euclidean transition steps in Half-GCD.

theorem Cslib.Algorithms.PolynomialArithmetic.PolyMat2.ext {F : Type u_1} {inst✝ : Semiring F} {x y : PolyMat2 F} (a : x.a = y.a) (b : x.b = y.b) (c : x.c = y.c) (d : x.d = y.d) :
x = y
theorem Cslib.Algorithms.PolynomialArithmetic.PolyMat2.ext_iff {F : Type u_1} {inst✝ : Semiring F} {x y : PolyMat2 F} :
x = y ↔ x.a = y.a ∧ x.b = y.b ∧ x.c = y.c ∧ x.d = y.d

Identity 2 × 2 polynomial matrix.

Product of two 2 × 2 polynomial matrices.

Single Euclidean step matrix [[0, 1], [1, -q]].

Determinant a * d - b * c.

Matrix-vector action on a pair of polynomials.

theorem Cslib.Algorithms.PolynomialArithmetic.PolyMat2.apply_mul {F : Type u_1} [CommRing F] (M N : PolyMat2 F) (u v : Polynomial F) :
(M.mul N).apply u v = M.apply (N.apply u v).1 (N.apply u v).2

Degree bound on all four entries of M: every entry has natDegree ≤ k.

theorem Cslib.Algorithms.PolynomialArithmetic.PolyMat2.entryDegLe_mono {F : Type u_1} [Semiring F] {M : PolyMat2 F} {k₁ k₂ : ℕ} (h : M.EntryDegLe k₁) (hle : k₁ ≤ k₂) :
M.EntryDegLe k₂
theorem Cslib.Algorithms.PolynomialArithmetic.PolyMat2.entryDegLe_mul {F : Type u_1} [Semiring F] {M N : PolyMat2 F} {k₁ k₂ : ℕ} (hM : M.EntryDegLe k₁) (hN : N.EntryDegLe k₂) :
(M.mul N).EntryDegLe (k₁ + k₂)
theorem Cslib.Algorithms.PolynomialArithmetic.PolyMat2.dvd_iff_of_det_eq_pm_one {F : Type u_1} [CommRing F] {M : PolyMat2 F} (hdet : M.det = 1 ∨ M.det = -1) (u v g : Polynomial F) :
g ∣ u ∧ g ∣ v ↔ g ∣ (M.apply u v).1 ∧ g ∣ (M.apply u v).2

Every common divisor of (u, v) divides (M.apply u v), and when det M = ±1, conversely.

Polynomial Truncation by X^m #

Knuth–Schönhage Half-GCD (hgcd) and Fast Extended GCD (fastExtGCD) #

@[irreducible]
noncomputable def Cslib.Algorithms.PolynomialArithmetic.hgcd {p : ℕ} [Fact (Nat.Prime p)] (r₀ r₁ : Polynomial (ZMod p)) :

Recursive Knuth–Schönhage Half-GCD over ZMod p (with prime p). Given r₀, r₁ with r₁.natDegree < r₀.natDegree (and r₀ ≠ 0), computes a unimodular transition matrix R reducing (r₀, r₁) to (b₀, b₁) with b₀.natDegree ≥ (r₀.natDegree + 1) / 2 and b₁ = 0 ∨ b₁.natDegree < (r₀.natDegree + 1) / 2.

theorem Cslib.Algorithms.PolynomialArithmetic.hgcd_spec {p : ℕ} [Fact (Nat.Prime p)] (r₀ r₁ : Polynomial (ZMod p)) :
r₀ ≠ 0 → r₁ = 0 ∨ r₁.natDegree < r₀.natDegree → have R := hgcd r₀ r₁; match R.apply r₀ r₁ with | (b₀, b₁) => have m := (r₀.natDegree + 1) / 2; (R.det = 1 ∨ R.det = -1) ∧ b₀ ≠ 0 ∧ m ≤ b₀.natDegree ∧ b₀.natDegree ≤ r₀.natDegree ∧ (b₁ = 0 ∨ b₁.natDegree < m) ∧ R.EntryDegLe (r₀.natDegree - b₀.natDegree)

Algebraic correctness and entry-degree bound for hgcd.

@[irreducible]

Extended Euclidean algorithm using hgcd, returning (g, u, v) with u * r₀ + v * r₁ = g, g ∣ r₀, and g ∣ r₁.

theorem Cslib.Algorithms.PolynomialArithmetic.fastExtGCDCore_spec {p : ℕ} [Fact (Nat.Prime p)] (r₀ r₁ : Polynomial (ZMod p)) :
r₀ ≠ 0 → r₁ = 0 ∨ r₁.natDegree < r₀.natDegree → match fastExtGCDCore r₀ r₁ with | (g, u, v) => u * r₀ + v * r₁ = g ∧ g ≠ 0 ∧ g ∣ r₀ ∧ g ∣ r₁ ∧ ∀ (d : Polynomial (ZMod p)), d ∣ r₀ → d ∣ r₁ → d ∣ g

Correctness specification of fastExtGCDCore for r₁.natDegree < r₀.natDegree.

Modular polynomial inverse in (ZMod p)[X] modulo M via fastExtGCDCore: returns some U with (U * G) % M = 1 % M when gcd(G, M) is a nonzero constant, and none otherwise.

theorem Cslib.Algorithms.PolynomialArithmetic.fastInvMod_spec {p : ℕ} [Fact (Nat.Prime p)] {G M : Polynomial (ZMod p)} (hM : M ≠ 0) :
have res := fastInvMod G M; (IsCoprime G M → ∃ (U : Polynomial (ZMod p)), res = some U ∧ U * G % M = 1 % M) ∧ ∀ (U : Polynomial (ZMod p)), res = some U → U * G % M = 1 % M

Specification of fastInvMod: whenever M ≠ 0 and IsCoprime G M, fastInvMod G M returns some U satisfying (U * G) % M = 1 % M, and any returned some U satisfies (U * G) % M = 1 % M.