Path: blob/main/foundations/04-number-theory-rsa/sage/04b-extended-euclidean-algorithm.ipynb
483 views
The Extended Euclidean Algorithm
Module 04b | Number Theory and RSA
You can find the gcd. But can you express it as a linear combination of the inputs?
Motivating Question: You know from 04a that . But can you find integers such that ? And if you can, why would that be enormously useful?
Here's a hint: if , then reducing both sides modulo 60 gives . That means is the multiplicative inverse of 17 mod 60. In Module 01b we saw that such inverses exist whenever , but we never showed how to compute them. This notebook fills that gap.
Objectives
By the end of this notebook you will be able to:
State Bezout's identity and explain why it guarantees that is a linear combination of and
Execute the extended Euclidean algorithm using a table (forward pass + backward substitution)
Use the extended GCD to compute modular inverses
Connect this to RSA: the private key is literally computed by the extended GCD
Prerequisites
Completion of 04a: Divisibility and the Euclidean Algorithm, you should be comfortable with the division algorithm and running Euclid's algorithm step by step.
Familiarity with modular arithmetic from Module 01, in particular, the concept of multiplicative inverses and units.
Bezout's Identity
Theorem (Bezout's Identity). For any integers (not both zero), there exist integers such that
The integers and are called Bezout coefficients.
This is a remarkable existence result: the gcd of and can always be written as a linear combination of and with integer coefficients. The extended Euclidean algorithm is a constructive proof, it doesn't just tell you and exist, it computes them.
Let's see this in action before we learn the algorithm.
Checkpoint: Look at the row for . You should see with some coefficients . Before moving on, verify by hand that .
Now reduce both sides mod 60. What do you get? What does represent in modular arithmetic?
The Algorithm: Forward Pass (Review)
The extended Euclidean algorithm has two phases. The first phase is just the regular Euclidean algorithm you learned in 04a, we compute a sequence of divisions:
until we reach a remainder of 0. The last non-zero remainder is .
Let's trace through as a refresher.
The Algorithm: Backward Substitution
The key insight: every remainder in the forward pass can be rewritten as . Since and are themselves remainders from earlier steps, we can substitute backwards until we express as a combination of the original and .
Worked example:
Forward pass gave us:
The gcd is , appearing in step 2. Now substitute backwards:
From step 2:
From step 1:
Substitute:
So , , and indeed .
Common mistake: "The extended GCD is just the GCD plus extra bookkeeping." The insight is deeper than that. The extended GCD proves constructively that is always a linear combination of and . This existence result is what makes modular inverses computable, and it's not obvious at all that such a representation should exist.
The Table Method
Backward substitution works but is error-prone for large examples. The table method computes the Bezout coefficients alongside the forward pass, avoiding back-substitution entirely.
We maintain a table with columns , , , where each row satisfies the invariant:
Initialization (two special rows before the algorithm starts):
| Step | Invariant check | ||||
|---|---|---|---|---|---|
| 0 | , | 1 | 0 | ||
| 1 | , | 0 | 1 |
Recurrence: At each step , compute and then:
When , the previous row gives with coefficients , .
Why does this work?
The invariant holds at every step. Proof by induction:
Base cases: Row 0: . Row 1: .
Inductive step: If and , then
So the and columns are built by the exact same recurrence as the column, just with different starting values. That's the entire trick.
Checkpoint: Look at the table above. For each row, verify mentally that the "Check" column equals . This is the invariant in action.
Now, before running the next cell, try filling in the table for by hand:
Step 0 , 17 1 0 1 , 60 0 1 2 ? ? ? ? 3 ? ? ? ? Wait, , so the first quotient is 0 and the algorithm swaps them. Keep going from there.
From Extended GCD to Modular Inverses
This is where everything connects. Suppose . Then the extended GCD gives us:
Reduce both sides modulo :
So is the multiplicative inverse of modulo .
That's it. No trial and error. No searching. The extended GCD directly computes the modular inverse.
Back in Module 01b, we established that is a unit in if and only if . Now we finally have the algorithm that computes the inverse: run the extended GCD and read off .
So , because .
SageMath also provides inverse_mod(a, n) as a convenience function.
Common mistake: "I can just try all values from 1 to until I find the inverse." For small , sure. But RSA uses with 2048+ bits ( digits). The extended GCD runs in , it's practically instant even for cryptographic sizes. Brute force would take longer than the age of the universe.
Crypto Connection: RSA Private Keys
Foreshadowing: In RSA, you choose two large primes , set , and pick a public exponent with . The private key is
The extended Euclidean algorithm is literally the algorithm that generates RSA private keys. When you generate an RSA key pair, under the hood, the software runs
xgcd(e, phi_n)and extracts the Bezout coefficient.We'll see this in full detail in 04e. For now, let's see a tiny example.
Exercises
Exercise 1 (Worked)
Use the extended Euclidean algorithm (table method) to find and express it as a linear combination .
Since (not 1), 161 and 28 are not coprime, and neither has an inverse modulo the other. But the Bezout equation still holds, the extended GCD always works, whether or not the gcd is 1.
Exercise 2 (Guided)
Find using the extended GCD. Then verify your answer.
Hint: is prime, so is guaranteed.
Exercise 3 (Independent)
In a toy RSA setup, you're given:
,
Public exponent
Tasks:
Compute and
Verify that (so is a valid public exponent)
Compute the private key using the extended GCD
Encrypt the message as
Decrypt as and verify
Summary
| Concept | Key idea |
|---|---|
| Bezout's identity | For any integers , there exist with |
| Backward substitution | Express the gcd as a linear combination by substituting remainders back through the Euclidean algorithm steps |
| The table method | Computes Bezout coefficients alongside the gcd using the same recurrence () applied to all three columns |
| Modular inverses | If , then implies |
| RSA connection | The private key is computed by running the extended GCD on and |
Crypto preview: We now know how to compute modular inverses. In the next notebook, we'll learn about Euler's totient function and Fermat's little theorem, the why behind RSA's correctness: why does ?