The extended Euclidean algorithm and modular inverses
The extended Euclidean algorithm worked as a full table, then used to compute a modular inverse and to show when one does not exist.
- Run the extended Euclidean algorithm on two integers and produce every row of the table.
- Read the Bezout coefficients x and y off the finished table and verify ax + by = gcd(a, b).
- Compute a modular inverse with the extended Euclidean algorithm, and explain why one can fail to exist.
18 min read
Intuition
The plain Euclidean algorithm on the previous page answers “what is ?” and stops there. Encryption needs a second, related question answered: given and a modulus , is there a number that “undoes” multiplication by , modulo ? That number is the modular inverse, and RSA’s private key is computed by finding exactly one. The extended Euclidean algorithm answers both questions in a single pass — it never costs more work than the plain version, and it hands back the modular inverse as a byproduct.
Mechanism
For integers and , the extended Euclidean algorithm finds not just but two integers and satisfying It does this by running the same division steps as the plain Euclidean algorithm, while carrying two extra running values, and , alongside the remainder at every row. Each row satisfies : the remainder, expressed as a combination of the original and . The last row before the remainder hits gives , and together.
Worked example
Answergcd(240, 46) = 2, x = -9, y = 47
Start the table with () and (). Each later row divides the previous remainder by the current one and updates and the same way.
- . . . . Row: .
- . . . . Row: .
- . . . . Row: .
- . . . . Row: .
- . . The remainder has reached , so the table stops here.
- The last row with a non-zero remainder is row 4: , , . So , , . Check: .
Mechanism
This reproduces the same five division steps the lecture writes out directly — , , , , — and reaches the same coefficients the lecture finds by substituting each equation back into the last: , i.e. , . The table method and the back-substitution method are the same algorithm; the table just keeps the running and instead of unwinding the equations afterward.
Mechanism
For coprime to (that is, ), the extended Euclidean algorithm on and gives . Reducing both sides modulo makes the term vanish, leaving — so , reduced into the range , is the modular inverse of modulo , written .
Worked example
Answer3^-1 mod 11 = 4
Run the table on , .
- . . , . Row: . ( is smaller than , so the first quotient is — the algorithm still runs correctly.)
- . . . . Row: .
- . . . . Row: .
- . . Remainder is , table stops.
- Last non-zero remainder: row 3, , . Since , a modular inverse exists, and it is . Check: .
Aside
This reaches the same coefficient the lecture’s back-substitution gets from the same two division steps, and : working backward gives , so is the coefficient of — the modular inverse. Typing , into the stepper above reproduces this exact table, including the leading row.
Mechanism
Not every has an inverse modulo . Since , reducing mod only isolates a on the right-hand side when . If , no satisfies — the table still runs, but its final gcd is not .
Worked example
Answer2x ≡ 1 (mod 10) has no solution
- . . , . Row: .
- . . Remainder is , table stops.
- Last non-zero remainder: row 0, . So , not . Since and share the common factor , has no modular inverse mod .
Exam detail
The lecture’s own check confirms this the direct way: every product for is even (), and is odd, so it never appears. Both arguments — the gcd test and the direct enumeration — agree, and either is an acceptable exam answer.
Pitfall
A modular inverse requires , full stop — not “small,” not “close to coprime.” and share only the factor , and that is already enough to rule out every inverse. Do not confuse this with ordinary division, where every non-zero number has a reciprocal; modular arithmetic on only grants that to the elements coprime to , which is exactly the property counts, on the next page.
Formula
Bezout identity
- the two input integers
- gcd(a, b)
- the Bezout coefficients
Exam detail
This is the step RSA depends on directly. The private exponent is — the modular inverse of the public exponent , modulo the totient of the modulus. Key generation runs exactly the algorithm on this page: if , there is no valid , and must be rejected and chosen again. Module 6 relies on this fact without re-deriving it.
Recall
Why does the extended Euclidean algorithm always find a modular inverse when one exists, without any extra searching?
Because it computes and with as a direct side effect of finding the gcd, not by trying candidate inverses one at a time. When , that same , reduced mod , already is the inverse — no search step is needed.
Source
Week 4 Number Theory PDF