Title: Primality Testing
1Primality Testing
- Lecture 35 Primality Testing
- Fermats Theorem
- Primality Testing
- Miller-Rabin
- Lecture 34
- Randomized Algorithms
- Introduction
- Numerical Algorithms
- Monte Carlo Algorithms
- Las Vegas Algorithms
- Average vs. Expected Runtime
- Random vs. Pseudorandom
2Fermats Little Theorem
- If n is prime, then a n-1 mod n 1
- for any a such that 1 lt a lt n-1
n 3, a 2 then 22 mod 3 1
n 7, a 4 then 46 mod 7 4096 mod 7 1
3First 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
4Recall Expomod
function expomod (a,n,z) computes an mod z i
n r 1 x a mod z while i gt 0 if i is
odd then r rx mod z x x2 mod z i i
?2 return r
5Converse of Fermats Little Thm.
- Converse of a then b might not hold
- converse if b then a
- a is sufficient, but not necessary, for b.
- What is the converse of Fermats little theorem?
6Converse of Fermats Little Theorem
- If a n-1 mod n 1 for any a such that 1 lt a
lt n-1, - then n is prime.
This is not a true statement.
7Converse of Fermats Little Thm.
- What does that mean for the algorithm?
8First 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 dont you know?
- Why do you get might be prime?
9False Witnesses
If Fermat(n) returns true then n might or might
not be prime. Consider 15 414mod151, but 15
clearly isnt prime We call such a number a
in an-1modn1 a false witness that n
is prime How common are false witnesses?
Less than 3.3 of the possible witnesses for n lt
1000 are false. But consider
651,693,055,693,681. Have 99.9965 chance of
picking a false witness (!)
10Fermat Test
- For any probability d gt 0, there are infinitely
many numbers for which Fermat discovers
compositeness with probability less than d.
i.e. (1-d) chance of false witness. - Or, Fermat is not p-correct for any p. An
algorithm is p-correct if it yields a correct
answer with probability at least p for any
instance - Implication repeating Fermat doesnt improve our
confidence in the result.
11A 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 lt s, a(2i) t mod n n - 1
- Then a in B(n) for all a between 2 and n-2 when n
is prime.
12A Better Test
- function Btest(a,n)
- s? 0 t? n-1
- repeat
- s? s1 t? floor(t/2)
- until tmod21
- x? expomod(a,t,n)
- if x1 or x n-1 then return true
- for i 1 to s-1 do
- x? x2modn
- if x n-1 then return true
- return false
13A Better Test
- function Btest(a,n)
- s? 0 t? n-1
- repeat
- s? s1 t? floor(t/2)
- until tmod21
- x? expomod(a,t,n)
- if x1 or x n-1 then return true
- for i 1 to s-1 do
- x? x2modn
- if x n-1 then return true
- return false
Check if 158 belongs to B(289)
288/2 144 s1 144/2 72 s2 72/2 36 s3 36/2
18 s4 18/2 9 s5
x atmodn 1589mod289131
true
14A Better Test
158 is a strong false witness for 289 172
- function Btest(a,n)
- s? 0 t? n-1
- repeat
- s? s1 t? floor(t/2)
- until tmod21
- x? expomod(a,t,n)
- if x1 or x n-1 then return true
- for i 1 to s-1 do
- x? x2modn
- if x n-1 then return true
- return false
Check if 158 belongs to B(289)
288/2 144 s1 144/2 72 s2 72/2 36 s3 36/2
18 s4 18/2 9 s5
x atmodn 1589mod289131
true
15A Better Test
158 is a strong false witness for 289 172
- function Btest(a,n)
- s? 0 t? n-1
- repeat
- s? s1 t? floor(t/2)
- until tmod21
- x? expomod(a,t,n)
- if x1 or x n-1 then return true
- for i 1 to s-1 do
- x? x2modn
- if x n-1 then return true
- return false
Check if 158 belongs to B(289)
288/2 144 s1 144/2 72 s2 72/2 36 s3 36/2
18 s4 18/2 9 s5
x atmodn 1589mod289131
true
There are considerably fewer strong false
witnesses than false witnesses
16A Better Test
- Theorem Consider arbitrary odd n gt 4
- If n is prime, then B(n) a2ltaltn-2
- If n is composite, then B(n) lt (n-9)/4
- If you choose a number between 2 and n-2 with
equal probability, then there are only (n-9)/4
opportunities for a false positive. - What percentage is this? Is there a p for
which this algorithm is p-correct?
17A 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.
- Implication repeating Miller Rabin does increase
our confidence in the result (!) - Two witness give .9375-correct answer.