MathLabs

Arithmetic and number theory

Quadratic residues

Numbers that are perfect squares modulo a given integer, studied via the law of quadratic reciprocity.

IntuitionIntuition: which remainders are perfect squares?

Fix an odd prime pp and square every remainder 0,1,…,p−10,1,\dots,p-1 modulo pp. Because x2≡(p−x)2(modp)x^2 \equiv (p-x)^2 \pmod p, the squares repeat in pairs and only about half of the nonzero remainders ever show up. A number aa with gcd⁡(a,p)=1\gcd(a,p)=1 is called a quadratic residue modulo pp when the congruence x2≡a(modp)x^2 \equiv a \pmod{p} has a solution, and a quadratic nonresidue otherwise. For p=7p=7: squaring 1,2,3,4,5,61,2,3,4,5,6 gives 1,4,2,2,4,11,4,2,2,4,1, so the quadratic residues modulo 77 are exactly {1,2,4}\{1,2,4\} and the nonresidues are {3,5,6}\{3,5,6\}.

Directed graph of the squaring map modulo a small prime, with quadratic-residue nodes highlighted.
Residue chords modulo m=11m = 11: among the 1010 nonzero residues, exactly half (1,3,4,5,91, 3, 4, 5, 9) are quadratic residues.

UndergraduateDefinition and the Legendre symbol

Definition: Legendre symbol

For an odd prime pp and integer aa with p∤ap \nmid a, the Legendre symbol (ap)\left(\dfrac{a}{p}\right) equals +1+1 if aa is a quadratic residue modulo pp, and −1-1 if it is a quadratic nonresidue. By convention (ap)=0\left(\dfrac{a}{p}\right)=0 when p∣ap \mid a. The Legendre symbol is completely multiplicative in its top argument: (abp)=(ap)(bp)\left(\frac{ab}{p}\right) = \left(\frac{a}{p}\right)\left(\frac{b}{p}\right).

(ap)≡ap−12(modp)(Euler’s criterion)\left(\dfrac{a}{p}\right) \equiv a^{\frac{p-1}{2}} \pmod{p}\qquad\text{(Euler’s criterion)}

Euler's criterion lets us compute (ap)\left(\dfrac{a}{p}\right) without factoring anything: raise aa to the power p−12\frac{p-1}{2} modulo pp and read off ±1\pm 1. It also immediately gives the value (−1p)=(−1)p−12\left(\dfrac{-1}{p}\right) = (-1)^{\frac{p-1}{2}}, which tells us that −1-1 is a quadratic residue exactly for primes p≡1(mod4)p \equiv 1 \pmod 4.

(pq)(qp)=(−1)p−12⋅q−12(quadratic reciprocity)\left(\dfrac{p}{q}\right)\left(\dfrac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}\qquad\text{(quadratic reciprocity)}
Value of (−1p)\left(\frac{-1}{p}\right) and (2p)\left(\frac{2}{p}\right) by residue class of pp
Class of pp(−1p)\left(\frac{-1}{p}\right)(2p)\left(\frac{2}{p}\right)Example
p≡1(mod4)p \equiv 1 \pmod 4+1+1depends on p mod 8p \bmod 8p=13p=13
p≡3(mod4)p \equiv 3 \pmod 4−1-1depends on p mod 8p \bmod 8p=7p=7
p≡1,7(mod8)p \equiv 1, 7 \pmod 8see row above+1+1p=7p=7
p≡3,5(mod8)p \equiv 3, 5 \pmod 8see row above−1-1p=13p=13

UndergraduateTheorems and proofs

For an odd prime pp and gcd⁡(a,p)=1\gcd(a,p)=1: (ap)≡ap−12(modp)\left(\dfrac{a}{p}\right) \equiv a^{\frac{p-1}{2}} \pmod{p}; equivalently, aa is a quadratic residue modulo pp if and only if a(p−1)/2≡1(modp)a^{(p-1)/2} \equiv 1 \pmod p.

Why is it true?

It turns an existence question (does some xx square to aa?) into a single modular exponentiation, which is exactly what makes the Legendre symbol computable and multiplicative.

Proof

Since (Z/pZ)∗(\mathbb{Z}/p\mathbb{Z})^{*} is cyclic of order p−1p-1 (a standard fact for prime moduli), fix a primitive root gg and write a≡gk(modp)a \equiv g^k \pmod p for some 0≤k≤p−20 \le k \le p-2. The quadratic residues modulo pp are exactly the even powers of gg: if a=g2ma=g^{2m} then x=gmx=g^m satisfies x2≡ax^2\equiv a, and conversely every square x2=(gj)2=g2jx^2=(g^j)^2=g^{2j} is an even power. So aa is a quadratic residue exactly when kk is even.

