Skip to main content

Powering Algorithms and Primality Testing

note

This is Lab 7 from Clint by Hugh Montgomery, edited by Elad Zelingher. All rights reserved to Hugh Montgomery.

The number ak(modm)a^{k}(\bmod m) can be determined by k1k-1 multiplications of residue classes, but this is slow if kk is large. There is a much faster way: The values of a,a2,a4,a8,a, a^{2}, a^{4}, a^{8}, \ldots, a2i,,(modm)a^{2^{i}}, \ldots, \quad(\bmod m) can be determined, by repeated squaring, in only ii multiplications. The binary expansion of kk provides a representation of kk as a sum of powers of 2 , and hence aka^{k} is a product of an appropriate collection of the numbers a2ia^{2^{i}}. For example, 13=23+22+2013=2^{3}+2^{2}+2^{0}, and hence a13=a23a22a20a^{13}=a^{2^{3}} \cdot a^{2^{2}} \cdot a^{2^{0}}. The exact number of multiplications required by this method varies irregularly with kk, but it never exceeds 2log2k2 \log _{2} k. The binary expansion of kk can be built from the bottom up, or from the top down. The former of these two methods is discussed on pages 76, 77 of §2.4 of NZM.

Problem 1

Apply the program PwrDem using its three different algorithm types to several values of a,ma, m until the process is clear to you. Apply the Left-to-Right and the Right-to-Left binary methods to the same kk. How do the number of multiplications compare?

Problem 2

If kk has binary expansion k=2i1+2i2++2irk=2^{i_{1}}+2^{i_{2}}+\cdots+2^{i_{r}} with i1<i2<<iri_{1}<i_{2}<\cdots<i_{r}, then our powering algorithm requires ir+r1i_{r}+r-1 multiplications to calculate aka^{k}. In particular, it takes 6 multiplications to calculate a15a^{15}. Show that a15a^{15} can be obtained with only 5 multiplications.

Problem 3

The program Power evaluates ak(modm)a^{k}(\bmod m). Use the program Power to evaluate 2m1(modm)2^{m-1}(\bmod m) where m=(10171)/9=m=\left(10^{17}-1\right) / 9= 11111111111111111. Assuming Fermat's congruence, (Theorem 2.7 of NZM), this provides a quick (but indirect) proof that 11111111111111111 is composite. Apply the program Factor to 11111111111111111, and note how long it runs. With large numbers mm (of hundreds of digits), it is often the case that a quick proof that mm is composite can be given, even though we know of no way to obtain the factors of mm within a reasonable amount of time.

Problem 4

Is 91 prime? Evaluate 290(mod91)2^{90}(\bmod 91). Is 341 prime? Evaluate 2340(mod341)2^{340}(\bmod 341). Now evaluate 33403^{340} (mod 341). What do you conclude?

Problem 5

We have no quick method to find k!(modm)k!(\bmod m) akin to our quick method for calculating powers. There are a few special cases (such as (p1)!(modp)(p-1)! \pmod{p}), but in general the fastest method known involves simply performing the k1k-1 multiplications. If a quick method could be found, then it would have important applications (to factoring, for example). Suppose that you are in possession of a quick method for calculating (2kk)(modm)\binom{2 k}{k}(\bmod m).

Explain how this could be used to provide a quick method for calculating k!(modm)k!(\bmod m). Suppose you have a quick method for calculating k!(modm)k!(\bmod m). Explain how this could be used to provide a quick method for factoring mm.

Discussion

