Finding Large Primes Gavriel Yarmish Brooklyn College Joshua Yarmish Pace University Jason Yarmish NYU Tandon School of Engineering
Total Page:16
File Type:pdf, Size:1020Kb
Page 1 of 14 Finding Large Primes Gavriel Yarmish Brooklyn College Joshua Yarmish Pace University Jason Yarmish NYU Tandon School of Engineering Keywords: Prime Number Theorem, Digital Root, Fermat primality test, Euler Test, Miller-Rabin test, GNU GMP Library Abstract: In this paper we present and expand upon procedures for obtaining large d digit prime number to an arbitrary probability. We use a layered approach. The first step is to limit the pool of random number to exclude numbers that are obviously composite. We first remove any number ending in 1,3,7 or 9. We then exclude numbers whose digital root is not 3, 6, or 9. This sharply reduces the probability of the random number being composite. We then use the Prime Number Theorem to find the probability that the selected number n is prime and use primality tests to increase the probability to an arbitrarily high degree that n is prime. We apply primality tests including Euler’s test based on Fermat Little theorem and the Miller-Rabin test. We computed these conditional probabilities and implemented it using the GNU GMP library. 1. Introduction In 1978 Rivest, Shamir and Adleman (RSA) [1] created the RSA cryptosystem. This system plays a significant role in securing our information on the internet. The security of this system is based on the difficulty in factoring large numbers, which are created by multiplying two very large prime numbers. As we will see, although factoring large integers is a very difficult problem, finding large primes is relatively easy. The Prime Number Theorem tells us that if we 1 choose a number x at random the chance that x is prime is about ~ i.e. If we pick 푙표푔푒푥 푙표푔푒푥 numbers at random we expect about 1 to be prime. But how do we know when this x is prime? We use a probabilistic approach. We choose a large random number of a particular digit size but exclude numerous classes of numbers that we know are composite. We use the prime number theorem to calculate the probability that a random d-digit size number is prime. We then show how this probability increases when we exclude classes of composites. The first step is to limit the pool of random number to exclude numbers that are obviously composite; we remove any number ending in 1,3,7 or 9. We then exclude numbers whose digital root is not 3, 6, or 9. These steps sharply reduce the probability of the random number being composite. We then apply primality tests to increase the probability to an arbitrarily high degree that n is Page 2 of 14 prime. If the test indicates the random number is likely prime we calculate the increased probability. In section 2 we review the Prime Number Theorem and calculate the base probability. We also adjust this probability calculation assuming we exclude numbers ending in 2, 4, 5, 6 or 8. We again adjust the probabilities by excluding numbers with specific digital roots. In section 3 we review the Fermat Primality Test, Euler Test and Miller-Rabin Test. We also calculate exactly how the probability is affected by application of these tests. Finally, in Section 4 we present our application of these tests using C++ and the GNU GMP library. 2. Calculating and increasing probabilities 2.1 Probability a d-digit prime: the Prime Number theorem The Prime Number Theorem [2](PNT) gives an asymptotic approximation for 휋(x) = number of prime numbers ≤ x i. e. ln 푥 푥 lim 휋(푥) = 1 표푟 lim 휋(푥) = 푥→∞ 푥 푥→∞ ln 푥 Thus the odds that a randomly selected number not exceeding x is a prime can be approximated by 휋(푥) (푥/ ln 푥) 1 = = 푥 푥 ln 푥 So the number of 75 digit primes is 휋(1075) − 휋(1074) = 푛푢푚푏푒푟 표푓 푝푟푖푚푒푠 ≤ 1075 − 푛푢푚푏푒푟 표푓 푝푟푖푚푒푠 ≤ 1074 = 푛푢푚푏푒푟 표푓 푝푟푖푚푒푠 푖푛 푖푛푡푒푟푣푎푙 (1099, 10100) Let N(x)=number of x digit primes. 1075 1074 1074 665 N(75) can be approximated by − = ( ) = .052037087 ∗ 1074 푙푛 1075 푙푛 1074 푙푛10 5550 10푘−1 9푘−10 In general 푁(푘)~ ( ) 푙푛10 푘(푘−1) There are more precise estimates of 휋(x), one of which is [3] 푥 푥 < 휋(푥) < 푓표푟 푥 ≥ 60184 ln 푥 − 1 푙푛 푥 − 1.1 With this formula we get Page 3 of 14 . 05233970251 ∗ 1074 < 휋(75) < .05237015782 ∗ 1074 To find the probability that a generated k digit number is actually a prime we divide N(k) by the total number of k digit numbers: 9 ∗ 10푘−1 푡ℎ푢푠 10푘−1 9푘 − 10 ( ) 푁(푘) 푙푛10 푘(푘 − 1) 푃(퐴) = = = 9푘 − 10 9 ∗ 10푘−1 9 ∗ 10푘−1 9푘(푘 − 1) ln 10 Where A is the event that the k digit number selected is a prime. And for k=75 we get P(A)=.005781899 2.2 Excluding obvious composites: Increasing the prime probability If we use k digits which end in 1, 3, 7 or 9, we can increase the probability that the k digit number is prime. (any multi-digit number ending with 2, 4, 5, 6 or 8 cannot be prime). The number of k digit numbers having a 1, 3, 7 or 9 in the last digits is 9 ∗ 10푘−2 ∗ 4 = 36 ∗ 10푘−2 so the probability now becomes 푁(푘) 10 푃(퐴) = = ( 9푘 − 10 ) 36 ∗ 10푘−2 4 9푘(푘 − 1) ln 10 10 So we increase the probability by a factor = 2.5. We now have P(A)=.014454748 4 2.3 Digital Roots: Further increasing the prime probability We can further increase the probability of A by avoiding all k digit numbers where digital root are 3, 6 or 9. Recall [4] that the digital root of a nonnegative integer n (dr(n)) is a single digit obtained by continually summing the digits until a single digit is obtained. dr(n) can be defined using the 푛−1 floor function ⌊푥⌋ 푎푠 푑푟(푛) = 푛 − 9 ⌊ ⌋ or in terms of congruences 9 0 푖푓 푛 = 0 푑푟(푛) = { 9 푖푓 푛 ≠ 0, 푛 ≡ 0 푚표푑 푛 (푛 푖푠 푎 푚푢푙푡푖푝푙푒 표푓 9) 푛 푚표푑 9 푖푓 푛 ≢ 0 푚표푑 9 Thus if Page 4 of 14 푑푟(푛) = 3 ⟹ 푛 = 9푘 + 3 푓표푟 푘 = 0,1,2, … 푑푟(푛) = 6 ⟹ 푛 = 9푘 + 6 푓표푟 푘 = 0,1,2, … 푑푟(푛) = 9 ⟹ 푛 = 9푘 푓표푟 푘 = 1,2, … So that if dr(n)= 3, 6 or 9, n is divisible by 3 and is composite. If we eliminate all n whose digital root is 3, 6 or 9 we decrease the k digit pool that we can choose n from by 1/3 thus increasing the P(A) by a factor of 3, thus P(A)=.043364243 i.e. If we restrict our k digit number choice to only those that end in 1,3,7 or 9 and whose digital 1 root is not 3,6, or 9 we can expect about 1 prime in about 23 attempts or . But how do we 23 know whether the selected number n is actually prime? We discuss some primality tests [5] [6] [7] which can determine that a given n is definitely not a prime but can tell us that n is a prime with a very high probability i.e. these tests allow for false positives (n is prime when it is actually not) and no false negatives (n is not prime when it actually is). 3. Primality tests 3.1 Fermat primality test Fermat’s Little Theorem (FLT): If p is prime and a is an integer not divisible by p then 푎푝−1 ≡ 1 푚표푑 푝 (and for all a 푎푝 ≡ 푎 푚표푑 푃). By the contrapositive if 푎푛−1 ≠ 1 푚표푑 푛 for some a (푎 ≠ 0 푚표푑 푛) then n is composite. Using the contrapositive of FLT we can prove that a number is composite without actually factoring it. However if n passes the test i.e. 푎푛−1 ≡ 1 푚표푑 푛 it is not a proof that n is prime because the converse of FLT is not necessarily true. Example 1: 푛 = 561 푎 = 2 푎푛푑 푦푒푡 2560 ≡ 1 푚표푑 561, 퐵푢푡 561 = 3 ∗ 11 ∗ 7 푖푠 푐표푚푝표푠푖푡푒 Example 2: 푛 = 341 푎 = 2 푎푛푑 푦푒푡 2340 ≡ 1 푚표푑 561, 퐵푢푡 341 = 11 ∗ 38 푖푠 푐표푚푝표푠푖푡푒 Any composite number m that passes the test is called a “Fermat Pseudoprime to the base a.” [8] Thus 561 and 341 are both Fermat Pseudoprimes to the base 2 . In fact there are many numbers n that are composite such that 푎푛−1 ≡ 1 푚표푑 푛 for every a such that gcd(a,n)=1 (i.e. a and n are relatively prime). Such numbers are called Carmichael numbers. FERMAT PRIMALITY TEST: To test if n is prime or composite Page 5 of 14 1) Choose a number a so that a is not divisible by n 2) Compute 푎푛−1푚표푑 푛 3) If 푎푛−1 ≡ 1 푚표푑 푛 claim “n is a probable prime” Repeat steps 1, 2 and 3 with a different a. The probability of n being a prime number increases with each iteration 4) If 푎푛−1 ≢ 1 푚표푑 푛 output “n is definitely composite.” As we noted above Fermat’s test fails for all a when n is a Carmichael number. So what are the chances that a Carmichael number will be randomly chosen to be tested – Not likely 1) There are only 7 Carmichael numbers within the first 104 numbers. 2) There are only 585,355 Carmichael numbers within the first 1017 numbers.