Sub topic 11
Computing Modular Inverses
This chapter assumes you already know three things. You should know modular arithmetic notation, such as a ≡ b (mod m). You should be able to read the extended_gcd function from the previous chapter. You should also know Bézout's identity: for any integers a and b there are integers x and y with ax + by = gcd(a, b).
What a Modular Inverse Is
A modular inverse of a modulo m is an integer x such that
a · x ≡ 1 (mod m).
It does the job of division in modular arithmetic. Integers modulo m have no fraction like 1/7. If 7 has an inverse, though, multiplying by that inverse "divides by 7." For example, 3 · 4 = 12 ≡ 1 (mod 11), so 4 is the inverse of 3 modulo 11.
Inverses show up wherever code works in modular arithmetic. RSA key generation uses them to find the private exponent. Linear congruential generators and hash functions use them, and so does any exact computation over a finite field.
When an Inverse Exists
An inverse of a modulo m exists exactly when gcd(a, m) = 1.
The reasoning goes in both directions:
- If gcd(a, m) = 1, Bézout's identity gives integers x and y with ax + my = 1. Take this equation modulo m. The term my is a multiple of m, so it becomes 0. That leaves ax ≡ 1 (mod m), so x is the inverse.
- If gcd(a, m) = g > 1, then g divides both a and m. Every value of ax − km is then a multiple of g, so it can never equal 1. No inverse exists.
Here is the table of inverses modulo 7. Because 7 is prime, every nonzero value has an inverse:
| a | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| a⁻¹ mod 7 | 1 | 4 | 5 | 2 | 3 | 6 |
Modulo 15 the picture changes. The values 3, 5, 6, 9, 10, and 12 share a factor with 15, so none of them has an inverse.
A Worked Example: 7 Modulo 26
To find the inverse of 7 modulo 26, first run Euclid's algorithm:
- 26 = 3 · 7 + 5
- 7 = 1 · 5 + 2
- 5 = 2 · 2 + 1
- 2 = 2 · 1 + 0
The last nonzero remainder is 1, so an inverse exists. Next, substitute backward to write 1 as a combination of 26 and 7:
- 1 = 5 − 2 · 2
- 1 = 5 − 2 · (7 − 5) = 3 · 5 − 2 · 7
- 1 = 3 · (26 − 3 · 7) − 2 · 7 = 3 · 26 − 11 · 7
The coefficient of 7 is −11, and −11 mod 26 = 15. To check: 7 · 15 = 105 = 4 · 26 + 1.
Implementation
The iterative extended algorithm keeps track of the coefficients as it goes, so you never do the back-substitution by hand. The last step, x % m, maps a negative coefficient into the range 0 to m − 1.
def extended_gcd(a, b):
# Returns (g, x, y) with a*x + b*y == g == gcd(a, b), for a, b >= 0
old_r, r = a, b
old_s, s = 1, 0
old_t, t = 0, 1
while r != 0:
q = old_r // r
old_r, r = r, old_r - q * r
old_s, s = s, old_s - q * s
old_t, t = t, old_t - q * t
return old_r, old_s, old_t
def mod_inverse(a, m):
if m <= 0:
raise ValueError("modulus must be positive")
g, x, _ = extended_gcd(a % m, m)
if g != 1:
raise ValueError("inverse does not exist")
return x % m
print(mod_inverse(3, 11)) # 4
print(mod_inverse(7, 26)) # 15
print(mod_inverse(-7, 26)) # 11, since -7 * 11 = -77 = 1 (mod 26)
print(pow(7, -1, 26)) # 15, built-in since Python 3.8
try:
mod_inverse(6, 15)
except ValueError as e:
print(e) # inverse does not existThe function reduces a % m first. That makes negative inputs safe, because Python's % always returns a value in [0, m) when m is positive. It also keeps the inputs to extended_gcd inside the range where that function's precondition (a, b ≥ 0) holds.
Using Inverses to Solve Congruences
Inverses let you solve a linear congruence ax ≡ b (mod m) when gcd(a, m) = 1. Multiply both sides by a⁻¹. For 7x ≡ 3 (mod 26):
x ≡ 3 · 15 = 45 ≡ 19 (mod 26).
To check: 7 · 19 = 133 = 5 · 26 + 3.
Common Errors
- Returning the raw coefficient. The extended algorithm often returns a negative x, such as −11 above. Always reduce it with
x % m. - Skipping the gcd check. If g ≠ 1, the value
x % mis still a number, but it is not an inverse. Code that skips the check fails silently. - Assuming every nonzero value is invertible. That is true only when m is prime. For composite moduli, many values have no inverse.
- Using a modulus of 1 or less. When m = 1, every integer is congruent to 0, and the function returns 0. That result is defined but rarely what you intend. Moduli of 0 or below should be rejected, as the code above does.
Exercise
Find the inverse of 5 modulo 17 using Euclid's algorithm and back-substitution. Verify your answer.
Show answer
17 = 3 · 5 + 2, then 5 = 2 · 2 + 1. Back-substituting: 1 = 5 − 2 · 2 = 5 − 2 · (17 − 3 · 5) = 7 · 5 − 2 · 17. The inverse is 7. Check: 5 · 7 = 35 = 2 · 17 + 1.
Exercise
Does 10 have an inverse modulo 25? What does mod_inverse(10, 25) do, and why?
Show answer
No. gcd(10, 25) = 5, so every value of 10x − 25k is a multiple of 5 and can never equal 1. The function raises ValueError("inverse does not exist") because extended_gcd returns g = 5.
Exercise
Solve 4x ≡ 9 (mod 11).
Show answer
The inverse of 4 modulo 11 is 3, because 4 · 3 = 12 ≡ 1. Then x ≡ 9 · 3 = 27 ≡ 5 (mod 11). Check: 4 · 5 = 20 = 11 + 9.