https://samnot.es/rsa/extended-euclidean/

# Extended Euclidean Algorithm Part of my series explaining the RSA Algorithm The Extended Euclidean Algorithm is, as you might imagine, an extension of the standard Euclidean Algorithm. The standard version was developed in order to find the greatest common divisor (GCD) of two numbers. It also allows us to prove Lagrange’s four-square theorem. The Extended algorithm adds the capability to find the Bézout coefficients for the two input numbers.

Sam

# rsa / Extended Euclidean Algorithm — Number Theory

# Extended Euclidean Algorithm

Part of my series explaining the RSA Algorithm

The Extended Euclidean Algorithm is, as you might imagine, an extension of the standard Euclidean Algorithm. The standard version was developed in order to find the greatest common divisor (GCD) of two numbers. It also allows us to prove Lagrange’s four-square theorem.

The Extended algorithm adds the capability to find the Bézout coefficients for the two input numbers.

We’ll cover the Extended algorithm & the Bézout identity but first we need to understand the core algorithm, for finding the GCD of 2 numbers.

The Euclidean Algorithm

Define a & b as the two numbers for which we want to find the GCD. Here’s how the algorithm works:

Step 1) If a is 0 then b is the GCD.

Step 2) Else, set a = b % a. and set b = previous a. Repeat these steps until a is 0. b is your GCD.

Here’s an example for finding the GCD of 273 & 399:

    > a = 273, b = 399
    > a is not 0
    a₁ = 126 (399 % 273)
    b₁ = a = 273

    > a₁ is not 0
    a₂ = 21 (273 % 126)
    b₂ = a₁ = 126

    > a₂ is not 0
    a₃ = 0 (126 % 21)
    b₃ = a₂ = 21

    > a₃ is 0
    > b₃ is 21
    > So GCD is 21

And we can verify that 21 is a divisor of both 462 & 1071:

    273 % 21 = 0
    399 % 21 = 0

Why does that work? We can visualise the reasoning using a rectangle, where its lengths take the size of the two numbers in question.

1

To find a number which divides both 399 and 273, we need to find a square of some size which can tile the entire rectangle. To find the GCD, we are looking for the largest square that can do this.

Euclid’s algorithm works as follows. Take the length of the smaller side of the rectangle. 273 in this case. Fill the rectangle with as many 273 x 273 squares as will fit. Find the sides of the remaining rectangle, if any. One side will have length 273, the shorter length of the original rectangle.

2

We filled the rectangle with squares of length 273, physically dividing it. The remainder of the length is therefore found with long_side % short_side e.g. 399 % 273 = 126. If the remainder is 0, the whole rectangle is filled. The size of the square is our GCD. Otherwise, we treat the leftover area as a new rectangle, and repeat the process. This continues until no space is left. At that point, the size of the last square used is the GCD.

This is a visual indication of how Euclid’s Algorithm works. The process of redefining a & b is equivalent to repeating the process of filling a rectangle as much as possible, then choosing a new size of square, and filling again.

3

21 is our smallest square length size. With those squares we are able to completely cover the rectangle. Because these 21 x 21 squares could cover the whole rectangle we know that 21 literally fits into both 399 & 273.

That’s how the Euclidean algorithm operates. We can use it to determine what the greatest common divisor of two numbers is, or determine whether two numbers are coprime (have a GCD of 1).

Bézout’s Identity

Bézout’s Identity states that for the GCD of a & b, which we’ll call d, there are integers x & y such that ax + by = d.

It’s used in modular arithmetic to help find modular multiplicative inverses, because it rearranges like so:

    ax + by = d

    > subtract d, subtract -by
    ax - d = -by

    > this tells us that (ax - d) is a multiple of b
    ax - d = (-y)b

    > So we know that
    ax % b = d

More generally, the Bézout Identity gives us a way of reasoning about coprimes in relation to one another. How could we calculate these values? Up steps the Extended Euclidean Algorithm.

The Extended Euclidean Algorithm

Euclid didn’t stop with GCDs. We can extend the algorithm we looked at earlier to calculate not just the GCD of two numbers, but their Bézout coefficients too. For finding modular multiplicative inverses this algorithm is a one-stop shop.

You’ll remember that Euclid’s algorithm recursively redefined a & b until we found a divisor which left no remainder. That part of the process does not change. Now though, alongside finding the GCD of a & b we will also find their Bézout coefficients as we go along.

At each step of the Euclidian Algorithm b is the proposed GCD. At the end of the algorithm the final value of b is the correct GCD. The Extended Algorithm works with this assumption, by calculating the Bézout coeffecients for each attempt at a GCD, the value bᵢ, at each step.

