The Tonelli–Shanks algorithm (referred to by Shanks as the RESSOL algorithm) is used in modular arithmetic to solve for r in a congruence of the form r2 ≡ n (mod p), where p is a prime: that is, to find a square root of n modulo p.
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 byAlberto Tonelli[2] [3] 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.[4]
According to Dickson,[3] Tonelli's algorithm can take square roots of x modulo prime powers pλ apart from primes.
Given a non-zero
n
p>2
n
n
| ||||
n |
\equiv1\pmodp
In contrast, if a number
z
| ||||
z |
\equiv-1\pmodp
It is not hard to find such
z
p-1
By (normally) dividing by 2 repeatedly, we can write
p-1
Q2S
Q
R\equiv
| ||||
n |
\pmodp
then
R2\equivnQ+1=(n)(nQ)\pmodp
t\equivnQ\equiv1\pmodp
R
n
M=S
R
t
R2\equivnt\pmodp
t
2M-1
2M-1 | |
t |
=
2S-1 | |
t |
\equiv
Q2S-1 | |
n |
=
| ||||
n |
If, given a choice of
R
t
M
R
n
R
t
M-1
t
20
t=1
R
n
We can check whether
t
2M-2
M-2
R
t
2M-2 | |
t |
p
To find a new pair of
R
t
R
b
t
b2
R2\equivnt\pmodp
2M-2 | |
t |
b2
tb2
2M-2
b2
2M-2
The trick here is to make use of
z
z
zQ
2S-1
zQ
2i
b
Z/pZ
Inputs:
Z/pZ
Outputs:
Z/pZ
p-1=Q2S
Z/pZ
\begin{align} M&\leftarrowS\\ c&\leftarrowzQ\\ t&\leftarrownQ\\ R&\leftarrow
| ||||
n |
\end{align}
2i | |
t |
=1
b\leftarrow
2M-i-1 | |
c |
\begin{align} M&\leftarrowi\\ c&\leftarrowb2\\ t&\leftarrowtb2\\ R&\leftarrowRb \end{align}
Once you have solved the congruence with r the second solution is
-r\pmodp
2i | |
t |
=1
This is most useful when p ≡ 1 (mod 4).
For primes such that p ≡ 3 (mod 4), this problem has possible solutions
r=\pm
| ||||
n |
\pmodp
r2\equivn\pmodp
r2\equiv-n\pmodp
We can show that at the start of each iteration of the loop the following loop invariants hold:
2M-1 | |
c |
=-1
2M-1 | |
t |
=1
R2=tn
Initially:
2M-1 | |
c |
=
Q2S-1 | |
z |
=
| ||||
z |
=-1
2M-1 | |
t |
=
Q2S-1 | |
n |
=
| ||||
n |
=1
R2=nQ+1=tn
At each iteration, with M' , c' , t' , R' the new values replacing M, c, t, R:
2M'-1 | |
c' |
=(b2)
2i-1 | |
=
2M-i2i-1 | |
c |
=
2M-1 | |
c |
=-1
2M'-1 | |
t' |
=(tb2)
2i-1 | |
=
2i-1 | |
t |
2i | |
b |
=-1 ⋅ -1=1
2i-1 | |
t |
=-1
2i | |
t |
=1
2i-1 | |
t |
≠ 1
2i | |
t |
=1
2i | |
b |
=
2M-i-12i | |
c |
=
2M-1 | |
c |
=-1
R'2=R2b2=tnb2=t'n
From
2M-1 | |
t |
=1
2i | |
t |
=1
We can alternately express the loop invariants using the order of the elements:
\operatorname{ord}(c)=2M
\operatorname{ord}(t)|2M-1
R2=tn
Each step of the algorithm moves t into a smaller subgroup by measuring the exact order of t and multiplying it by an element of the same order.
Solving the congruence r2 ≡ 5 (mod 41). 41 is prime as required and 41 ≡ 1 (mod 4). 5 is a quadratic residue by Euler's criterion:
| ||||
5 |
=520=1
(Z/41Z) x
p-1=40=5 ⋅ 23
Q\leftarrow5
S\leftarrow3
| ||||
2 |
=1
| ||||
3 |
=40=-1
z\leftarrow3
M\leftarrowS=3
c\leftarrowzQ=35=38
t\leftarrownQ=55=9
R\leftarrow
| ||||
n |
=
| ||||
5 |
=2
t ≠ 1
21 | |
t |
=40
22 | |
t |
=1
i\leftarrow2
b\leftarrow
2M-i-1 | |
c |
=
23-2-1 | |
38 |
=38
M\leftarrowi=2
c\leftarrowb2=382=9
t\leftarrowtb2=9 ⋅ 9=40
R\leftarrowRb=2 ⋅ 38=35
t ≠ 1
21 | |
t |
=1
i\leftarrow1
b\leftarrow
2M-i-1 | |
c |
=
22-1-1 | |
9 |
=9
M\leftarrowi=1
c\leftarrowb2=92=40
t\leftarrowtb2=40 ⋅ 40=1
R\leftarrowRb=35 ⋅ 9=28
t=1
r=R=28
Indeed, 282 ≡ 5 (mod 41) and (−28)2 ≡ 132 ≡ 5 (mod 41). So the algorithm yields the two solutions to our congruence.
The Tonelli–Shanks algorithm requires (on average over all possible input (quadratic residues and quadratic nonresidues))
2m+2k+ | S(S-1) | + |
4 |
1 | |
2S-1 |
-9
modular multiplications, where
m
p
k
p
z
y
2
y
\tfrac{\tfrac{p+1}{2}}{p}=\tfrac{1+\tfrac{1}{p}}{2}
1
\geq\tfrac{1}{2}
y
This shows essentially that the Tonelli–Shanks algorithm works very well if the modulus
p
S
p
S(S-1)>8m+20
\ast | |
F | |
p |
S(S-1)
O(SlogS/loglogS)
e
ce\equivnQ
R\equivc-e/2n(Q+1)/2
R2\equivn
e
n
The algorithm requires us to find a quadratic nonresidue
z
z
z<2ln2{p}
z
z
z
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 and in the sieving step of the quadratic sieve.
Tonelli–Shanks can be generalized to any cyclic group (instead of
(Z/pZ) x
If many square-roots must be done in the same cyclic group and S is not too large, a table of square-roots of the elements of 2-power order can be prepared in advance and the algorithm simplified and sped up as follows.
p-1=Q2S
R\leftarrow
| ||||
n |
,t\leftarrownQ\equivR2/n
b
b2\equivt
R\equivR/b
According to Dickson's "Theory of Numbers"[3]
A. Tonelli[7] gave an explicit formula for the roots of}[3]x2=c\pmod{pλ
The Dickson reference shows the following formula for the square root of
x2\bmod{pλ
when
p=4 ⋅ 7+1
s=2
A=7
29=22 ⋅ 7+1
for
x2\bmod{pλ
x\bmod{pλ
\beta\equiva ⋅ pλ-1
Noting that
232\bmod{293
\beta=7 ⋅ 292
(5297+
7 ⋅ 292 | |
3) |
⋅
(7 ⋅ 292+1)/2 | |
529 |
\bmod{293
To take another example:
23332\bmod{293
(41427+
7 ⋅ 292 | |
3) |
⋅
(7 ⋅ 292+1)/2 | |
4142 |
\bmod{293
Dickson also attributes the following equation to Tonelli:
X\bmod{pλ
X2\bmod{pλ
x2\bmod{p}\equivc
Using
p=23
p3
11152\bmod{233
First, find the modular square root mod
p
11152\bmod{23}\equiv6
\sqrt{6}\bmod{23}\equiv11
And applying Tonelli's equation (see above):
232 | |
11 |
⋅
(233-2 ⋅ 232+1)/2 | |
2191 |
\bmod{233
Dickson's reference[3] clearly shows that Tonelli's algorithm works on moduli of
pλ