Inverses, Fermat and Euler
Division modulo means multiplying by an inverse. This lesson finds inverses with the extended Euclidean algorithm, then proves two theorems about powers that make modular exponentiation predictable: Fermat's little theorem for primes and Euler's theorem for any modulus. Both are needed for RSA in the next lesson.
Modular inverses
An inverse of modulo is a number with .
Theorem. has an inverse modulo if and only if .
Proof. If , Bézout gives , so : is an inverse. Conversely, if , then , so any common divisor of and divides 1.
The proof is an algorithm: run the extended Euclidean algorithm and read off .
def inverse(k, n):
r0, r1, s0, s1 = n, k % n, 0, 1
while r1:
q = r0 // r1
r0, r1 = r1, r0 - q * r1
s0, s1 = s1, s0 - q * s1
if r0 != 1:
raise ValueError(f"{k} has no inverse mod {n} (gcd = {r0})")
return s0 % n
print("7^-1 mod 26 =", inverse(7, 26), " check:", 7 * inverse(7, 26) % 26)
print("17^-1 mod 3120 =", inverse(17, 3120), " built-in:", pow(17, -1, 3120))
try:
inverse(6, 15)
except ValueError as e:
print(e)7^-1 mod 26 = 15 check: 1
17^-1 mod 3120 = 2753 built-in: 2753
6 has no inverse mod 15 (gcd = 3)
With inverses you can solve linear congruences: gives .
Fermat's little theorem
Theorem. If is prime and , then
Proof. Multiplying by permutes the nonzero residues (it is injective because has an inverse, and an injective map of a finite set to itself is a bijection). So the products are equal:
The left side is . Since is coprime to , cancel it to get .
Consequences: (another way to compute inverses), and exponents can be reduced modulo : .
p = 13
for a in [2, 5, 12]:
print(f"a={a}: a^(p-1) mod p = {pow(a, p - 1, p)}, inverse via a^(p-2): {pow(a, p - 2, p)}")
print("2^1000 mod 13 =", pow(2, 1000, 13), " = 2^(1000 mod 12) mod 13 =", pow(2, 1000 % 12, 13))
# the converse fails: 561 = 3*11*17 passes the Fermat test for every coprime base
print("561 passes for base 2?", pow(2, 560, 561) == 1)a=2: a^(p-1) mod p = 1, inverse via a^(p-2): 7
a=5: a^(p-1) mod p = 1, inverse via a^(p-2): 8
a=12: a^(p-1) mod p = 1, inverse via a^(p-2): 12
2^1000 mod 13 = 3 = 2^(1000 mod 12) mod 13 = 3
561 passes for base 2? True
The last line shows a Carmichael number: composite, yet for every coprime to it. That is why real primality tests use the stronger Miller-Rabin test.
Euler's theorem
For general , let Euler's totient be the number of integers in coprime to . Examples: for a prime, , and is multiplicative for coprime arguments, so
Theorem (Euler). If , then .
The proof is Fermat's argument applied to the residues coprime to , which multiplication by permutes.
from math import gcd
from sympy import totient
def phi(n):
return sum(1 for k in range(1, n + 1) if gcd(k, n) == 1)
for n in [10, 12, 13, 15, 3233]:
a = 7
print(f"phi({n}) = {phi(n)} (sympy {totient(n)}), 7^phi(n) mod n = {pow(a, phi(n), n)}")phi(10) = 4 (sympy 4), 7^phi(n) mod n = 1
phi(12) = 4 (sympy 4), 7^phi(n) mod n = 1
phi(13) = 12 (sympy 12), 7^phi(n) mod n = 1
phi(15) = 8 (sympy 8), 7^phi(n) mod n = 1
phi(3233) = 3120 (sympy 3120), 7^phi(n) mod n = 1
, and .
In software
Cryptographic libraries compute inverses with the extended Euclidean algorithm (or via Fermat when the modulus is prime, as in elliptic-curve code over a prime field). Python offers both: pow(a, -1, n) and pow(a, p - 2, p). Fermat's theorem is also behind a cheap first filter in primality testing.
Exercises
Exercise 1: An inverse by hand
Find the inverse of 5 modulo 17.
Answer
Solution
. Via Fermat: gives the same 7.
Exercise 2: Solve a congruence
Solve .
Answer
.
Solution
; , so . Then . Check: .
Exercise 3: Big exponent
Compute by hand with Fermat.
Answer
Solution
By Fermat, . , so ().
Exercise 4: Totients (code)
Compute for and check that for each.
Answer
The identity holds for all of them.
Solution
Each has for exactly one divisor , and the with correspond to the numbers coprime to , so the classes have sizes .
from math import gcd
phi = lambda n: sum(1 for k in range(1, n + 1) if gcd(k, n) == 1)
print([phi(n) for n in range(1, 21)])
print(all(sum(phi(d) for d in range(1, n + 1) if n % d == 0) == n for n in range(1, 21)))[1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6, 18, 8]
True
Test yourself
When does have an inverse modulo ?
- Always
- When
- When
- When is even
By Fermat, for not divisible by 17 is…
- 0
- 1
- 16
What is ?
- 34
- 24
- 12
- 35