Documentation

CslibFrontier.Computability.Complexity.Problems.ModularSquareRoot

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:

  1. if P ≡ 3 (mod 4), then s ^ ((P + 1) / 4) is a square root of s (ModularSquareRoot.pow_add_one_div_four_sq_modEq);
  2. if s ^ e * t ^ e' ≡ 1 for an odd e and an even e', then s ^ ((e + 1) / 2) * t ^ (e' / 2) is a square root of s (ModularSquareRoot.sq_eq_of_pow_mul_pow_eq_one);
  3. given a quadratic non-residue t, such e and e' 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 #

Main results #

References #

theorem Cslib.Complexity.ModularSquareRoot.pow_add_one_div_four_sq_modEq {p y : ℕ} (hp : Nat.Prime p) (hp3 : p % 4 = 3) (hsq : ∃ (x : ℕ), x ^ 2 ≡ y [MOD p]) :
(y ^ ((p + 1) / 4) % p) ^ 2 ≡ y [MOD p]

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.

theorem Cslib.Complexity.ModularSquareRoot.sq_eq_of_pow_mul_pow_eq_one {M : Type u_1} [CommMonoid M] {s t : M} {e e' : ℕ} (he : Odd e) (he' : Even e') (h : s ^ e * t ^ e' = 1) :
(s ^ ((e + 1) / 2) * t ^ (e' / 2)) ^ 2 = s

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.

One round of the descent of footnote 23 on the exponent pair (e, e'): if e is even, halve both exponents and, when s ^ (e / 2) * t ^ (e' / 2) is not 1 modulo P, add P / 2 to the second; if e is odd, stop.

The exponents of footnote 23: Nat.size P descent rounds from (P / 2, 0).

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 #

theorem Cslib.Complexity.ModularSquareRoot.descentExponents_spec {P s t : ℕ} (hP : Nat.Prime P) (hs0 : ↑s ≠ 0) (hs : IsSquare ↑s) (ht : P % 4 = 3 ∨ ↑t ^ (P / 2) = -1) :
Odd (descentExponents P s t).1 ∧ Even (descentExponents P s t).2 ∧ ↑s ^ (descentExponents P s t).1 * ↑t ^ (descentExponents P s t).2 = 1

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.

theorem Cslib.Complexity.ModularSquareRoot.sqrtMod_sq {P s t : ℕ} (hP : Nat.Prime P) (hs : IsSquare ↑s) (ht : P % 4 = 3 ∨ ↑t ^ (P / 2) = -1) :
↑(sqrtMod P s t) ^ 2 = ↑s

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.