Notes on primality testing In order to use the RSA algorithm or other related cryptosystems effectively, it is necessary to generate large prime numbers. Formal proofs that very large (say 100 digit) numbers are prime are not easy to come by. However, there are tests which will allow one to conclude with an arbitrarily high degree of probability that a randomly chosen 100 digit number is prime (or that it isn't). Furthermore, the probability that a randomly chosen 100 digit number is prime is high enough that it is worth searching for primes in this way. Prime Number Theorem: the number of primes less than N is approximately N/ln(N). This means that a randomly chosen number less than N has a probability of about 1/ln(N) of being a prime. If N is 10^100, for example, ln(N) is (very) approximatly 200, so about 1 in 200 of numbers less than 10^100 (or 1 percent of odd numbers less than 10^100) will be prime. We need to develop some theory. First, we need a fact about multiplication mod p, where p is prime: for any prime p, there is a positive x < p such that x^(p-1) = 1 (this is always true because p-1 = phi(p)) but x^b is not 1 for any b < p-1. This can be proved by a counting argument. Such an x is called a "primitive root" mod p. There are phi(p-1) primitive roots mod p. (I can present the proof that there is at least one primitive root from the book). 1. if a has order l (l is the minimum number such that a^l = 1 mod p-1) and b has order k, and gcd(k,l) = 1, then ab has order kl. Certainly ab^kl = 1. It follows that the order of ab must be a divisor of kl. If ab^e = 1, then a^e and b^e are reciprocals. It is easy to see that reciprocals have the same order. But the only powers of a and b that can have the same order are the powers equal to 1, because k and l are relatively prime (the order of a^e must be a divisor of l, and the order of b^e must be a divisor of k, and the only common divisor is 1, so a^e = b^e = 1). 2. Now let q be a prime divisor of p-1 and let q^n be the largest power of q which is a divisor of p-1. We show that there is a residue class mod p of order q^n. There can be no more than q^n "q^nth roots of unity mod p" by the usual considerations -- for any exponent d, there can be no more than d dth roots of 1 for familiar algebraic reasons. Note that this is not true mod n when n is not prime -- for example, every odd number is a square root of 1 mod 8. Look at the proof to see why (ab = 0 implies a = 0 or b = 0 plays an essential role). Algebra mod p is especially well-behaved because every residue class has a reciprocal and there are no "zero divisors" except 0 itself. An element of order q^n will be a q^nth root of unity which is not also a q^(n-1)th root of unity (notice that the order of any q^nth root of unity has to be a divisor of q^n and so a power of q). Thus, if we can show that there are _exactly_ q^n q^nth roots of unity mod p, it follows that there are at least q^n-q^(n-1) elements of order q^n, because there can be no more than q^(n-1) q^(n-1)th roots of unity. Review the factorization x^k-1 = (x-1)(1+x+...+x^(k-1)). Prove that a polynomial of degree n can have no more than n roots -- if a is a root of P(x) = 0, then we have P(x)-P(a) = 0 as well, and P(x)-P(a) has x-a as a factor (because x^n-a^n always has x-a as a factor). We need to do the proof with care because we have to be able to see that it works in the unfamiliar mod p environment. Consider the dth roots of unity where d is any factor of p-1. x^(p-1) = 0 has p-1 roots -- every residue mod p other than 0 satisfies this because p-1 = phi(p). Now p-1 = de. x^(p-1) - 1 = (x^d)^e - 1 = (x^d-1)(x^d+x^(2d)+...+x^(e-1)d) The right hand factor has no more than d(e-1) = (p-1)-d roots because of its degree. The left hand factor has no more than d roots because of its degree. But the total number of roots must be p-1, so x^d-1 must have exactly d roots (and the other polynomial must have exactly (p-1)-d roots). Let d = q^n and we see that there must be an element of order q^n. Now for each prime factor q and maximal power q^n dividing p-1 we can find an element of this order, and multiplying all these elements together we find that the order of their product is the product of their orders (because the orders are relatively prime) so the order of the product must be p-1 which is what we wanted. There is a different way to do this proof: 1. prove that if order of a is k and order of b is n and gcd(k,n)=1 then order of ab is kn (as above). 2. observe that if a is an element of order k and n is a divisor of k, then there is an element of order n: a^(k/n) works. 3. Given an element a of order k and an element b of order n, it is then always possible to construct an element of order the least common multiple (lcm) of k and n. Observe that there will be an element c of order l/gcd(k,l). It then follows, because k and n/gcd(k,n) are relatively prime, that ac will be an element of order kn/gcd(k,n) = lcm(k,n). 4. It is then clear that it is possible to construct an element whose order is the least common multiple of _all_ orders of nonzero numbers mod p. Now suppose that this least common multiple (call it m) were less than p-1. It would follow that every nonzero residue mod p is an mth root of 1 (since its order is a divisor of m). But there can be no more than m mth roots of unity mod p, which is impossible if m < p-1. Definition: Suppose p is an odd prime. We call a positive x