← LibraryComputing Modular Square Roots: Prime ModulusEngineering · MathematicsLesson 143/385← PrevNext →
ArticlePublished 7 Aug 20263 min readBy Kevin Jogin

Engineering  /  Mathematics  — Quadratic Residues

Computing Modular Square Roots: Prime Modulus

Extracting square roots modulo a prime, the easy case for p congruent to 3 mod 4, and the randomised algorithm in general.

Page KV-MATH-0415Reading time 4 minReviewed 2026-08-07Author Kevin Jogin

Executive summary

For primes congruent to 3 modulo 4, a square root is a single exponentiation. For primes congruent to 1 modulo 4 the problem is genuinely harder and requires a randomised algorithm.

The general method needs a quadratic non-residue, which is found by random search since no deterministic method is known unconditionally.

Learning objectives

  1. Derive the direct formula for p congruent to 3 mod 4.
  2. State the general randomised algorithm.
  3. Explain why finding a non-residue requires randomness.

01The easy case

Theorem

Square roots for p ≡ 3 (mod 4)

If p ≡ 3 (mod 4) and a is a quadratic residue modulo p, then

x = a^{(p+1)/4} mod p

satisfies x² ≡ a (mod p).

Verification: x² = a^{(p+1)/2} = a · a^{(p−1)/2} = a · 1 = a, using Euler's criterion in the last step. The exponent (p+1)/4 is an integer exactly because p ≡ 3 (mod 4).

p ≡ 3 (mod 4)  ⇒  √a = a^{(p+1)/4} mod p

02The general case

For p ≡ 1 (mod 4) no analogous closed form exists. The standard method writes p − 1 = 2^s · q with q odd and works down the powers of two, using a known non-residue to correct at each stage.

Algorithm

Square root modulo a prime, general case

Inputprime p, quadratic residue a
Outputx with x² ≡ a (mod p)
  1. Write p − 1 = 2^s · q with q odd.
  2. Find a quadratic non-residue z by random search.
  3. Set c = z^q, x = a^{(q+1)/2}, t = a^q, m = s.
  4. While t ≠ 1:
  5.   Find the least i < m with t^{2^i} = 1.
  6.   Set b = c^{2^{m−i−1}}, then x = xb, t = tb², c = b², m = i.
  7. Return x.
Cost  expected O(len(p)³) bit operations

The algorithm maintains the invariant that x² = at with t of decreasing 2-power order, driving t to 1 and leaving x as the root.

03Why randomness is needed

Half of all units are non-residues, so random search finds one after two attempts on average. But producing one deterministically is open.

Finding a quadratic non-residue
ApproachStatus
Random searchExpected two attempts; the practical method
Smallest non-residueUnder the generalised Riemann hypothesis, bounded by O((log p)²)
Deterministic, unconditionalNo polynomial-time method known

So square root extraction modulo a prime is in ZPP but not known to be in P — one of the cleaner examples of a problem where randomisation is not obviously removable. Choosing p ≡ 3 (mod 4) sidesteps the issue entirely, which is the practical resolution.

04Frequently asked questions

How many square roots does a residue have modulo a prime?

Exactly two, x and −x, since the polynomial X² − a has at most two roots over a field and both signs work. They are distinct because p is odd.

Which root is canonical?

There is no universal convention. For p ≡ 3 (mod 4) a natural choice is the root that is itself a quadratic residue, since exactly one of the pair is. Otherwise implementations pick the smaller representative.

Is the general algorithm related to polynomial factorisation?

Closely. Extracting a square root modulo p is factoring X² − a over the field of p elements, and the equal-degree factorisation step of Cantor–Zassenhaus uses the same random-element-and-exponentiate structure.

Sources and method

Structural reference: Victor Shoup, A Computational Introduction to Number Theory and Algebra, Version 1, Cambridge University Press, 2005 — book pages 292-295.

This page carries the durable method layer only: definitions, constructions, algorithms, complexity results and selection criteria, authored originally for KEVOS. No text is transcribed or paraphrased from the source, and no numeric tables or benchmark data are reproduced — these are routed to live authoritative sources instead.

Author: Kevin Jogin. Last reviewed 2026-08-07.

Continue learning

Testing Quadratic Residuosity: Prime Power and Composite ModulusArticle · MathematicsNEXT LESSON →Computing Modular Square Roots: Prime Power ModulusArticle · MathematicsTesting Quadratic Residuosity: Prime ModulusArticle · MathematicsComputing Modular Square Roots: Composite ModulusArticle · Mathematics