Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

In the last section you saw that a multiplicative key kk works for an alphabet of mm letters exactly when kk and mm share no common factor, and that deciphering needs the multiplicative inverse of kk. Both questions come down to one number: the greatest common divisor of kk and mm.

For a 26 letter alphabet you can answer them by trying every number. That stops working for big numbers. Later in the course, in public key cryptography, the numbers have hundreds of digits. Factoring numbers that large is very slow, and it gets slower as the numbers grow. This section shows a method that finds greatest common divisors, and multiplicative inverses, without factoring anything.

Greatest Common Divisors

For example, gcd⁡(26,4)=2\gcd(26, 4) = 2, because 2 divides both numbers and nothing larger does. And gcd⁡(26,7)=1\gcd(26, 7) = 1, because 26=2⋅1326 = 2 \cdot 13 and 7 share no factor other than 1. When gcd⁡(a,b)=1\gcd(a, b) = 1, we say aa and bb are relatively prime (or co-prime).

We write d∣nd \mid n, read “dd divides nn”, when nn is a whole number multiple of dd. For example, 7∣9807 \mid 980 because 980=7⋅140980 = 7 \cdot 140.

The Euclidean Algorithm

The Euclidean algorithm appears in Euclid’s Elements, written around 300 BC. Euclid often collected earlier mathematicians’ work, so the method is probably older than the book. It rests on one fact:

Why is this true?

So you can replace the larger number by the difference of the two numbers without changing the gcd. Keep doing that until the two numbers are equal; that number is the gcd. Here’s gcd⁡(707,980)\gcd(707, 980):

gcd⁡(707,980)=gcd⁡(707,980−707)=gcd⁡(707,273)=gcd⁡(707−273,273)=gcd⁡(434,273)=gcd⁡(434−273,273)=gcd⁡(161,273)=gcd⁡(161,273−161)=gcd⁡(161,112)=gcd⁡(161−112,112)=gcd⁡(49,112)=gcd⁡(49,63)=gcd⁡(49,14)=gcd⁡(35,14)=gcd⁡(21,14)=gcd⁡(7,14)=gcd⁡(7,7)=7\begin{align} \gcd(707, 980) &= \gcd(707, 980 - 707) = \gcd(707, 273) \\ &= \gcd(707 - 273, 273) = \gcd(434, 273) \\ &= \gcd(434 - 273, 273) = \gcd(161, 273) \\ &= \gcd(161, 273 - 161) = \gcd(161, 112) \\ &= \gcd(161 - 112, 112) = \gcd(49, 112) \\ &= \gcd(49, 63) = \gcd(49, 14) = \gcd(35, 14) = \gcd(21, 14) = \gcd(7, 14) = \gcd(7, 7) = 7 \end{align}

So gcd⁡(707,980)=7\gcd(707, 980) = 7, and we never had to factor 707 or 980. (Check: 707=7⋅101707 = 7 \cdot 101 and 980=7⋅140980 = 7 \cdot 140.)

This “subtract the smaller number from the larger one” loop is short in Python:

7

Even for bigger numbers it doesn’t take long. The next cell finds gcd⁡(765873,3945934)\gcd(765873, 3945934) and counts the subtractions it needs:

gcd: 1  subtractions: 48

The gcd is 1, so these two numbers are relatively prime, and it took only 48 subtractions to find out.

Dividing Instead of Subtracting

Look back at gcd⁡(49,112)\gcd(49, 112) above. We subtracted 49 twice in a row to get down to 14. Subtracting the same number over and over is just division with a remainder: 112=2⋅49+14112 = 2 \cdot 49 + 14. So we can skip ahead:

Here qq is the quotient (how many times aa fits into bb) and rr is the remainder, r=b MOD ar = b \text{ MOD } a. Repeat until the remainder is 0. The last non-zero remainder is the gcd.

980 = 1 · 707 + 273
707 = 2 · 273 + 161
273 = 1 · 161 + 112
161 = 1 · 112 +  49
112 = 2 ·  49 +  14
 49 = 3 ·  14 +   7
 14 = 2 ·   7 +   0

The last non-zero remainder is 7, so gcd⁡(707,980)=7\gcd(707, 980) = 7 again, this time in 7 divisions instead of 11 subtractions. For 765873 and 3945934, the division version needs only 15 steps. Division matters most when one number is much bigger than the other: gcd⁡(1,1000000)\gcd(1, 1000000) takes 999999 subtractions, but a single division. In Python, the remainder is b % a, so each step replaces the pair of numbers with the smaller number and the remainder.

When Does an Inverse Exist?

The previous section showed that a multiplicative key kk is valid for an alphabet of mm letters exactly when gcd⁡(k,m)=1\gcd(k, m) = 1. The valid keys are also exactly the numbers with a multiplicative inverse:

For example, 14 has no inverse modulo 35. Here gcd⁡(14,35)=7\gcd(14, 35) = 7. Both 14 and 35 are multiples of 7, so 14⋅x MOD 3514 \cdot x \text{ MOD } 35 is always a multiple of 7 (0, 7, 14, 21 or 28), and never 1.

