Documentation

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

Polynomial-time modular exponentiation #

Square-and-multiply computes a ^ e % m on binary naturals with a positive modulus. It folds over the exponent's binary digits from the most significant one, squaring the accumulator and multiplying by a at each 1 digit, reducing modulo m after every product. Every intermediate value is below m, so the work is polynomial in the lengths of a, e and m.

Without a modulus, a ^ e has about e times as many digits as a, exponentially many in the length of e; the statement therefore requires a positive modulus. The total function powMod returns 1 at the modulus 0 instead, so it is polynomial-time on every input. This is where it differs from core's Nat.powMod, which returns the unreduced power at the modulus 0. Powers compose (Turing.MultiTapeTM.powMod_powMod), and powers of a unit are units (Turing.MultiTapeTM.coprime_powMod).

Main definitions #

Main results #

References #

theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_powMod {α : Type} {input : α ↪ List Bool} {base exponent modulus : α → ℕ} (hbase : ComputableInPolyTime base input encNat) (hexponent : ComputableInPolyTime exponent input encNat) (hmodulus : ComputableInPolyTime modulus input encNat) (hpos : ∀ (a : α), 0 < modulus a) :
ComputableInPolyTime (fun (a : α) => base a ^ exponent a % modulus a) input encNat

Modular exponentiation of computed binary values with a positive modulus, by square-and-multiply.

Total modular exponentiation #

Modular exponentiation x ^ e mod M. For the degenerate modulus M = 0 it returns 1 instead of the unreduced power, so that it is computable in polynomial time on every input.

theorem Turing.MultiTapeTM.powMod_of_ne_zero {M : ℕ} (hM : M ≠ 0) (x e : ℕ) :
powMod M x e = x ^ e % M
theorem Turing.MultiTapeTM.powMod_powMod (N g a b : ℕ) :
powMod N (powMod N g a) b = powMod N g (a * b)

Raising powMod N g a to the power b modulo N is powMod N g (a * b).

theorem Turing.MultiTapeTM.coprime_powMod {N g : ℕ} (hg : g.Coprime N) (e : ℕ) :
(powMod N g e).Coprime N

A power of a unit modulo N is a unit modulo N.

theorem Turing.MultiTapeTM.computableInPolyTime_powMod {α : Type} {input : α ↪ List Bool} {M x e : α → ℕ} (hM : ComputableInPolyTime M input encNat) (hx : ComputableInPolyTime x input encNat) (he : ComputableInPolyTime e input encNat) :
ComputableInPolyTime (fun (a : α) => powMod (M a) (x a) (e a)) input encNat

powMod is computable in polynomial time.