On El-Gamal and Fermat's Primality Test
Total Page:16
File Type:pdf, Size:1020Kb
On El-Gamal and Fermat’s Primality Test IRVAN JAHJA / 13509099 Program Studi Teknik Informatika Sekolah Teknik Elektro dan Informatika Institut Teknologi Bandung, Jl. Ganesha 10 Bandung 40132, Indonesia [email protected] May 6, 2012 Abstract Gamal reacts with non prime p, and then discuss the case with Carmichael Numbers. The paper then closes We discuss the feasibility of using the simple Fermat’s with a possible way to take advantage of the behavior Primality Test to generate prime required as a key to the of El-Gamal with composite p. public key El-Gamal cryptosystem. This option is ap- Throughout this paper, pseudocodes in Python-like pealing due to the simplicity of Fermat’s Primality Test, languages will be presented as to make things less ab- but is marred with the existance of Carmichael Num- stract. bers. 2 Background Theory 1 Introduction 2.1 Fermat Primality Testing 1.1 Background Fermat Primality Testing is one of the fastest primality El-Gamal is one of the most used public key cryptosys- testing algorithm. Interested readers are referred to the tem. This accounts to its simplicity of implementation book by Cormen et. al [20], and we will only give a as well for being one of the earliest relatively secure brief explanation in this section. public key cryptosystem to be published. For instance, The Fermat’s Primality Testing is based on the Fer- GNU Privacy Guard software and recent versions of mat’s Little Theorem: PGP are major user of this system. If p is prime and 1 ≤ p < p, then ap−1 ≡ 1 (mod p). One of the major challenges in this algorithm is the generation of sufficiently large prime, used to generate Which was given by Pierre D. Fermat and whose for- the Cyclic Group G required in the entire computation mal proof were only given 40 years after it was pub- of the algorithm. One of the most popular method is the lished. Rabin-Miller Primality testing, but it suffers from the To test whether n is prime, the Fermat’s Primality lack of ability to detect Carmichael Numbers. Testing repeatedly pick a and tests the following equiv- There has been numerous studies on Carmichael alence: Number, proving many of its properties [1] [8] [16] [10] an−1 ≡ 1 (mod n). [7] [4]. The study of El-gamal itself is equally numer- ous [22] [19], beginning with its classic paper [6]. If an a is found for which the equation above fails to hold, then n is declared composite and a is said to be its 1.2 Roadmap witness. The Fermat Little Theorem, however, suffers from The paper will begin with some background theories on the numbers known as Carmichael Numbers, which are fermat primality testing as well as the El-Gamal Cryp- composite numbers for which the equation an−1 ≡ 1 tosystem itself. Then, the paper will discuss how El- (mod n) holds for all a. 1 2.2 Carmichael Number rithms [12]. Discrete Logarithm is stated formally as follows. The properties of Carmichael Numbers have been well- Given a, b, and p, find x such that studied. There are an infinite number of Carmichael Numbers [2]. Furthermore, from [16], Carmichael ax ≡ b (mod p). Numbers n has the following properties: • n is square-free El-Gamal is widely used. For instance, GNU Privacy • n has at least three prime factors Guard software and recent versions of PGP are major user of this system. 0:5 • pjn implies p − 1jn − 1 and p < n As with many public key cryptosystems, the El- Although there are an infinite number of Carmichael Gamal algorithm consists of three distinct steps: Numbers, it is not known whether there also exists an infinite amount of Carmichael Numbers with exactly • Key Generation - Generates both public and pri- three factors. For instance, up to 1018, there are 35585 vate key from a given prime p Carmichael Numbers with exactly 3 prime divisors [15]. For comparison, there are 24739954287740860 • Encryption - The process to encrypt a message us- primes [5]. ing the public key in such way that only the holder There are numerous number of Carmichael Numbers of the private key is able to decipher the message. C(X), with its asymptotic growh being at least X0:332 • Decryption - The process of inverting the en- [9]. An upperbound on the number of Carmichael crypted message to obtain the original message. Number has also been established, but is rather diffi- cult to phrase concisely [7]. This numerous number of Their respective pseudocodes are presented in the ac- Carmichael Numbers inhibit the usefulness of the Fer- companying python-like pseudocodes. mat Primality Testing to test in general whether a num- It is worth noting that it is possible to create a re- ber is prime. versed variant of El-Gamal, (signature scheme instead In general, there is no known easy way to distinguish of encrypting message) – that is, to create a message a Carmichael number with a prime number except by that can only be deciphered by the public key and can- testing its primality using some other methods [13]. not be constructed without creating the private key. This is known as the El-Gamal Signature Scheme. 2.3 Other Primality Tests PRIMES, the problem of deciding whether a number 2.5 Inverse N is prime, has been proven to be in P [1], giving an O(log1 2N) algorithm. Lenstra and Hendrick proceeds Finding inverse quickly is important for the El-Gamal to improve its complexity to O(log6 N) unconditionally cryptosystem, in the decryption phase. Algorithms to [11]. These discoveries have been made recently and find inverses quickly are well-known - Fermat little the- until such it was widely believed that PRIMES is not in orem provides one such alternative for prime p. Recall P. that Fermat Little theorem states for a prime p and any Rabin Miller, a probability primalistic test, achieves integer 1 ≤ a < p: respectable accuracy using log4 N complexity, and is ap−1 ≡ 1 (mod p) widely used [17] [14]. A deterministic variant of this Hence, algorithm is also present and its currently tightest com- ap−2 ∗ a ≡ 1 (mod p) plexity is due to [3]. It is worth noting that this com- Therefore, the inverse of a mod p is ap−2, which can plexity is not strictly polynomial in log N, unlike the be computed efficiently using modular exponentiation. one due presented in [1]. This equation is, however, only valid for prime p. The following theorem generalizes inverses for non 2.4 El-Gamal primes p: El-Gamal [6] is an asymmetric public key cryptosys- Theorem 1. If a is relatively prime to p, then there ex- tem based on the difficulty of finding discrete loga- ists an integer b such that a ∗ b ≡ 1 (mod p). 2 # Produces the private and public keys def g e n e r a t e k e y ( p ) g = random number between 0 and p−1 x = random number between 0 and p−1 h = g∗∗x public key = (p, g, h) private key = (x) Figure 1: Key Generation # Encrypts a message m that can only be read by private key owner # Assumes that m < p def encrypt(p, g, h, m) y = random number between 0 and q−1 c1 = g∗∗y s = h∗∗y c2 = m ∗ s return ( c1 , c2 ) Figure 2: Encryption # Decrypts c1 and c2 into m def encrypt(p, g, h, x, c1, c2) s = c1 ∗∗x m = c2 ∗ i n v e r s e ( s ) Figure 3: Encryption 3 To proof this, we will show the famous Bezout’s With this change, what are the consequences? We Identity. For a given a and b, form all values ax + by, claim that the algorithm will work as intended. Indeed and pick the smallest positive number d amongst all of at no other stage of the algorithm the fact that p is not them. Now, the remainder of dividing either a or b prime makes difference. Hence, the algorithm is indeed by d is also of the form ax0 + by0, since we assumed stay correct. d = ax + by. However, since this remainder must be Our worries is then whether or not the problem of dis- strictly smaller than d, it follows from the fact that d is crete logarithm can be efficiently solved for Carmichael the smallest positive ax + by that d is zero, so that d Numbers. Citing [23], the problem of discrete logar- divides both a and b. tihm over composite number is still difficult as long as If c is another divisors of both a and b, c also divides the factorization of the composite number remains un- ax+by = d. This implies that d is the greatest common known. divisor of a and b. Recalling that Carmichael numbers must have at least Hence, if a is relatively prime to p, there exists x and three distinct prime divisors, the problem of factoriz- y such that ax + py = 1. Hence, ax ≡ 1 (mod p), and ing it becomes significantly easier. Indeed, the above so x is the inverse of a, completing the proof. discussion on Carmichael detecting already yields an This theorem is founded for a long time, for instance O(N 1=3) algorithm, which may get even weaker since in [21]. there are no strict upper bound on the number of divi- The extended euclid algorithm is one of the possible sors of a Carmichael Number. implementations to compute the inverses of numbers ef- We demonstrated this fact by performing experiment ficiently.