Now compute a(p−1)/2≡gk(p−1)/2(modp)a^{(p-1)/2} \equiv g^{k(p-1)/2} \pmod p. If kk is even, write k=2mk=2m; then gk(p−1)/2=gm(p−1)=(gp−1)m≡1m=1(modp)g^{k(p-1)/2}=g^{m(p-1)}=(g^{p-1})^m\equiv 1^m=1\pmod p by Fermat's little theorem, matching the residue case.

If kk is odd, then gk(p−1)/2g^{k(p-1)/2} is a square root of gk(p−1)=(gp−1)k≡1(modp)g^{k(p-1)}=(g^{p-1})^k\equiv 1\pmod p, so a(p−1)/2≡±1a^{(p-1)/2}\equiv \pm 1. It cannot be 11: if it were, the order of gg would have to divide k(p−1)/2k(p-1)/2, but ord⁡(g)=p−1\operatorname{ord}(g)=p-1 and kk is odd means k(p−1)/2k(p-1)/2 is not a multiple of p−1p-1 unless (p−1)/2(p-1)/2 already is, contradicting that gg is a primitive root of exact order p−1p-1. Hence a(p−1)/2≡−1(modp)a^{(p-1)/2}\equiv -1\pmod p exactly when kk is odd, i.e. exactly when aa is a nonresidue.

Combining both cases: a(p−1)/2≡1(modp)a^{(p-1)/2}\equiv 1\pmod p iff aa is a quadratic residue, and ≡−1\equiv -1 otherwise, which is precisely the statement (ap)≡ap−12(modp)\left(\dfrac{a}{p}\right) \equiv a^{\frac{p-1}{2}} \pmod{p}.

For distinct odd primes pp and qq: (pq)(qp)=(−1)p−12⋅q−12\left(\dfrac{p}{q}\right)\left(\dfrac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}.

Why is it true?

It says whether pp is a square mod qq and whether qq is a square mod pp are the same question, up to a sign that depends only on the residues of p,qp,q mod 44 — turning a hard-looking two-variable problem into a one-line rule.

Proof

We use Gauss's lemma as a stepping stone: for an odd prime pp and gcd⁡(a,p)=1\gcd(a,p)=1, look at the least positive residues of a,2a,…,p−12aa, 2a, \dots, \frac{p-1}{2}a modulo pp, and let μ\mu be how many of them exceed p/2p/2. Gauss's lemma states (ap)=(−1)μ\left(\dfrac{a}{p}\right)=(-1)^{\mu}; it follows from pairing each such "large" residue rr with p−r≤p/2p-r\le p/2 and tracking signs when multiplying the p−12\frac{p-1}{2} numbers together in two different ways.

Eisenstein's refinement expresses μ\mu as a lattice-point count: one shows μ≡∑k=1(p−1)/2⌊kqp⌋(mod2)\mu \equiv \sum_{k=1}^{(p-1)/2} \left\lfloor \frac{kq}{p} \right\rfloor \pmod 2 when a=qa=q is odd, by comparing ⌊kq/p⌋\lfloor kq/p\rfloor (the number of multiples of pp below kqkq) to how far kq mod pkq \bmod p sits from p/2p/2. Applying the same counting argument symmetrically gives (qp)=(−1)S(q,p)\left(\frac{q}{p}\right)=(-1)^{S(q,p)} and (pq)=(−1)S(p,q)\left(\frac{p}{q}\right)=(-1)^{S(p,q)}, where S(q,p)=∑k=1(p−1)/2⌊kqp⌋S(q,p)=\sum_{k=1}^{(p-1)/2}\left\lfloor \frac{kq}{p}\right\rfloor and S(p,q)=∑k=1(q−1)/2⌊kpq⌋S(p,q)=\sum_{k=1}^{(q-1)/2}\left\lfloor \frac{kp}{q}\right\rfloor.

Geometrically, S(q,p)+S(p,q)S(q,p)+S(p,q) counts the lattice points (x,y)(x,y) with 1≤x≤p−121\le x\le \frac{p-1}{2}, 1≤y≤q−121\le y\le \frac{q-1}{2} lying strictly below the line qx=pyqx=py (that count is S(q,p)S(q,p)) plus those strictly above it (that count is S(p,q)S(p,q), by the symmetric roles of p,qp,q). No lattice point lies exactly on the line since gcd⁡(p,q)=1\gcd(p,q)=1 and x<px<p, so together these two counts exhaust the full rectangle of p−12⋅q−12\frac{p-1}{2}\cdot\frac{q-1}{2} lattice points.

