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):
- fixes the state once
a = 0; - for even
n, returnsa = 0ifais even too (both symbols vanish), and otherwise replacesnbyn / 2andabya % (n / 2), sinceJ(a | 2) = 1for odda; - for odd
nand evena, halvesa, flipping the sign whenn % 8 ∈ {3, 5}(jacobiSym.even_odd); - for odd
nand odda, replaces(a, n)by(n % a, a), flipping the sign whena % 4 = n % 4 = 3(jacobiSym.quadratic_reciprocity_if).
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 #
Turing.MultiTapeTM.BinaryJacobi.State,.encState: state⟨num, mod, flip⟩of the Jacobi-symbol loop and its framed binary encoding.Turing.MultiTapeTM.BinaryJacobi.jacobiStep: one reduction step onState.Turing.MultiTapeTM.BinaryJacobi.jacobiInit,.jacobiCount,.jacobiReadout: the initial state⟨a % n, n, false⟩, the step count2 * Nat.size n, and the final readout.
Main results #
Turing.MultiTapeTM.BinaryJacobi.jacobiReadout_iterate_jacobiStep:2 * Nat.size nsteps computeJ(a | n)for everya n : ℕ, including evennandn = 0.Turing.MultiTapeTM.ComputableInPolyTime.binary_jacobiSym,.binary_jacobiSym': the Jacobi symbol of binary naturals in polynomial time, as a binary signed integer.Turing.MultiTapeTM.ComputableInPolyTime.intBin_jacobiSym: the same for a binary signed numerator, by first reducing it modulon.
References #
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).
The Jacobi symbol of computed binary naturals, fixing their intermediate encodings for proof search.