Quadratic Residues
Which numbers are perfect squares modulo ? Exactly half of the nonzero residues, and Euler found a one-line test for which half. Squares modulo appear in elliptic-curve point decompression, in the Rabin cryptosystem, in hash-to-curve algorithms and in attacks on badly designed Diffie-Hellman.
Definition
Let be an odd prime and . Then is a quadratic residue (QR) modulo if has a solution, and a quadratic non-residue (QNR) otherwise.
p = 11
squares = {x: x * x % p for x in range(1, p)}
print(squares)
qr = sorted(set(squares.values()))
print("QRs mod 11:", qr, " QNRs:", [a for a in range(1, p) if a not in qr]){1: 1, 2: 4, 3: 9, 4: 5, 5: 3, 6: 3, 7: 5, 8: 9, 9: 4, 10: 1}
QRs mod 11: [1, 3, 4, 5, 9] QNRs: [2, 6, 7, 8, 10]
Exactly half are squares
The map sends and to the same value, and forces (in a field, ). So the nonzero residues pair up, and there are exactly QRs and QNRs.
With a primitive root , the picture is even clearer: is a square iff is even.
p, g = 23, 5
evens = sorted(pow(g, k, p) for k in range(0, p - 1, 2))
squares = sorted({x * x % p for x in range(1, p)})
print(evens == squares, squares)True [1, 2, 3, 4, 6, 8, 9, 12, 13, 16, 18]
Multiplication rules follow: QR QR = QR, QR QNR = QNR, QNR QNR = QR (even + even, even + odd, odd + odd exponents).
Euler's criterion
Proof. Write . Then , which is 1 iff , iff is even. Otherwise it is a square root of 1 other than 1, hence .
One modular exponentiation decides squareness, even for 256-bit primes, without finding the root.
def is_qr(a, p):
return pow(a, (p - 1) // 2, p) == 1
p = 2**255 - 19 # the Curve25519 prime
for a in (2, 3, 5, 7, 486662):
print(a, is_qr(a, p))2 False
3 True
5 True
7 False
486662 False
Is a square?
Euler's criterion gives , so is a QR iff . For example , while modulo 7 no square equals 6. This fact powers Fermat's theorem that primes are sums of two squares.
for p in (5, 7, 13, 19, 29, 31):
roots = [x for x in range(p) if x * x % p == p - 1]
print(p, p % 4, roots, [(a, b) for a in range(1, 6) for b in range(a, 6) if a * a + b * b == p])5 1 [2, 3] [(1, 2)]
7 3 [] []
13 1 [5, 8] [(2, 3)]
19 3 [] []
29 1 [12, 17] [(2, 5)]
31 3 [] []
In software
- Elliptic-curve points are often sent compressed as plus one bit; the receiver computes and must take a modular square root. Euler's criterion first checks that a root exists, which also rejects invalid points.
- Hash-to-curve (RFC 9380) maps arbitrary strings to curve points with square-root and QR tests in constant time.
Exercises
Exercise 1: List them
List the quadratic residues modulo 13.
Answer
1, 3, 4, 9, 10, 12.
Solution
Squares of (the others repeat them): . Six residues, as expected since .
Exercise 2: Euler's criterion by hand
Is 3 a quadratic residue modulo 7?
Answer
No.
Solution
. The squares modulo 7 are 1, 2 and 4.
Exercise 3: Products (code)
For , confirm in code that the product of two non-residues is always a residue.
Answer
True for all 225 pairs.
Solution
p = 31
qnr = [a for a in range(1, p) if pow(a, (p - 1) // 2, p) == p - 1]
pairs = [(a, b) for a in qnr for b in qnr]
print(len(qnr), len(pairs), all(pow(a * b, (p - 1) // 2, p) == 1 for a, b in pairs))15 225 True
There are 15 non-residues, so ordered pairs.
Test yourself
How many quadratic residues are there among for an odd prime ?
- 1
Euler's criterion: is a QR modulo iff…
is a square modulo exactly when…
- only
- never