Tonelli–Shanks algorithm
The Tonelli–Shanks algorithm (referred to by Shanks as the RESSOL algorithm) is used within modular arithmetic to solve a congruence of the form
where n is a quadratic residue (mod p), and p is an odd prime.
Tonelli–Shanks cannot be used for composite moduli; finding square roots modulo composite numbers is a computational problem equivalent to integer factorization.[1]
An equivalent, but slightly more redundant version of this algorithm was developed by Alberto Tonelli in 1891. The version discussed here was developed independently by Daniel Shanks in 1973, who explained:
"My tardiness in learning of these historical references was because I had lent Volume 1 of Dickson's History to a friend and it was never returned."[2]
Contents |
[edit] The algorithm
(Note: All
are taken to mean (mod p), unless indicated otherwise).
Inputs: p, an odd prime. n, an integer which is a quadratic residue (mod p), meaning that the Legendre symbol
.
Outputs: R, an integer satisfying
.
- Factor out powers of 2 from p − 1, defining Q and S as: p − 1 = Q2S with Q odd. Note that if S = 1, i.e.
, then solutions are given directly by
. - Select a z such that the Legendre symbol
(that is, z should be a quadratic non-residue modulo p), and set
. - Let

- Loop:
- If
, return R. - Otherwise, find the lowest i, 0 < i < M, such that
; e.g. via repeated squaring. - Let
, and set
and
.
- If
Once you have solved the congruence with R the second solution is p − R.
[edit] Example
Solving the congruence
. It is clear that 13 is odd, and since
, 10 is a quadratic residue (by Euler's criterion).
- Step 1: Observe
so Q = 3, S = 2.
- Step 2: Take z = 2 as the quadratic nonresidue (2 is a quadratic nonresidue since
(again, Euler's criterion)). Set 
- Step 3:

- Step 4: Now we start the loop:
so
; i.e.
- Let
, so
. - Set
. Set
, and 
- We restart the loop, and since
we are done, returning 
- Let
Indeed, observe that
and naturally also
. So the algorithm yields two solutions to our congruence.
[edit] Proof
First write p − 1 = Q2S. Now write
and
, observing that
. This latter congruence will be true after every iteration of the algorithm's main loop. If at any point,
then
and the algorithm terminates with
.
If
, then consider z, a quadratic non-residue of p. Let
. Then
and
, which shows that the order of c is 2S.
Similarly we have
, so the order of t divides 2S. Suppose the order of t is 2S'. Since n is a square modulo p,
is also a square, and hence
.
Now we set
and with this
,
and
. As before,
holds; however with this construction both t and c' have order 2S'. This implies that t' has order 2S'' with S'' < S'.
If
then
, and the algorithm stops, returning
. Else, we restart the loop with analogous definitions of b', r'', c'' and t'' until we arrive at an S(j)' that equals 0. Since the sequence of S is strictly decreasing the algorithm terminates.
[edit] Speed of the algorithm
The Tonelli–Shanks algorithm requires (on average over all possible input (quadratic residues and quadratic nonresidues))
modular multiplications, where m is the number of digits in the binary representation of p and k is the number of ones in the binary representation of p. If the required quadratic nonresidue z is to be found by checking if a randomly taken number y is a quadratic nonresidue, it requires (on average) 2 computations of the Legendre symbol.[3] The average of two computations of the Legendre symbol are explained as follows: y is a quadratic residue with chance
, which is smaller than 1 but
, so we will on average need to check if a y is a quadratic residue two times.
This shows essentially that the Tonelli–Shanks algorithm works very well if the modulus p is random, that is, if S is not particularly large with respect to the number of digits in the binary representation of p. Cipolla's algorithm works better than Tonelli–Shanks if (and only if) S(S − 1) > 8m + 20.
The algorithm requires us to find a quadratic nonresidue z. There is no known deterministic algorithm that runs in polynomial time for finding such a z. However, if the generalized Riemann hypothesis is true, there exists a quadratic nonresidue z < 2ln 2p,[4] making it possible to check every z up to that limit and find a suitable z within polynomial time. Keep in mind, however, that this is a worst case scenario; in general, z is found in on average 2 trials as stated above.
[edit] Uses
The Tonelli–Shanks algorithm can (naturally) be used for any process in which square roots modulo a prime are necessary. For example, it can be used for finding points on elliptic curves. It is also useful for the computations in the Rabin cryptosystem.
[edit] Generalization
Tonelli–Shanks can be generalized to any cyclic group (instead of
) and to kth roots for arbitrary integer k, in particular to taking the kth root of an element of a finite field.
[edit] Notes
- ^ Oded Goldreich, Computational complexity: a conceptual perspective, Cambridge University Press, 2008, p. 588.
- ^ Daniel Shanks. Five Number-theoretic Algorithms. Proceedings of the Second Manitoba Conference on Numerical Mathematics. Pp. 51–70. 1973.
- ^ Gonzalo Tornaria - Square roots modulo p, page 2 http://www.springerlink.com/content/xgxe68edy03la96p/fulltext.pdf
- ^ Bach, Eric (1990), "Explicit bounds for primality testing and related problems", Mathematics of Computation 55 (191): 355–380, JSTOR 2008811
[edit] References
- Niven, Ivan; Herbert S. Zuckerman, Hugh L. Montgomery (1991). An Introduction to the Theory of Numbers (5th edition ed.). Wiley. ISBN 0-471-62546-9.
Pages 110–115 describe the algorithm and explain the group theory behind it.
- Daniel Shanks. Five Number Theoretic Algorithms. Proceedings of the Second Manitoba Conference on Numerical Mathematics. Pp. 51–70. 1973.
- Alberto Tonelli, Bemerkung über die Auflösung quadratischer Congruenzen. Nachrichten von der Königlichen Gesellschaft der Wissenschaften und der Georg-Augusts-Universität zu Göttingen. Pp. 344–346. 1891. [1]
- Gagan Tara Nanda - Mathematics 115: The RESSOL Algorithm [2]
[edit] External links
- Implementation in C# http://shankstonelli.blogspot.com/2010/12/shanks-tonelli-algorithm-in-c.html
|
|||||||||||||||||||||||||||||

, then solutions are given directly by
.
(that is, z should be a quadratic non-residue modulo p), and set
.
, return R.
; e.g. via repeated squaring.
, and set
and
.
so
(again, Euler's criterion)). Set 

so
; i.e.
, so
.
. Set
, and 
we are done, returning 
