Learn / Math / Quadratic Residues & Square Roots
20. Quadratic Residues & Square Roots
Quadratic Residues · Lesson 21 of 26 · 15:45
Why some numbers in a finite field have a square root and some don't, a one-exponentiation test for telling them apart, two ways to find the root itself, and how both get used to compress elliptic-curve points and to map arbitrary data onto a curve.

Last time added subgroups, cosets, and roots of unity. This lesson asks a question those tools didn’t answer: inside a finite field, does every number have a square root? In ordinary math, yes, always. In a finite field, no — some numbers have two square roots and some have none at all, and ZK systems need to know which is which. One small example carries the whole lesson: the prime 1313.

Which numbers are squares?

Square every nonzero number from 11 to 1212 mod 1313, and a pattern shows up immediately: every result appears exactly twice.

which numbers are squares? Which numbers mod 13 are the square of something? STEP 1 — square every number 1 1 2 4 3 9 4 3 5 12 6 10 7 10 8 12 9 3 10 9 11 4 12 1 x x2 72 = 49 = 3 × 13 + 10, so 10 STEP 2 — each value appears twice x and 13 − x give the same square: 1&12→1 2&11→4 3&10→9 4&9→3 5&8→12 6&7→10 STEP 3 — collect them squares: 1, 3, 4, 9, 10, 12 non-squares: 2, 5, 6, 7, 8, 11 6 squares and 6 non-squares 6 + 6 = 12 ✓ EXACTLY HALF OF THE NONZERO NUMBERS ARE SQUARES Each square has two roots: x and −x.
Fig. 1 — squaring every number from 1 to 12 mod 13. Each result appears twice, because x and 13 − x always give the same square, leaving six squares and six non-squares.
Quadratic residues

That symmetry isn’t a coincidence: 13−x13 - x is the same as −x-x mod 1313, and (−x)2(-x)^2 is just x2x^2. A number that is the square of something is called a quadratic residue. Here, exactly six of the twelve nonzero numbers are — half.

Squares are the even powers

Topic 19 built this group from a generator, 22. Looking at squares through that lens explains why it’s exactly half, not just confirms it.

squares are the even powers Using the generator 2, which powers give squares? STEP 1 — list the powers 1 2 2 4 3 8 4 3 5 6 6 12 7 11 8 9 9 5 10 10 11 7 12 1 k 2k STEP 2 — why (2j)2 = 22j, which is an even power STEP 3 — compare even powers: 1, 3, 4, 9, 10, 12 = the squares from before ✓ odd powers: 2, 5, 6, 7, 8, 11 = the non-squares ✓ squares = even powers, half of 12 SQUARING DOUBLES THE EXPONENT A generator splits the group into squares and non-squares, half and half.
Fig. 2 — the powers of 2 mod 13, colored by whether the exponent is even or odd. The even powers are exactly the squares from Fig. 1, and the odd powers are exactly the non-squares.
Squares as even powers

(gj)2=g2j(g^j)^2 = g^{2j}

Squaring any power of the generator doubles its exponent, landing on an even power every time. So every square is an even power of gg, and every even power is a square — splitting the group’s twelve elements into six even-power squares and six odd-power non-squares.

The square test

Building a full table doesn’t scale to a 254-bit prime. One exponentiation does the same job in a single step.

the square test Is there a quick test for whether a is a square mod p? a(p−1)/2 = 1 if a is a square, −1 if it is not STEP 1 — why it works (p = 13, so the power is 6) write a = 2k. Then a6 = 26k if k is even, 26k = (212)k÷2 = 1 if k is odd, we get 26 = 64 = 12 = −1 STEP 2 — try it 36 = 729 = 56 × 13 + 1 = 1 ✓ square 26 = 64 = 12 = −1 ✗ not a square 56 = (52)3 = (−1)3 = −1 ✗ not a square 106 = (103)2 = (−1)2 = 1 ✓ square matches the table: 3 and 10 are squares, 2 and 5 are not ✓ ONE POWER TELLS SQUARE FROM NON-SQUARE This is the Legendre symbol, written (a / p).
Fig. 3 — the square test worked through four values mod 13: raising a to (p − 1)/2 gives 1 for a square and −1 for a non-square, matching the table from Fig. 1 exactly.
The square test

a(p−1)/2≡1(modp) if a is a square, and −1 otherwisea^{(p-1)/2} \equiv 1 \pmod p \text{ if } a \text{ is a square, and } -1 \text{ otherwise}

