Tonelli-Shanks Algorithm
The Tonelli–Shanks algorithm is a classical and efficient method to compute modular square roots over finite fields , especially when is odd and , where simpler methods like Shanks do not apply. The method is based on Algorithm 5 from ePrint 2012/685.
Preconditions
To use Tonelli–Shanks:
must be odd.
must be an odd prime
is a quadratic residue.
We express as:
where is odd.
This decomposition is known as the 2-adic decomposition of , and is the 2-adicity.
Algorithm Steps (High-level)
Let’s outline the algorithm assuming :
Precomputation
Write
Find a known quadratic non-residue
Compute — a -th primitive root of unity
Initial values
Main loop
While , do:
Find the smallest such that
Let
Update:
Output
is the square root of
Intuition Behind the Updates in Tonelli–Shanks
The Tonelli–Shanks algorithm iteratively computes the square root of a quadratic residue , given the decomposition , with odd. Its central mechanism relies on maintaining a key invariant and gradually reducing the complexity of the problem step by step.
Invariant:
At each iteration , the algorithm maintains:
We define:
Then:
Inductive step: Suppose at iteration , the invariant holds:
Then the algorithm updates:
Then we have:
Therefore, the invariant is preserved at every step:
Why : Convergence of the Loop
At each iteration of Tonelli–Shanks, the algorithm updates:
To show that , we analyze how the order of decreases over time. Suppose that at iteration , the smallest integer such that , and . Then is a primitive -th root of unity.
We now prove that the update:
forces to have strictly lower order.
Let’s compute:
Why does this work?
Since and , we know by the Halving Lemma that
Now for , recall that:
Then:
because is a quadratic non-residue, and by construction(when ):
This identity continues to hold at every step due to the invariant:
Result
Since both and , their product is 1:
Thus, the order of is now at most , which is strictly less than the order of . As a result, the order of decreases with each iteration, and within at most steps, we reach:
Once this happens, the invariant gives , completing the algorithm.
Last updated