Square roots modulo a prime (Goldreich, Exercise 6.16, items 1–3) #
Goldreich, Exercise 6.16 (February 2007 r3.pdf, PDF p. 55 / printed
p. 257) builds a probabilistic polynomial-time algorithm that, given a prime P and a quadratic
residue s modulo P, finds a square root of s modulo P. Its first three items are the
deterministic part:
- if
P ≡ 3 (mod 4), thens ^ ((P + 1) / 4)is a square root ofs(ModularSquareRoot.pow_add_one_div_four_sq_modEq); - if
s ^ e * t ^ e' ≡ 1for an oddeand an evene', thens ^ ((e + 1) / 2) * t ^ (e' / 2)is a square root ofs(ModularSquareRoot.sq_eq_of_pow_mul_pow_eq_one); - given a quadratic non-residue
t, sucheande'are found deterministically in polynomial time (ModularSquareRoot.descentExponents_spec,ModularSquareRoot.computableInPolyTime_descentExponents).
Item 3 is the descent of the source's footnote 23. Start from e = P / 2 and e' = 0, so that
s ^ e * t ^ e' = 1 by Euler's criterion. While e is even, x = s ^ (e / 2) * t ^ (e' / 2)
squares to 1, so x = ±1; halve both exponents, and if x = -1 also add P / 2 to e',
using t ^ (P / 2) = -1. Throughout, 2 * e divides e', so e' is even once e is odd.
e reaches its odd part after at most log₂ P halvings; the loop runs Nat.size P rounds. When
P ≡ 3 (mod 4), P / 2 is already odd and the loop does nothing, so the same algorithm returns
the root of item 1 and needs no non-residue.
ModularSquareRoot.sqrtMod combines items 2 and 3 into a native polynomial-time algorithm
(ModularSquareRoot.computableInPolyTime_sqrtMod, ModularSquareRoot.sqrtMod_sq). Item 4 and the
randomized search for a non-residue are not formalized here: the source's item 4 needs the
correction recorded in the exercise inventory (row 6.16(4)).
Main definitions #
Cslib.Complexity.ModularSquareRoot.descentStep,descentExponents: footnote 23's descent.Cslib.Complexity.ModularSquareRoot.sqrtMod: the square root it produces.
Main results #
Cslib.Complexity.ModularSquareRoot.pow_add_one_div_four_sq_modEq: item 1.Cslib.Complexity.ModularSquareRoot.sq_eq_of_pow_mul_pow_eq_one: item 2.Cslib.Complexity.ModularSquareRoot.descentExponents_spec,Cslib.Complexity.ModularSquareRoot.computableInPolyTime_descentExponents: item 3.Cslib.Complexity.ModularSquareRoot.sqrtMod_sq,Cslib.Complexity.ModularSquareRoot.computableInPolyTime_sqrtMod: the deterministic square-root algorithm given a non-residue.
References #
- [O. Goldreich, Computational Complexity: A Conceptual Perspective, 2007 draft] [Gol07b]
Goldreich, Exercise 6.16, item 1. For a prime p ≡ 3 (mod 4) and a quadratic residue y
modulo p, y ^ ((p + 1) / 4) mod p is a square root of y modulo p.
Goldreich, Exercise 6.16, item 2. If s ^ e * t ^ e' = 1 with e odd and e' even, then
s ^ ((e + 1) / 2) * t ^ (e' / 2) is a square root of s.
The square root of s modulo P found with the non-residue t:
s ^ ((e + 1) / 2) * t ^ (e' / 2) mod P for the descent exponents (e, e').
Correctness #
Goldreich, Exercise 6.16, item 3. For a prime P, a non-zero quadratic residue s
and a quadratic non-residue t (t ^ (P / 2) = -1, Euler's criterion), the descent finds an odd
e and an even e' with s ^ e * t ^ e' = 1 modulo P. When P ≡ 3 (mod 4) no non-residue is
needed.
A square root modulo a prime from a non-residue. For a prime P, a quadratic residue s
modulo P (zero included) and a quadratic non-residue t, sqrtMod P s t is a square root of
s modulo P (Goldreich, Exercise 6.16, items 2 and 3). When P ≡ 3 (mod 4) no non-residue is
needed, and the result is s ^ ((P + 1) / 4) mod P of item 1.
Running time #
Item 3 runs in polynomial time: the descent exponents are computable in polynomial time, from binary naturals to a pair of binary naturals.
The square-root algorithm runs in polynomial time, on binary naturals.