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 #
Turing.MultiTapeTM.BinaryGcd.gcdStep: one Euclidean remainder step onℕ × ℕ.Turing.MultiTapeTM.BinaryGcd.gcdCount: the fuela.bits.length + b.bits.length + 1sufficient to reach a zero remainder.Turing.MultiTapeTM.BinaryGcd.modInv: unsigned extended-Euclidean modular inverse modulom.
Main results #
Turing.MultiTapeTM.BinaryGcd.gcd_product_halving: when0 < a ≤ b,2 * (a * (b % a)) ≤ a * b.Turing.MultiTapeTM.BinaryGcd.iterate_gcdStep_eq: iteratinggcdStepforgcdCount psteps produces(Nat.gcd p.1 p.2, 0).Turing.MultiTapeTM.BinaryGcd.mul_modInv_modEq_gcd,Turing.MultiTapeTM.BinaryGcd.mul_modInv_modEq_one,Turing.MultiTapeTM.BinaryGcd.modInv_lt,Turing.MultiTapeTM.BinaryGcd.modInv_eq_zmod_inv_val: correctness, range bounds, andZModinverse bridge forBinaryGcd.modInv.Turing.MultiTapeTM.ComputableInPolyTime.binary_gcd,.binary_gcd',Turing.MultiTapeTM.ComputableInPolyTime.binary_modInv,.binary_modInv': polynomial-time certificates forNat.gcdandBinaryGcd.modInvonencNat.
References #
gcd_product_halvingis adapted from OpenAI's formalization [OAI26h] at revision adc7f1241b42e322a6451854ab7e4b4c146bf78a (file lean/OAI/Computability/RationalHitting/BinaryFactorial.lean), via the complexitylib port [Sch26d] at revision add0aca8ee0d71e68969f2cb45cf91554a50f787.
Next unsigned Bézout coefficient (t₀ + m - (q * t₁) % m) % m modulo m.
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].
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.
Unsigned modular inverse of computed binary values, fixing their intermediate encodings for proof search.