Write aa as gkg^k for the generator gg. Then a(p−1)/2=gk(p−1)/2a^{(p-1)/2} = g^{k(p-1)/2}, and the last section showed this collapses to 11 when kk is even and −1-1 when kk is odd — exactly the square / non-square split. This quantity is called the Legendre symbol, written (ap)\left(\frac{a}{p}\right).

An easy square root

The test says whether a root exists. Finding the root itself is just as fast — when the prime has the right shape.

an easy square root Find √3 mod 11. (11 leaves remainder 3 when divided by 4.) if p leaves remainder 3 mod 4, then √a = a(p+1)/4 STEP 1 — is 3 a square? 35 = 243 = 22 × 11 + 1 = 1 ✓ STEP 2 — apply the formula (11 + 1) ÷ 4 = 3, so √3 = 33 = 27 = 5 STEP 3 — check 52 = 25 = 2 × 11 + 3 = 3 ✓ the other root is −5 = 6: 62 = 36 = 3 × 11 + 3 = 3 ✓ STEP 4 — why it works (a(p+1)÷4)2 = a(p+1)÷2 = a × a(p−1)÷2 = a × 1 = a A SQUARE ROOT IS JUST A POWER This works when p leaves remainder 3 when divided by 4.
Fig. 4 — finding √3 mod 11 in one exponentiation, since 11 leaves remainder 3 when divided by 4. Both roots check out, and a short derivation shows why the formula works.
A square root, when p ≡ 3 (mod 4)

a≡a(p+1)/4(modp)\sqrt{a} \equiv a^{(p+1)/4} \pmod p

Squaring this candidate gives a(p+1)/2=a⋅a(p−1)/2a^{(p+1)/2} = a \cdot a^{(p-1)/2}, and the square test says that last factor is 11 whenever aa is a square — so the square of the candidate really is aa. This only works when pp leaves remainder 33 mod 44; 1313 leaves remainder 11, so 1313 needs a different tool.

When the shortcut fails

For a prime like 1313, the easy formula doesn’t apply. The Tonelli–Shanks algorithm handles every prime, by maintaining a guess RR and an error term tt, and fixing the guess until the error disappears.

when the shortcut fails Find √10 mod 13. The shortcut needs remainder 3 mod 4, but 13 = 4 × 3 + 1. SETUP p − 1 = 12 = 3 × 22, so Q = 3 and S = 2 Take a non-square z = 2 (from the square test) c = 23 = 8, t = 103 = 1000 = −1, R = 102 = 100 = 9 R2 = 81 = 3 and 10 × t = 120 = 3, so R2 = 10 × t ✓ STEP 1 — find i t2 = 144 = 1, so i = 1, and b = c = 8 STEP 2 — fix the guess R = 9 × 8 = 72 = 7, c = b2 = 64 = 12, t = 12 × 12 = 144 = 1 STEP 3 — stop t = 1, so √10 = 7 (and 6) 72 = 49 = 3 × 13 + 10 ✓ and 62 = 36 = 2 × 13 + 10 ✓ FIX A WRONG GUESS UNTIL t BECOMES 1 This is the Tonelli–Shanks algorithm.
Fig. 5 — finding √10 mod 13 with Tonelli–Shanks. R is a running guess satisfying R² = 10 × t; one round of fixing the guess drives the error t to 1, which means R is exact.
Tonelli–Shanks

R2≡a⋅t(modp),fix R and t together until t≡1R^2 \equiv a \cdot t \pmod p, \qquad \text{fix } R \text{ and } t \text{ together until } t \equiv 1

Split p−1=Q⋅2Sp - 1 = Q \cdot 2^S, pick any non-square zz as a lever, and start from R=a(Q+1)/2R = a^{(Q+1)/2} and t=aQt = a^Q. Each round measures how many squarings turn tt into 11, then multiplies RR and tt by a correction built from zz that cancels exactly that much error. When tt finally equals 11, R2=aR^2 = a, and RR is the square root.

One extra bit instead of y

Here’s the first payoff. A curve point has two coordinates, but xx almost determines yy on its own — only the sign is missing, and a square root recovers it.

