Lecture 12: RSA Encryption and Primality Testing February 19, 2019 Lecturer: Eric Vigoda Scribes: Shengding Sun and Yuanyuan Yang

Lecture 12: RSA Encryption and Primality Testing February 19, 2019 Lecturer: Eric Vigoda Scribes: Shengding Sun and Yuanyuan Yang

CS 6550: Randomized Algorithms Spring 2019 Lecture 12: RSA Encryption and Primality Testing February 19, 2019 Lecturer: Eric Vigoda Scribes: Shengding Sun and Yuanyuan Yang Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. 12.1 Preliminaries in number theory The RSA public key encryption scheme relies on certain important results in number theory, in particular Fermat's little theorem and Euler's theorem. Theorem 12.1 (Fermat's little theorem) Let p be any prime number. Then for any a relatively prime to p, i.e. gcd(a; p) = 1, we have ap−1 ≡ 1 mod p. Proof: Let S = f1; :::; p − 1g. We let aS denote the set obtained from multiplying each element of S by a and then mod p , i.e. aS = fa mod p; 2a mod p; :::; (p − 1)a mod pg. We claim that aS = S. First, we show that any two elements in aS are distinct. Suppose not, given i; j with ai ≡ aj, since a is relatively prime to p, its multiplicative inverse in Zp exists. In other words, there exists b such that ba ≡ 1 mod p. So we have i ≡ bai ≡ baj ≡ j mod p. A contradiction. Next we show that 0 2= aS. Suppose otherwise, then there exists i 2 f1; :::; p − 1g such that ai ≡ 0 mod p. But this is impossible since a and p are relatively prime. Q Q Therefore aS = S, and in particular we have i2S i = i2aS i. In other words, (p − 1)! ≡ an−1(p − 1)! mod p n−1 Since (p − 1)! is not 0 in Zp, its multiplicative inverse exists, and we have a ≡ 1 mod p. The generalization of this theorem for non-prime numbers is called the Euler's theorem, which is closely related to the Euler totient function. Definition 12.2 Given any integer n ≥ 2, we define the Euler totient function φ(n) to be the number of elements in f1; :::; n − 1g that is relatively prime to n. The main properties we need for Euler totient function is that φ(p) = p − 1 for any prime p, and φ(pq) = pq − p − q + 1 = (p − 1)(q − 1) for any prime p; q. The Euler's theorem is the following. Theorem 12.3 (Euler's theorem) Given n ≥ 2 and a relatively prime to n. Then for any a relatively prime to n we have aφ(n) ≡ 1 mod n. Proof: We omit the proof since it is very similar to the one of Fermat's little theorem. The set S in this case is the elements in f1; :::; n − 1g that are relatively prime to n. 12.2 RSA encryption algorithm The RSA encryption is based on the hardness of factorizing large numbers. In this public key encryption scheme, Alice encrypts the message using the public key and send encrypted it to Bob, who possesses the private key that decrypts the message. This algorithm is designed in such a way that it is difficult for any adversary Eve to decrypt the ciphertext even if she knows the public key used in encryption. 12-1 Lecture 12: RSA Encryption and Primality Testing 12-2 Algorithm 1: RSA key generation input : size N output: public key fM; eg and private key fM; dg 1 Choose p; q to be random N bit prime ; 2 M = pq; 3 Choose e relatively prime to φ(M) = pq − p − q + 1; 4 Compute d satisfying ed ≡ 1 mod φ(M); 5 return public key fM; eg, private key fM; dg; Algorithm 2: RSA encryption input : public key fM; eg, plaintext m output: ciphertext c e 1 return ciphertext c = m mod M; Algorithm 3: RSA decryption input : private key fM; dg, ciphertext c output: plaintext m d 1 return plaintext m = c mod M; 12.2.1 Correctness For the correctness of this algorithm, we need to show that med ≡ m mod M, where m is any plaintext and M; e; d are the numbers in public and private keys. Since ed ≡ 1 mod φ(M), we have ed = 1 + kφ(M) for some integer k. Then med ≡ mkφ(M)m ≡ m mod M, since mφ(M) ≡ 1 mod M from Euler's theorem. 12.2.2 Implementation When we choose e in step 3 of RSA key generation, we can try small primes 3; 5; 7; 11; 13, etc to see whether any of it is a factor of φ(M). After a fixed number of trials, redo step 1 and 2 if all these small prime numbers fail. In step 4, d can be effectively computed using Extended Euclidean algorithm. Notice that the runtime of step 3 and 4 is polynomial in N, which is the bit size of public key. Now for step 1, for each p; q we are going to choose a random N bit number and test its primality. We will show, using the Prime number theorem, that the expected number of trials needed for the chosen number to be prime is linear in N. Theorem 12.4 (Prime number theorem) Let X ≥ 55. Then X π(X) > ln X + 2 where π(X) is the number of prime numbers less than or equal to X. Therefore, the probability that a random N bit number is prime is at least 2X =(ln X + 2) 1 = = Θ(1=N) 2X 2 + N ln 2 Therefore in expectation, Θ(N) trials suffices. In the next section, we will describe a randomized algo- rithm that tests whether a given number is prime. Lecture 12: RSA Encryption and Primality Testing 12-3 12.3 Primality testing 12.3.1 Fermat witness Due to Fermat's little theorem, if a number n is prime, then for any 1 ≤ a < n, we have an−1 ≡ 1 mod n. Therefore, if there exists an a 2 [1; n − 1] with an−1 6≡ 1 mod n, then n is certified to be not prime. This is called a Fermat witness. Now if a and n have a common factor, then obviously an−1 6≡ 1 mod n since it will be a multiple of gcd(a; n). Such a Fermat witness will be called trivial. These concepts are formalized in the following definition. Definition 12.5 Let n be a composite number. A number a 2 [1; n − 1] is a Fermat witness if an−1 6≡ 1 mod n. A nontrivial Fermat witness is a Fermat witness a such that gcd(a; n) = 1. Definition 12.6 (Carmichael Number) A Carmichael Number is a composite n with no nontrivial Fer- mat witness. 1 For example, the first and smallest Charmichael Number is 561 = 3 · 11 · 17 Lemma 12.7 If n has ≥ 1 nontrivial Fermat witness, then the number of g 2 f1; : : : ; n − 1g which are Fermat witness is ≥ (n − 1)=2. Proof: We prove by showing the size of non-Fermat witnesses of n is ≤ that of Fermat witnesses of n: Let a be a non-trivial Fermat witness for n, and we define B and G as follows: B = fb 2 f1; : : : ; n − 1g : bn−1 ≡ 1 mod ng G = fg 2 f1; : : : ; n − 1g : gn−1 6≡ 1 mod ng Then, we denote a nontrivial Fermat witness of n as a and we use a to construct an injective function f : B ! G as follows: • From the definition of a, we have: gcd(a; n) = 1; an−1 6≡ 1 mod n • Then, for every b 2 B, we construct f(b) as follows: f(n) ≡ ba mod n f n−1(n) ≡ bn−1an−1 ≡ an−1 6≡ 1 mod n Hence, we have f(b) 2 G. • Now, let's prove that f is an injective function. Suppose we have b; b0 satisfies the following: f(b) ≡ f(b0) mod n Then we have the following: ba mod b0a mod n Notice that for non-trivial Fermat witness a, it has a multiplicative inverse, multiply this number for both sides of the equation, and then we have the following: b ≡ b0 mod n Since b; b0 2 [n], it immediately follows that b = b0. Hence f is an injective function and jBj ≤ jGj 1Ignoring Charmichaels, then every composite n has ≥ 1 nontrivial Fermat witness. Lecture 12: RSA Encryption and Primality Testing 12-4 Then, we have B \ G = ;, B [ G = f1; : : : ; n − 1g and jBj ≤ jGj. Hence, we have: jGj ≥ (n − 1)=2 The number of g 2 f1; : : : ; n − 1g which are Fermat witness is ≥ (n − 1)=2. Definition 12.8 (Square Root) If a2 ≡ 1 mod n, then a is a square root of 1 mod n. Moreover, 1 and -1 are always square roots of 1 mod n Lemma 12.9 For prime p, 1 and -1 are the only square roots of 1 mod p. Proof: Suppose a2 ≡ 1 mod p, then there exists k 2 Z+, such that: a2 = 1 + kp It immediately follows that: (a + 1)(a − 1) = kp Hence, a ≡ ±1 mod p. 12.3.2 Primality Test when n is not Carmichael Algorithm 4: Primality Test (not Carmichael) input : N-bit number x, number of rounds k output: x is prime/composite 1 Choose a1; : : : ; ak uniformly at random from f1; : : : ; x − 1g ; x−1 2 if for i = 1 : : : k, (ai) ≡ 1 mod x then 3 return x is prime; 4 else 5 return x is composite; 12.3.2.1 Correctness For x is not Carmichael, from Lemma 12.7, we know that for a random chosen ai, its probability of being a Fermat witness is ≥ 0:5 by the following: P r(output compositej x is composite and not Carmichael) =P r(ai is a F ermat witness of x jx is composite and not Carmichael) 0:5(x − 1) 1 ≥ = x − 1 2 Thus, for fixed k, the probability that our algorithm will output prime when x is composite and not Carmichael is: P r(output primej x is composite and not Carmichael) k Y = P r(ai is not a F ermat witness of x jx is composite and not Carmichael) i=1 1 ≤ 2k Lecture 12: RSA Encryption and Primality Testing 12-5 12.3.3 MillerRabin Primality Test While computing each modular exponentiation, the algorithm looks for a nontrivial square root of 1, modulo n, during the final set of squarings.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us