The Tonelli–Shanks algorithm is a classical and efficient method to compute modular square roots over finite fields Fpm, especially when m is odd and p≡1(mod4), where simpler methods like Shanks do not apply. The method is based on Algorithm 5 from ePrint 2012/685.
Preconditions
To use Tonelli–Shanks:
m must be odd.
p must be an odd prime
a is a quadratic residue.
We express p−1 as:
p−1=2s⋅T
where T is odd.
This decomposition is known as the 2-adic decomposition of p−1, and s is the 2-adicity.
Algorithm Steps (High-level)
Let’s outline the algorithm assuming a∈Fpm∗:
Precomputation
Write p−1=2s⋅T
Find a known quadratic non-residuec∈Fpm
Compute z=cT — a 2s-th primitive root of unity
Initial values
x=a(T+1)/2
b=aT
v=s
Main loop
While b=1, do:
Find the smallest k such that b2k≡1
Let w=z2v−k−1
Update:
x←x⋅w,b←b⋅w2,z←w2,v←k
Output
x is the square root of a
Intuition Behind the Updates in Tonelli–Shanks
The Tonelli–Shanks algorithm iteratively computes the square root of a quadratic residue a∈Fp, given the decomposition p−1=2s⋅T, with T odd. Its central mechanism relies on maintaining a key invariant and gradually reducing the complexity of the problem step by step.
Invariant: xi2=a⋅bi
At each iteration i, the algorithm maintains:
xi2=a⋅bi
We define:
x0=a(T+1)/2
b0=aT
Then:
x02=aT+1=a⋅aT=a⋅b0
Inductive step: Suppose at iteration i, the invariant holds:
Therefore, the invariant is preserved at every step:
xi2=a⋅bi
Why bi→1: Convergence of the Loop
At each iteration of Tonelli–Shanks, the algorithm updates:
bi+1=bi⋅wi2
To show that bi→1, we analyze how the order of bi decreases over time. Suppose that at iteration i, the smallest integer k such that bi2k=1, and bi2k−1=1. Then bi is a primitive2k-th root of unity.
Since bi2k=1 and bi2k−1=1, we know by the Halving Lemma that bi2k−1=−1
Now for wi, recall that:
wi=z2v−k−1⇒wi2=z2v−k
Then:
(wi2)2k−1=z2v−k⋅2k−1=z2v−1=−1
because z is a quadratic non-residue, and by construction(when i=0,v=s):
z2s−1=(cT)2s−1=c2s−1⋅T=−1
This identity continues to hold at every step due to the invariant:
zi2v−1=−1
Result
Since both bi2k−1=−1 and (wi2)2k−1=−1, their product is 1:
(bi⋅wi2)2k−1=1
Thus, the order of bi+1 is now at most 2k−1, which is strictly less than the order of bi. As a result, the order of b decreases with each iteration, and within at most s steps, we reach:
blast=1
Once this happens, the invariant x2=a⋅b gives x2=a, completing the algorithm.