Modular Inverses
"Dividing" modulo means multiplying by an inverse: the number with . RSA's private key is an inverse, every elliptic-curve addition computes one, and the Chinese remainder theorem is built from them. There are three ways to compute it.
Existence
has an inverse modulo if and only if , and then the inverse is unique modulo . (If , multiply by to get .)
Method 1: extended Euclid
From we read . This works for any modulus and costs steps (The Extended Euclidean Algorithm). Python's pow(a, -1, n) does exactly this.
Method 2: Fermat (prime modulus)
If is prime and , then (Fermat's Little Theorem), so . One modular exponentiation, and it is easy to implement in constant time, which is why curve libraries often use it.
def egcd_inverse(a, n):
r0, x0, r1, x1 = a % n, 1, n, 0
while r1:
q = r0 // r1
r0, r1, x0, x1 = r1, r0 - q * r1, x1, x0 - q * x1
if r0 != 1:
raise ValueError("not invertible")
return x0 % n
p = 2**127 - 1 # a Mersenne prime
a = 123456789123456789
i1, i2, i3 = egcd_inverse(a, p), pow(a, p - 2, p), pow(a, -1, p)
print(i1 == i2 == i3, a * i1 % p)
print(egcd_inverse(7, 40), pow(7, -1, 40))True 1
23 23
Method 3: batch inversion
Need the inverses of many numbers modulo ? Montgomery's trick computes the prefix products, inverts only the last one, and walks back: multiplications and one inversion instead of inversions.
def batch_inverse(xs, p):
prefix = [1]
for x in xs:
prefix.append(prefix[-1] * x % p)
inv = pow(prefix[-1], -1, p) # the only inversion
out = [0] * len(xs)
for i in range(len(xs) - 1, -1, -1):
out[i] = inv * prefix[i] % p # = 1 / xs[i]
inv = inv * xs[i] % p # = 1 / (x_0 ... x_{i-1})
return out
p = 10**9 + 7
xs = [2, 3, 10, 999, 123456]
inv = batch_inverse(xs, p)
print(inv)
print([x * y % p for x, y in zip(xs, inv)])[500000004, 333333336, 700000005, 874874881, 78351802]
[1, 1, 1, 1, 1]
A table of all inverses modulo
For a prime there is even a recurrence that fills in all inverses of in linear time: . It comes from .
p = 13
inv = [0, 1]
for i in range(2, p):
inv.append(-(p // i) * inv[p % i] % p)
print(inv[1:])
print(all(i * inv[i] % p == 1 for i in range(1, p)))[1, 7, 9, 10, 8, 11, 2, 5, 3, 4, 6, 12]
True
In software
- RSA libraries compute and the CRT coefficient once at key generation.
- Elliptic-curve code works in projective coordinates and does a single inversion (often by Fermat, ) at the end of a scalar multiplication. Batch inversion is used when many points are normalized together, for example in signature batch verification.
Exercises
Exercise 1: By hand
Find .
Answer
Solution
, , . Back-substitute: . So . Check: .
Exercise 2: Fermat inverse
Use to find .
Answer
Solution
: , , , . Check: .
Exercise 3: Count the work (code)
Batch-invert the numbers modulo and confirm every product is 1. How many inversions did Montgomery's trick use?
Answer
One inversion; all 1000 products are 1.
Solution
p = 10**9 + 7
xs = list(range(1, 1001))
prefix = [1]
for x in xs:
prefix.append(prefix[-1] * x % p)
inv_all, out = pow(prefix[-1], -1, p), [0] * len(xs)
for i in range(len(xs) - 1, -1, -1):
out[i] = inv_all * prefix[i] % p
inv_all = inv_all * xs[i] % p
print(all(x * y % p == 1 for x, y in zip(xs, out)), out[1], out[999])True 500000004 857000006
out[1] is the inverse of 2, which is .
Test yourself
For a prime and , the inverse of modulo is…
Montgomery's batch inversion of numbers needs how many modular inversions?
- 1
Which has no inverse modulo 26?
- 3
- 5
- 13
- 25