Tonelli–Shanks algorithm

From The Right Wiki
(Redirected from Shanks–Tonelli algorithm)
Jump to navigationJump to search

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 r2n (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 by Alberto 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.

Core ideas

Given a non-zero n and a prime p>2 (which will always be odd), Euler's criterion tells us that n has a square root (i.e., n is a quadratic residue) if and only if:

np121(modp).

In contrast, if a number z has no square root (is a non-residue), Euler's criterion tells us that:

zp121(modp).

It is not hard to find such z, because half of the integers between 1 and p1 have this property. So we assume that we have access to such a non-residue. By (normally) dividing by 2 repeatedly, we can write p1 as Q2S, where Q is odd. Note that if we try

RnQ+12(modp),

then R2nQ+1=(n)(nQ)(modp). If tnQ1(modp), then R is a square root of n. Otherwise, for M=S, we have R and t satisfying:

  • R2nt(modp); and
  • t is a 2M1-th root of 1 (because t2M1=t2S1nQ2S1=np12).

If, given a choice of R and t for a particular M satisfying the above (where R is not a square root of n), we can easily calculate another R and t for M1 such that the above relations hold, then we can repeat this until t becomes a 20-th root of 1, i.e., t=1. At that point R is a square root of n. We can check whether t is a 2M2-th root of 1 by squaring it M2 times and check whether it is 1. If it is, then we do not need to do anything, as the same choice of R and t works. But if it is not, t2M2 must be -1 (because squaring it gives 1, and there can only be two square roots 1 and -1 of 1 modulo p). To find a new pair of R and t, we can multiply R by a factor b, to be determined. Then t must be multiplied by a factor b2 to keep R2nt(modp). So, when t2M2 is -1, we need to find a factor b2 so that tb2 is a 2M2-th root of 1, or equivalently b2 is a 2M2-th root of -1. The trick here is to make use of z, the known non-residue. The Euler's criterion applied to z shown above says that zQ is a 2S1-th root of -1. So by squaring zQ repeatedly, we have access to a sequence of 2i-th root of -1. We can select the right one to serve as b. With a little bit of variable maintenance and trivial case compression, the algorithm below emerges naturally.

The algorithm

Operations and comparisons on elements of the multiplicative group of integers modulo p /p are implicitly mod p. Inputs:

  • p, a prime
  • n, an element of /p such that solutions to the congruence r2 = n exist; when this is so we say that n is a quadratic residue mod p.

Outputs:

  • r in /p such that r2 = n

Algorithm:

  1. By factoring out powers of 2, find Q and S such that p1=Q2S with Q odd
  2. Search for a z in /p which is a quadratic non-residue
  3. Let
    MSczQtnQRnQ+12
  4. Loop:
    • If t = 0, return r = 0
    • If t = 1, return r = R
    • Otherwise, use repeated squaring to find the least i, 0 < i < M, such that t2i=1
    • Let bc2Mi1, and set
      Micb2ttb2RRb

Once you have solved the congruence with r the second solution is r(modp). If the least i such that t2i=1 is M, then no solution to the congruence exists, i.e. n is not a quadratic residue. This is most useful when p ≡ 1 (mod 4). For primes such that p ≡ 3 (mod 4), this problem has possible solutions r=±np+14(modp). If these satisfy r2n(modp), they are the only solutions. If not, r2n(modp), n is a quadratic non-residue, and there are no solutions.

Proof

We can show that at the start of each iteration of the loop the following loop invariants hold:

  • c2M1=1
  • t2M1=1
  • R2=tn

Initially:

  • c2M1=zQ2S1=zp12=1 (since z is a quadratic nonresidue, per Euler's criterion)
  • t2M1=nQ2S1=np12=1 (since n is a quadratic residue)
  • R2=nQ+1=tn

At each iteration, with M' , c' , t' , R' the new values replacing M, c, t, R:

  • c'2M1=(b2)2i1=c2Mi2i1=c2M1=1
  • t'2M1=(tb2)2i1=t2i1b2i=11=1
    • t2i1=1 since we have that t2i=1 but t2i11 (i is the least value such that t2i=1)
    • b2i=c2Mi12i=c2M1=1
  • R'2=R2b2=tnb2=tn

From t2M1=1 and the test against t = 1 at the start of the loop, we see that we will always find an i in 0 < i < M such that t2i=1. M is strictly smaller on each iteration, and thus the algorithm is guaranteed to halt. When we hit the condition t = 1 and halt, the last loop invariant implies that R2 = n.

Order of t

We can alternately express the loop invariants using the order of the elements:

  • ord(c)=2M
  • ord(t)|2M1
  • R2=tn as before

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.

Example

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: 54112=520=1 (as before, operations in (/41)× are implicitly mod 41).

  1. p1=40=523 so Q5, S3
  2. Find a value for z:
    • 24112=1, so 2 is a quadratic residue by Euler's criterion.
    • 34112=40=1, so 3 is a quadratic nonresidue: set z3
  3. Set
    • MS=3
    • czQ=35=38
    • tnQ=55=9
    • RnQ+12=55+12=2
  4. Loop:
    • First iteration:
      • t1, so we're not finished
      • t21=40, t22=1 so i2
      • bc2Mi1=382321=38
      • Mi=2
      • cb2=382=9
      • ttb2=99=40
      • RRb=238=35
    • Second iteration:
      • t1, so we're still not finished
      • t21=1 so i1
      • bc2Mi1=92211=9
      • Mi=1
      • cb2=92=40
      • ttb2=4040=1
      • RRb=359=28
    • Third iteration:
      • t=1, and we are finished; return 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.

Speed of the algorithm

The Tonelli–Shanks algorithm requires (on average over all possible input (quadratic residues and quadratic nonresidues))

2m+2k+S(S1)4+12S19

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.[5] The average of two computations of the Legendre symbol are explained as follows: y is a quadratic residue with chance p+12p=1+1p2, which is smaller than 1 but 12, 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. As written above, Cipolla's algorithm works better than Tonelli–Shanks if (and only if) S(S1)>8m+20. However, if one instead uses Sutherland's algorithm to perform the discrete logarithm computation in the 2-Sylow subgroup of 𝔽p, one may replace S(S1) with an expression that is asymptotically bounded by O(SlogS/loglogS).[6] Explicitly, one computes e such that cenQ and then Rce/2n(Q+1)/2 satisfies R2n (note that e is a multiple of 2 because n is a quadratic residue). 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<2ln2p,[7] 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.

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 and in the sieving step of the quadratic sieve.

Generalizations

Tonelli–Shanks can be generalized to any cyclic group (instead of (/p)×) and to kth roots for arbitrary integer k, in particular to taking the kth root of an element of a finite field.[8] 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.

  1. Factor out powers of 2 from p − 1, defining Q and S as: p1=Q2S with Q odd.
  2. Let RnQ+12,tnQR2/n
  3. Find b from the table such that b2t and set RR/b
  4. return R.

Tonelli's algorithm will work on mod p^k

According to Dickson's "Theory of Numbers"[3]

A. Tonelli[9] gave an explicit formula for the roots of x2=c(modpλ)[3]

The Dickson reference shows the following formula for the square root of x2modpλ.

when p=47+1, or s=2 (s must be 2 for this equation) and A=7 such that 29=227+1
for x2modpλc then
xmodpλ±(cA+3)βc(β+1)/2 where βapλ1

Noting that 232mod293529 and noting that β=7292 then

(5297+3)7292529(7292+1)/2mod2932436623

To take another example: 23332mod2934142 and

(41427+3)72924142(7292+1)/2mod2932333

Dickson also attributes the following equation to Tonelli:

Xmodpλxpλ1c(pλ2pλ1+1)/2 where X2modpλc and x2modpc;

Using p=23 and using the modulus of p3 the math follows:

11152mod233=2191

First, find the modular square root mod p which can be done by the regular Tonelli algorithm:

11152mod236 and thus 6mod2311

And applying Tonelli's equation (see above):

112322191(2332232+1)/2mod2331115

Dickson's reference[3] clearly shows that Tonelli's algorithm works on moduli of pλ.

Notes

  1. Oded Goldreich, Computational complexity: a conceptual perspective, Cambridge University Press, 2008, p. 588.
  2. Volker Diekert; Manfred Kufleitner; Gerhard Rosenberger; Ulrich Hertrampf (24 May 2016). Discrete Algebraic Methods: Arithmetic, Cryptography, Automata and Groups. De Gruyter. pp. 163–165. ISBN 978-3-11-041632-9.
  3. 3.0 3.1 3.2 3.3 3.4 Leonard Eugene Dickson (1919). History of the Theory of Numbers. Vol. 1. Washington, Carnegie Institution of Washington. pp. 215–216.
  4. Daniel Shanks. Five Number-theoretic Algorithms. Proceedings of the Second Manitoba Conference on Numerical Mathematics. Pp. 51–70. 1973.
  5. Tornaría, Gonzalo (2002). "Square Roots Modulo P". LATIN 2002: Theoretical Informatics. Lecture Notes in Computer Science. Vol. 2286. pp. 430–434. doi:10.1007/3-540-45995-2_38. ISBN 978-3-540-43400-9.
  6. Sutherland, Andrew V. (2011), "Structure computation and discrete logarithms in finite abelian p-groups", Mathematics of Computation, 80 (273): 477–500, arXiv:0809.3413, doi:10.1090/s0025-5718-10-02356-2, S2CID 13940949
  7. Bach, Eric (1990), "Explicit bounds for primality testing and related problems", Mathematics of Computation, 55 (191): 355–380, doi:10.2307/2008811, JSTOR 2008811
  8. Adleman, L. M., K. Manders, and G. Miller: 1977, `On taking roots in finite fields'. In: 18th IEEE Symposium on Foundations of Computer Science. pp. 175-177
  9. "Accademia nazionale dei Lincei, Rome. Rendiconti, (5), 1, 1892, 116-120."

References