We know the formula for Bézout’s Identity is ax + by = GCD. At each step bᵢ is the proposed GCD. So aᵢxᵢ + bᵢyᵢ = bᵢ. At the beginning this is simple. b₀ is the proposed GCD.

> Where i = 0, we want to satisfy
a₀x₀ + b₀y₀ = b₀

> Solved by x₀ = 0, y₀ = 1
a₀(0) + b₀(1)
= b₀(1) 
= b₀

So we have our starting values x₀, y₀ = 0, 1. In the following steps the value of b will change. We need to adjust x & y accordingly, so that:

> for every step, i:
a₀xᵢ + b₀yᵢ = bᵢ

For each new value of the potential GCD, bᵢ, we need to find xᵢ and yᵢ so that they conform to the Bezout Identity for the original a and b values, a₀ & b₀ . This would be intensive, if we did not have the x & y values from the previous step. With those values we are able to deduce the new ones.

> Remember how we find b for the next step?
bᵢ₊₁ = aᵢ

> And also how we find b for the next step
aᵢ = bᵢ₋₁ % aᵢ₋₁

> We can substitute "a" out altogether
bᵢ₊₁ = aᵢ
bᵢ₊₁ = bᵢ₋₁ % bᵢ

Note: A “Euclidean division” separates the “quotient”, which is the integer part of the result, and the remainder For example 62/7 would give a quotient of 8 and a remainder of 6. A modulus operation is simply the dividend, 62, minus the divisor multiplied by the quotient: 7 * 8. 62 - (7 * 8) = 6

Another example:

43 % 8 = 3

> OR
QUOTIENT OF 43/8 = 5
43 - (8 * 5) = 3

With this in mind, let’s start to prep for the Extended algorithm.

> We want an easy way to find the Bézout Coefficients at each step
bᵢ₊₁ = a₀xᵢ₊₁ + b₀yᵢ₊₁

> We know how to find bᵢ₊₁:
bᵢ₊₁ = bᵢ₋₁ % bᵢ

> Or in terms of Euclidean division
bᵢ₊₁ = bᵢ₋₁ - bᵢ(QUOTIENT(bᵢ₋₁ / bᵢ))

> Let us define the quotient at each step:
qᵢ = QUOTIENT(bᵢ₋₁ / bᵢ)

> So to find the new b value:
bᵢ₊₁ = bᵢ₋₁ - bᵢqᵢ

> We will have Bézout's Identity for the previous step
bᵢ₋₁ = a₀xᵢ₋₁ + b₀yᵢ₋₁

> And we know the formula for the ith step
bᵢ = a₀xᵢ + b₀yᵢ

If that all makes sense, now we’re going to do some substitutions using what we’ve established, in order to find some way of actually calculating these Bézout coefficients going forward.

> given the equations we have derived
bᵢ = a₀xᵢ + b₀yᵢ
bᵢ₋₁ = a₀xᵢ₋₁ + b₀yᵢ₋₁

> and our formula for finding the next b value
bᵢ₊₁ = bᵢ₋₁ - bᵢqᵢ

> we can substitute them in
bᵢ₊₁ = (a₀xᵢ₋₁ + b₀yᵢ₋₁) - (a₀xᵢ + b₀yᵢ)qᵢ
bᵢ₊₁ = a₀xᵢ₋₁ + b₀yᵢ₋₁ - a₀xᵢqᵢ - b₀yᵢqᵢ

> group a & b terms
bᵢ₊₁ = (a₀xᵢ₋₁ - a₀xᵢqᵢ) + (b₀yᵢ₋₁ - b₀yᵢqᵢ)

> and factorise
bᵢ₊₁ = a₀(xᵢ₋₁ - xᵢqᵢ) + b₀(yᵢ₋₁ - yᵢqᵢ)

> compare this to the original Bézout formula
bᵢ₊₁ = a₀(xᵢ₊₁) + b₀(yᵢ₊₁)

> we can see that
xᵢ₊₁ = xᵢ₋₁ - xᵢqᵢ
yᵢ₊₁ = yᵢ₋₁ - yᵢqᵢ

This gives us a way to understand the relationship between the Bézout coefficients at consecutive steps of the Euclidean Algorithm.

Remember these steps from the original algorithm:

Define a & b as the two numbers for which we want to find the GCD. Here’s how the algorithm works: Step 1) If a is 0 then b is the GCD. Step 2) Else, set a = b % a. and set b = previous a. Repeat these steps until a is 0. b is your GCD.

