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 readJump to the quiz ↓
The extended algorithm returns the gcd as a combination of a and b
The plain Euclidean algorithm on the previous page answers “what is ?” and stops there. Encryption needs a second 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, and it hands back the modular inverse as a byproduct.
For integers and , it finds not just but two integers and satisfying It runs the same division steps as the plain algorithm, 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.
Formula
Bezout identity
- the two input integers
- gcd(a, b)
- the Bezout coefficients
Try it — worked example
Use the extended Euclidean algorithm on a = 240, b = 46. Find gcd and x, y with 240x + 46y = gcd.
Show answer and steps
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: .
The table reproduces the five division steps the lecture writes out directly, , , , , , and reaches the same coefficients the lecture finds by substituting each equation back into the last: . The table method and back-substitution are the same algorithm; the table keeps the running and instead of unwinding the equations afterward.
When gcd(a, n) = 1, the coefficient x is the modular inverse
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 .
Try it — worked example
Find the inverse of 3 mod 11.
Show answer and steps
Answer3^-1 mod 11 = 4
Run the table on , . The lecture’s back-substitution uses the same division steps, and , and working backward gives , so is the coefficient of . Typing , into the stepper above reproduces this table.
- . . , . Row: . ( is smaller than , so the first quotient is and 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: .
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.
No inverse exists unless a and n are coprime, and RSA relies on this
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 .
Try it — worked example
Find the inverse of 2 mod 10, or show it does not exist.
Show answer and steps
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 .
The lecture’s own check confirms this the direct way: every product for is even (), and is odd, so it never appears. Either the gcd test or the direct enumeration is an acceptable exam answer.
Pitfall
A modular inverse requires , full stop. 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.
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.
Check yourself
Source
Week 4 Number Theory PDF