one extra bit instead of y On the curve y2 = x3 + 3 mod 13, can we store (3, 11) with x and one bit? STEP 1 — compress keep x = 3, and one bit for the parity of y: 11 is odd, so bit = 1 STEP 2 — decompress, compute y2 y2 = 33 + 3 = 30 = 4 (mod 13) STEP 3 — take the square root y = ±2, so the candidates are 2 and 11 (since −2 = 11) STEP 4 — pick by the bit the bit says odd, so y = 11 (3, 11) recovered from x = 3 and one bit 112 = 121 = 9 × 13 + 4 ✓ if y2 is not a square, such as x = 2 giving 11, there is no point with that x A SQUARE ROOT REBUILDS THE MISSING HALF Compressed public keys use this same idea.
Fig. 6 — compressing the point (3, 11) on y² = x³ + 3 mod 13 down to x and a single parity bit, then rebuilding y with a square root and that bit.
Point compression

y=±x3+3,picked by one stored parity bity = \pm\sqrt{x^3 + 3}, \quad \text{picked by one stored parity bit}

Store xx and one bit for whether yy is even or odd. To rebuild the point, compute x3+3x^3 + 3, take its square root — which gives both yy and −y-y — and use the bit to pick the right one. If x3+3x^3 + 3 isn’t a square at all, no point exists with that xx. This is exactly how a compressed public key fits a 256-bit xx plus one bit instead of a full 512-bit pair.

Turning a number into a point

The reverse direction matters too: given an arbitrary number — say, the output of a hash — can it be turned into a point on the curve?

turning a number into a point Can we turn any number into a point on y2 = x3 + 3 mod 13? STEP 1 — try x = 2 y2 = 23 + 3 = 11. Square test: 11 = −2, so 116 = (−2)6 = 64 = −1 ✗ STEP 2 — increment to x = 3 y2 = 33 + 3 = 30 = 4. Square test: 46 = 4096 = 315 × 13 + 1 = 1 ✓ STEP 3 — take the root y = 2 (or 11), giving the point (3, 2) 22 = 4 and 33 + 3 = 30 = 4 ✓ this is the simplest method; real systems use more careful versions TRY, TEST, INCREMENT On a real curve about half of all x values work, so a few tries are enough.
Fig. 7 — mapping a number onto the curve by trying it as x: x = 2 fails the square test, so the search increments to x = 3, which works, giving the point (3, 2).
A basic hash-to-curve

try x,x+1,x+2,… until x3+3 is a square\text{try } x, x+1, x+2, \ldots \text{ until } x^3 + 3 \text{ is a square}

Try a candidate xx, test whether x3+3x^3 + 3 is a square, and increment if it isn’t. Because exactly half of all numbers are squares — the fact this lesson opened with — about half of all xx values work, so the odds of failing nn times in a row fall off as 1/2n1/2^n and a few tries are always enough. Real systems use more careful versions of this, since the number of tries taken can leak timing information.

Where these tools show up

Three tools, all built from the same idea: telling squares apart from non-squares, and moving between a number and its root.

where these tools show up THE SQUARE TEST is this number a square? one exponentiation answers it A fast yes or no. SQUARE ROOTS a shortcut when p leaves remainder 3 mod 4, and Tonelli–Shanks otherwise Solving y² = a. COMPRESSION AND HASH-TO-CURVE store one bit instead of y, and map data onto the curve Points from numbers. ONE TEST, ONE ROOT, ONE BIT Next up: extension fields and towers.
Fig. 8 — the square test, square roots, and compression/hash-to-curve, and what each one is for.

The square test gives a fast yes-or-no on whether a root exists at all. Square roots — the easy shortcut or Tonelli–Shanks — actually solve y2=ay^2 = a. And compression plus hash-to-curve are where both get used in practice: shrinking a stored point down to xx and one bit, and turning arbitrary data into a point on the curve. But every one of these tools has a gap: 22 has no square root mod 1313 at all. The next lesson builds a bigger field that contains one.

Key takeaways
  • Exactly half of the nonzero numbers mod a prime are squares (quadratic residues), because xx and −x-x always square to the same value.
  • A square is exactly an even power of a generator, since squaring any power doubles its exponent — this is why the split is exactly half and half.
  • The square test — raising aa to (p−1)/2(p-1)/2 — answers “is aa a square?” in one exponentiation; this quantity is the Legendre symbol.
  • When p≡3(mod4)p \equiv 3 \pmod 4, a square root is a single power, a(p+1)/4a^{(p+1)/4}; otherwise the Tonelli–Shanks algorithm finds it by fixing a guess until its error term reaches 11.
  • Point compression stores a curve point as xx plus one parity bit and rebuilds yy with a square root; hash-to-curve runs the idea in reverse, trying values of xx until x3+3x^3 + 3 is a square.