Path: blob/main/foundations/04-number-theory-rsa/sage/04a-divisibility-gcd-euclid.ipynb
483 views
Notebook 04a: Divisibility, GCD, and the Euclidean Algorithm
Module 04. Number Theory and RSA
Motivating Question. Computing by listing all divisors of each number would require checking up to a billion candidates for each. Is there a shortcut that finds the answer in just a handful of steps?
The Euclidean algorithm, over 2300 years old, answers yes. It is one of the oldest algorithms still in daily use, and it sits at the heart of RSA key generation.
Prerequisites. You should be comfortable with:
Modular arithmetic and residues (Module 01)
The idea of units in (Module 01/02): an element is a unit iff it has a multiplicative inverse mod
Learning objectives. By the end of this notebook you will be able to:
State the definition of divisibility and verify its key properties.
Compute the GCD of two integers using the Euclidean algorithm by hand and in SageMath.
Explain why the algorithm works (the key invariant).
Relate coprimality () to the existence of modular inverses.
Appreciate why the Euclidean algorithm is efficient: steps.
1. Divisibility
Definition. Let with . We say divides , written , if there exists an integer such that
Equivalently, is a multiple of , or is a factor (divisor) of .
Examples:
because .
because . (Every nonzero integer divides 0!)
because there is no integer with .
Key properties:
| Property | Statement | Why? |
|---|---|---|
| Reflexive | Because | |
| Transitive | If and then | |
| Antisymmetric (on positives) | If and with then | and , so , giving , so |
Checkpoint 1. Before running the cell below, predict: does divide ? Does divide ? What are the divisors of ?
2. Common Divisors and the GCD
Definition. A common divisor of integers and is any integer such that and .
The greatest common divisor is the largest positive common divisor.
Example. Let , .
Divisors of 48:
Divisors of 18:
Common divisors:
This "list all divisors" approach works for small numbers. But what about ? Listing a billion divisors is not practical. We need something smarter.
3. The Euclidean Algorithm
3.1 The Key Insight
The Euclidean algorithm rests on a single, beautiful fact:
Theorem. For any integers with :
Why does this work? Write where . We claim that and have exactly the same set of common divisors as and .
Forward direction: Suppose and . Then . So and .
Backward direction: Suppose and . Then . So and .
Since the set of common divisors is identical, the greatest common divisor is the same. And crucially, , so the numbers get strictly smaller each step.
3.2 The Algorithm
Repeatedly apply until the remainder is 0. The last nonzero remainder is the GCD.
Misconception alert. "The Euclidean algorithm tries all possible divisors." No! It never tests a single divisor. It uses division (specifically, the remainder) to shrink the problem at each step. That is why it is so fast.
3.3 Worked Example:
Let us trace every step by hand first:
| Step | Equation | Remainder |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 |
The last nonzero remainder is , so .
Notice how the chain of equalities shows:
Only 3 steps, no trial division needed!
Checkpoint 2. Before running the next cell, trace by hand on paper. How many steps does it take? What is the GCD?
3.4 Why It's Fast: Steps
The Euclidean algorithm is remarkably efficient. The key observation is that every two consecutive steps reduce the larger number by at least half:
After two steps, the pair is replaced by a pair where both numbers are strictly less than , and specifically after at most two remainder operations.
This means the number of steps is at most , i.e., logarithmic in the input size.
For a 1000-digit number, the algorithm needs at most about 6600 steps, not billions!
The worst case is consecutive Fibonacci numbers. Let us verify experimentally.
Notice that for Fibonacci inputs, the number of steps equals : the algorithm peels off one Fibonacci number per step, which is the slowest possible shrinkage. Any other input shrinks faster.
Let us now answer the motivating question.
4. Coprimality and Modular Inverses
Definition. Two integers and are coprime (or relatively prime) if .
Bridge from Modules 01 and 02. Remember that in Module 01, we saw that the units (invertible elements) of are exactly those residues with . In Module 02, we formalised this: coprimality determines unit status in the ring .
Now we can state this precisely:
Theorem. has a multiplicative inverse modulo if and only if .
This is why coprimality matters for cryptography. In the next notebook (04b), we will see that the Extended Euclidean Algorithm does not just check coprimality, it actually computes the inverse.
Crypto foreshadowing. In RSA, we pick two large primes , set , and compute . The public exponent must satisfy so that has an inverse modulo . That inverse is the private key. The extended Euclidean algorithm (notebook 04b) is what actually computes .
Checkpoint 3. Consider . Which elements of are coprime to 12? How many units does have? Predict first, then run the cell below.
5. SageMath GCD and LCM Toolkit
SageMath provides several built-in functions for divisibility and GCD computations. Let us survey the most useful ones.
6. Exercises
Exercise 1 (Worked): Trace the Euclidean Algorithm
Problem. Compute by hand, showing each division step. Verify with SageMath.
Solution.
| Step | Equation | Remainder |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 |
So .
Exercise 2 (Guided): Coprimality Check for RSA
Problem. In a toy RSA setup, suppose , , so and . A common choice for the public exponent is . Check whether so that is a valid RSA exponent.
Then find two values of in that are coprime to , and two that are not.
Hint: Use gcd() in a list comprehension to filter.
Exercise 3 (Independent): Worst-Case Analysis
Problem.
Write a function
gcd_with_steps(a, b)that returns a tuple(gcd_value, step_count, step_list)wherestep_listis a list of strings showing each division step.Use it to compute where is the -th Fibonacci number.
Compare the step count to the theoretical bound . Is the Fibonacci case close to the bound?
Summary
| Concept | Key Fact |
|---|---|
| Divisibility | means ; reflexive, transitive, antisymmetric (on positives) |
| GCD | = largest positive common divisor |
| Euclidean algorithm | Uses repeatedly; terminates when remainder = 0 |
| Correctness | Any common divisor of also divides (and vice versa) |
| Efficiency | steps, worst case is Fibonacci inputs |
| Coprimality | iff is a unit (has inverse) in |
We now have the tool to check coprimality. But for RSA, we need to compute the inverse . That requires the Extended Euclidean Algorithm, which is the subject of the next notebook.
Next: 04b. The Extended Euclidean Algorithm and Modular Inverses