400 likes | 619 Views
Lecture 26. Primality Test. Professor Sin-Min Lee Department of Computer Science San Jose State University. Primality Testing A Classical Number Theoretic Problem:. Definition: A positive integer n is prime iff its only divisors are 1 and n . Examples: 7 is a prime.
E N D
Lecture 26 Primality Test Professor Sin-Min Lee Department of Computer Science San Jose State University
Primality Testing A Classical Number Theoretic Problem: Definition: A positive integer n is prime iff its only divisors are 1 and n. Examples: 7 is a prime. 111 is not a prime. Is 225593397919 a prime? Is 2^229-91 a prime? (2^229-91 =862718293348820473429344482784628181556388621521298319395315527974821 )
Primality Testing: Naïve n is a prime iff its only divisors are 1 and n
Analysis Time complexity: T(n)= (n) – linear in n In fact, for every prime n, the running time is n. If n is a 400 digit number, that’s bad news. Absolutely infeasible.
Primality Testing - II (define (divides? a b) (= (remainder b a) 0)) (define (prime? n) (= n (smallest-divisor n))) (define (smallest-divisor n) (define (find-divisor n i) (cond ((> i (sqrt n)) n) ((divides? i n) i) (else (find-divisor n (+ i 1))))) (find-divisor n 2))
Analysis • Correctness: If n is not a prime, then n=a * b for a,b>1. Then at least one of them is n. So n must have a divisor smaller then n. • Time complexity: • (n) . For a number n, we test at most n • numbers to see if they divide n. If n is a 800 digit number, that’s is also very bad. Absolutely infeasible.
The Fermat Test: Do 1000 times: Pick a random a < n , and compute an (mod n) If a then for sure n is not a prime. If all 1000 tests passed, declare that n is a prime. The Fermat Primality Test Fermat’s little theorem: If n is a prime number then: an = a (mod n) for every 0 < a < n, integer
Time complexity To test if n is a prime. We run 100 tests. Each takes about log(n) multiplcations. T(n) = O(log n)
Some mathematical facts Fermat’s theorem: Every prime will always pass the test. A fact: If n is not a Carmichael number then for at least half of the choices of a, an <> a (mod n). • Definition: A Carmichael number, is a number • such that • n is Composite, and • n always passes the test. • For every a, an = a (mod n) • For example, n=225593397919 is a Carmichael • number!.
Correctness Suppose we do the test t=100 times. • If n is a prime we are never wrong. • If n is a composite and not a Carmichael number • we are wrong with probability at most 2-100 . • Error probability smaller than the chance the hardware • is faulty. • If n is a Carmichael number, we are always wrong
A probabilistic algorithm An algorithm that uses random coins, and for every input gives the right answer with a good probability. Even though Carmichael numbers are very rare Fermat test is not good enough. There are inputs on which it is wrong. There are modifications of Fermat’s test, that for every input give the right answer, with a high probability.
Fermat’s Little Theorem If n is prime, then a n-1 mod n = 1 for any a such that 1 <= a <= n-1 n = 3, a = 2 then 22 mod 3 = 1 n = 7, a = 4 then (46 mod 7) = (4096 mod 7) = 1
First Prime Number Test function Fermat (n) a := random integer between 1 and n-1 if (expomod (a,n-1,n) = 1 then return true else return false
First Prime Number Test function Fermat (n) a := random integer between 1 and n-1 if (expomod (a,n-1,n) = 1 then return true // n might be prime else return false // n is not prime • Know if n is not prime, but what don’t you know? • Why do you get “might be prime”?
Converse of Fermat’s Little Thm. • Converse of “a then b” might not hold: • converse: if b then a • What is the converse of Fermat’s little theorem?
Converse of Fermat’s Little Theorem If a n-1 mod n = 1 for any a such that 1 <= a <= n-1, then n is prime. This is not a true statement.
Converse of Fermat’s Little Thm. • What does that mean for the algorithm?
False Witnesses If Fermat(n) returns “true” then n might or might not be prime. Consider 15. How common are false witnesses? less than 3.3% of the possible witnesses for n < 1000 are false. Consider 651,693,055,693,681. Have 99.9965% chance of picking a false witness.
Fermat Test • For any probability d > 0, there are infinitely many numbers for which Fermat discovers compositeness with probability less than d. • Or, Fermat is not p-correct for any p. • Repeating Fermat doesn’t guarantee convergence.
A better test • n is an odd integer greater than 4. • s,t so that n-1 = 2st where t is odd. • Define B(n) so that a in B(n) if: • at mod n = 1, or • for some i < s, a(2^i) tmod n = n - 1 • Then a in B(n) for all a between 2 and n-2 when n is prime.
The better test • This is called the Miller Rabin test. • to find out if n is prime, pick a number between 2 and n-2, • see if that number is in B(n). • It is 3/4-correct in the worst case. • Two witness give .938-correct answer.