Extended Euclid's Algorithm

Introduction

  The Extended Euclidean Algorithm is a fundamental algorithm in number theory that not only computes the greatest common divisor (GCD) of two integers but also provides coefficients that express the GCD as a linear combination of these integers.

This additional information is invaluable in various mathematical and cryptographic applications.

The Standard Euclidean Algorithm

  Before delving into the extended version, let's briefly review the standard Euclidean Algorithm. It's a method for finding the GCD of two non-negative integers.

The algorithm repeatedly applies the division algorithm to obtain a sequence of remainders until a remainder of is reached. The last non-zero remainder is the GCD.

Example:  

Let's apply the Euclidean algorithm to find the GCD of and :

  • Step 1: 

  • Step 2: 

  • Step 3: 

Since the remainder is , the GCD is the last non-zero remainder, which is .

In summary, the Euclidean algorithm is a simple yet efficient method for finding the GCD of two numbers.

It involves repeated division and remainder calculations until a remainder of is reached. The last non-zero remainder is the GCD.

Extending the Algorithm

  The Extended Euclidean Algorithm builds upon the standard algorithm by introducing two auxiliary sequences of integers. These sequences, often denoted as and , are calculated at each step of the algorithm and are used to express the current remainder as a linear combination of the original two numbers.

Formal Description:

Given two non-negative integers and , the Extended Euclidean Algorithm calculates integers , , and such that:

Our main goal is to see which and value multiplication produces the number pair whose gcd we know. We are actually putting Euclid's operations in reverse order.

Example:  

Given:

  (as calculated using the standard Euclidean algorithm)

Goal:

Find integers and such that  

Steps:

  • Step 1:   So,  

  • Step 2: 

  • Step 3:    So, 

Rearranging to match the desired form:

Final Solution:

Therefore, for the equation , the values of and are:

So, can be expressed as:

The Algorithm

def extended_gcd(a, b):
  assert a >= b and b >= 0 and a + b > 0

  if b == 0:
    d, x, y = a, 1, 0
  else:
    (d, p, q) = extended_gcd(b, a % b)
    x = q
    y = p - q * (a // b)

  assert a % d == 0 and b % d == 0
  assert d == a * x + b * y
  return (d, x, y)

Explanation

  • Function Definition:

    extended_gcd(a, b):  This defines a function that takes two non-negative integers, and , as input and returns a tuple containing the greatest common divisor (GCD) d, and the coefficients and such that .

  • Preconditions:

    assert a >= b and b >= 0 and a + b > 0:  Ensures that the input numbers are valid for the algorithm. a must be greater than or equal to b, both must be non-negative, and their sum must be greater than .

  • Base Case:

    if b == 0: d, x, y = a, 1, 0:  If b is 0, the GCD is , and the coefficients are and , respectively. This is the base case of the recursion.

  • Recursive Case:

    (d, p, q) = extended_gcd(b, a % b):  Recursively calls the function with b and the remainder of divided by . This is the core of the Euclidean algorithm.

    x = q:  The coefficient is assigned the value of from the recursive call.

    y = p - q * (a // b):  The coefficient is calculated using the values of , , and the integer division of by . This step is crucial for expressing the GCD as a linear combination of and .

  • Postconditions:

    assert a % d == 0 and b % d == 0:  Verifies that the calculated GCD divides both and .

    assert d == a _ x + b _ y:  Ensures that the calculated GCD can be expressed as a linear combination of and using the coefficients and .

  • Return Value:

    return (d, x, y):  Returns a tuple containing the GCD and the coefficients and .

Applications

The Extended Euclidean Algorithm has numerous applications, including:

  • Modular Arithmetic:

    • Finding modular inverses:  Given two relatively prime integers and , the algorithm can be used to find an integer such that . This is crucial in cryptography for operations like decryption.

    • Solving linear Diophantine equations:  Equations of the form can be solved efficiently using the algorithm.

  • Cryptography:

    • RSA algorithm:  The Extended Euclidean Algorithm is used to compute the private key in the RSA cryptosystem.

    • Diffie-Hellman key exchange:  It is used to compute the shared secret key.

  • Number theory:

    • Continued fractions:  The algorithm can be used to find the continued fraction representation of a rational number.

Conclusion

  The Extended Euclidean Algorithm is a fundamental algorithm with far-reaching implications in number theory and cryptography.

Its ability to compute the GCD and provide coefficients for a linear combination makes it an essential tool for solving various mathematical problems.

By understanding the principles behind this algorithm, one can gain a deeper appreciation for its applications in modern cryptography and computer science.