Tonelli–Shanks Algorithm
   HOME

TheInfoList



OR:

The Tonelli–Shanks
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
(referred to by Shanks as the RESSOL algorithm) is used in
modular arithmetic In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" when reaching a certain value, called the modulus. The modern approach to modular arithmetic was developed by Carl Friedrich Gauss in his book ...
to solve for ''r'' in a congruence of the form ''r''2 ≡ ''n'' (mod ''p''), where ''p'' is a
prime A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways ...
: 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 In number theory, integer factorization is the decomposition of a composite number into a product of smaller integers. If these factors are further restricted to prime numbers, the process is called prime factorization. When the numbers are suf ...
. 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 Daniel Shanks (January 17, 1917 – September 6, 1996) was an American mathematician who worked primarily in numerical analysis and number theory. He was the first person to compute π to 100,000 decimal places. Life and education Shanks was b ...
in 1973, who explained:
My tardiness in learning of these historical references was because I had lent Volume 1 of Dickson's
History History (derived ) is the systematic study and the documentation of the human activity. The time period of event before the History of writing#Inventions of writing, invention of writing systems is considered prehistory. "History" is an umbr ...
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 an odd prime p,
Euler's criterion In number theory, Euler's criterion is a formula for determining whether an integer is a quadratic residue modulo a prime. Precisely, Let ''p'' be an odd prime and ''a'' be an integer coprime to ''p''. Then : a^ \equiv \begin \;\;\,1\pmod& \text ...
tells us that n has a square root (i.e., n is a
quadratic residue In number theory, an integer ''q'' is called a quadratic residue modulo ''n'' if it is congruent to a perfect square modulo ''n''; i.e., if there exists an integer ''x'' such that: :x^2\equiv q \pmod. Otherwise, ''q'' is called a quadratic no ...
) if and only if: :n^ \equiv 1 \pmod p. In contrast, if a number z has no square root (is a non-residue), Euler's criterion tells us that: :z^ \equiv -1 \pmod p. 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 :R \equiv n^ \pmod p, then R^2 \equiv n^ = (n)(n^Q) \pmod p. If t \equiv n^Q \equiv 1 \pmod p, then R is a square root of n. Otherwise, for M = S, we have R and t satisfying: * R^2 \equiv nt \pmod p; and * t is a 2^-th root of 1 (because t^ = t^ \equiv n^ = n^). 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^-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, the same choice of R and t works. But if it is not, t^ 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 R^2 \equiv nt \pmod p. So we need to find a factor b^2 so that tb^2 is a 2^-th root of 1, or equivalently b^2 is a 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^-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 \mathbb/p\mathbb are implicitly mod ''p''. Inputs: * ''p'', a prime * ''n'', an element of \mathbb/p\mathbb such that solutions to the congruence ''r''2 = ''n'' exist; when this is so we say that ''n'' is a
quadratic residue In number theory, an integer ''q'' is called a quadratic residue modulo ''n'' if it is congruent to a perfect square modulo ''n''; i.e., if there exists an integer ''x'' such that: :x^2\equiv q \pmod. Otherwise, ''q'' is called a quadratic no ...
mod ''p''. Outputs: * ''r'' in \mathbb/p\mathbb such that ''r''2 = ''n'' Algorithm: # By factoring out powers of 2, find ''Q'' and ''S'' such that p-1=Q 2^S with ''Q'' odd # Search for a ''z'' in \mathbb/p\mathbb which is a quadratic non-residue #* Half of the elements in the set will be quadratic non-residues #* Candidates can be tested with
Euler's criterion In number theory, Euler's criterion is a formula for determining whether an integer is a quadratic residue modulo a prime. Precisely, Let ''p'' be an odd prime and ''a'' be an integer coprime to ''p''. Then : a^ \equiv \begin \;\;\,1\pmod& \text ...
or by finding the
Jacobi symbol Jacobi symbol for various ''k'' (along top) and ''n'' (along left side). Only are shown, since due to rule (2) below any other ''k'' can be reduced modulo ''n''. Quadratic residues are highlighted in yellow — note that no entry with a ...
# Let #:\begin M &\leftarrow S \\ c &\leftarrow z^Q \\ t &\leftarrow n^Q \\ R &\leftarrow n^\frac \end # 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 t^ = 1 #* Let b \leftarrow c^, and set #*:\begin M &\leftarrow i \\ c &\leftarrow b^2 \\ t &\leftarrow tb^2 \\ R &\leftarrow Rb \end Once you have solved the congruence with ''r'' the second solution is -r \pmod p. If the least ''i'' such that t^ = 1 is ''M'', then no solution to the congruence exists, ie ''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 = \pm n^\pmod p. If these satisfy r^2 \equiv n \pmod p, they are the only solutions. If not, r^2 \equiv -n \pmod p, ''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 invariant In computer science, a loop invariant is a property of a program loop that is true before (and after) each iteration. It is a logical assertion, sometimes checked within the code by an assertion call. Knowing its invariant(s) is essential in und ...
s hold: * c^ = -1 * t^ = 1 * R^2 = tn Initially: * c^ = z^ = z^\frac = -1 (since ''z'' is a quadratic nonresidue, per Euler's criterion) * t^ = n^ = n^\frac = 1 (since ''n'' is a quadratic residue) * R^2 = n^ = tn At each iteration, with ''M' '', ''c' '', ''t' '', ''R' '' the new values replacing ''M'', ''c'', ''t'', ''R'': * c'^ = (b^2)^ = c^ = c^ = -1 * t'^ = (tb^2)^ = t^b^ = -1 \cdot -1 = 1 ** t^ = -1 since we have that t^ = 1 but t^ \neq 1 (''i'' is the least value such that t^ = 1) ** b^ = c^= c^ = -1 * R'^2 = R^2b^2 = tnb^2 = t'n From t^ = 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 t^ = 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 ''R''2 = ''n''.


Order of ''t''

We can alternately express the loop invariants using the
order Order, ORDER or Orders may refer to: * Categorization, the process in which ideas and objects are recognized, differentiated, and understood * Heterarchy, a system of organization wherein the elements have the potential to be ranked a number of d ...
of the elements: * \operatorname(c) = 2^M * \operatorname(t) , 2^ * R^2 = 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 ''r''2 ≡ 5 (mod 41). 41 is prime as required and 41 ≡ 1 (mod 4). 5 is a quadratic residue by Euler's criterion: 5^ = 5^ = 1 (as before, operations in (\mathbb/41\mathbb)^\times are implicitly mod 41). # p-1 = 40 = 5 \cdot 2^3 so Q \leftarrow 5, S \leftarrow 3 # Find a value for z: #* 2^ = 1, so 2 is a quadratic residue by Euler's criterion. #* 3^ = 40 = -1, so 3 is a quadratic nonresidue: set z \leftarrow 3 # Set #*M \leftarrow S = 3 #*c \leftarrow z^Q = 3^5 = 38 #*t \leftarrow n^Q = 5^5 = 9 #*R \leftarrow n^ = 5^ = 2 # Loop: #* First iteration: #** t \neq 1, so we're not finished #** t^ = 40, t^ = 1 so i \leftarrow 2 #** b \leftarrow c^ = 38^ = 38 #** M \leftarrow i = 2 #** c \leftarrow b^2 = 38^2 = 9 #** t \leftarrow tb^2 = 9 \cdot 9 = 40 #** R \leftarrow Rb = 2 \cdot 38 = 35 #* Second iteration: #** t \neq 1, so we're still not finished #** t^ = 1 so i \leftarrow 1 #** b \leftarrow c^ = 9^ = 9 #** M \leftarrow i = 1 #** c \leftarrow b^2 = 9^2 = 40 #** t \leftarrow tb^2 = 40 \cdot 40 = 1 #** R \leftarrow Rb = 35 \cdot 9 = 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+\frac +\frac - 9 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 \tfrac = \tfrac, which is smaller than 1 but \geq \tfrac, 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 In computational number theory, Cipolla's algorithm is a technique for solving a congruence of the form :x^2\equiv n \pmod, where x,n \in \mathbf_, so ''n'' is the square of ''x'', and where p is an odd prime. Here \mathbf_p denotes the finite f ...
works better than Tonelli–Shanks if (and only if) S(S-1) > 8m + 20. However, if one instead uses Sutherland's algorithm to perform the discrete logarithm computation in the 2-Sylow subgroup of \mathbb_p, one may replace S(S-1) with an expression that is asymptotically bounded by O(S\log S/\log\log S). Explicitly, one computes e such that c^e\equiv n^Q and then R\equiv c^ n^ 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 The Riemann hypothesis is one of the most important conjectures in mathematics. It is a statement about the zeros of the Riemann zeta function. Various geometrical and arithmetical objects can be described by so-called global ''L''-functions, whic ...
is true, there exists a quadratic nonresidue z < 2\ln^2, making it possible to check every z up to that limit and find a suitable z within
polynomial time In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
. 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 In mathematics, an elliptic curve is a smooth, projective, algebraic curve of genus one, on which there is a specified point . An elliptic curve is defined over a field and describes points in , the Cartesian product of with itself. If the ...
. It is also useful for the computations in the
Rabin cryptosystem The Rabin cryptosystem is a family of public-key encryption schemes based on a trapdoor function whose security, like that of RSA, is related to the difficulty of integer factorization. The Rabin trapdoor function has the advantage that invertin ...
and in the sieving step of the
quadratic sieve The quadratic sieve algorithm (QS) is an integer factorization algorithm and, in practice, the second fastest method known (after the general number field sieve). It is still the fastest for integers under 100 decimal digits or so, and is considerab ...
.


Generalizations

Tonelli–Shanks can be generalized to any cyclic group (instead of (\mathbb/p\mathbb)^\times) and to ''k''th roots for arbitrary integer ''k'', in particular to taking the ''k''th root of an element of a
finite field In mathematics, a finite field or Galois field (so-named in honor of Évariste Galois) is a field that contains a finite number of elements. As with any field, a finite field is a set on which the operations of multiplication, addition, subtr ...
. 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. # Factor out powers of 2 from ''p'' − 1, defining ''Q'' and ''S'' as: p-1 = Q2^S with ''Q'' odd. # Let R \leftarrow n^, t\leftarrow n^Q \equiv R^2/n # Find b from the table such that b^2 \equiv t and set R \equiv R/b #return ''R''.


Tonelli's algorithm will work on mod p^k

According to Dickson's "Theory of Numbers"
A. Tonelli"Accademia nazionale dei Lincei, Rome. Rendiconti, (5), 1, 1892, 116-120." gave an explicit formula for the roots of x^=c \pmod
The Dickson reference shows the following formula for the square root of x^\bmod. :when p=4\cdot7+1, or s=2 (s must be 2 for this equation) and A=7 such that 29=2^\cdot7+1 ::for x^\bmod\equiv c then :::x \bmod\equiv \pm (c^+3)^\cdot c^ where \beta \equiv a\cdot p^ Noting that 23^ \bmod\equiv 529 and noting that \beta = 7\cdot29^ then :(529^ + 3)^\cdot 529^\bmod\equiv 24366 \equiv -23 To take another example: 2333^ \bmod\equiv 4142 and ::(4142^ + 3)^\cdot 4142^\bmod\equiv 2333 Dickson also attributes the following equation to Tonelli: :X\bmod\equiv x^\cdot c^ where X^\bmod\equiv c and x^\bmod\equiv c; Using p=23 and using the modulus of p^ the math follows: :1115^\bmod=2191 First, find the modular square root mod p which can be done by the regular Tonelli algorithm: :1115^\bmod\equiv 6 and thus \sqrt\bmod\equiv 11 And applying Tonelli's equation (see above): :11^\cdot 2191^\bmod \equiv 1115 Dickson's reference clearly shows that Tonelli's algorithm works on moduli of p^.


Notes


References

* * 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

*Gagan Tara Nanda - Mathematics 115: The RESSOL Algorith

*Gonzalo Tornari

{{DEFAULTSORT:Tonelli-Shanks algorithm Modular arithmetic Number theoretic algorithms Articles containing proofs