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 a and p satisfy (a,p)=1. If there exists an integer x such that
x2≡a(modp),
then a is called a quadratic residue modulo p; otherwise, a is called a quadratic non-residue modulo p. When the modulus p 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 p and an integer a satisfying (a,p)=1,
By Theorem 5 of congruence equations, a is a quadratic residue modulo p if and only if a(p−1)/2≡1(modp). Consequently, a is a quadratic non-residue modulo p if and only if a(p−1)/2≡−1(modp).
Based on Euler's criterion, we obtain the following corollary:
Number of quadratic residues
For an odd prime p, there are 2p−1 quadratic residues and quadratic non-residues modulo p.
Proof
By Euler's criterion, consider a2p−1≡1(modp).
Since 2p−1∣(p−1), Theorem 6 of congruence equations shows that a2p−1≡1(modp) has 2p−1 solutions. Therefore, modulo p, there are 2p−1 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 primep and an integer a, define the Legendre symbol by:
Based on these properties, if (qp) can be computed for every pair of odd primes p and q, then we can compute the Legendre symbol for every valid case. We now have an elegant theorem that cleverly establishes a connection between (qp) and (pq), allowing us to complete the computation using an idea similar to the Euclidean algorithm.
Quadratic reciprocity
Quadratic reciprocity
Let p and q be two distinct odd primes. Then
(qp)(pq)=(−1)2p−12q−1.
There are many proofs1. One proof is based on the following lemma2:
Gauss's lemma
Let p be an odd prime and (n,p)=1. For each integer k(1≤k≤(p−1)/2), let rk=nkmodp, and let A={rk:rk<p/2} and B={rk:rk>p/2}. Then
(pn)=(−1)∣B∣.Proof
Let λ=∣A∣ and μ=∣B∣. Clearly λ+μ=(p−1)/2, so
n2p−1(2p−1)!=k=1∏2p−1nk≡a∈A∏ab∈B∏b(modp).
For any element b in B, we have 2p<b<p, so 0<p−b<2p. Moreover, for any b in B, p−b∈/A: otherwise, if A,B contained elements a,b with a=p−b, then there would be integers 0<k1,k2<(p−1)/2 such that a=nk1, b=nk2, and p∣n(k1+k2). Since (n,p)=1, this would imply p∣(k1+k2). But 0<k1+k2<p, a contradiction. Therefore
By the above corollary, proving quadratic reciprocity only requires verifying
2p−12q−1=k=1∑2p−1⌊pqk⌋+k=1∑2q−1⌊qpk⌋.
Consider the set S of points (px,qy), where 1≤x≤2q−1,1≤y≤2p−1. Divide it into two parts according to whether px is larger or smaller than qy (clearly px=qy), and verify the sizes of the three resulting sets.
Quadratic reciprocity can be used not only to determine whether n is a quadratic residue modulo p, but also to determine the structure of the moduli for which n is a quadratic residue.
Example
Odd primes p for which 5 is a quadratic residue satisfy p≡±1(mod5).
Odd primes p for which −3 is a quadratic residue satisfy p≡1(mod3).
Odd primes p for which both −2 and 3 are quadratic residues satisfy p≡11(mod24).
We can also prove conclusions such as “there are infinitely many primes of the form 4k+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 integerm=p1α1…pkαk and an integer a, where p1,…,pk are primes and α1,…,αk are positive integers, define the Jacobi symbol as follows:
(ma):=i=1∏k(pia)αi.
Here, the (pia) on the right-hand side is the Legendre symbol. For an integer a, we also define (1a)=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 mis not an odd prime, the value of (ma) is unrelated to whether a is a quadratic residue modulo m. If (ma)=−1, however, then at least one (in fact, an odd number of) prime factors p of m has a as a quadratic non-residue modulo p, so a is a quadratic non-residue modulo m in this case.
We can further generalize the modulus to be any integer by defining (−1a), (0a), and (2a); 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), where p is an odd prime and a is a quadratic residue, when pmod4=3 there is a simpler solution. Consider:
(a(p+1)/4)2≡a(p+1)/2≡xp+1≡(x2)(xp−1)≡x2(modp)(modp)(modp)(modp)(∵Fermat’s little theorem)
Thus a(p+1)/4modp is a solution.
Atkin's algorithm
Still consider the congruence above. Now pmod8=5. Let b≡(2a)(p−5)/8(modp) and i≡2ab2(modp). Then i2≡−1(modp), and ab(i−1)modp is a solution.
Cipolla's algorithm solves the congruence y2≡a(modp), where p is an odd prime and a is a quadratic residue.
This section considers operations in Fp[x]/(x2−g), where g∈Fp.
Computation
Readers unfamiliar with polynomial rings can simply understand that every element of this set has the form a0+a1x with a0,a1∈Fp, and follows these rules:
Note that x 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)) to convert quadratic terms into linear terms and constants. In addition, all integer operations must be taken modulo p.
The first step of the algorithm is to find an r such that r2−a is a quadratic non-residue. Of course, such an r cannot be found when a≡0(modp), which requires a special case. We discuss only the case a≡0(modp) below. We can choose a random r and test it; in expectation, 2 attempts suffice. Then (r−x)2p+1mod(x2−(r2−a)) is a solution, which can be evaluated by fast exponentiation.
Why are only two attempts needed in expectation?
Consider the case where r2−a is a quadratic residue. Then there exists an x such that r2−a≡x2(modp), and rearranging gives (r+x)(r−x)≡a(modp). It is easy to see that every (r+x)∈[1,p−1] corresponds bijectively to a solution (r,x), so there are p−1 solutions to the original equation. We distinguish the cases x≡0 and x≡0. When x≡0, since a is a quadratic residue, there are 2 possible values of r. When x≡0, there are p−1−2 cases, and each r corresponds to two of them, giving 2p−3 possible values of r. In total, there are 2+2p−3=2p+1 cases in which r2−a is a quadratic residue. Thus the probability of obtaining a quadratic non-residue on each random attempt is 2pp−1, and the expected number of attempts is p−12p≈2.
Proof
For convenience, first let f(x)=x2−(r2−a)∈Fp[x].
We need to prove that (r−x)2p+1modf(x) is a solution of the original equation and belongs to Fp. First consider the former, namely prove that (r−x)p+1≡a(modf(x)). We first need two lemmas:
Lemma 1:xp≡−x(modf(x))
Proof:
xp=x(x2)2p−1≡x(r2−a)2p−1≡−x(modf(x))(modf(x))(∵x2≡r2−a(modf(x)))(∵r2−a is quadratic non-residue)
Lemma 2:(a+b)p≡ap+bp(modp)
By the binomial theorem, it is easy to see that apart from the first and last terms, the p in the numerator cancels, leaving only ap+bp.
We now prove by contradiction that the solution we obtain belongs to Fp, that is, its coefficient of x is 0.
Suppose there exists an (a0+a1x)2≡a(modf(x)) with a1≡0(modp), namely a02+2a0a1x+a12x2≡a(modf(x)). Moving terms and simplifying gives:
a02+a12(r2−a)−a≡−2a0a1x(modf(x))
The coefficient of x on the left is 0, so the coefficient of x on the right is also 0, meaning a0a1≡0(modp). Since we assumed a1≡0(modp), we must have a0≡0(modp), and hence (a1x)2≡a(modf(x)), that is, r2−a≡aa1−2(modp).
Since both a and a1−2 are quadratic residues, complete multiplicativity of the Legendre symbol shows that aa1−2 is also a quadratic residue, contradicting the fact that r2−a is a quadratic non-residue. Thus the original expression has no solution whose coefficient of x is not 0, and the x coefficient of the solution we obtain must also be 0.
Template problem Luogu P5491【Template】Quadratic residue
#include<iostream>#include<random>longlong p, v; // modulus and the value of r^2 - astructPoly {
longlong a, b;
Poly(longlong _a = 0, longlong _b = 0) : a(_a), b(_b) {}
};
Poly operator*(const Poly& x, const Poly& y) {
// Overload multiplication; see the discussion of the operation abovereturnPoly((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 answerPoly modpow(Poly a, longlong 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-residueslonglongmodpow(longlong a, longlong b){
longlong res = 1;
while (b) {
if (b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
// Used to generate random numbersstd::mt19937 rng(std::random_device{}());
longlongcipolla(longlong a, longlong _p){
p = _p;
if (a == 0)
return0; // Handle the case 0 separatelyelseif (modpow(a, (p - 1) / 2) == p - 1)
return-1; // A quadratic non-residue; there is no solutionelse {
// Randomly choose r such that r^2 - a is a quadratic non-residuelonglong 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;
returnmodpow(Poly(r, 1), (p + 1) / 2).a; // Compute the result from the conclusion
}
}
intmain(){
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;
elseif (ans == 0)
std::cout << 0 << std::endl;
else {
// The opposite number is the other solutionint ans2 = (p - ans) % p;
if (ans2 < ans) std::swap(ans, ans2);
std::cout << ans << " " << ans2 << std::endl;
}
}
return0;
}
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)/2mod(x2−tx+a) is a solution satisfying b2≡a(modp)4, where x2−tx+a∈Fp[x] is an irreducible polynomial. The coefficient t 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]1−tx+ax21
and
[xn]1+k2x+k3x2k0+k1x=⎩⎨⎧[x(n−1)/2]1+(2k3−k22)x+k32x2k1−k0k2+k1k3x,[xn/2]1+(2k3−k22)x+k32x2k0+(k0k3−k1k2)x,if nmod2=1else if n=0
When n=0, clearly [x0]1+k2x+k3x2k0+k1x=k0. 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), where p is an odd prime and a is a quadratic residue, Legendre's algorithm can be described as finding an r such that r2−a is a quadratic non-residue, and letting a0+a1x=(r−x)2p−1mod(x2−a). Then a0≡0(modp) and a1−2≡a(modp).
Proof
Choose b such that b2≡a(modp). Then (r−b)(r+b)=r2−a is a quadratic non-residue, so
Therefore 2a0=(±1)+(∓1)=0 and 2a1b=(±1)−(∓1)=±2.
Tonelli–Shanks algorithm
The Tonelli–Shanks algorithm solves the congruence x2≡a(modp) using discrete logarithms7, where p is an odd prime and a is a quadratic residue modulo p.
Let p−1=m2n, where m is odd. Again use a random method to find r∈Fp such that r is a quadratic non-residue. Let g≡rm(modp) and b≡a(m−1)/2(modp). Then there exists an integer e∈{0,1,2,…,2n−1} such that ab2≡ge(modp). If a is a quadratic residue, then e is even and (abg−e/2)2≡a(modp).
Proof
By Fermat's little theorem,
g2n≡rm2n=rp−1≡1(modp).
Since r is a quadratic non-residue,
g2n−1≡rm2n−1=r2p−1≡−1(modp).
Therefore, the order of g modulo p is 2n. Since ab2≡am(modp) is a solution of x2n≡1(modp), am is a power of g. Let am≡ge(modp).
Since a is a quadratic residue,
ge2n−1≡am2n−1=a2p−1≡1(modp).
By the properties of orders, 2n∣e2n−1, so e is even. Therefore, abg−e/2modp is well-defined, and
(abg−e/2)2=a2b2g−e≡am+1g−e≡a(modp).
The remaining problem is how to compute e. Tonelli and Shanks proposed determining one binary digit of e at a time. Write e in binary as e=e0+2e1+4e2+⋯, where ek∈{0,1}. Since a is a quadratic residue, e0=0 initially. We then determine each ek one bit at a time using the following property:
Here ge≡ab2(modp) is known, and emod2k can be calculated from the previous digits e0,e1,⋯,ek−1. In an implementation, it is enough to maintain the product geg−(emod2k)modp directly.
A. Menezes, P. van Oorschot and S. Vanstone. Handbook of Applied Cryptography, 1996. ↩
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. ↩
S. Müller, On the computation of square roots in finite fields, Design, Codes and Cryptography, Vol.31, pp. 301-312, 2004. ↩
Daniel. J. Bernstein. Faster Square Roots in Annoying Finite Fields. ↩