Therefore S(q,p)+S(p,q)=p−12⋅q−12S(q,p)+S(p,q) = \frac{p-1}{2}\cdot\frac{q-1}{2}, and multiplying the two Legendre-symbol formulas gives (pq)(qp)=(−1)S(p,q)+S(q,p)=(−1)p−12⋅q−12\left(\frac{p}{q}\right)\left(\frac{q}{p}\right)=(-1)^{S(p,q)+S(q,p)}=(-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}, which is exactly (pq)(qp)=(−1)p−12⋅q−12\left(\dfrac{p}{q}\right)\left(\dfrac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}.

UndergraduateReal-World Applications and Worked Examples

Quadratic residues are not just a number-theory curiosity: they underlie a public-key encryption scheme (Goldwasser–Micali), a widely used pseudorandom bit generator (Blum–Blum–Shub), and the Legendre sequences used to design spread-spectrum radar and GPS-like ranging codes with sharp autocorrelation peaks.

Example: Deciding a quadratic residue by reciprocity

Is 1010 a quadratic residue modulo 1313?

Solution

Write 10=2⋅510=2\cdot 5, so (1013)=(213)(513)\left(\frac{10}{13}\right)=\left(\frac{2}{13}\right)\left(\frac{5}{13}\right) by multiplicativity.

For (213)\left(\frac{2}{13}\right): since 13≡5(mod8)13 \equiv 5 \pmod 8, the supplementary formula for 22 gives (213)=−1\left(\frac{2}{13}\right)=-1.

For (513)\left(\frac{5}{13}\right): since 5≡1(mod4)5\equiv 1\pmod 4, reciprocity gives (513)(135)=+1\left(\frac{5}{13}\right)\left(\frac{13}{5}\right)=+1, so (513)=(135)=(35)\left(\frac{5}{13}\right)=\left(\frac{13}{5}\right)=\left(\frac{3}{5}\right) (since 13≡3(mod5)13\equiv 3\pmod 5). Squares mod 55 are 1,41,4, and 33 is not among them, so (35)=−1\left(\frac{3}{5}\right)=-1, hence (513)=−1\left(\frac{5}{13}\right)=-1.

Multiplying: (1013)=(−1)(−1)=+1\left(\frac{10}{13}\right)=(-1)(-1)=+1, so 1010 is a quadratic residue modulo 1313 — indeed 62=36≡10(mod13)6^2=36\equiv 10\pmod{13}.

Example: Legendre sequences for spread-spectrum ranging codes

GPS-style ranging systems need binary codes with a sharp autocorrelation peak at zero shift and near-zero correlation elsewhere. Build a length-1111 Legendre sequence from the Legendre symbols modulo the prime 1111 and check its autocorrelation qualitatively.

Solution

The quadratic residues modulo 1111 are {1,3,4,5,9}\{1,3,4,5,9\} (squares of 1,…,51,\dots,5), so define si=+1s_i=+1 if ii is a residue, si=−1s_i=-1 if ii is a nonresidue {2,6,7,8,10}\{2,6,7,8,10\}, and s0=−1s_0=-1 by convention, giving the ±1\pm 1 sequence s=(−1,+1,−1,+1,+1,+1,−1,−1,−1,+1,−1)s=(-1,+1,-1,+1,+1,+1,-1,-1,-1,+1,-1) for i=0,…,10i=0,\dots,10.

The key structural fact is that for a shift k≢0k\not\equiv 0, the correlation ∑isisi+k\sum_i s_i s_{i+k} reduces (using multiplicativity of the Legendre symbol) to a sum closely related to ∑x(x(x+k)11)\sum_x \left(\frac{x(x+k)}{11}\right), and a classical character-sum estimate shows such sums equal −1-1 for every nonzero shift kk modulo an odd prime — dramatically smaller than the peak value 1010 at k=0k=0.

This two-level autocorrelation (a fixed off-peak value for every nonzero shift) is exactly the property that makes Legendre sequences attractive for synchronizing a receiver: correlating the incoming signal against every cyclic shift of the known sequence produces one unmistakably large spike exactly at the true alignment, which is how a GPS receiver locks onto the timing of a satellite's code.

What is (ap)\left(\frac{a}{p}\right) called when x2≡a(modp)x^2\equiv a\pmod p has no solution?

According to Euler's criterion, (ap)\left(\frac{a}{p}\right) is congruent modulo pp to which power of aa?

For which residue class of an odd prime pp modulo 44 is −1-1 a quadratic residue?

Which cryptographic building block is based directly on the hardness of deciding quadratic residuosity modulo a composite number?

References

  1. Wikipedia contributors (2024). Quadratic reciprocity
  2. Kenneth Ireland, Michael Rosen (1990). A Classical Introduction to Modern Number Theory