Documentation

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

Polynomial-time Jacobi symbol on binary naturals #

The Jacobi symbol J(a | n) (Mathlib's jacobiSym) is computable in polynomial time on binary inputs. This is the symbol the Solovay–Strassen primality test Solovay and Strassen compares with a ^ ((n - 1) / 2) modulo n.

The algorithm runs a loop on states (a, n, flip) with a < n, keeping J(a₀ | n₀) = (-1) ^ flip · J(a | n). One step (Turing.MultiTapeTM.BinaryJacobi.jacobiStep):

Every step at least halves the product a * n; the odd–odd case is Euclid's halving Turing.MultiTapeTM.BinaryGcd.gcd_product_halving. The initial product is below n₀ ^ 2, so 2 * Nat.size n₀ steps reach a = 0, where J(0 | n) is 1 for n = 1 and 0 otherwise (BinaryJacobi.jacobiReadout_iterate_jacobiStep). The state never exceeds max n₀ 1, so its encoding stays linear in the input length.

The reductions are those of Mathlib's private fastJacobiSym, which evaluates the symbol by well-founded recursion. Here they are a counted loop, so that the iteration rule for ComputableInPolyTime applies. The four loop definitions are exposed so that clients can run the loop by decide on concrete inputs.

Main definitions #

Main results #

References #

State of the Jacobi-symbol loop: current numerator num, modulus mod, and accumulated sign flip flip, representing (-1) ^ flip · J(num | mod) with num < mod.

  • num : ℕ

    Current numerator a, below mod.

  • mod : ℕ

    Current modulus n.

  • flip : Bool

    Whether the accumulated sign is -1.

Instances For
    theorem Turing.MultiTapeTM.BinaryJacobi.State.ext {x y : State} (num : x.num = y.num) (mod : x.mod = y.mod) (flip : x.flip = y.flip) :
    x = y
    theorem Turing.MultiTapeTM.BinaryJacobi.encState_mod_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → State} :
    ComputableInSpace g✝ encIn✝ encState S✝ → ComputableInSpace (fun (c : γ✝) => (g✝ c).mod) encIn✝ encNat S✝
    theorem Turing.MultiTapeTM.BinaryJacobi.encState_flip_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → State} :
    ComputableInSpace g✝ encIn✝ encState S✝ → ComputableInSpace (fun (c : γ✝) => (g✝ c).flip) encIn✝ encBool S✝
    theorem Turing.MultiTapeTM.BinaryJacobi.encState_mk_polyTime {γ : Type} {encIn : γ ↪ List Bool} {x0 x1 : γ → ℕ} {x2 : γ → Bool} (h0 : ComputableInPolyTime x0 encIn encNat) (h1 : ComputableInPolyTime x1 encIn encNat) (h2 : ComputableInPolyTime x2 encIn encBool) :
    ComputableInPolyTime (fun (x : γ) => { num := x0 x, mod := x1 x, flip := x2 x }) encIn encState
    theorem Turing.MultiTapeTM.BinaryJacobi.encState_mk_budget {γ : Type} {encIn : γ ↪ List Bool} {budget x0 x1 : γ → ℕ} {x2 : γ → Bool} (h0 : ComputableInSpace x0 encIn encNat budget) (h1 : ComputableInSpace x1 encIn encNat budget) (h2 : ComputableInSpace x2 encIn encBool budget) :
    ComputableInSpace (fun (x : γ) => { num := x0 x, mod := x1 x, flip := x2 x }) encIn encState budget
    theorem Turing.MultiTapeTM.BinaryJacobi.encState_mod_polyTime {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {g✝ : γ✝ → State} :
    ComputableInPolyTime g✝ encIn✝ encState → ComputableInPolyTime (fun (c : γ✝) => (g✝ c).mod) encIn✝ encNat
    theorem Turing.MultiTapeTM.BinaryJacobi.encState_num_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → State} :
    ComputableInSpace g✝ encIn✝ encState S✝ → ComputableInSpace (fun (c : γ✝) => (g✝ c).num) encIn✝ encNat S✝
    theorem Turing.MultiTapeTM.BinaryJacobi.encState_flip_polyTime {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {g✝ : γ✝ → State} :
    ComputableInPolyTime g✝ encIn✝ encState → ComputableInPolyTime (fun (c : γ✝) => (g✝ c).flip) encIn✝ encBool
    @[reducible, inline]

    The tuple of the fields of encState.

    theorem Turing.MultiTapeTM.BinaryJacobi.encState_num_polyTime {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {g✝ : γ✝ → State} :
    ComputableInPolyTime g✝ encIn✝ encState → ComputableInPolyTime (fun (c : γ✝) => (g✝ c).num) encIn✝ encNat

    Binary numerator and modulus, each framed, followed by the sign-flip bit.

    theorem Turing.MultiTapeTM.BinaryJacobi.encState_mk_deg :
    ComputableInDegree (fun (p : ℕ × ℕ × Bool) => { num := p.1, mod := p.2.1, flip := p.2.2 }) encStateFields encState 1 1
    theorem Turing.MultiTapeTM.BinaryJacobi.encState_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 T0 S0 x1 T1 S1 : γ → ℕ} {x2 : γ → Bool} {T2 S2 : γ → ℕ} (h0 : ComputableInO x0 encIn encNat T0 S0) (h1 : ComputableInO x1 encIn encNat T1 S1) (h2 : ComputableInO x2 encIn encBool T2 S2) :
    ComputableInO (fun (x : γ) => { num := x0 x, mod := x1 x, flip := x2 x }) encIn encState (fun (x : γ) => T0 x + (T1 x + T2 x)) fun (x : γ) => S0 x + (S1 x + S2 x)

    One step of the Jacobi-symbol loop on a state s, which stands for (-1) ^ s.flip · J(s.num | s.mod) and satisfies s.num < s.mod.

    The initial state ⟨a % n, n, false⟩ for J(a | n), or ⟨0, 1, false⟩ when n = 0, since J(a | 0) = 1.

    The number of loop steps for J(a | n): 2 * Nat.size n.

    The value (-1) ^ s.flip · J(0 | s.mod) of a final state ⟨0, s.mod, s.flip⟩ with s.mod ≥ 1.

    Correctness and step bound. For all a n : ℕ, running jacobiStep for 2 * Nat.size n steps from jacobiInit (a, n) and reading out gives J(a | n).

    The Jacobi symbol in polynomial time. J(a | n) of binary naturals a, n, returned as a binary signed integer, for every a and n (even n and n = 0 included).

    theorem Turing.MultiTapeTM.ComputableInPolyTime.binary_jacobiSym' {α : Type} {input : α ↪ List Bool} {left right : α → ℕ} (hleft : ComputableInPolyTime left input encNat) (hright : ComputableInPolyTime right input encNat) :
    ComputableInPolyTime (fun (x : α) => jacobiSym (↑(left x)) (right x)) input encIntBin

    The Jacobi symbol of computed binary naturals, fixing their intermediate encodings for proof search.

    The Jacobi symbol J(a | n) of a binary signed integer a and a binary natural n, computed as J(a % n | n).