So the Euclidean algorithm tells you whether an inverse exists. The next step is to find it.

Bézout’s Identity

There’s one more fact about gcds that turns out to be exactly what we need.

When gcd⁡(k,m)=1\gcd(k, m) = 1, this says 1=k⋅x+m⋅y1 = k \cdot x + m \cdot y for some integers xx and yy. Reduce both sides modulo mm. The m⋅ym \cdot y term is a multiple of mm, so it disappears:

1≡k⋅x(modm).1 \equiv k \cdot x \pmod{m}.

That means xx is the multiplicative inverse of kk modulo mm. So to find an inverse, we need a way to find the xx in Bézout’s identity. The Extended Euclidean Algorithm does this.

The Extended Euclidean Algorithm

Let’s find the multiplicative inverse of 7 modulo 26. Our goal is an equation of the form 1=7x+26y1 = 7x + 26y. Start with two equations that are obviously true, writing each number as “something times 7 plus something times 26”:

Equation 1:   26 = 7( 0) + 26( 1)
Equation 2:    7 = 7( 1) + 26( 0)

Now run the Euclidean algorithm on the left-hand sides, and do the same thing to the whole equation. 7 goes into 26 three times (q=3q = 3), so subtract 3 copies of Equation 2 from Equation 1:

Eq1 - 3·Eq2:   5 = 7(-3) + 26( 1)

Repeat with the last two equations. 5 goes into 7 once (q=1q = 1):

Equation 1:    7 = 7( 1) + 26( 0)
Equation 2:    5 = 7(-3) + 26( 1)
Eq1 - 1·Eq2:   2 = 7( 4) + 26(-1)

And again. 2 goes into 5 twice (q=2q = 2):

Equation 1:    5 = 7(-3) + 26( 1)
Equation 2:    2 = 7( 4) + 26(-1)
Eq1 - 2·Eq2:   1 = 7(-11) + 26( 3)

We’ve found x=−11x = -11 and y=3y = 3. Check: 7⋅(−11)+26⋅3=−77+78=17 \cdot (-11) + 26 \cdot 3 = -77 + 78 = 1. Reducing modulo 26 gives 1≡7⋅(−11)(mod26)1 \equiv 7 \cdot (-11) \pmod{26}, so -11 is an inverse of 7. Since −11≡15(mod26)-11 \equiv 15 \pmod{26}, the multiplicative inverse of 7 modulo 26 is 15. (Check: 7⋅15=105=4⋅26+17 \cdot 15 = 105 = 4 \cdot 26 + 1.)

The Shorthand Table

Writing out the equations gets repetitive, so people usually keep only the numbers in a table. Each row stands for one equation: the remainder rr, then the coefficient of 26, then the coefficient of 7. The column qq records the quotient used to make that row.

 q  |  r  | coefficient of 26 | coefficient of 7
----+-----+-------------------+------------------
    | 26  |         1         |         0
    |  7  |         0         |         1
 3  |  5  |         1         |        -3
 1  |  2  |        -1         |         4
 2  |  1  |         3         |       -11
 2  |  0  |        -7         |        26

To make each new row:

The last non-zero remainder is gcd⁡(m,k)\gcd(m, k). If it is 1, the coefficient of kk in that row, reduced modulo mm, is the inverse of kk. Here that row is 1=26(3)+7(−11)1 = 26(3) + 7(-11), and −11≡15(mod26)-11 \equiv 15 \pmod{26}.

If the last non-zero remainder is bigger than 1, there is no inverse. For example, for k=4k = 4 and m=26m = 26 the remainders are 26,4,2,026, 4, 2, 0. The gcd is 2, so 4 has no inverse modulo 26, which matches what you saw for key 4 in the multiplicative cipher.

The number of rows grows with the number of digits, not with the size of the numbers. Even for numbers with 200 digits it’s at most about a thousand rows, which a computer finishes in a fraction of a second. In Chapter 12 you’ll see that this speed is exactly what makes the KidRSA cipher easy to break: see Cracking KidRSA.

Inverses in Python

Python (version 3.8 or later) can compute a multiplicative inverse directly: pow(k, -1, m) returns the inverse of k modulo m.

15
20

If there is no inverse, pow raises a ValueError instead of returning an answer:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[4], line 1
----> 1 pow(14, -1, 35)

ValueError: base is not invertible for the given modulus

This book’s toolkit.py has the same tools under these names:

  • gcd(a, b) returns gcd⁡(a,b)\gcd(a, b), using the division version of the Euclidean algorithm.

  • egcd(a, b) runs the Extended Euclidean Algorithm and returns three numbers (g, x, y), in that order, where g is gcd⁡(a,b)\gcd(a, b) and a⋅x+b⋅y=ga \cdot x + b \cdot y = g.

  • eea(k, m) returns the multiplicative inverse of k modulo m, and raises a ValueError when there isn’t one.

7
(1, -11, 3)
15

egcd(7, 26) returns (1, -11, 3): the gcd is 1 and 7⋅(−11)+26⋅3=17 \cdot (-11) + 26 \cdot 3 = 1, the same equation the table found. eea reduces -11 to 15 for you.

Exercises for the Reader