The Extended Euclidean Algorithm
Euclid's algorithm tells you that . The extended version also tells you why: it finds integers and with . Those coefficients are modular inverses, which RSA, Diffie-Hellman, elliptic curves and the Chinese remainder theorem all need.
Bézout's identity
For integers , not both 0, there are integers with
Moreover is the smallest positive integer of the form . (Sketch: let be that smallest positive value. Dividing by leaves a remainder that is again of the form and smaller than , so it is 0; hence and likewise . Any common divisor of and divides , so is the greatest.)
A corollary used constantly: and are coprime if and only if has a solution.
Back-substitution by hand
Run Euclid on and , then walk back up, writing each remainder as a combination of and :
So .
The iterative algorithm
Instead of walking back, keep the coefficients alongside the remainders. Each row satisfies , and the new row is the old one minus times the current one.
def ext_gcd(a, b):
"""Return (g, x, y) with a*x + b*y == g == gcd(a, b)."""
r0, x0, y0 = a, 1, 0
r1, x1, y1 = b, 0, 1
while r1:
q = r0 // r1
r0, r1 = r1, r0 - q * r1
x0, x1 = x1, x0 - q * x1
y0, y1 = y1, y0 - q * y1
return r0, x0, y0
for a, b in [(240, 46), (1071, 462), (17, 3120)]:
g, x, y = ext_gcd(a, b)
print(f"gcd({a}, {b}) = {g} = {a}*({x}) + {b}*({y})", a * x + b * y == g)gcd(240, 46) = 2 = 240*(-9) + 46*(47) True
gcd(1071, 462) = 21 = 1071*(-3) + 462*(7) True
gcd(17, 3120) = 1 = 17*(-367) + 3120*(2) True
From Bézout to inverses
If , then , and reducing modulo gives : is the inverse of modulo . This is exactly how RSA computes its private exponent , and it is what pow(e, -1, phi) does internally.
def ext_gcd(a, b):
r0, x0, r1, x1 = a, 1, b, 0
while r1:
q = r0 // r1
r0, r1, x0, x1 = r1, r0 - q * r1, x1, x0 - q * x1
return r0, x0
def inverse(a, n):
g, x = ext_gcd(a % n, n)
if g != 1:
raise ValueError(f"{a} has no inverse mod {n} (gcd {g})")
return x % n
e, phi = 17, 3120
d = inverse(e, phi)
print(d, (e * d) % phi, pow(e, -1, phi))2753 1 2753
Size of the coefficients
The coefficients never get large: Bézout coefficients from the algorithm satisfy and (when , apart from trivial cases). So inverses modulo a 2048-bit number are themselves at most 2048 bits, and computing them is fast.
In software
- Every big-number library has an
invert/modInversefunction built on the extended Euclidean algorithm (or its binary variant); OpenSSL's isBN_mod_inverse. - Elliptic-curve point addition needs one modular inverse per operation, so libraries use projective coordinates to postpone the division until the very end.
Exercises
Exercise 1: By hand
Find integers with .
Answer
, (one solution).
Solution
, . Back-substitute: . So , : .
Exercise 2: An inverse
Use Exercise 1 to find the inverse of 12 modulo 35.
Answer
Solution
From , reducing modulo 35 gives . Check: .
Exercise 3: No inverse (code)
Use ext_gcd to explain why 21 has no inverse modulo 91, and find the smallest positive integer of the form .
Answer
, so the smallest positive value is 7, never 1.
Solution
def ext_gcd(a, b):
r0, x0, y0, r1, x1, y1 = a, 1, 0, b, 0, 1
while r1:
q = r0 // r1
r0, r1, x0, x1, y0, y1 = r1, r0 - q * r1, x1, x0 - q * x1, y1, y0 - q * y1
return r0, x0, y0
print(ext_gcd(21, 91))(7, -4, 1)
Every value is a multiple of 7, so is impossible.
Test yourself
Bézout's identity says that is…
- the largest number of the form
- the smallest positive number of the form
- always 1
- divided by the lcm squared
If , then the inverse of modulo is…
RSA uses the extended Euclidean algorithm to compute…
- the primes and
- the private exponent
- the ciphertext
- a hash