In the last section you saw that a multiplicative key works for an alphabet of letters exactly when and share no common factor, and that deciphering needs the multiplicative inverse of . Both questions come down to one number: the greatest common divisor of and .
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, , because 2 divides both numbers and nothing larger does. And , because and 7 share no factor other than 1. When , we say and are relatively prime (or co-prime).
We write , read “ divides ”, when is a whole number multiple of . For example, because .
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 :
So , and we never had to factor 707 or 980. (Check: and .)
This “subtract the smaller number from the larger one” loop is short in Python:
a = 707
b = 980
while a != b:
if a > b:
a = a - b
else:
b = b - a
a7Even for bigger numbers it doesn’t take long. The next cell finds and counts the subtractions it needs:
a = 765873
b = 3945934
steps = 0
while a != b:
if a > b:
a = a - b
else:
b = b - a
steps += 1
print('gcd:', a, ' subtractions:', steps)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 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: . So we can skip ahead:
Here is the quotient (how many times fits into ) and is the remainder, . 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 + 0The last non-zero remainder is 7, so 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: 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.
Solution to Exercise 1
, then , then , then . The last non-zero remainder is 32, so .
When Does an Inverse Exist?¶
The previous section showed that a multiplicative key is valid for an alphabet of letters exactly when . The valid keys are also exactly the numbers with a multiplicative inverse:
For example, 14 has no inverse modulo 35. Here . Both 14 and 35 are multiples of 7, so 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 , this says for some integers and . Reduce both sides modulo . The term is a multiple of , so it disappears:
That means is the multiplicative inverse of modulo . So to find an inverse, we need a way to find the 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 . 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 (), 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 ():
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 ():
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 and . Check: . Reducing modulo 26 gives , so -11 is an inverse of 7. Since , the multiplicative inverse of 7 modulo 26 is 15. (Check: .)
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 , then the coefficient of 26, then the coefficient of 7. The column 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 | 26To make each new row:
The last non-zero remainder is . If it is 1, the coefficient of in that row, reduced modulo , is the inverse of . Here that row is , and .
If the last non-zero remainder is bigger than 1, there is no inverse. For example, for and the remainders are . The gcd is 2, so 4 has no inverse modulo 26, which matches what you saw for key 4 in the multiplicative cipher.
Solution to Exercise 2
q | r | coefficient of 89 | coefficient of 49
----+-----+-------------------+-------------------
| 89 | 1 | 0
| 49 | 0 | 1
1 | 40 | 1 | -1
1 | 9 | -1 | 2
4 | 4 | 5 | -9
2 | 1 | -11 | 20The last row says , so the inverse of 49 modulo 89 is 20. Check: .
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.
pow(7, -1, 26)15pow(49, -1, 89)20If there is no inverse, pow raises a ValueError instead of returning an answer:
pow(14, -1, 35)---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[4], line 1
----> 1 pow(14, -1, 35)
ValueError: base is not invertible for the given modulusThis book’s toolkit.py has the same tools under these names:
gcd(a, b)returns , 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, wheregis and .eea(k, m)returns the multiplicative inverse ofkmodulom, and raises aValueErrorwhen there isn’t one.
gcd(707, 980)7egcd(7, 26)(1, -11, 3)eea(7, 26)15egcd(7, 26) returns (1, -11, 3): the gcd is 1 and , the same equation the table found. eea reduces -11 to 15 for you.
Exercises for the Reader¶
Solution to Exercise 3
, then , then . The last non-zero remainder is 21, so .
Solution to Exercise 4
10 and 25 are valid, because and . The other two share a factor with : and .
Solution to Exercise 5
q | r | coefficient of 60 | coefficient of 17
----+-----+-------------------+-------------------
| 60 | 1 | 0
| 17 | 0 | 1
3 | 9 | 1 | -3
1 | 8 | -1 | 4
1 | 1 | 2 | -7The last row says , and , so the inverse of 17 modulo 60 is 53. Check: , and pow(17, -1, 60) returns 53.