Tonelli–Shanks algorithm

1

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. 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. According to Dickson, 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: In contrast, if a number z has no square root (is a non-residue), Euler's criterion tells us that: It is not hard to find such z, because half of the integers between 1 and p-1 have this property. So we assume that we have access to such a non-residue. By (normally) dividing by 2 repeatedly, we can write p-1 as Q 2^S, where Q is odd. Note that if we try then. If, then R is a square root of n. Otherwise, for M = S, we have R and t satisfying: 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 M - 1 such that the above relations hold, then we can repeat this until t becomes a 2^0-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 2^{M-2}-th root of 1 by squaring it M-2 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, t^{2^{M-2}} 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 b^2 to keep. So, when t^{2^{M-2}} is -1, we need to find a factor b^2 so that tb^2 is a 2^{M-2}-th root of 1, or equivalently b^2 is a 2^{M-2}-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 z^Q is a 2^{S-1}-th root of -1. So by squaring z^Q repeatedly, we have access to a sequence of 2^i-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 are implicitly mod p. Inputs: Outputs: Algorithm: Once you have solved the congruence with r the second solution is -r \pmod p. If the least i such that t^{2^i} = 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. If these satisfy, they are the only solutions. If not,, 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: Initially: At each iteration, with M' , c' , t' , R' the new values replacing M, c, t, R: From 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 t^{2^i} = 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: 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: (as before, operations in are implicitly mod 41). 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)) 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. 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. As written above, Cipolla's algorithm works better than Tonelli–Shanks if (and only if). However, if one instead uses Sutherland's algorithm to perform the discrete logarithm computation in the 2-Sylow subgroup of, one may replace S(S-1) with an expression that is asymptotically bounded by. Explicitly, one computes e such that and then satisfies R^2\equiv n (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, 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 ) and to kth roots for arbitrary integer k, in particular to taking the kth root of an element of a finite field. 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.

Tonelli's algorithm will work on mod p^k

According to Dickson's "Theory of Numbers" A. Tonelli gave an explicit formula for the roots of The Dickson reference shows the following formula for the square root of. Noting that and noting that then To take another example: and Dickson also attributes the following equation to Tonelli: Using p=23 and using the modulus of p^{3} the math follows: First, find the modular square root mod p which can be done by the regular Tonelli algorithm: And applying Tonelli's equation (see above): Dickson's reference clearly shows that Tonelli's algorithm works on moduli of p^{\lambda}.

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article