Shanks Algorithm
When the prime modulus p satisfies p≡3(mod4), computing the square root of a quadratic residue a∈Fpm becomes remarkably simple when m is odd. This is because the field size q=pm then satisfies q≡3(mod4), enabling a shortcut formula for extracting square roots. The method is based on Algorithm 2 from ePrint 2012/685.
Goal
Find x∈Fpm such that:
if a is a quadratic residue.
Original Algorithm
The algorithm consists of the following steps:
Compute a1=a(p−3)/4
Compute a0=a12⋅a=a(p−1)/2
If a0=−1, then a is a quadratic nonresidue
Otherwise, return x=a1⋅a=a(p+1)/4
This version performs:
1 exponentiation,
1 squaring,
2 multiplications.
Modified Algorithm
A simplified version is often used in practice:
Compute x=a(p+1)/4
If x2=a, return x
Otherwise, a is a quadratic nonresidue
This version only requires:
1 exponentiation,
1 squaring
and defers the residuosity check to a final comparison.
Why It Works (when m=1)
To understand why this shortcut is valid, consider the case where a∈Fp and p≡3(mod4). Suppose x is a square root of a, i.e.,
Then,
We know:
But since p≡3(mod4), taking the 4th root of both sides gives:
Therefore, x=a(p+1)/4 is indeed a valid square root of amodp — if a is a quadratic residue.
Why It Requires m to Be Odd
In the general case over Fpm, we require the field size q=pm to satisfy q≡3(mod4) for the exponent (q+1)/4 to be an integer and the formula to hold. This congruence only holds when:
p≡3(mod4), and
m is odd
For example:
If p=3, then:
31≡3(mod4) ✅ (odd m=1)
32=9≡1(mod4) ❌ (even m=2)
So this algorithm is only valid when both conditions are met.
Written by Ryan Kim of Fractalyze
Last updated