YALE UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE

CPSC 467a: Cryptography and Computer Security Notes 11 (rev. 1)
Professor M. J. Fischer October 13, 2008



Lecture Notes 11

45 Generating RSA Encryption and Decryption Exponents

We showed in section 44 (lecture notes 10) that RSA decryption works for m ∈ Z* n if e and d are chosen so that

ed ≡ 1  (mod  ϕ(n)),
(1)

that is, d is e-1 (the inverse of e) in Z*ϕ(n).

We now turn to the question of how Alice chooses e and d to satisfy (1). One way she can do this is to choose a random integer e ∈ Z*ϕ(n) and then solve (1) for d. We will show how to solve for d in Sections 46 and 47 below.

However, there is another issue, namely, how does Alice find random e ∈ Z*ϕ(n)? If Z*ϕ(n) is large enough, then she can just choose random elements from Zϕ(n) until she encounters one that also lies in Z*ϕ(n). A candidate element e lies in Z*ϕ(n) if gcd(e,ϕ(n)) = 1, which can be computed efficiently using Algorithm 42.2 (Euclidean algorithm).1

But how large is large enough? If ϕ(ϕ(n)) (the size of Z*ϕ(n)) is much smaller than ϕ(n) (the size of Zϕ(n)), Alice might have to search for a long time before finding a suitable candidate for e.

In general, Z*m can be considerably smaller than m. For example, if m = |Zm| = 210, then |Z*m| = 48. In this case, the probability that a randomly-chosen element of Zm falls in Z*m is only 48210 = 835 = 0.228 .

The following theorem provides a crude lower bound on how small Z*m can be relative to the size of Zm that is nevertheless sufficient for our purposes.

Theorem 1 For all m 2,

   *
|Z-m|≥  -----1-----.
|Zm |   1+ ⌊log2m ⌋

Proof: Write m in factored form as m = i=1tpiei, where pi is the ith prime that divides m and ei 1. Then ϕ(m) = i=1t(pi - 1)piei-1, so
               ∏                     (      )
|Z-*m|-  ϕ(m-)   --ti=1-(pi---1)peii-1-  ∏t   pi --1-
|Zm | =  m   =      ∏t   pei     =        pi   .
                     i=1 i        i=1
(2)

To estimate the size of i=1t(pi - 1)∕pi, note that (pi - 1)∕pi i∕(i + 1). This follows since (x- 1)∕x is monotonic increasing in x, and pi i + 1. Then

 t (      )     t (     )
∏    pi --1  ≥ ∏    -i--- =  1⋅ 2⋅ 3⋅⋅⋅--t--=  -1--.
       pi           i+ 1     2  3  4   t+  1   t+ 1
i=1            i=1
(3)

Clearly t ≤⌊log 2msince 2t i=1tpi m and t is an integer. Combining this fact with equations (2) and (3) gives the desired result. __

For n a 1024-bit integer, ϕ(n) < n < 21024. Hence, log 2(ϕ(n)) < 1024, so log 2(ϕ(n))⌋≤ 1023. By Theorem 1, the fraction of elements in Zϕ(n) that also lie in Z*ϕ(n) is at least 1/1024. Therefore, the expected number of random trials before Alice finds a number in Z*ϕ(n) is provably at most 1024 and is most likely much smaller.

46 Diophantine equations and modular inverses

Now that Alice knows how to choose e ∈ Z*ϕ(n), how does she find d? That is, how does she solve (1)? Note that d, if it exists, is a multiplicative inverse of e (mod n), that is, a number that, when multiplied by e, gives 1 (mod n).

Equation (1) is an instance of the general Diophantine equation

ax + by = c
(4)

Here, a,b,c are given integers. A solution consists of integer values for the unknowns x and y. To put (1) into this form, we note that ed 1 (mod ϕ(n)) iff ed + (n) = 1 for some integer u. This is seen to be an equation in the form of (4) where the unknowns x and y are d and u, respectively, and the coefficients a,b,c are e,ϕ(n),1, respectively.

47 Extended Euclidean algorithm

It turns out that (4) is closely related to the greatest common divisor, for it has a solution iff gcd(a,b)c. It can be solved by a process akin to the Euclidean algorithm, which we call the Extended Euclidean algorithm. Here’s how it works.

The algorithm generates a sequence of triples of numbers Ti = (ri,ui,vi), each satisfying the invariant

ri = aui + bvi ≥ 0.
(5)

The first triple T1 is (a,1,0) if a 0 and (-a,-1,0) if a < 0. The second trip T2 is (b,0,1) if b 0 and (-b,0,-1) if b < 0.

The algorithm generates Ti+2 from Ti and Ti+1 much the same as the Euclidean algorithm generates (a mod b) from a and b. More precisely, let qi+1 = ri∕ri+1. Then Ti+2 = Ti - qi+1Ti+1, that is,

 ri+2  =   ri - qi+1ri+1
u     =   u - q   u
  i+2       i   i+1 i+1
 vi+2  =   vi - qi+1vi+1
Note that ri+2 = (ri mod ri+1), 2 so one sees that the sequence of generated pairs (r1,r2), (r2,r3), (r3,r4), …, is exactly the same as the sequence of pairs generated by the Euclidean algorithm. Like the Euclidean algorithm, we stop when rt = 0. Then rt-1 = gcd(a,b), and from (5) it follows that
gcd(a,b) = au   + bv
            t-1     t- 1
(6)

Returning to equation (4), if c = gcd(a,b), then x = ut-1 and y = vt-1 is a solution. If c is a multiple of gcd(a,b), then c = k gcd(a,b) for some k and x = kut-1 and y = kvt-1 is a solution. Otherwise, gcd(a,b) does not divide c, and one can show that (4) has no solution. See Handout 6 for further details, as well as for a discussion of how many solutions (4) has and how to find all solutions.

Here’s an example. Suppose one wants to solve the equation

31x- 45y = 3
(7)

In this example, a = 31 and b = -45. We begin with the triples

T1  =  (31,1,0)
T2  =  (45,0,- 1)
The computation is shown in the following table:
   |              |
-i-|-ri---ui----vi|qi-
 1 |31     1    0 |
 2 |45     0   - 1| 0
 3 |31     1    0 | 1
 4 |14   - 1   - 1| 2
 5 | 3     3    2 | 4
   |              |
 6 | 2  - 13   - 9| 1
 7 | 1    16   11 | 2
 8 | 0  - 45  - 31|

From T7 = (1,16,11) and (5), we obtain

1 = a × 16+ b × 11

Plugging in values a = 31 and b = -45, we compute

31× 16 + (- 45 )× 11 = 496- 495 = 1

as desired. The solution to (7) is then x = 3 × 16 = 48 and y = 3 × 11 = 33.