Documentation

CslibFrontier.Computability.Machines.Turing.MultiTape.Complexity.PolyTime.Nat.Gcd

Polynomial-time greatest common divisor and modular inverse on binary naturals #

Euclid's algorithm on the binary encoding Turing.MultiTapeTM.encNat. Each step Turing.MultiTapeTM.BinaryGcd.gcdStep replaces (a, b) by (b, a % b) when b ≠ 0 and fixes (a, 0). After the first step the state satisfies b ≤ a, and every subsequent step with 0 < b at least halves the product a * b (BinaryGcd.gcd_product_halving). Because a * b < 2 ^ (a.size + b.size), iterating gcdStep for BinaryGcd.gcdCount (a, b) = a.bits.length + b.bits.length + 1 steps reaches (Nat.gcd a b, 0) (BinaryGcd.iterate_gcdStep_eq). At every intermediate step both components are at most max a b, so the encoded pair length never exceeds a linear bound in the input length.

Tracking the unsigned Bézout coefficients modulo m alongside (r₀, r₁) yields the polynomial-time modular inverse Turing.MultiTapeTM.BinaryGcd.modInv: each step BinaryGcd.modInvStep m updates (t₀, t₁) in {0, …, m - 1} so that a * t₀ ≡ r₀ [MOD m] and a * t₁ ≡ r₁ [MOD m]. After gcdCount (m, a) steps, a * modInv a m ≡ Nat.gcd m a [MOD m], giving a * modInv a m ≡ 1 [MOD m] whenever Nat.Coprime a m.

Main definitions #

Main results #

References #

theorem Turing.MultiTapeTM.BinaryGcd.gcd_product_halving {a b : ℕ} (ha : 0 < a) (hab : a ≤ b) :
2 * (a * (b % a)) ≤ a * b

Euclid's product is halved at every ordered step.

One step of Euclid's algorithm: replace (a, b) by (b, a % b) while b ≠ 0.

Number of Euclidean steps sufficient to reduce (p.1, p.2) to (Nat.gcd p.1 p.2, 0).

theorem Turing.MultiTapeTM.BinaryGcd.mul_gcdStep_two_le (s : ℕ × ℕ) (hs : s.2 ≤ s.1) :
2 * ((gcdStep s).1 * (gcdStep s).2) ≤ s.1 * s.2
theorem Turing.MultiTapeTM.BinaryGcd.iterate_gcdStep_product_le (s : ℕ × ℕ) (hs : s.2 ≤ s.1) (k : ℕ) :
(gcdStep^[k] s).2 ≤ (gcdStep^[k] s).1 ∧ 2 ^ k * ((gcdStep^[k] s).1 * (gcdStep^[k] s).2) ≤ s.1 * s.2

Iterating gcdStep for gcdCount p steps reaches (Nat.gcd p.1 p.2, 0).

At every Euclidean step, the encoded pair has length at most 3 * (encPair encNat encNat p).length.

Initial state ((m, a), (0, if m ≤ 1 then 0 else 1)) for the unsigned extended-Euclidean modular-inverse loop.

Next unsigned Bézout coefficient (t₀ + m - (q * t₁) % m) % m modulo m.

@[reducible, inline]

First remainder s.1.1 of the extended-Euclidean state ((r₀, r₁), (t₀, t₁)).

@[reducible, inline]

Second remainder s.1.2 of the extended-Euclidean state ((r₀, r₁), (t₀, t₁)).

@[reducible, inline]

First Bézout coefficient s.2.1 of the extended-Euclidean state ((r₀, r₁), (t₀, t₁)).

@[reducible, inline]

Second Bézout coefficient s.2.2 of the extended-Euclidean state ((r₀, r₁), (t₀, t₁)).

One step of the unsigned extended-Euclidean loop modulo m: advance the remainder pair (r₀, r₁) by gcdStep and update (t₀, t₁) to (t₁, modInvNext m (r₀ / r₁) t₀ t₁) while r₁ ≠ 0.

@[irreducible]

Unsigned modular inverse of a modulo m, computed by the extended Euclidean algorithm. When 0 < m, a * modInv a m ≡ Nat.gcd m a [MOD m] and modInv a m < m; when Nat.Coprime a m, a * modInv a m ≡ 1 [MOD m].

theorem Turing.MultiTapeTM.BinaryGcd.modInv_lt {a m : ℕ} (hm : 0 < m) :
modInv a m < m

When 0 < m, modInv a m < m.

For every a and m, modInv a m ≤ m.

theorem Turing.MultiTapeTM.BinaryGcd.mul_modInv_modEq_gcd (a : ℕ) {m : ℕ} (hm : 0 < m) :
a * modInv a m ≡ m.gcd a [MOD m]

Extended Euclidean correctness modulo m: a * modInv a m ≡ Nat.gcd m a [MOD m].

theorem Turing.MultiTapeTM.BinaryGcd.mul_modInv_modEq_one {a m : ℕ} (hm : 0 < m) (hcop : a.Coprime m) :
a * modInv a m ≡ 1 [MOD m]

Modular inverse correctness: when Nat.Coprime a m, a * modInv a m ≡ 1 [MOD m].

theorem Turing.MultiTapeTM.BinaryGcd.modInv_eq_zmod_inv_val {a m : ℕ} (hm : 0 < m) (hcop : a.Coprime m) :
modInv a m = (↑a)⁻¹.val

When 0 < m and Nat.Coprime a m, modInv a m equals the canonical natural-number representative ((a : ZMod m)⁻¹).val of the modular inverse in ZMod m.

Iterate the binary Euclidean step a supplied unary number of times.

Greatest common divisor of binary naturals via Euclid's algorithm.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_gcd' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encNat) (hright : ComputableInPolyTime right input encNat) :
ComputableInPolyTime (fun (a : α) => (left a).gcd (right a)) input encNat

Greatest common divisor of computed binary values, fixing their intermediate encodings for proof search.

Unsigned modular inverse of binary naturals via the extended Euclidean algorithm.

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_modInv' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encNat) (hright : ComputableInPolyTime right input encNat) :
ComputableInPolyTime (fun (a : α) => BinaryGcd.modInv (left a) (right a)) input encNat

Unsigned modular inverse of computed binary values, fixing their intermediate encodings for proof search.