We need to append to and amend these slightly in order to find both the GCD and Bézout’s Identity.

Performing the Extended Euclidean Algorithm

Prep Step 1) Define r₀ & r₁. r₀ is the value a, r₁ is the value b. These work just like a & b but makes successive steps easier to follow.

Prep Step 2) Define x₀ & x₁ as 1 & 0 respectively. Also, define y₀ & y₁ as 0 & 1 respectively. These values are pre-selected so that Bézout’s Identity is correct from the start. Remember that r₁ is the initial candidate for GCD. Therefore r₀x + r₁y = r₁ should be satisfied. This is ensured by setting y₁ = 1 & x₁ = 0 because b = r₁ , therefore r₀(0) + r₁(1) = r₁ .

Now we have those initial values, we can begin the algorithm.

Step 1) If rᵢ is 0, stop. The GCD value is rᵢ₋₁. The Bézout coefficients are xᵢ₋₁ & yᵢ₋₁ . This is the base case, which we’ll hit eventually.

Step 2) Find the new quotient, qᵢ = rᵢ₋₁ / rᵢ.

Step 3) Find the next value of r, rᵢ₊₁ = rᵢ₋₁ — (qᵢ * rᵢ). (Reminder: this is equivalent to rᵢ₋₁ % rᵢ. We use the quotient method because we need the quotient for the following steps.)

Step 4) Find the next value of x, xᵢ₊₁ = xᵢ₋₁ — (qᵢ * xᵢ).

Step 5) Find the next value of y, yᵢ₊₁ = yᵢ₋₁ — (qᵢ * yᵢ).

Step 6) Return to Step 1 and repeat.

That’s how it works. Example time! I’ll include the index, i, at the beginning of each cycle to keep track of where we are.

Take the numbers a = 7 & b = 93.

r₀ = a = 7
r₁ = b = 93

x₀ = 1
x₁ = 0

y₀ = 0
y₁ = 1

> AND begin!

i = 1
r₁ is not 0
q₁ = 0  (quotient of 7/93)
r₂ = 7  (7 - 0 * 93)
x₂ = 1  (1 - 0 * 0)
y₂ = 0  (0 - 0 * 1)

i = 2
r₂ is not 0
q₂ = 13  (quotient of 93 / 7)
r₃ = 2   (93 - 13 * 7)
x₃ = -13 (0 - 13 * 1)
y₃ = 1   (1 - 13 * 0)

i = 3
r₃ is not 0
q₃ = 3  (quotient 7 / 2)
r₄ = 1  (7 - 3 * 2)
x₄ = 40 (1 - 3 * -13)
y₄ = -3 (0 - 3 * 1)

i = 4
r₄ is not 0
q₄ = 2   (quotient 2 / 1)
r₅ = 0   (2 - 2 * 1)
x₅ = -93 (-13 - 2 * 40)
y₅ = 7   (1 - 2 * -3)

i = 5
r₅ IS 0
x = 40  (x₄)
y = -3  (y₄)
GCD = 1 (r₄)

There we have it. A greatest common divisor and Bézout coefficients for the values a = 7, b = 93 . A GCD of 1 means they are coprime. We can confirm the Bézout identity is correct by substituting into the equation.

> ax + by = GCD
(7 * 40) + (93 * -3)
= 280 - 279
= 1

That’s It

The Extended Euclidean Algorithm is an important contribution to number theory, very commonly used in cryptography. By finding coefficients for the Bézout Identity and proving whether a number is coprime, it allows us to find modular multiplicative inverses, as used in RSA encryption.

ARCHIVE

