Notes on Primality Testing Dan Klain last updated April 23, 2013 Comments and corrections are welcome
These supplementary notes summarize some of results on primality testing pre- sented at the end of class on April 11, as well as some material to be presented on April 18.
l
Given n ∈ Z, how can we determine if n is prime without actually factoring n? √ If n is composite, then n must have a prime factor p ≤ n. So one√ way to test for primality is to use test division of n by p for all primes p ≤ n. But this is impractical for large n, even on a fast computer. There are better approaches.
l
Fermat’s theorem says that, if p is prime and gcd(a, p) = 1, then ap ≡ a mod p. Suppose that n is a large odd number. It follows from Fermat’s theorem that, if 2p 6≡ 2 mod n, then n cannot be prime. On the other hand, if 2n ≡ 2 mod n, we still don’t know whether or not n is prime. If such a number n is composite, n is said to be a pseudoprime to base 2. For example, 341 = 31 · 11 is a pseudoprime to base 2. Moreover, if n is a pseudoprime to base 2, so is 2n − 1. It follows that there are infinitely many pseudoprimes to base 2. Pseudoprimes to other bases are defined analogously. A composite number n is a pseudoprime to base a if gcd(a, n) = 1 and an ≡ a mod n. Unfortunately there are pseudoprimes for every base. In fact, some numbers are pseudoprimes for every base at once! An composite integer is a Carmichael number if an ≡ a mod n for all integers a. In particular, a Carmichael number is a pseudoprime to every base. The smallest Carmichael number is 561. Here is a condition for finding more of them. Theorem 1. A composite positive integer n is a Carmichael number if and only if n is square-free, and if, for each prime p|n, we have (p − 1)|(n − 1).
Proof. To begin, suppose that n is a Carmichael number. If a prime p divides n, k write n = p Q, where gcd(p, Q) = 1. Let r be a primitive root for Upk . By the 1 2
Chinese remainder theorem, there exists s ∈ Un such that s ≡ 1 mod Q and s ≡ r mod pk. Since n is a Carmichael number and s is a unit mod n, we have sn−1 ≡ 1 mod n, so that sn−1 = 1 + tn = 1 + tpkQ, for some integer t. Modulo pk this becomes
rn−1 ≡ 1 mod pk.
Since r is primitive mod pk, the divisibility theorem for order implies that
φ(pk)|(n − 1), so that
1) pk−1(p − 1)|(n − 1).(
In particular, (p − 1)|(n − 1) for each prime p dividing n. Moreover, if k > 1, then (1) implies that p|(n − 1). Since p|n, this contradicts the assumption that p is prime. It follows that k = 1, so that n is square-free. Conversely, suppose that n is a square-free composite number, and that each prime p dividing n satisfies (p − 1)|(n − 1). In this case we can write n = p1 ··· pk pi for primes p1 < ··· < pk. Let a ∈ Z. By Fermat, we have a ≡ a mod pi for each i.
Suppose that gcd(a, n) = 1. For each i we have n − 1 = (pi − 1)bi for some bi. It follows that
n−1 (pi−1)bi pi−1 bi a = a = (a ) ≡ 1 mod pi. The Chinese remainder theorem then implies an−1 ≡ 1 mod n, so that an ≡ a mod n. n n By a similar argument, pj ≡ pj mod pi for each i 6= j, while obviously pj ≡ 0 ≡ n pj mod pj as well. Again the Chinese remainder theorem implies that pj ≡ pj mod n for each j. If gcd(a, n) = d, then d is a square-free product of some of the distinct primes pj, and a = dk with gcd(k, n) = 1, so that
an = dnkn ≡ dk ≡ a mod n.
In other words, n is a Carmichael number. K
The existence of Carmichael numbers suggests a better approach is needed for efficient primality testing.
l 3
Jacobi symbols. We now introduce a generalization of the Legendre symbol to a larger class of numbers. If p is an odd prime and a ∈ Z, the Legendre symbol for the pair (a, p) is defined by 0 if p|a, a = 1 if a is a quadratic residue mod p, p −1 otherwise. When p6 | a, the Euler criterion asserts that a p−1 = (−1) 2 . p Gauss’s Lemma and the Law of Quadratic Reciprocity gave us more efficient means for computing Legendre symbols. Suppose instead that a ∈ Z and n > 1 is an odd integer, including the case where n is composite. By the unique factorization theorem, n has a unique expression as a product of powers of distinct odd primes:
e1 e2 ek n = p1 p2 ··· pk . Define the Jacobi symbol for the pair (a, n) by a a e1 a e2 a ek (2) = ··· n p1 p2 pk where each factor in the product above is a Legendre symbol. Evidently the Jacobi symbols can take values 0, 1 or −1. Note that if n is itself an odd prime, then the Jacobi symbol is simply the usual Legendre symbol. Euler’s criterion does not typically tell us the value of the Jacobi symbol directly, although if gcd(a, n) = 1 then Euler’s criterion can be applied to each prime pi in the factorization of n to compute each factor in the expression (2). The downside of this approach is that one needs to know the factorization of the number n. Fortunately, there are easier ways to compute Jacobi symbols. The following elementary properties follow easily from similar properties of Legendre symbols. Proposition 1. Let a, b ∈ Z, and let m, n > 1 be odd integers.