If 0<a<m0<a<m and am1≢1(modm)a^{m-1} \not \equiv 1(\bmod m) then mm is composite. Since it is easy to calculate powers modulo mm, this provides a quick proof that mm is composite-when it works. Unfortunately, the converse is false, but the counterexamples seem to be rare, so we call mm a probable prime base aa if mm is odd and am11(modm)a^{m-1} \equiv 1(\bmod m). If mm is a probable prime base aa but is nevertheless composite, then we call mm a pseudoprime base aa, or, briefly, mm is a PSP(a)\operatorname{PSP}(a). If mm is found to be a probable prime base 2 , then we might try base 3, and so on, but there exist composite mm that are probable primes to every base aa for which (a,m)=1(a, m)=1. To see how this might happen, suppose that mm is a composite squarefree number with the peculiar property that (p1)(m1)(p-1) \mid(m-1) for every prime number pp dividing mm. (The least such mm is 561 .) Suppose that (a,m)=1(a, m)=1. If pmp \mid m then (a,p)=1(a, p)=1, and hence ap11(modp)a^{p-1} \equiv 1(\bmod p). Since (p1)(m1)(p-1) \mid(m-1), it follows that am11(modp)a^{m-1} \equiv 1(\bmod p). Since this congruence holds for every pp dividing mm, it holds modulo the product of all the primes dividing mm. But we have assumed that mm is squarefree; hence am11(modm)a^{m-1} \equiv 1(\bmod m). An odd composite number such that am11(modm)a^{m-1} \equiv 1(\bmod m) whenever (a,m)=1(a, m)=1 is called an absolute pseudoprime, or Carmichael number. The least Carmichael number is 561 ; indeed, it can be shown that if mm is a Carmichael number then mm is of the form we considered: mm is squarefree and (p1)(m1)(p-1) \mid(m-1) whenever pmp \mid m. (This is called Korselt's criterion; see Problems 25-27 at the end of §2.8\S 2.8 of NZM.) It is not hard to show that there exist infinitely many pseudoprimes to any given base (see Problem 19 at the end of §2.4\S 2.4 of NZM), and it is easy to construct numerical examples of Carmichael numbers and to give arguments that suggest that Carmichael numbers form a fairly rich subset of the integers (by methods akin to the construction of Problem 20 of §2.8\S 2.8 of NZM). In particular, P. Erdős (On pseudoprimes and Carmichael numbers, Publ. Math. Debrecen 4 (1956), 201-206) formulated a heuristic argument that suggests that the number C(x)C(x) of Carmichael numbers not exceeding xx is larger than x1ϵx^{1-\epsilon} for all sufficiently large xx. Although Erdős's conjecture is presumably true, it seems that the ϵ\epsilon tends to 0 slowly, since numerical studies have revealed that C(1010)=1547C\left(10^{10}\right)=1547, and that C(1015)=105212C\left(10^{15}\right)=105212. Nevertheless, it was finally proved that there do indeed exist infinitely many Carmichael numbers. W. R. Alford, A. Granville, and C. Pomerance, (There are infinitely many Carmichael numbers, Ann. of Math. (2) 139 (1994), 703-722) showed that C(x)>x2/7C(x)>x^{2 / 7} for all sufficiently large xx.

Since the pseudoprime test fails to establish the compositeness of some composite numbers, we consider a slightly more elaborate test, which, however, involves no more calculation than before. If mm is odd, we repeatedly divide 2 into m1m-1, until we obtain a representation m1=2rdm-1=2^{r} \cdot d with dd odd. Suppose that a≢0(modm)a \not \equiv 0(\bmod m). Compute ad(moda^{d}(\bmod m)m). Next, repeatedly square, forming the sequence a2d,a4d,,a(m1)/2(modm)a^{2 d}, a^{4 d}, \ldots, a^{(m-1) / 2}(\bmod m). Let xx denote this last residue class computed. If x2≢1(modm)x^{2} \not \equiv 1(\bmod m) then am1≢1a^{m-1} \not \equiv 1 (modm)(\bmod m), and hence mm is composite, by Fermat's congruence. Suppose now that x21x^{2} \equiv 1 (modm)(\bmod m). If x≢±1(modm)x \not \equiv \pm 1(\bmod m) then mm is composite by virtue of Lemma 2.10 of NZM. More generally, if in the sequence of powers computed we find an entry x≢±1(modm)x \not \equiv \pm 1(\bmod m) followed by an entry 1 , then x21(modm)x^{2} \equiv 1(\bmod m), and hence mm is composite. This test is more stringent than the previous one; if it is inconclusive then we call mm a strong probable prime base aa. If in addition mm is composite then we call mm a strong pseudoprime base aa, or mm is an SPSP(a)\operatorname{SPSP}(a). In practice, we abandon the repeated squaring if a value ±1\equiv \pm 1 (modm)(\bmod m) is encountered, since the conclusion is already clear. The exact sequence of steps performed is exhibited on p. 78 of NZM. It is known that if mm is composite then there are at least m/4m / 4 bases aa such that the compositeness of mm is demonstrated by applying this strong pseudoprime test base aa. Thus if mm survives this test for several values of aa, we can be reasonably confident that mm is prime - such an mm might be called an "industrial grade prime".

Problem 6

By means of lengthy calculation (see C. Pomerance, J. L. Selfridge, and S. S. Wagstaff Jr., The pseudoprimes to 2510925 \cdot 10^{9}, Math. Comp. 35 (1980), 1003-1026), it has been found that there are only 13 odd integers m<25109m<25 \cdot 10^{9} that are SPSP(a)\operatorname{SPSP}(a) for a=2,a=3a=2, a=3, and a=5a=5. Of these, only one, namely m=3215031751m=3215031751 is also a SPSP(7)\operatorname{SPSP}(7). Apply the strong pseudoprime test to this mm with bases a=2,3,5,7a=2,3,5,7, and 11. For example, try using the program SPsPDem with input a=2a=2, m=3215031751m = 3215031751.

Problem 7

By appropriate use of the program Power, show that 4369 and 4371 are both probable primes base 2. Are either of these numbers strong probable primes base 2? Are either of these numbers prime? (Use the program SPsP to answer this question, not Factor.) Are either of these numbers Carmichael numbers?

Problem 8

Factor 561 using Factor, verify that 561 is squarefree, and that p1560p-1 \mid 560 for every prime pp dividing 561. Hence deduce that 561 is a Carmichael number.

Problem 9

If mm is a PSP(a)\operatorname{PSP}(a) but not a SPSP(a)\operatorname{SPSP}(a) then the strong pseudoprime test locates a number xx such that x≢±1(modm)x \not \equiv \pm 1(\bmod m), but x21(modm)x^{2} \equiv 1(\bmod m). In such a situation not only is it established that mm is composite, but also a proper divisor of mm can be exhibited, namely gcd(x1,m)\gcd(x-1, m). Apply the program SPsPDem to m=561m=561 with a=2a=2.

Problem 10

Numerical evidence suggests that most pseudoprimes are squarefree. To explain this, show that if mm is a PSP(a)\operatorname{PSP}(a), and if pp is a prime such that p2mp^{2} \mid m, then

ap11(modp2).\begin{equation*} a^{p-1} \equiv 1 \quad\left(\bmod p^{2}\right) . \tag{1} \end{equation*}

(Hint: ama(modm)a^{m} \equiv a(\bmod m), and hence ap1(am)p1am(p1)(modm)a^{p-1} \equiv\left(a^{m}\right)^{p-1} \equiv a^{m(p-1)}(\bmod m). But ϕ(p2)\phi\left(p^{2}\right) divides m(p1)m(p-1).) Conversely, show that if pp is a prime such that (1) holds then p2p^{2} is a PSP(a)\operatorname{PSP}(a). Only a few primes have been found for which 2p11(modp2)2^{p-1} \equiv 1\left(\bmod p^{2}\right), although it is believed that infinitely many exist. The least such prime is 1093 . Use the program Factor to verify that 1093 is prime, and the program Power to verify that 2109212^{1092} \equiv 1 (mod10932)\left(\bmod 1093^{2}\right). Is 1093 a SPSP(2)\operatorname{SPSP}(2) ?

For an extensive account of primality testing see H. C. Williams, Primality testing on a computer, Ars Comb. 5 (1978), 127-185.