Square Roots Modulo p
This is Lab 14 from Clint by Hugh Montgomery, edited by Elad Zelingher. All rights reserved to Hugh Montgomery.
In discussing pseudoprime tests and primitive roots we have generated a circle of ideas that we now harness to give a quick method for finding the roots of the quadratic congruence . The algorithm involved is described in detail in §2.9 of NZM. Before confronting the full algorithm, we consider two instructive examples.
Problem 1
If then the solutions of the congruence are given by . For example, suppose we wish to find solutions of the congruence . By using the program Power we find that (mod 103). Hence the desired solutions are , as we may confirm by verifying that . Use the program Power in this way to find the solutions of the congruence . What happens to this procedure if is a quadratic nonresidue of ? For example, what happens if you try to use this method to solve the congruence ? Explain why it is always the case that exactly one of and is a quadratic residue, if is a prime, , and .
Problem 2
Suppose that is a quadratic nonresidue of , so that by Euler's criterion . If then it follows that solutions of the congruence are given by . However, to make use of this observation, we need to find the quadratic nonresidue . Rather than give a deterministic algorithm for this, we simply try at random, until a quadratic nonresidue is found. When is selected, we compute . Then either , in which case we are done, or else (i.e., ), in which case we start over with a new value of . Since exactly half of the nonzero residue classes are quadratic nonresidues, the expected number of such trials is 2 . An algorithm of this kind is referred to as a Monte Carlo algorithm, or as a probabilistic algorithm. Since the quadratic nonresidues seem to be randomly distributed between 0 and , we do not take the trouble to use a random-number generator in selecting the values of : It is enough to try consecutive integers (skipping the perfect squares). For example, , and , but , and hence the solutions of are given by .
Problem 3
We now proceed to the general case. To see how one would find the solutions of the congruence , use SqrtDem with input and , and follow the prompts. What happens if you input input and ? To get the same result without all the discussion, use SqrtModP.
Problem 4
Apply the program SqrtDem to various values of with . What is the power of 2 dividing ?
Problem 5
Suppose that is prime and that . Explain why is the sole solution of the congruence . Use this principle and the program Power to determine the unique root of the congruence .
Problem 6
Suppose that is prime and that . Explain how a probabilistic algorithm might be constructed to locate the roots of the congruence . (Hint: One might , where is chosen randomly.) The congruence in question has exactly 3 roots, say . Since 1 is one of these roots, we may suppose that . Explain why , and . Thus if one of these roots can be found then so can the other. Use your method to find the solutions of the congruence . What is the probability that a given trial will be successful?
Problem 7
For the programmer. Write a program that finds the roots of the congruence .(Hint: Recall Problems 6 and 8 on p. 115 of the text.)
Discussion
The algorithm we have used to take squareroots modulo was invented by Dan Shanks in 1972; he called it RESSOL, because it SOLves for RESidues. This algorithm is very similar to one described much earlier by Tonelli. Other methods for taking squareroots modulo have been given by Lehmer (related to earlier work of Cipolla), by Peralta, and by Adleman, Manders, and Miller. In addition, more general algorithms have been devised for factoring a polynomial modulo ; such an algorithm could be applied to the polynomial in order to find the squareroots of modulo . One such algorithm has been proposed by Berlekamp, but the more recent method of Cantor and Zassenhaus seems to be the method of choice. Among the various methods, it is interesting to note that while Shanks' method is somewhat slower if is divisible by a high power of , Peralta' method, which depends on the arithmetic of polynomials (mod ), is faster in this case. For more details one may consult the following papers.
- L. Adleman, K. Manders, and G. Miller, On taking roots in finite fields, 18th IEEE Annual Sympos. Foundations of Computer Science, Providence, RI, 1977.
- E. R. Berlekamp, Factoring polynomials over large finite fields, Math. Comp. 24 (1970), 713-735.
- D. G. Cantor and H. Zassenhaus, A new algorithm for factoring polynomials over finite fields, Math. Comp. 36 (1981), 587-592.
- D. H. Lehmer, Computer technology applied to the theory of numbers, Studies in Number Theory (W. J. LeVeque, ed.), Math. Assoc. Amer., 1969.
- R. Peralta, A simple and fast probabilistic algorithm for computing square roots modulo a prime number, IEEE Trans. Info. Thy. IT-32 (1986), 846-848.
- M. Rabin, Probabilistic algorithms in finite fields, SIAM J. Comp. 9 (1980), 273-280.
- D. Shanks, Five number-theoretic algorithms, Proceedings of the Second Manitoba Conference on Numerical Mathematics, 1972, pp. 51-70.