TExcelTAD
Home › Number Theory & Cryptography › Quadratic Residues

Quadratic Residues

Which numbers are perfect squares modulo pp? Exactly half of the nonzero residues, and Euler found a one-line test for which half. Squares modulo pp 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 pp be an odd prime and p∤ap \nmid a. Then aa is a quadratic residue (QR) modulo pp if x2≡a(modp)x^2 \equiv a \pmod{p} has a solution, and a quadratic non-residue (QNR) otherwise.

python · try it yourself
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 x↦x2x \mapsto x^2 sends xx and −x-x to the same value, and x2≡y2x^2 \equiv y^2 forces x≡±yx \equiv \pm y (in a field, (x−y)(x+y)=0(x - y)(x + y) = 0). So the p−1p - 1 nonzero residues pair up, and there are exactly p−12\frac{p-1}{2} QRs and p−12\frac{p-1}{2} QNRs.

With a primitive root gg, the picture is even clearer: gkg^k is a square iff kk is even.

python · try it yourself
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 ⋅\cdot QR = QR, QR ⋅\cdot QNR = QNR, QNR ⋅\cdot QNR = QR (even + even, even + odd, odd + odd exponents).

Euler's criterion

a(p−1)/2≡{−1(modp)if a is a QR−1(modp)if a is a QNRa^{(p-1)/2} \equiv \begin{cases} \phantom{-}1 \pmod{p} & \text{if } a \text{ is a QR} \\ -1 \pmod{p} & \text{if } a \text{ is a QNR} \end{cases}

Proof. Write a=gka = g^k. Then a(p−1)/2=gk(p−1)/2a^{(p-1)/2} = g^{k(p-1)/2}, which is 1 iff p−1∣k(p−1)/2p - 1 \mid k(p-1)/2, iff kk is even. Otherwise it is a square root of 1 other than 1, hence −1-1.

One modular exponentiation decides squareness, even for 256-bit primes, without finding the root.

python · try it yourself
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 −1-1 a square?

Euler's criterion gives (−1)(p−1)/2(-1)^{(p-1)/2}, so −1-1 is a QR iff p≡1(mod4)p \equiv 1 \pmod{4}. For example −1≡12=52(mod13)-1 \equiv 12 = 5^2 \pmod{13}, while modulo 7 no square equals 6. This fact powers Fermat's theorem that primes p≡1(mod4)p \equiv 1 \pmod 4 are sums of two squares.

python · try it yourself
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 xx plus one bit; the receiver computes y2=x3+ax+by^2 = x^3 + ax + b 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 1,…,61, \ldots, 6 (the others repeat them): 1,4,9,16≡3,25≡12,36≡101, 4, 9, 16 \equiv 3, 25 \equiv 12, 36 \equiv 10. Six residues, as expected since (13−1)/2=6(13 - 1)/2 = 6.

Exercise 2: Euler's criterion by hand

Is 3 a quadratic residue modulo 7?

Answer

No.

Solution

3(7−1)/2=33=27≡6≡−1(mod7)3^{(7-1)/2} = 3^3 = 27 \equiv 6 \equiv -1 \pmod 7. The squares modulo 7 are 1, 2 and 4.

Exercise 3: Products (code)

For p=31p = 31, confirm in code that the product of two non-residues is always a residue.

Answer

True for all 225 pairs.

Solution
python · try it yourself
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 152=22515^2 = 225 ordered pairs.

Test yourself

How many quadratic residues are there among 1,…,p−11, \ldots, p - 1 for an odd prime pp?

  1. p−1p - 1
  2. (p−1)/2(p - 1)/2
  3. p\sqrt{p}
  4. 1

Euler's criterion: aa is a QR modulo pp iff…

  1. ap−1≡1a^{p-1} \equiv 1
  2. a(p−1)/2≡1a^{(p-1)/2} \equiv 1
  3. a2≡1a^2 \equiv 1
  4. a<p/2a < p/2

−1-1 is a square modulo pp exactly when…

  1. p≡1(mod4)p \equiv 1 \pmod 4
  2. p≡3(mod4)p \equiv 3 \pmod 4
  3. p=2p = 2 only
  4. never