Euler's Theorem
Euler extended Fermat's theorem from primes to every modulus: raising a unit to the power gives 1. With it, RSA's correctness is a two-line proof.
Statement
If , then
For a prime this is Fermat's little theorem, since .
Proof. The same rearrangement as for Fermat: let be the units modulo . Multiplying each by the unit permutes them, so , and the product of units can be cancelled.
A group-theory view: the units form a group of order , and by Lagrange's theorem the order of every element divides the order of the group.
from math import gcd
from sympy import totient
for n in (10, 12, 21, 100):
ph = int(totient(n))
units = [a for a in range(1, n) if gcd(a, n) == 1]
print(n, ph, all(pow(a, ph, n) == 1 for a in units))
print("non-unit 2 mod 10:", [pow(2, k, 10) for k in range(1, 9)])10 4 True
12 4 True
21 12 True
100 40 True
non-unit 2 mod 10: [2, 4, 8, 6, 2, 4, 8, 6]
For a non-unit the theorem fails: powers of 2 modulo 10 cycle through and never reach 1.
Reducing exponents
For a unit , . Example: the last two digits of . and , so we need . With it is even quicker: .
Towers of exponents reduce recursively: uses .
print(pow(7, 2026, 100), pow(7, 2026 % 40, 100))
# 3^(3^(3^3)) mod 1000: reduce the exponent modulo phi(1000) = 400
e = pow(3, 3**3, 400)
print(pow(3, e, 1000))49 49
387
Why RSA works
RSA picks , a public exponent and with , so . For a message coprime to :
The rare messages sharing a factor with also decrypt correctly; check modulo and separately (Fermat for each, then CRT).
p, q, e = 101, 113, 17
n, ph = p * q, (p - 1) * (q - 1)
d = pow(e, -1, ph)
bad = [m for m in range(n) if pow(pow(m, e, n), d, n) != m]
print("messages that fail:", bad)
print("m = 202 (a multiple of p):", pow(pow(202, e, n), d, n))messages that fail: []
m = 202 (a multiple of p): 202
Every one of the 11413 possible messages decrypts correctly, including multiples of or .
In software
- Exponent reduction modulo or is how RSA private keys are derived, and how CRT decryption shortens the exponents to and .
- Verifiable delay functions and time-lock puzzles rely on the opposite: without knowing you cannot shortcut , so you must do all squarings.
Exercises
Exercise 1: Last two digits
Find the last two digits of .
Answer
Solution
Euler gives , but the order of 3 is even smaller: and , so . Since , . Check with pow(3, 2026, 100).
Exercise 2: Inverse via Euler
Show that for a unit , and use it to invert 3 modulo 10.
Answer
.
Solution
. With : , and .
Exercise 3: A power tower (code)
Compute , that is , both directly and by reducing the exponent. Why must you be careful, given that 2 is not a unit modulo 1000?
Answer
736 both ways, but the shortcut needs care.
Solution
direct = pow(2, 65536, 1000)
# 1000 = 8 * 125: modulo 8 the power is 0; modulo 125 Euler applies (phi(125) = 100)
m125 = pow(2, 65536 % 100, 125)
x = next(x for x in range(1000) if x % 8 == 0 and x % 125 == m125)
print(direct, x)736 736
Euler's theorem needs . Split : modulo 8 any high power of 2 is 0, modulo 125 the exponent reduces modulo 100, and the CRT combines them.
Test yourself
Euler's theorem requires…
- prime
- prime
Modulo 15, equals 1 for every unit because…
- 8 is a power of 2
- it is a coincidence
RSA decryption works because…
- and Euler's theorem
- is prime