Primality Test
Primality Test
Security
Primality testing
Agenda
– Primality testing
• Deterministic Algo. (Division Test)
• Probabilistic Algo. (Miller Rabin Test)
– The distribution of the set of primes
Deterministic Primality testing Algorithm
Divisibility Algorithm
LEMMA: If n is a composite, then its smallest prime factor is in 𝑛
Boolean isPrime(integer n)
if ( n < 2 ) return false
for(i = 2 to 𝑛)
if( i |n ) // “divides”
return false
return true
Test n=143
𝑛=12
Prime numbers < 12 are: 2,3,5,7,11
9.4
Deterministic Primality testing Algorithm
9.5
Probabilistic Primality testing
• Fermat Test
• Miller-Rabin Test
Primality testing
• Could we use Fermat’s theorem to test the
primality of a given number n?
Fermat’s little theorem,
if p is prime, then ap−1 ≡ 1 (mod p)
(unless p divides a).
• Thus if p was prime, then the right-hand
side of the theorem would equal 1;
• since it does not equal 1, n is not prime
Primality testing
Primality testing
• example: N=33
• Let’s try 232 mod 33, we get:
• 232 ≡ 31 𝑚𝑜𝑑 33 ,thus 232 ≢ 1 𝑚𝑜𝑑 33
• Which means that 33 is a composite number.
• Also we can apply Fermat’s little theorem
version 2 as:
• 233 ≡ 29 𝑚𝑜𝑑 33 ,thus 233 ≢ 2 𝑚𝑜𝑑 33 and
thus 33 is a composite.
Primality testing
• Example : N=341
• Let’s try 2341 mod 341, we get:
2341 ≡ 2 𝑚𝑜𝑑 341
Does it mean that 341 is a prime number?
• Remember that:
• (Fermat Primality Test)
Let n > 1 be an odd integer.
Choose an integer a with 1 < a < n. (Often, we
choose a = 2)
If an-1 ≢ 1 (mod n) then n is composite.
Observations:
1. No witnesses for 561 → we can’t say it’s a composite
due to the above test.
2. Since 561=3.11.17 (it’s a composite by factorization)
3. Composite numbers having no witnesses are called
Carmichael numbers
Primality testing
• We need something stronger than Fermat’s
little theorem in order to test whether a
number is (probably) prime.
• The following property of prime numbers is
used to formulate the Miller–Rabin Test,
which has the agreeable property that
every composite number has a large
number of witnesses
Miller–Rabin test
Proof of prop. 3.16
Miller–Rabin test
Test fails
Miller–Rabin test
Summary:
1. Miller Test is polynomial algorithm
2. The Miller–Rabin test is a powerful and practical method
for finding large numbers that are “probably prime.”
3. every composite number has many Miller–Rabin
witnesses
4. To have solid evidence that n is prime :
1. Do 50 or 100 repetitions of the Miller–Rabin test and for each:
1. Select a as a random base
2. If a is a witness for n , then n is not a prime. Stop the test.
2. if there is no witnesses for n, then n is probably prime.
Miller–Rabin test
Use Miller test for n=91?
The distribution of the set of primes
First 100 primes
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53
59 61 67 71 73 79 83 89 97 101 103 107
109 113 127 131 137 139 149 151 157 163
167 173 179 181 191 193 197 199 211 223
227 229 233 239 241 251 257 263 269 271
277 281 283 293 307 311 313 317 331 337
347 349 353 359 367 373 379 383 389 397
401 409 419 421 431 433 439 443 449 457
461 463 467 479 487 491 499 503 509 521
523 541 …
Eratosthenes and the Primes
• Eratosthenes (276 B.C. - 194 B.C., Greece) was a
Greek mathematician.
• Eratosthenes was the librarian at Alexandria,
Egypt.
• He made several discoveries and inventions
including a system of latitude and longitude. He
was the first person to calculate the
circumference of the Earth, and the tilt of the
earth's axis.
• Eratosthenes devised a 'sieve' to discover prime
numbers.
Sieve
The Sieve of Eratosthenes
• Algorithm to enumerate primes ≤ n :
2,3,5,7,11,13,17,19,
23,29,31,37,41,43,47,
53,59,61,67,71,73,
79,83,89,97
The distribution of the set of primes
• Definition.
For any number X, let
π(X) = (# of primes p satisfying 2 ≤ p ≤ X).
• For example,
• π(10) = 4, since the primes between 2 and 10
are 2, 3, 5, and 7.
The distribution of the set of primes
• Theorem 3.20 (The Prime Number Theorem).
Example:
• How many primes would we expect to find between 900000 and
1000000?
• Number of primes between 900000 and 1000000
= π(1000000) − π(900000) ≈ 6737
The distribution of the set of primes
• How many primes p satisfy
21023 < p < 21024?
END