26 Apr 2026 London Marathon: One Year On 15 Apr 2026 A Little Humanity 13 Apr 2026 Hamlet - William Shakespeare 13 Apr 2026 The Rise and Fall of the Third Reich - William L. Shirer 13 Apr 2026 The Secret History - Donna Tartt 11 Apr 2026 A Year of Living Simply - Kate Humble 08 Apr 2026 40 Before 40 08 Apr 2026 Montaigne - Stefan Zweig 04 Apr 2026 William Shakespeare: A Very Short Introduction - Stanley Wells 19 Mar 2026 Unreasonable Hospitality - Will Guidara 15 Mar 2026 Hitler's Secret - Rory Clements 15 Mar 2026 Nemesis - Rory Clements 08 Mar 2026 HRV & Me: Taming a messy stressy mind 02 Mar 2026 Nucleus - Rory Clements 19 Feb 2026 Corpus - Rory Clements 08 Feb 2026 Resonance 16 Jan 2026 A Night to Remember: Sinking of the Titanic - Walter Lord 01 Jan 2026 Everything I've read in 2026 (so far) 15 Dec 2025 Someone from the Past (British Library Crime Classics) - Margot Bennett 01 Dec 2025 Death in Ambush (British Library Crime Classics) - Susan Gilruth 23 Nov 2025 A Cold Wind From Moscow - Rory Clements 10 Nov 2025 The Boleyn Traitor - Philippa Gregory 24 Oct 2025 Death Makes a Prophet (British Library Crime Classics) - John Bude 13 Oct 2025 The Cheltenham Square Murder (British Library Crime Classics) - John Bude 04 Oct 2025 Sussex Downs Murders (British Library Crime Classics) - John Bude 22 Sep 2025 The Lake District Murder (British Library Crime Classics) - John Bude 15 Sep 2025 The Mayor of Casterbridge - Thomas Hardy 10 Sep 2025 The Murder of Roger Ackroyd - Agatha Christie 30 Aug 2025 Marble Hall Murders - Anthony Horowitz 25 Jul 2025 Where Angels Fear to Tread -- EM Forster 10 Jul 2025 Steve Jobs -- Walter Isaacson 10 Jul 2025 The Fifth Risk -- Michael Lewis 10 Jul 2025 The Ride of a Lifetime -- Bob Iger 03 Jul 2025 James -- Percival Everett 01 Jul 2025 Great Expectations -- Charles Dickens 23 Jun 2025 Hillbilly Elegy -- JD Vance 10 Jun 2025 Principles 09 Jun 2025 Revenge of the Tipping Point -- Malcolm Gladwell 06 Jun 2025 The Grand Babylon Hotel -- Arnold Bennett 04 Jun 2025 The Seven Husbands of Evelyn Hugo -- Taylor Jenkins Reid 03 Jun 2025 Rebecca -- Daphne du Maurier 29 May 2025 A Promised Land - Barack Obama 29 May 2025 Less - Andrew Sean Greer 13 May 2025 Careless People - Sarah Wynn-Williams 07 May 2025 Looking Glass War - John Le Carre 04 May 2025 A Murder of Quality - John Le Carre 01 May 2025 London Marathon 2025: Training Retrospective 29 Apr 2025 The Human Factor - Graham Greene 28 Apr 2025 London Marathon 2025: Race Review 27 Apr 2025 Photos: London Marathon 2025 27 Apr 2025 Spectating the London Marathon 2025 [Sunday 27th April] 26 Apr 2025 London Marathon 2025: Week 16 23 Apr 2025 Call for the Dead - John Le Carre 21 Apr 2025 London Marathon 2025: Week 15 16 Apr 2025 The Manchurian Candidate - Richard Condon 13 Apr 2025 London Marathon 2025: Week 14 05 Apr 2025 London Marathon 2025: Week 13 30 Mar 2025 London Marathon 2025: Week 12 26 Mar 2025 Effortless - Greg Mckeown 23 Mar 2025 London Marathon 2025: Week 11 16 Mar 2025 London Marathon 2025: Week 10 09 Mar 2025 London Marathon 2025: Week 9 02 Mar 2025 London Marathon 2025: Week 8 22 Feb 2025 London Marathon 2025: Week 7 16 Feb 2025 London Marathon 2025: Week 6 16 Feb 2025 Problems & [Meta] Problem Solving 14 Feb 2025 Little Dribbling - Bill Bryson 10 Feb 2025 Bring Up the Bodies - Hilary Mantel 09 Feb 2025 London Marathon 2025: Week 5 09 Feb 2025 Three Zero 03 Feb 2025 The iPad mini has genuinely changed my life [no hyperbole] 02 Feb 2025 London Marathon 2025: Week 4 28 Jan 2025 Coming AI: Valuing Humans in a world where they have no economic value 28 Jan 2025 Value & Price 27 Jan 2025 The Vegetarian - Han Kang 27 Jan 2025 Wolf Hall - Hilary Mantel 26 Jan 2025 London Marathon 2025: Week 3 19 Jan 2025 Deriving my own proof for Unitary matrices 19 Jan 2025 London Marathon 2025: Week 2 17 Jan 2025 David Copperfield - Charles Dickens 12 Jan 2025 London Marathon 2025: Week 1 09 Jan 2025 NYC & DC '24 08 Jan 2025 Linear Algebra Playground 07 Jan 2025 Configuring an IKEA wireless light switch: Saving you the pain 07 Jan 2025 Goals & Goal-setting 06 Jan 2025 Digital Feeds 05 Jan 2025 London Marathon 2025: Training Begins 01 Jan 2025 Everything I've read in 2025