Modular Square Root
Introduction
Suppose we want to find the square root x2≡amodp, where p is an odd prime. That is, we seek some x∈Zp such that the square of x is congruent to a modulo p.
If such an x exists, we say that a is a quadratic residue modulo p; otherwise, it is a quadratic nonresidue. The trivial case a=0 is typically excluded when enumerating quadratic residues, since 0 has a unique square root (itself), and we are often more interested in the structure of the nonzero squares in Zp∗.
For example, when p=7, the quadratic residues modulo 7 are:
Thus, the quadratic residues modulo 7 are {1,2,4}, and the quadratic nonresidues are {3,5,6}.
Computing square roots modulo a prime or composite number is a fundamental operation in number theory with important applications in cryptography and computational mathematics. One example is when constructing elliptic curves or decompressing elliptic curve points from compressed form, one must compute square roots modulo a prime field to recover the y-coordinate from the x-coordinate.
To efficiently determine whether a square root exists and to compute it when it does, number theorists use symbolic tools such as the Legendre symbol, Jacobi symbol, and Euler’s criterion, which we now briefly introduce.
Legendre Symbol
The Legendre symbol (pa) is a notation that encodes whether a is a quadratic residue modulo an odd prime p. It is defined as:
The Legendre symbol is a multiplicative function, and is a key component in quadratic reciprocity and in algorithms like the Tonelli–Shanks square root algorithm.
Euler’s Criterion
Euler’s criterion provides a direct method to evaluate the Legendre symbol using modular exponentiation:
This gives a simple way to test whether a is a square modulo p:
If a2p−1≡1modp, then a is a quadratic residue.
If a2p−1≡−1modp, then a is a nonresidue.
Generalization to Extension Fields
Euler’s criterion generalizes naturally to extension fields Fq, where q=pm, as follows:
That is, an element a∈Fq∗ is a quadratic residue if and only if:
Jacobi Symbol
The Jacobi symbol (na) is a generalization of the Legendre symbol to odd composite moduli n, where n=p1e1p2e2⋯pkek is the product of odd primes. It is defined as:
Note that, unlike the Legendre symbol, the Jacobi symbol does not determine whether a is a square modulo n when n is composite. For example, it is possible that (na)=1 even though a is not a square mod n. Nonetheless, the Jacobi symbol is widely used in square root algorithms in composite fields.
Square Root Algorithm Branch

The diagram above is taken from https://eprint.iacr.org/2012/685.pdf and illustrates how different square root algorithms can be selected based on the modulus and the degree of the extension field. In the following subsections, we introduce several of these algorithms in more detail.
Written by Ryan Kim of Fractalyze
Last updated