Quadratic residues

Introduction

Quadratic residues can be viewed as a discussion of the feasibility of taking square roots modulo a number. For roots of higher degree, see k-th power residues.

Definition

Quadratic residue

Let integers aa and pp satisfy (a,p)=1(a,p)=1. If there exists an integer xx such that

x2≡a(modp),x^2\equiv a\pmod p,

then aa is called a quadratic residue modulo pp; otherwise, aa is called a quadratic non-residue modulo pp. When the modulus pp is clear later, we may abbreviate these as quadratic (non-)residues.

Euler's criterion

When the modulus is an odd prime, we have the following theorem:

Euler's criterion

For an odd prime pp and an integer aa satisfying (a,p)=1(a,p)=1,

ap−12≡{1(modp),(∃x∈Z),  a≡x2(modp),−1(modp),otherwise.a^{\frac{p-1}{2}}\equiv\begin{cases} 1 \pmod p, & (\exists x\in\mathbf{Z}),~~a\equiv x^2\pmod p,\\ -1 \pmod p, & \text{otherwise}.\\ \end{cases}

In other words, for the above pp and aa:

  1. aa is a quadratic residue of pp if and only if ap−12≡1(modp)a^{\frac{p-1}{2}}\equiv 1 \pmod p.
  2. aa is a quadratic non-residue of pp if and only if ap−12≡−1(modp)a^{\frac{p-1}{2}}\equiv -1 \pmod p.
Proof

First, by Fermat's little theorem, ap−1≡1(modp)a^{p-1}\equiv 1\pmod p, so

(ap−12+1)(ap−12−1)≡0(modp),\left(a^{\frac{p-1}{2}}+1\right)\left(a^{\frac{p-1}{2}}-1\right)\equiv 0\pmod p,

Thus every aa satisfying (a,p)=1(a,p)=1 has a(p−1)/2≡±1(modp).a^{(p-1)/2}\equiv \pm 1\pmod p..

Since pp is an odd prime, we also have:

xp−1−ap−12=(x2)p−12−ap−12=(x2−a)P(x),x^{p-1}-a^{\frac{p-1}{2}}={\left(x^2\right)}^{\frac{p-1}{2}}-a^{\frac{p-1}{2}}=(x^2-a)P(x),

where P(x)P(x) is a polynomial with integer coefficients. Therefore:

xp−x=x(xp−1−ap−12)+x(ap−12−1)=(x2−a)xP(x)+(ap−12−1)x.\begin{aligned} x^p-x&=x\left(x^{p-1}-a^{\frac{p-1}{2}}\right)+x\left(a^{\frac{p-1}{2}}-1\right)\\ &=(x^2-a)xP(x)+\left(a^{\frac{p-1}{2}}-1\right)x.\\ \end{aligned}

By Theorem 5 of congruence equations, 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. Consequently, aa is a quadratic non-residue modulo pp if and only if a(p−1)/2≡−1(modp)a^{(p-1)/2}\equiv -1\pmod p.

Based on Euler's criterion, we obtain the following corollary:

Number of quadratic residues

For an odd prime pp, there are p−12\dfrac{p-1}{2} quadratic residues and quadratic non-residues modulo pp.

Proof

By Euler's criterion, consider ap−12≡1(modp).a^{\frac{p-1}{2}}\equiv 1\pmod p.

Since p−12∣(p−1)\dfrac{p-1}{2}\mid (p-1), Theorem 6 of congruence equations shows that ap−12≡1(modp)a^{\frac{p-1}{2}}\equiv 1\pmod p has p−12\dfrac{p-1}{2} solutions. Therefore, modulo pp, there are p−12\dfrac{p-1}{2} quadratic residues and quadratic non-residues.

Legendre symbols

For convenience in the discussion that follows, we introduce the following notation:

Legendre symbol

For an odd prime pp and an integer aa, define the Legendre symbol by:

(ap)={0,p∣a,1,(p∤a)∧((∃x∈Z),  a≡x2(modp)),−1,otherwise.\left(\frac{a}{p}\right)=\begin{cases} 0, & p\mid a,\\ 1, & (p\nmid a) \land ((\exists x\in\mathbf{Z}),~~a\equiv x^2\pmod p),\\ -1, & \text{otherwise}.\\ \end{cases}

That is, for aa satisfying (a,p)=1(a,p)=1:

  • aa is a quadratic residue modulo pp if and only if (ap)=1.\left(\dfrac{a}{p}\right)=1.
  • aa is a quadratic non-residue modulo pp if and only if (ap)=−1.\left(\dfrac{a}{p}\right)=-1.

The following table lists some values of Legendre symbols (from Wikipedia).

Figure: quad residue

Properties

  1. For every integer aa,

    ap−12≡(ap)(modp).a^{\frac{p-1}{2}}\equiv \left(\frac{a}{p}\right)\pmod p.

    We further have the following corollaries:

    • (1p)=1.\left(\dfrac{1}{p}\right)=1.
    • (−1p)=(−1)p−12={1,p≡1(mod4),−1,p≡3(mod4).\left(\dfrac{-1}{p}\right)=(-1)^{\frac{p-1}{2}}=\begin{cases} 1, & p\equiv 1\pmod 4,\\ -1, & p\equiv 3\pmod 4. \end{cases}
  2. a1≡a2(modp)  ⟹  (a1p)=(a2p).a_1\equiv a_2\pmod p\implies \left(\dfrac{a_1}{p}\right)=\left(\dfrac{a_2}{p}\right).

  3. ( Complete multiplicativity ) For every integer a1,a2a_1,a_2,

    (a1a2p)=(a1p)(a2p).\left(\frac{a_1a_2}{p}\right)=\left(\frac{a_1}{p}\right)\left(\frac{a_2}{p}\right).

    We have the corollary that for integers a,ba,b with p∤bp\nmid b,

    (ab2p)=(ap).\left(\frac{ab^2}{p}\right)=\left(\frac{a}{p}\right).
  4. (2p)=(−1)p2−18={1,p≡±1(mod8),−1,p≡±3(mod8).\left(\frac{2}{p}\right)=(-1)^{\frac{p^2-1}{8}}=\begin{cases} 1, & p\equiv \pm 1\pmod 8, \\ -1, & p\equiv \pm 3\pmod 8. \\ \end{cases}
Proof
  1. This follows directly from the definition of the Legendre symbol and Euler's criterion.

  2. Note that

    a1≡a2(modp)  ⟹  (a1p)≡(a2p)(modp),a_1\equiv a_2\pmod p\implies \left(\frac{a_1}{p}\right)\equiv\left(\frac{a_2}{p}\right)\pmod p,

    while ∣(a1p)−(a2p)∣≤2\left|\left(\dfrac{a_1}{p}\right)-\left(\dfrac{a_2}{p}\right)\right|\leq 2 and p>2p>2, so:

    a1≡a2(modp)  ⟹  (a1p)=(a2p).a_1\equiv a_2\pmod p\implies \left(\frac{a_1}{p}\right)=\left(\frac{a_2}{p}\right).
  3. By 1,

    (a1a2p)≡a1p−12a2p−12≡(a1p)(a2p)(modp).\left(\frac{a_1a_2}{p}\right)\equiv a_1^{\frac{p-1}{2}}a_2^{\frac{p-1}{2}}\equiv\left(\frac{a_1}{p}\right)\left(\frac{a_2}{p}\right)\pmod p.

    Since ∣(a1a2p)−(a1p)(a2p)∣≤2\left|\left(\dfrac{a_1a_2}{p}\right)-\left(\dfrac{a_1}{p}\right)\left(\dfrac{a_2}{p}\right)\right|\leq 2 and p>2p>2,

    (a1a2p)=(a1p)(a2p).\left(\frac{a_1a_2}{p}\right)=\left(\frac{a_1}{p}\right)\left(\frac{a_2}{p}\right).
  4. See quadratic reciprocity.

Based on these properties, if (pq)\left(\dfrac{p}{q}\right) can be computed for every pair of odd primes pp and qq, then we can compute the Legendre symbol for every valid case. We now have an elegant theorem that cleverly establishes a connection between (pq)\left(\dfrac{p}{q}\right) and (qp)\left(\dfrac{q}{p}\right), allowing us to complete the computation using an idea similar to the Euclidean algorithm.

Quadratic reciprocity

Quadratic reciprocity

Let pp and qq be two distinct odd primes. Then

(pq)(qp)=(−1)p−12q−12.\left(\frac{p}{q}\right)\left(\frac{q}{p}\right)=(-1)^{\frac{p-1}{2}\frac{q-1}{2}}.

There are many proofs1. One proof is based on the following lemma2:

Gauss's lemma

Let pp be an odd prime and (n,p)=1(n,p)=1. For each integer k (1≤k≤(p−1)/2)k~\left(1\leq k\leq (p-1)/2\right), let rk=nk mod pr_k=nk\bmod p, and let A={rk:rk<p/2}A=\{r_k:r_k < p/2\} and B={rk:rk>p/2}B=\{r_k:r_k > p/2\}. Then

(np)=(−1)∣B∣.\left(\frac{n}{p}\right)=(-1)^{|B|}.
Proof

Let λ=∣A∣\lambda=|A| and μ=∣B∣\mu=|B|. Clearly λ+μ=(p−1)/2\lambda+\mu=(p-1)/2, so

np−12(p−12)!=∏k=1p−12nk≡∏a∈Aa∏b∈Bb(modp).n^{\frac{p-1}{2}}\left(\frac{p-1}{2}\right)!=\prod_{k=1}^{\frac{p-1}{2}} nk\equiv\prod_{a\in A}a\prod_{b\in B}b\pmod{p}.

For any element bb in BB, we have p2<b<p\dfrac{p}{2} < b < p, so 0<p−b<p20 < p-b < \dfrac{p}{2}. Moreover, for any bb in BB, p−b∉Ap-b\notin A: otherwise, if A,BA,B contained elements a,ba,b with a=p−ba=p-b, then there would be integers 0<k1,k2<(p−1)/20 < k_1,k_2 < (p-1)/2 such that a=nk1a=nk_1, b=nk2b=nk_2, and p∣n(k1+k2)p\mid n(k_1+k_2). Since (n,p)=1(n,p)=1, this would imply p∣(k1+k2)p\mid (k_1+k_2). But 0<k1+k2<p0 < k_1+k_2 < p, a contradiction. Therefore

np−12(p−12)!≡(−1)μ∏a∈Aa∏b∈B(p−b)=(−1)μ(p−12)!(modp),n^{\frac{p-1}{2}}\left(\frac{p-1}{2}\right)!\equiv(-1)^{\mu}\prod_{a\in A}a\prod_{b\in B}(p-b)=(-1)^{\mu}\left(\frac{p-1}{2}\right)!\pmod{p},

that is,

np−12≡(−1)μ(modp).n^{\frac{p-1}{2}}\equiv(-1)^{\mu}\pmod{p}.

The result follows from Property 1 of the Legendre symbol.

Generalization

Gauss's lemma can be generalized as follows3:

Let pp be an odd prime, and let I⊂Zp∗I\subset\mathbf{Z}_p^* satisfy I∪−I=Zp∗I\cup -I=\mathbf{Z}_p^* and I∩−I=∅I\cap -I=\varnothing, where −I:={−i:i∈I}-I:=\{-i:i\in I\}. Then for every integer nn coprime to pp,

(np)=(−1)∣J∣,\left(\frac{n}{p}\right)=(-1)^{|J|},

where J={j∈I:nj∈−I}J=\{j\in I:nj\in -I\}.

Taking I={1,2,…,(p−1)/2}I=\{1,2,\dots,(p-1)/2\} yields Gauss's lemma. The proof is essentially the same as the proof of Gauss's lemma and is omitted.

The following corollaries are easy to obtain:

Corollary

For an odd prime pp,

(2p)=(−1)p2−18={1,p≡±1(mod8),−1,p≡±3(mod8).\left(\frac{2}{p}\right)=(-1)^{\frac{p^2-1}{8}}=\begin{cases} 1, & p\equiv \pm 1\pmod 8, \\ -1, & p\equiv \pm 3\pmod 8. \\ \end{cases}

For an odd prime pp and an odd nn satisfying (n,p)=1(n,p)=1,

(np)=(−1)∑i=1(p−1)/2⌊ni/p⌋.\left(\frac{n}{p}\right)=(-1)^{\sum_{i=1}^{(p-1)/2}\lfloor ni/p \rfloor}.
Proof

For n,k,rk,A,B,λ,μn,k,r_k,A,B,\lambda,\mu in Gauss's lemma, we have nk=p⌊nkp⌋+rknk=p\left\lfloor\dfrac{nk}{p}\right\rfloor+r_k, and hence

n⋅p2−18=∑k=1p−12nk=p∑k=1p−12⌊nkp⌋+∑a∈Aa+∑b∈Bb=p∑k=1p−12⌊nkp⌋+∑a∈Aa+∑b∈B(p−b)+2∑b∈Bb−pμ=p∑k=1p−12⌊nkp⌋+∑k=1p−12k+2∑b∈Bb−pμ=p∑k=1p−12⌊nkp⌋+p2−18+2∑b∈Bb−pμ,\begin{aligned} n\cdot\frac{p^2-1}{8}=\sum_{k=1}^{\frac{p-1}{2}}nk&=p\sum_{k=1}^{\frac{p-1}{2}}\left\lfloor\dfrac{nk}{p}\right\rfloor+\sum_{a\in A}a+\sum_{b\in B}b\\ &=p\sum_{k=1}^{\frac{p-1}{2}}\left\lfloor\dfrac{nk}{p}\right\rfloor+\sum_{a\in A}a+\sum_{b\in B}(p-b)+2\sum_{b\in B}b-p\mu\\ &=p\sum_{k=1}^{\frac{p-1}{2}}\left\lfloor\dfrac{nk}{p}\right\rfloor+\sum_{k=1}^{\frac{p-1}{2}}k+2\sum_{b\in B}b-p\mu\\ &=p\sum_{k=1}^{\frac{p-1}{2}}\left\lfloor\dfrac{nk}{p}\right\rfloor+\frac{p^2-1}{8}+2\sum_{b\in B}b-p\mu, \end{aligned}

Thus

(n−1)p2−18=p∑k=1p−12⌊nkp⌋+2∑b∈Bb−pμ.(n-1)\frac{p^2-1}{8}=p\sum_{k=1}^{\frac{p-1}{2}}\left\lfloor\dfrac{nk}{p}\right\rfloor+2\sum_{b\in B}b-p\mu.

If n=2n=2, then 0<nkp≤p−1p<10 < \dfrac{nk}{p}\leq\dfrac{p-1}{p} < 1, so

p2−18≡μ(mod2).\frac{p^2-1}{8}\equiv\mu\pmod{2}.

If 2∤n2\nmid n, then

∑k=1p−12⌊nkp⌋≡μ(mod2).\sum_{k=1}^{\frac{p-1}{2}}\left\lfloor\dfrac{nk}{p}\right\rfloor\equiv\mu\pmod{2}.

By the above corollary, proving quadratic reciprocity only requires verifying

p−12q−12=∑k=1p−12⌊qkp⌋+∑k=1q−12⌊pkq⌋.\frac{p-1}{2}\frac{q-1}{2}=\sum_{k=1}^{\frac{p-1}{2}}\left\lfloor\dfrac{qk}{p}\right\rfloor+\sum_{k=1}^{\frac{q-1}{2}}\left\lfloor\dfrac{pk}{q}\right\rfloor.

Consider the set SS of points (px,qy)(px,qy), where 1≤x≤q−12,1≤y≤p−121\leq x\leq \dfrac{q-1}{2},1\leq y\leq \dfrac{p-1}{2}. Divide it into two parts according to whether pxpx is larger or smaller than qyqy (clearly px≠qypx\neq qy), and verify the sizes of the three resulting sets.

Quadratic reciprocity can be used not only to determine whether nn is a quadratic residue modulo pp, but also to determine the structure of the moduli for which nn is a quadratic residue.

Example
  • Odd primes pp for which 55 is a quadratic residue satisfy p≡±1(mod5).p\equiv \pm 1\pmod 5.
  • Odd primes pp for which −3-3 is a quadratic residue satisfy p≡1(mod3).p\equiv 1\pmod 3.
  • Odd primes pp for which both −2-2 and 33 are quadratic residues satisfy p≡11(mod24).p\equiv 11\pmod{24}.

We can also prove conclusions such as “there are infinitely many primes of the form 4k+14k+1”; these conclusions are actually simple corollaries of Dirichlet's theorem.

Jacobi symbols

Based on quadratic reciprocity, it is natural to consider a generalization of the Legendre symbol:

Jacobi symbol

For a positive odd integer m=p1α1…pkαkm=p_1^{\alpha_1}\dots p_k^{\alpha_k} and an integer aa, where p1,…,pkp_1,\dots,p_k are primes and α1,…,αk\alpha_1,\dots,\alpha_k are positive integers, define the Jacobi symbol as follows:

(am):=∏i=1k(api)αi.\left(\frac{a}{m}\right):=\prod_{i=1}^k\left(\frac{a}{p_i}\right)^{\alpha_i}.

Here, the (api)\left(\frac{a}{p_i}\right) on the right-hand side is the Legendre symbol. For an integer aa, we also define (a1)=1.\left(\dfrac{a}{1}\right)=1.

Warning

We generally do not distinguish between Legendre and Jacobi symbols. Complete multiplicativity gives Jacobi symbols the same properties as Legendre symbols, so their calculation methods are the same. However, note that when mm is not an odd prime, the value of (am)\left(\dfrac{a}{m}\right) is unrelated to whether aa is a quadratic residue modulo mm. If (am)=−1\left(\dfrac{a}{m}\right)=-1, however, then at least one (in fact, an odd number of) prime factors pp of mm has aa as a quadratic non-residue modulo pp, so aa is a quadratic non-residue modulo mm in this case.

We can further generalize the modulus to be any integer by defining (a−1)\left(\dfrac{a}{-1}\right), (a0)\left(\dfrac{a}{0}\right), and (a2)\left(\dfrac{a}{2}\right); this gives the Kronecker symbol.

Square roots modulo a number

This section discusses algorithms for taking square roots modulo a number. In particular, it mainly introduces the case of a prime modulus. For general moduli, see the discussion of taking higher-degree roots modulo a number.

Algorithms for special cases

For the congruence x2≡a(modp)x^2\equiv a\pmod p, where pp is an odd prime and aa is a quadratic residue, when p mod 4=3p\bmod 4=3 there is a simpler solution. Consider:

(a(p+1)/4)2≡a(p+1)/2(modp)≡xp+1(modp)≡(x2)(xp−1)(modp)≡x2(modp)(∵Fermat’s little theorem)\begin{aligned} \left(a^{(p+1)/4}\right)^2&\equiv a^{(p+1)/2}&\pmod p\\ &\equiv x^{p+1}&\pmod p\\ &\equiv \left(x^2\right)\left(x^{p-1}\right)&\pmod p\\ &\equiv x^2&\pmod p&\quad (\because{\text{Fermat's little theorem}}) \end{aligned}

Thus a(p+1)/4 mod pa^{(p+1)/4}\bmod p is a solution.

Atkin's algorithm

Still consider the congruence above. Now p mod 8=5p\bmod 8=5. Let b≡(2a)(p−5)/8(modp)b\equiv (2a)^{(p-5)/8}\pmod p and i≡2ab2(modp)\mathrm{i}\equiv 2ab^2\pmod p. Then i2≡−1(modp)\mathrm{i}^2\equiv -1\pmod p, and ab(i−1) mod pab(\mathrm{i}-1)\bmod p is a solution.

Proof i2≡(2ab2)2(modp)≡(2a⋅(2a)(p−5)/4)2(modp)≡((2a)(p−1)/4)2(modp)≡(2a)p−12(modp)≡−1(modp)\begin{aligned} \mathrm{i}^2&\equiv\left(2ab^2\right)^2&\pmod p\\ &\equiv \left(2a\cdot \left(2a\right)^{(p-5)/4}\right)^2&\pmod p\\ &\equiv \left(\left(2a\right)^{(p-1)/4}\right)^2&\pmod p\\ &\equiv \left(2a\right)^{\frac{p-1}{2}}&\pmod p\\ &\equiv -1&\pmod p \end{aligned}

Then

(ab(i−1))2≡a2⋅(2a)(p−5)/4⋅(−2i)(modp)≡a⋅(−i)⋅(2a)(p−1)/4(modp)≡a(modp)\begin{aligned} \left(ab(\mathrm{i}-1)\right)^2&\equiv a^2\cdot \left(2a\right)^{(p-5)/4}\cdot (-2\mathrm{i})&\pmod p\\ &\equiv a\cdot (-\mathrm{i})\cdot \left(2a\right)^{(p-1)/4}&\pmod p\\ &\equiv a&\pmod p \end{aligned}

Cipolla's algorithm

Cipolla's algorithm solves the congruence y2≡a(modp)y^2\equiv a\pmod p, where pp is an odd prime and aa is a quadratic residue.

This section considers operations in Fp[x]/(x2−g)\mathbf{F}_p\lbrack x\rbrack /(x^2-g), where g∈Fpg \in \mathbf{F}_p.

Computation

Readers unfamiliar with polynomial rings can simply understand that every element of this set has the form a0+a1xa_0+a_1x with a0,a1∈Fpa_0,a_1\in\mathbf F_p, and follows these rules:

(a0+a1x)+(b0+b1x)≡(a0+b0)+(a1+b1)x(mod(x2−g))(a0+a1x)(b0+b1x)≡(a0b0+a1b1g)+(a1b0+a0b1)x(mod(x2−g))\begin{aligned} (a_0+a_1x)+(b_0+b_1x) &\equiv (a_0+b_0)+(a_1+b_1)x &\pmod{(x^2-g)}\\ (a_0+a_1x)(b_0+b_1x) &\equiv (a_0b_0+a_1b_1g)+(a_1b_0+a_0b_1)x &\pmod{(x^2-g)} \end{aligned}

Note that xx here is not a specific number, but a formal symbol representing the indeterminate in a polynomial. A key point in the computation is using x2≡g(mod(x2−g))x^2 \equiv g \pmod{(x^2-g)} to convert quadratic terms into linear terms and constants. In addition, all integer operations must be taken modulo pp.

For more information about this structure, see pages on polynomials and field theory.

The first step of the algorithm is to find an rr such that r2−ar^2-a is a quadratic non-residue. Of course, such an rr cannot be found when a≡0(modp)a \equiv 0 \pmod p, which requires a special case. We discuss only the case a≢0(modp)a \not\equiv 0 \pmod p below. We can choose a random rr and test it; in expectation, 22 attempts suffice. Then (r−x)p+12 mod (x2−(r2−a))(r-x)^{\frac{p+1}{2}}\bmod (x^2-(r^2-a)) is a solution, which can be evaluated by fast exponentiation.

Why are only two attempts needed in expectation?

Consider the case where r2−ar^2-a is a quadratic residue. Then there exists an xx such that r2−a≡x2(modp)r^2-a \equiv x^2 \pmod p, and rearranging gives (r+x)(r−x)≡a(modp)(r+x)(r-x) \equiv a \pmod p. It is easy to see that every (r+x)∈[1,p−1](r+x) \in [1, p-1] corresponds bijectively to a solution (r,x)(r,x), so there are p−1p-1 solutions to the original equation. We distinguish the cases x≡0x \equiv 0 and x≢0x \not\equiv 0. When x≡0x \equiv 0, since aa is a quadratic residue, there are 22 possible values of rr. When x≢0x \not\equiv 0, there are p−1−2p-1-2 cases, and each rr corresponds to two of them, giving p−32\dfrac{p-3}{2} possible values of rr. In total, there are 2+p−32=p+122+\dfrac{p-3}{2}=\dfrac{p+1}{2} cases in which r2−ar^2-a is a quadratic residue. Thus the probability of obtaining a quadratic non-residue on each random attempt is p−12p\dfrac{p-1}{2p}, and the expected number of attempts is 2pp−1≈2\dfrac{2p}{p-1} \approx 2.

Proof

For convenience, first let f(x)=x2−(r2−a)∈Fp[x]f(x)=x^2-(r^2-a)\in\mathbf{F}_p\lbrack x\rbrack.

We need to prove that (r−x)p+12 mod f(x)(r-x)^{\frac{p+1}{2}} \bmod f(x) is a solution of the original equation and belongs to Fp\mathbf{F}_p. First consider the former, namely prove that (r−x)p+1≡a(modf(x))(r-x)^{p+1}\equiv a\pmod {f(x)}. We first need two lemmas:

Lemma 1: xp≡−x(modf(x))x^p \equiv -x \pmod {f(x)}

Proof:

xp=x(x2)p−12≡x(r2−a)p−12(modf(x))(∵x2≡r2−a(modf(x)))≡−x(modf(x))(∵r2−a is quadratic non-residue)\begin{aligned} x^p&= x(x^2)^{\frac{p-1}{2}}\\ &\equiv x(r^2-a)^{\frac{p-1}{2}}&\pmod{f(x)}&\quad (\because{x^2\equiv r^2-a\pmod{f(x)}})\\ &\equiv -x&\pmod{f(x)}&\quad (\because{r^2-a}\text{ is quadratic non-residue}) \end{aligned}

Lemma 2: (a+b)p≡ap+bp(modp)(a+b)^p \equiv a^p+b^p \pmod p

By the binomial theorem, it is easy to see that apart from the first and last terms, the pp in the numerator cancels, leaving only ap+bpa^p+b^p.

(a+b)p=∑i=0p(pi)aibp−i=∑i=0pp!i!(p−i)!aibp−i≡ap+bp(modp)\begin{aligned} (a+b)^p&=\sum_{i=0}^p\binom{p}{i}a^ib^{p-i}\\ &=\sum_{i=0}^p\frac{p!}{i!(p-i)!}a^ib^{p-i}\\ &\equiv a^p+b^p\pmod p \end{aligned}

With these two lemmas, let us prove the original expression:

(r−x)p+1=(r−x)p(r−x)≡(rp−xp)(r−x)(modf(x))≡(r+x)(r−x)(modf(x))=r2−x2≡r2−(r2−a)(modf(x))=a\begin{aligned} (r-x)^{p+1} &= (r-x)^p(r-x)\\ &\equiv (r^p-x^p)(r-x)&\pmod{f(x)}\\ &\equiv (r+x)(r-x)&\pmod{f(x)}\\ &= r^2-x^2\\ &\equiv r^2-(r^2-a)&\pmod{f(x)}\\ &= a\\ \end{aligned}

We now prove by contradiction that the solution we obtain belongs to Fp\mathbf{F}_p, that is, its coefficient of xx is 00.

Suppose there exists an (a0+a1x)2≡a(modf(x))(a_0+a_1x)^2 \equiv a \pmod {f(x)} with a1≢0(modp)a_1 \not\equiv 0 \pmod p, namely a02+2a0a1x+a12x2≡a(modf(x))a_0^2+2a_0a_1x+a_1^2x^2 \equiv a \pmod {f(x)}. Moving terms and simplifying gives:

a02+a12(r2−a)−a≡−2a0a1x(modf(x))a_0^2+a_1^2(r^2-a)-a \equiv -2a_0a_1x \pmod {f(x)}

The coefficient of xx on the left is 00, so the coefficient of xx on the right is also 00, meaning a0a1≡0(modp)a_0a_1 \equiv 0 \pmod p. Since we assumed a1≢0(modp)a_1 \not\equiv 0 \pmod p, we must have a0≡0(modp)a_0 \equiv 0 \pmod p, and hence (a1x)2≡a(modf(x))(a_1x)^2 \equiv a \pmod {f(x)}, that is, r2−a≡aa1−2(modp)r^2-a \equiv aa_1^{-2} \pmod p.

Since both aa and a1−2a_1^{-2} are quadratic residues, complete multiplicativity of the Legendre symbol shows that aa1−2aa_1^{-2} is also a quadratic residue, contradicting the fact that r2−ar^2-a is a quadratic non-residue. Thus the original expression has no solution whose coefficient of xx is not 00, and the xx coefficient of the solution we obtain must also be 00.

Template problem Luogu P5491【Template】Quadratic residue
#include <iostream>
#include <random>

long long p, v;  // modulus and the value of r^2 - a

struct Poly {
  long long a, b;

  Poly(long long _a = 0, long long _b = 0) : a(_a), b(_b) {}
};

Poly operator*(const Poly& x, const Poly& y) {
  // Overload multiplication; see the discussion of the operation above
  return Poly((x.a * y.a + v * (x.b * y.b % p)) % p,
              (x.a * y.b + x.b * y.a) % p);
}

// Fast exponentiation for polynomials, used to compute the answer
Poly modpow(Poly a, long long b) {
  Poly res(1, 0);
  while (b) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

// Ordinary fast exponentiation, used to test for quadratic non-residues
long long modpow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return res;
}

// Used to generate random numbers
std::mt19937 rng(std::random_device{}());

long long cipolla(long long a, long long _p) {
  p = _p;
  if (a == 0)
    return 0;  // Handle the case 0 separately
  else if (modpow(a, (p - 1) / 2) == p - 1)
    return -1;  // A quadratic non-residue; there is no solution
  else {
    // Randomly choose r such that r^2 - a is a quadratic non-residue
    long long r;
    for (r = rng() % p;; r = rng() % p) {
      if (modpow((r * r - a + p) % p, (p - 1) / 2) == p - 1) break;
    }
    v = (r * r - a + p) % p;
    return modpow(Poly(r, 1), (p + 1) / 2).a;  // Compute the result from the conclusion
  }
}

int main() {
  int t, a, p;
  std::cin >> t;
  while (t--) {
    std::cin >> a >> p;
    int ans = cipolla(a, p);
    if (ans == -1)
      std::cout << "Hola!" << std::endl;
    else if (ans == 0)
      std::cout << 0 << std::endl;
    else {
      // The opposite number is the other solution
      int ans2 = (p - ans) % p;
      if (ans2 < ans) std::swap(ans, ans2);
      std::cout << ans << " " << ans2 << std::endl;
    }
  }
  return 0;
}

Bostan–Mori algorithm

This algorithm is based on Cipolla's algorithm. We convert the problem into a constant-coefficient homogeneous linear recurrence and then apply the Bostan–Mori algorithm. Consider another common description of Cipolla's algorithm: b=x(p+1)/2 mod (x2−tx+a)b=x^{\left(p+1\right)/2}\bmod{\left(x^2-tx+a\right)} is a solution satisfying b2≡a(modp)b^2\equiv a\pmod{p}4, where x2−tx+a∈Fp[x]x^2-tx+a\in \mathbf{F}_p\lbrack x\rbrack is an irreducible polynomial. The coefficient tt is also chosen randomly. We omit the proof. From the algorithm in the paper by Bostan and Mori5, we can see that the problem can be transformed into finding one coefficient of the multiplicative inverse of a formal power series:

b=[x(p+1)/2]11−tx+ax2b=\left\lbrack x^{(p+1)/2}\right\rbrack\dfrac{1}{1-tx+ax^2}

and

[xn]k0+k1x1+k2x+k3x2={[x(n−1)/2]k1−k0k2+k1k3x1+(2k3−k22)x+k32x2,if n mod 2=1[xn/2]k0+(k0k3−k1k2)x1+(2k3−k22)x+k32x2,else if n≠0\left\lbrack x^n\right\rbrack\dfrac{k_0+k_1x}{1+k_2x+k_3x^2}= \begin{cases} \left\lbrack x^{(n-1)/2}\right\rbrack\dfrac{k_1-k_0k_2+k_1k_3x}{1+(2k_3-k_2^2)x+k_3^2x^2},&\text{if }n\bmod 2=1\\ \left\lbrack x^{n/2}\right\rbrack\dfrac{k_0+(k_0k_3-k_1k_2)x}{1+(2k_3-k_2^2)x+k_3^2x^2},&\text{else if }n\neq 0 \end{cases}

When n=0n=0, clearly [x0]k0+k1x1+k2x+k3x2=k0\left\lbrack x^0\right\rbrack\dfrac{k_0+k_1x}{1+k_2x+k_3x^2}=k_0. This algorithm uses fewer multiplications than Cipolla's algorithm. Other algorithms with fewer multiplications can be found in Müller's article6.

Legendre's algorithm

For the congruence x2≡a(modp)x^2\equiv a\pmod p, where pp is an odd prime and aa is a quadratic residue, Legendre's algorithm can be described as finding an rr such that r2−ar^2-a is a quadratic non-residue, and letting a0+a1x=(r−x)p−12 mod (x2−a)a_0+a_1x=(r-x)^{\frac{p-1}{2}}\bmod (x^2-a). Then a0≡0(modp)a_0\equiv 0\pmod p and a1−2≡a(modp)a_1^{-2}\equiv a\pmod p.

Proof

Choose bb such that b2≡a(modp)b^2\equiv a\pmod p. Then (r−b)(r+b)=r2−a(r-b)(r+b)=r^2-a is a quadratic non-residue, so

(r−b)p−12(r+b)p−12≡−1(modp)(r-b)^{\frac{p-1}{2}}(r+b)^{\frac{p-1}{2}}\equiv -1\pmod p

There is a ring homomorphism

ϕ:Fp[x]/(x2−a)→Fp×Fpx↦(b,−b)\begin{aligned} \phi:\mathbf{F}_p\lbrack x\rbrack/(x^2-a)&\to \mathbf{F}_p\times \mathbf{F}_p\\ x&\mapsto (b,-b) \end{aligned}

Thus

(a0+a1b,a0−a1b)=ϕ(a0+a1x)=ϕ(r−x)p−12=((r−b)p−12,(r+b)p−12)=(±1,∓1)\begin{aligned} (a_0+a_1b,a_0-a_1b)&=\phi(a_0+a_1x)\\ &=\phi(r-x)^{\frac{p-1}{2}}\\ &=((r-b)^{\frac{p-1}{2}},(r+b)^{\frac{p-1}{2}})\\ &=(\pm 1,\mp 1) \end{aligned}

Therefore 2a0=(±1)+(∓1)=02a_0=(\pm 1)+(\mp 1)=0 and 2a1b=(±1)−(∓1)=±22a_1b=(\pm 1)-(\mp 1)=\pm 2.

Tonelli–Shanks algorithm

The Tonelli–Shanks algorithm solves the congruence x2≡a(modp)x^2\equiv a\pmod p using discrete logarithms7, where pp is an odd prime and aa is a quadratic residue modulo pp.

Let p−1=m2np-1=m2^n, where mm is odd. Again use a random method to find r∈Fpr\in\mathbf{F}_p such that rr is a quadratic non-residue. Let g≡rm(modp)g\equiv r^m\pmod p and b≡a(m−1)/2(modp)b\equiv a^{(m-1)/2}\pmod p. Then there exists an integer e∈{0,1,2,…,2n−1}e\in\lbrace 0,1,2,\dots ,2^n-1\rbrace such that ab2≡ge(modp)ab^2\equiv g^e\pmod p. If aa is a quadratic residue, then ee is even and (abg−e/2)2≡a(modp)\left(abg^{-e/2}\right)^2\equiv a\pmod p.

Proof

By Fermat's little theorem,

g2n≡rm2n=rp−1≡1(modp).g^{2^n} \equiv r^{m2^n} = r^{p-1} \equiv 1 \pmod p.

Since rr is a quadratic non-residue,

g2n−1≡rm2n−1=rp−12≡−1(modp).g^{2^{n-1}} \equiv r^{m2^{n-1}} = r^{\frac{p-1}{2}} \equiv -1 \pmod p.

Therefore, the order of gg modulo pp is 2n2^n. Since ab2≡am(modp)ab^2\equiv a^m\pmod p is a solution of x2n≡1(modp)x^{2^n}\equiv 1\pmod p, ama^m is a power of gg. Let am≡ge(modp)a^m\equiv g^e\pmod p.

Since aa is a quadratic residue,

ge2n−1≡am2n−1=ap−12≡1(modp).g^{e2^{n-1}} \equiv a^{m2^{n-1}} = a^{\frac{p-1}{2}} \equiv 1 \pmod p.

By the properties of orders, 2n∣e2n−12^n\mid e2^{n-1}, so ee is even. Therefore, abg−e/2 mod pabg^{-e/2}\bmod p is well-defined, and

(abg−e/2)2=a2b2g−e≡am+1g−e≡a(modp).\left(abg^{-e/2}\right)^2 = a^2b^2g^{-e} \equiv a^{m+1}g^{-e} \equiv a \pmod p.

The remaining problem is how to compute ee. Tonelli and Shanks proposed determining one binary digit of ee at a time. Write ee in binary as e=e0+2e1+4e2+⋯e=e_0+2e_1+4e_2+\cdots, where ek∈{0,1}e_k\in\lbrace 0,1\rbrace. Since aa is a quadratic residue, e0=0e_0=0 initially. We then determine each eke_k one bit at a time using the following property:

(geg−(e mod 2k))2n−1−k≡g2n−1⋅ek≡{1(modp),if ek=0−1(modp),if ek=1\left(g^eg^{-(e\bmod 2^k)}\right)^{2^{n-1-k}}\equiv g^{2^{n-1}\cdot e_k}\equiv \begin{cases} 1\pmod p,&\text{if }e_k=0\\ -1\pmod p,&\text{if }e_k=1 \end{cases}

Here ge≡ab2(modp)g^e\equiv ab^2\pmod p is known, and e mod 2ke\bmod 2^k can be calculated from the previous digits e0,e1,⋯ ,ek−1e_0,e_1,\cdots,e_{k-1}. In an implementation, it is enough to maintain the product geg−(e mod 2k) mod pg^eg^{-(e\bmod 2^k)}\bmod p directly.

Exercises

References and notes

  1. Quadratic residue - Wikipedia
  2. Euler's criterion - Wikipedia

Footnotes

  1. Proofs of quadratic reciprocity - Wikipedia ↩

  2. Carl Friedrich Gauss. Untersuchungen über höhere Arithmetik, 1965. Page 458-462. ↩

  3. Kobi Kremnizer.Lectures in number theory 2022. Proposition 4.3. ↩

  4. A. Menezes, P. van Oorschot and S. Vanstone. Handbook of Applied Cryptography, 1996. ↩

  5. Alin Bostan, Ryuhei Mori. A Simple and Fast Algorithm for Computing the N-th Term of a Linearly Recurrent Sequence. Available at https://arxiv.org/abs/2008.08822. ↩

  6. S. Müller, On the computation of square roots in finite fields, Design, Codes and Cryptography, Vol.31, pp. 301-312, 2004. ↩

  7. Daniel. J. Bernstein. Faster Square Roots in Annoying Finite Fields. ↩

This content comes from OI Wiki: Quadratic residues. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.