Square Roots Modulo a Prime
Euler's criterion says whether has a square root modulo . To find it, there is a one-liner when and the Tonelli-Shanks algorithm in general. Modulo a composite , by contrast, square roots are exactly as hard as factoring, a fact behind the Rabin cryptosystem.
The easy case:
If is a QR and , then is a square root:
The exponent is an integer exactly because .
p = 10007 # 10007 = 3 (mod 4)
for a in (2, 3, 5):
x = pow(a, (p + 1) // 4, p)
print(a, "QR" if pow(a, (p - 1) // 2, p) == 1 else "QNR", x, x * x % p)2 QR 2641 2
3 QR 8530 3
5 QNR 794 10002
For the non-residue 5, the formula returns a root of instead; always check .
Tonelli-Shanks
For (like the P-224 prime, or , which is ), write with odd and take a non-residue . The algorithm starts with the guess , whose error has order a power of 2, and repeatedly multiplies by powers of to shrink that order to 1.
def tonelli_shanks(a, p):
a %= p
if a == 0:
return 0
if pow(a, (p - 1) // 2, p) != 1:
return None # no square root
q, s = p - 1, 0
while q % 2 == 0:
q, s = q // 2, s + 1
z = next(z for z in range(2, p) if pow(z, (p - 1) // 2, p) == p - 1)
m, c, t, x = s, pow(z, q, p), pow(a, q, p), pow(a, (q + 1) // 2, p)
while t != 1:
i, t2 = 0, t
while t2 != 1: # find the order 2^i of t
t2, i = t2 * t2 % p, i + 1
b = pow(c, 1 << (m - i - 1), p)
m, c, t, x = i, b * b % p, t * b * b % p, x * b % p
return x
for p, a in ((41, 5), (13, 10), (2**255 - 19, 4), (2**255 - 19, 5)):
r = tonelli_shanks(a, p)
print(p if p < 10**6 else "2^255-19", a, r if r is None or r < 10**6 else "big root", r is not None and r * r % p == a % p)41 5 28 True
13 10 7 True
2^255-19 4 big root True
2^255-19 5 big root True
Every root comes in a pair . The algorithm costs multiplications on top of one exponentiation, fast for all primes used in practice.
Composite moduli: as hard as factoring
Modulo , a QR has four square roots: combine and with the CRT. Knowing and you find them easily. Conversely, anyone who can compute square roots mod can factor : pick random , ask for a root of ; with probability 1/2, , and then is a factor.
from math import gcd
p, q = 10007, 10039 # both 3 mod 4
n = p * q
def sqrt_mod_n(a): # needs the factors
rp, rq = pow(a, (p + 1) // 4, p), pow(a, (q + 1) // 4, q)
roots = set()
for sp in (rp, p - rp):
for sq in (rq, q - rq):
roots.add((sp * q * pow(q, -1, p) + sq * p * pow(p, -1, q)) % n)
return sorted(roots)
x = 123456
roots = sqrt_mod_n(x * x % n)
print(roots, x in roots)
y = next(r for r in roots if r not in (x, n - x))
print("gcd(x - y, n) =", gcd(x - y, n))[123456, 23122805, 77337468, 100336817] True
gcd(x - y, n) = 10039
The Rabin cryptosystem
Rabin encryption is . Decryption with gives four candidates (so the message needs redundancy to pick the right one). Breaking it is provably as hard as factoring , a guarantee RSA lacks. It never became popular because of the four-root ambiguity and a chosen-ciphertext attack that is precisely the factoring argument above.
In software
- Point decompression for P-256 and secp256k1 uses , since those primes are . Ed25519 uses a variant of the formula.
sympy.sqrt_mod(a, p, all_roots=True)implements Tonelli-Shanks and the composite case.
Exercises
Exercise 1: Easy case
Find a square root of 2 modulo 7 using .
Answer
4 (and ).
Solution
, so . Check: . The other root is , and too.
Exercise 2: Four roots
How many square roots does 4 have modulo 77? List them.
Answer
Four: 2, 9, 68, 75.
Solution
Modulo 7 the roots are , modulo 11 they are . Combining gives , , and the mixed ones , , which is , and its negative . Check: .
Exercise 3: Factor with a root (code)
Someone tells you that and leave the same remainder modulo . Use this to factor .
Answer
, from .
Solution
, so without dividing either factor, and each gcd picks out one prime.
from math import gcd
n = 3233
print(100 * 100 % n, 1425 * 1425 % n, gcd(1425 - 100, n), gcd(1425 + 100, n))301 301 53 61
Test yourself
For and a QR , a square root of is…
How many square roots does a quadratic residue have modulo (distinct odd primes)?
- 1
- 2
- 4
Computing square roots modulo without the factors is…
- easy
- equivalent to factoring
- impossible even with the factors
- the same as computing gcds