0% found this document useful (0 votes)
8 views

Primality Test

The document discusses primality testing, covering both deterministic algorithms like the Division Test and probabilistic algorithms such as the Miller-Rabin Test. It explains the limitations of deterministic methods for large numbers and introduces Fermat's theorem as a basis for primality testing, while also highlighting the need for stronger tests like Miller-Rabin. Additionally, it includes historical context on prime number discovery methods, particularly the Sieve of Eratosthenes.

Uploaded by

kkaroundtheaxis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Primality Test

The document discusses primality testing, covering both deterministic algorithms like the Division Test and probabilistic algorithms such as the Miller-Rabin Test. It explains the limitations of deterministic methods for large numbers and introduces Fermat's theorem as a basis for primality testing, while also highlighting the need for stronger tests like Miller-Rabin. Additionally, it includes historical context on prime number discovery methods, particularly the Sieve of Eratosthenes.

Uploaded by

kkaroundtheaxis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Mathematical Foundations of Information

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

The complexity of the divisibility test is sub


exponential.
𝑶(𝟐𝒌/𝟐 )
9.3
Deterministic Primality testing Algorithm

Test n=143

𝑛=12
Prime numbers < 12 are: 2,3,5,7,11

11| 143 → it’s composite.

9.4
Deterministic Primality testing Algorithm

𝑤𝑖𝑡ℎ 𝑙𝑎𝑟𝑔𝑒 𝑛𝑢𝑚𝑏𝑒𝑟𝑠, 𝑡ℎ𝑖𝑠 𝑎𝑙𝑔𝑜𝑟𝑖𝑡ℎ 𝑖𝑠 𝑛𝑜𝑡 𝑝𝑟𝑎𝑐𝑡𝑖𝑐𝑎𝑙 𝑎𝑡 𝑎𝑙𝑙.

Think about n ≈ 21024 ≈ 10308.25 , , i.e 1024 bits of length


What is the required time to solve it by a computer
capable of doing 106 operations per second?

Tℎ𝑎𝑡 𝑐𝑜𝑚𝑝𝑢𝑡𝑒𝑟 𝑐𝑎𝑛 𝑑𝑜 106 ∗ 365 ∗ 24 ∗ 60 ∗ 60


= 3.15E+13≈ 1014 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠 𝑝𝑒𝑟 𝑦𝑒𝑎𝑟.

Thus, it needs 10308/2/1014=10140 years.

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.

• this test does not prove primality, but it can


be used to prove that a number is composite
Primality testing
Primality testing
• N=341
• Let’s try 3341 mod 341, we get:
3341 ≡ 56 𝑚𝑜𝑑 341
Does it mean that 341 is a composite number?
• Remember that:
• If bn-1 ≢ 1 (mod n) then n is composite

• We can say that 3 is a witness for the compositeness of 341


Primality testing
• N=561
• Given that:
a561 ≡ a (mod 561) for every integer a.
Does it pass the Fermat’s test?

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

• As in the Fermat Test, this test does not prove


primality, but it can be used to prove that a
number is composite
Miller–Rabin test
Input. Integer n to be tested, integer a as potential
witness.
1. If n is even or 1 < gcd(a, n) < n, return Composite.
2. Write n − 1 = 2kq with q odd.
3. Set a = aq (mod n).
4. If a ≡ 1 (mod n), return Test Fails.
5. Loop i = 0, 1, 2, . . . , k − 1
6. If a ≡ −1 (mod n), return Test Fails.
7. Set a = a^2 mod n.
8. Increment i and loop again at Step 5.
9. Return Composite.
Miller–Rabin test
Example: N=561
• Solution
• Using base 2, let 561 − 1 = 35 × 24, which
means q = 35, k = 4, and a = 2.
Miller–Rabin test
Example : N= 4033
Perform the Miller-Rabin test with a base of a=2, 4033
− 1 = 63 ×26, which means q is 63 and k is 6.

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 :

1. Generate the sequence 2 to n


2. Print the smallest number in the remaining
sequence, which is the new prime p.
3. Remove all the multiples of p.
4. Repeat 2 and 3 until the sequence is
exhausted.
Hundreds Chart
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
1 – Cross out 1; it is not prime.
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
2 – Leave 2; cross out multiples of 2
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
3– Leave 3; cross out multiples of 3
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
4– Leave 5; cross out multiples of 5
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
5– Leave 7; cross out multiples of 7
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
6–Leave 11; cross out multiples of 11
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
All the numbers left are prime
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
The Prime Numbers from 1 to 100 are as follows:

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

You might also like