Path: blob/main/foundations/04-number-theory-rsa/sage/04c-euler-totient-fermats-theorem.ipynb
483 views
Euler's Totient and Fermat's Theorem
Module 04c | Number Theory and RSA
euler_phi(), power_mod(), and the theoretical backbone of RSA
Question: Compute without a computer. Sounds impossible? Fermat says you only need to know that . Then . That's the power of this notebook.
Objectives
By the end of this notebook you will be able to:
Define Euler's totient function and compute it for any .
Derive the product formula that makes RSA work.
State and verify Fermat's Little Theorem and its generalization, Euler's theorem.
Connect to element orders and Lagrange's theorem from Module 01e.
Explain why Euler's theorem guarantees RSA decryption correctness.
Prerequisites
Completion of 04a: Divisibility and GCD, you know what means.
Completion of 04b: Extended Euclidean Algorithm, you know that has a modular inverse iff .
From 01e: Subgroups and Lagrange, element orders must divide the group size.
From Inverses to Counting: What Is ?
In 04b, we discovered that has a multiplicative inverse modulo precisely when . The set of all such invertible elements forms the multiplicative group .
A natural question: how big is this group? That is, how many integers in are coprime to ?
This count has a name: Euler's totient function, written .
Euler's Totient Function: Definition
Let's compute it the hard way first, by listing and counting.
Common mistake: " counts numbers less than ." No! It counts numbers from 1 to that are coprime to . For example, (only are coprime to 12), not 11.
Look at the table above. Notice anything about the prime values ()?
Formulas for
Rule 1: Primes
If is prime, then every integer from 1 to is coprime to (nothing shares a factor with a prime except its own multiples). So:
Rule 2: Prime powers
For , the only integers in that are not coprime to are the multiples of : there are of them. So:
Rule 3: Multiplicativity
When :
This follows from the Chinese Remainder Theorem (coming up in 04d).
Checkpoint: Before running the next cell, predict .
and , so ? Write your answer down, then verify.
The RSA Formula:
This is the single most important application of . If and are distinct primes:
This works because (distinct primes are always coprime), so the multiplicativity rule applies.
Crypto foreshadowing: RSA key generation picks two large primes and computes . The "secret" that makes RSA work is knowledge of . Anyone who knows but doesn't know and cannot efficiently compute , and without , they cannot find the decryption key. Factoring and computing are computationally equivalent problems.
Fermat's Little Theorem
Now we come to the power result. Fermat's Little Theorem (1640) says:
If is prime and , then .
Equivalently: for all (even when ).
Why does this work? The multiplicative group has order . By Lagrange's theorem (01e), every element's order divides the group size. So divides , which means .
Let's verify this experimentally.
Using Fermat to Simplify Large Powers
Remember our opening question? .
Since is prime and , Fermat tells us .
So we reduce the exponent modulo 6:
Euler's Theorem: The Generalization
Fermat's Little Theorem only works modulo a prime. Euler generalized it to any modulus:
If , then .
When is prime, , and Euler's theorem reduces to Fermat's. But Euler's theorem works for composite too, and that's what RSA needs.
Why does this work? Same argument as before, now in the group of order . By Lagrange's theorem, divides , so .
Checkpoint: Predict before running the next cell.
We computed above, and . So Euler says ? .
Common mistake: " for all ." No! The theorem requires . Look at in the table above: , so Euler's theorem does not apply, and indeed . The coprimality condition is essential.
Mass Verification: Euler's Theorem for Many
One example is convincing. Hundreds are better. Let's verify for all coprime and many values of .
Connection to Element Orders
In 01e, we learned that in a group of order , every element's order divides (Lagrange's theorem). The group has order . So:
This is exactly why Euler's theorem works: .
Some elements have order exactly (these are the generators of the group, recall 01d). Others have smaller orders, but those smaller orders still divide .
Why RSA Decryption Works
Now for the payoff. RSA encryption uses a public key and a private key where:
This means for some integer . Decryption raises the ciphertext to the power :
By Euler's theorem, (when ). So:
Euler's theorem is the ENTIRE reason RSA decryption recovers the original message.
Let's see this in action.
Exercises
Exercise 1 (Worked): Computing from Prime Factorization
Compute using the prime factorization .
Strategy: Use multiplicativity and the prime-power formula:
Exercise 2 (Guided): Fermat's Theorem as a Primality Hint
Fermat's Little Theorem says: if is prime, then for all .
What about the converse? If for some , does that prove is prime? (Spoiler: no! These are called Carmichael numbers.)
Your task: test whether passes the Fermat test for base , and then check if 561 is actually prime.
Exercise 3 (Independent): RSA by Hand
Carry out a complete mini-RSA by hand (with SageMath to check your work):
Let , . Compute and .
Choose . Verify .
Find using the extended Euclidean algorithm (or
inverse_mod).Encrypt the message : compute .
Decrypt: compute and verify you recover .
Explain which theorem guarantees that step 5 recovers .
Summary
| Concept | Key idea |
|---|---|
| Euler's totient | Counts integers in coprime to . Key formulas: , , and when |
| RSA formula | For distinct primes : |
| Fermat's Little Theorem | when is prime and |
| Euler's theorem | when , the generalization to composite moduli |
| Group theory connection | $\varphi(n) = |
| RSA correctness | . Euler's theorem is the entire reason RSA works |
Next: The Chinese Remainder Theorem, another essential tool for RSA, and the reason is multiplicative.