Shanks Algorithm

When the prime modulus pp satisfies p3(mod4)p \equiv 3 \pmod 4, computing the square root of a quadratic residue aFpma \in \mathbb{F}_{p^m} becomes remarkably simple when mm is odd. This is because the field size q=pmq = p^m then satisfies q3(mod4)q \equiv 3 \pmod 4, enabling a shortcut formula for extracting square roots. The method is based on Algorithm 2 from ePrint 2012/685.


Goal

Find xFpmx \in \mathbb{F}_{p^m} such that:

x2=ax^2 = a

if aa is a quadratic residue.


Original Algorithm

The algorithm consists of the following steps:

  1. Compute a1=a(p3)/4a_1 = a^{(p - 3)/4}

  2. Compute a0=a12a=a(p1)/2a_0 = a_1^2 \cdot a = a^{(p-1) / 2}

  3. If a0=1a_0 = -1, then aa is a quadratic nonresidue

  4. Otherwise, return x=a1a=a(p+1)/4x = a_1 \cdot a = a^{(p + 1)/4}

This version performs:

  • 1 exponentiation,

  • 1 squaring,

  • 2 multiplications.


Modified Algorithm

A simplified version is often used in practice:

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

  2. If x2=ax^2 = a, return xx

  3. Otherwise, aa 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=1m = 1)

To understand why this shortcut is valid, consider the case where aFpa \in \mathbb{F}_p and p3(mod4)p \equiv 3 \pmod 4. Suppose xx is a square root of aa, i.e.,

x2=ax^2 = a

Then,

x4=(x2)2=a2x^4 = (x^2)^2 = a^2

We know:

a2=ap+1modpa^2 = a^{p+1} \mod p

But since p3(mod4)p \equiv 3 \pmod 4, taking the 4th root of both sides gives:

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

Therefore, x=a(p+1)/4x = a^{(p+1)/4} is indeed a valid square root of amodpa \mod p — if aa is a quadratic residue.


Why It Requires mm to Be Odd

In the general case over Fpm\mathbb{F}_{p^m}, we require the field size q=pmq = p^m to satisfy q3(mod4)q \equiv 3 \pmod 4 for the exponent (q+1)/4(q+1)/4 to be an integer and the formula to hold. This congruence only holds when:

  • p3(mod4)p \equiv 3 \pmod 4, and

  • mm is odd

For example:

  • If p=3p = 3, then:

    • 313(mod4)3^1 \equiv 3 \pmod 4 ✅ (odd m=1m = 1)

    • 32=91(mod4)3^2 = 9 \equiv 1 \pmod 4 ❌ (even m=2m = 2)

So this algorithm is only valid when both conditions are met.

Written by ryan Kim from A41

Last updated