Intuition, Vocabulary and Foundations
Squaring modulo $m$ folds integers into residue classes. Modulo $7$, the nonzero squares are $1$, $2$, and $4$, so $x^2\equiv2\pmod7$ has roots $3$ and $4$, while $x^2\equiv3\pmod7$ has none. For an odd prime $p$, Euler's criterion states
$$a^{(p-1)/2}\equiv1\pmod p$$
for a nonzero quadratic residue and $-1$ for a nonresidue. The Legendre symbol records the three outcomes $-1$, $0$, and $1$. The Jacobi symbol extends the notation multiplicatively to odd composite denominators, but a Jacobi value of $1$ does not guarantee a root: $\left(\frac2{15}\right)=1$, yet $2$ is not a square modulo $15$.
Euler's criterion and Tonelli-Shanks require an odd prime modulus. For composites, solve every prime-power component, combine with CRT, and verify each candidate by checking $r^2\bmod m=a\bmod m$.
| Concept | Definition | Example | Boundary | Question answered |
|---|---|---|---|---|
| Quadratic residue | $a\equiv x^2\pmod m$ | $2\equiv3^2\pmod7$ | $3$ is a nonresidue modulo $7$ | Does a root exist? |
| Legendre symbol | Prime-modulus status in $\{-1,0,1\}$ | $\left(\frac27\right)=1$ | Denominator must be an odd prime | Is $a$ a prime-field square? |
| Jacobi symbol | Product over odd prime factors | $\left(\frac2{15}\right)=1$ | Value $1$ is inconclusive | Can a composite candidate be rejected? |
| Tonelli-Shanks | Iteratively reduces a power-of-two order | Roots of $10$ modulo $13$ are $6,7$ | Prime-only algorithm | How is a root constructed? |
| CRT combination | Combines roots for coprime factors | Four roots of $4$ modulo $15$ | Prime powers require lifting first | How are local roots combined? |
Step-by-Step Derivations and Algorithms
For an odd prime $p$, normalize $a$. Return $0$ when $a=0$ and reject when Euler's criterion gives $-1$. If $p\equiv3\pmod4$, compute $r=a^{(p+1)/4}$. Otherwise factor $p-1=q2^s$ with odd $q$, choose a nonresidue $z$, and initialize
$$c=z^q,\qquad r=a^{(q+1)/2},\qquad t=a^q\pmod p.$$
Tonelli-Shanks maintains $r^2\equiv at\pmod p$. Each iteration finds the least $i$ with $t^{2^i}=1$ and updates by a power of $c$, strictly decreasing the order bound until $t=1$. Cipolla instead exponentiates in a quadratic extension field. For a factored composite, solve each prime-power modulus and enumerate CRT combinations of local roots.
flowchart TD
A["Normalize a modulo m"] --> B{"Modulus domain?"}
B -->|"Odd prime"| C["Euler criterion"]
C --> D{"Residue?"}
D -->|"No"| E["No roots"]
D -->|"Yes"| F["Shortcut or Tonelli-Shanks"]
B -->|"Prime power"| G["Valuation and lifting"]
B -->|"Factored composite"| H["Solve local factors"]
G --> H
H --> I["CRT combinations"]
F --> J["Verify roots"]
I --> J
Tonelli-Shanks preserves $r^2\equiv at\pmod p$ while decreasing the exponent bound on the order of $t$; reaching $t=1$ proves correctness. Composite solving is output-sensitive because independent local sign choices can produce exponentially many roots.
Implementation, Complexity and Engineering
This reference supports odd primes and square-free products of supplied distinct odd primes. It rejects repeated factors rather than pretending that prime-power lifting is identical to the prime case, and it verifies all results.
| Method | Domain | Time profile | Output | Failure mode | Mitigation |
|---|---|---|---|---|---|
| Exhaustive search | Small positive modulus | $O(m)$ | All roots | Too slow for large $m$ | Use as a test oracle |
| Euler criterion | Odd prime | One exponentiation | Existence only | Composite input | Validate primality |
| Jacobi symbol | Positive odd modulus | $O(\log n)$ | Filter only | Treating $1$ as proof | Solve factors explicitly |
| Tonelli-Shanks | Odd prime | Polylogarithmic modular work | One root, then negate | Broken order update | Verify the candidate |
| Cipolla | Odd prime | $O(\log p)$ extension multiplications | One root, then negate | Faulty pair arithmetic | Unit-test field operations |
| Prime-power lifting | Prime power | Valuation-dependent | Variable multiplicity | Assuming unique lifting | Separate divisible cases |
| CRT enumeration | Coprime solved factors | Output-sensitive | All combined roots | Duplicate results | Normalize, verify, deduplicate |
Applications, Case Studies and Exercises
Modular roots occur in computational number theory and cryptographic constructions. A reliable API must expose whether its modulus is prime, a known prime power, or an already factored composite.
Apply Quadratic Residues & Modular Roots
Prime case: Solve $x^2\equiv10\pmod{13}$. Euler's criterion confirms existence; Tonelli-Shanks returns $6$, and the paired root is $7$. Verify both squares.
Composite case: Solve $x^2\equiv4\pmod{15}$. Local roots are $1,2$ modulo $3$ and $2,3$ modulo $5$. CRT gives $2,7,8,13$.
Hands-on challenge: Compare the solver with exhaustive search for every odd modulus up to $500$. Test zero, negative inputs, nonresidues, repeated factors, and mislabeled composites. Success requires sorted unique outputs and exact verification.
Exercises: list residues modulo $11$; evaluate $\left(\frac5{11}\right)$; trace Tonelli-Shanks for $5$ modulo $41$; explain why Jacobi value $1$ is inconclusive; implement Cipolla; extend the solver to odd prime powers.
Use Legendre or Euler for prime-field existence, Tonelli-Shanks or Cipolla for construction, valuation-aware lifting for prime powers, and CRT for factored composites. Never use a Jacobi value of $1$ as a composite existence proof, and verify every returned root.
Next in the Series
In Groups, Primitive Roots & Discrete Logarithms, we'll explore Reason about modular operations through finite-group structure..