LogoLogo
LogoLogo
  • Introduction
    • About Us
    • Notations & Definitions
      • MPC
      • ZK
    • Contribute to this Site!
  • Primitives
    • Multiplication
      • Karatsuba Multiplication
      • Toom-Cook Multiplication
    • NAF (Non-adjacent form)
    • Chinese Remainder Theorem (CRT)
    • Euclidean Algorithm
      • Extended Euclidean Algorithm
      • Binary Euclidean Algorithm
      • Extended Binary Euclidean Algorithm
    • Coding Theory
      • Linear Code
    • Number Theoretic Transform
    • Abstract Algebra
      • Group
        • -Morphisms
        • Batch Inverse
      • Elliptic Curve
        • Weierstrass Curve
          • Coordinate Forms
          • Fast Elliptic Curve Arithmetic and Improved WEIL Pairing Evaluation
        • Edwards Curve
          • Coordinate Forms
          • Twisted Edwards ↔ Short Weierstrass Transformation
        • Batch Inverse for Batch Point Additions
        • Scalar Multiplication
          • Double-and-add
          • GLV Decomposition
        • MSM
          • Pippenger's Algorithm
          • Signed Bucket Index
          • CycloneMSM
          • EdMSM
          • cuZK
        • 2-Chain and 2-Cycle of Elliptic Curves
    • Encryption Scheme
      • ElGamal Encryption
    • Modular Arithmetic
      • Modular Reduction
        • Barrett Reduction
        • Montgomery Reduction
      • Modular Inverse
        • Bernstein-Yang's Inverse
    • Multiset Check
    • Sumcheck
    • Commitment Scheme
      • Fflonk
      • SHPlonk
      • Zeromorph
  • MPC
    • Yao's Garbled Circuits
    • GMW
    • BMR
  • ZK
    • Arithmetization
      • R1CS
      • PLONK
      • AIR
    • Folding
      • LatticeFold
      • Nova
        • Nova over Cycles of Curves
    • Lookup
      • Lasso
      • LogUp-GKR
    • SNARK
      • Groth16
      • HyperPlonk
      • Spartan
        • SPARK
    • STARK
      • Additive NTT
      • Basefold
      • Binius
      • Brakedown
      • CircleSTARK
      • FRI
        • FRI Security Features and Optimizations
      • DEEP FRI
      • STIR
      • WHIR
    • Distributed ZK
      • Ryan's Trick for Distributed Groth16
  • Application
    • zkLogin
    • zkHoldem
    • zkTLS
      • DECO
      • Proxying is enough
  • zkVM
Powered by GitBook
On this page
  • Objective
  • Code (Recursive Version)
  • Proof
  • Code (Non-Recursive Version)
Export as PDF
  1. Primitives
  2. Euclidean Algorithm

Extended Euclidean Algorithm

PreviousEuclidean AlgorithmNextBinary Euclidean Algorithm

Last updated 1 month ago

Objective

The Extended Euclidean Algorithm not only computes gcd⁡(A,B)\gcd(A, B)gcd(A,B), but also finds integers xxx and yyy that satisfy:

Ax+By=gcd⁡(A,B)Ax + By = \gcd(A, B)Ax+By=gcd(A,B)

The pair (x,y)(x, y)(x,y) is also known as the .

The Extended Euclidean Algorithm is used to compute the of an integer, which is essential in many cryptographic algorithms like RSA, ECC, and digital signatures.

Code (Recursive Version)

def extended_gcd(a, b):
    if b == 0:
        return (a, 1, 0)
    else:
        d, x1, y1 = extended_gcd(b, a % b)
        x = y1
        y = x1 - (a // b) * y1
        return (d, x, y)

Proof

Base Case: B=0B = 0B=0

If B=0B = 0B=0, then:

gcd⁡(A,0)=A⇒x=1, y=0\gcd(A, 0) = A \Rightarrow x = 1,\ y = 0gcd(A,0)=A⇒x=1, y=0

which satisfies the identity:

A⋅1+B⋅0A \cdot 1 + B \cdot 0A⋅1+B⋅0

Inductive Hypothesis: Assume the recursive call returns (d,x1,y1)(d, x_1, y_1)(d,x1​,y1​) such that:

d=b⋅x1+(A mod B)⋅y1d = b \cdot x_1 + (A \bmod B) \cdot y_1d=b⋅x1​+(AmodB)⋅y1​

Then:

d=B⋅x1+(A−⌊AB⌋⋅B)⋅y1=A⋅y1+B(x1−⌊AB⌋⋅y1)\begin{align*} d &= B \cdot x_1+ \left(A - \left\lfloor\frac{A}{B}\right\rfloor \cdot B\right) \cdot y1 \\ &= A \cdot y_1 + B\left(x_1 - \left\lfloor\frac{A}{B}\right\rfloor \cdot y1 \right) \end{align*}d​=B⋅x1​+(A−⌊BA​⌋⋅B)⋅y1=A⋅y1​+B(x1​−⌊BA​⌋⋅y1)​

Now define:

x=y1,y=x1−⌊AB⌋⋅y1x = y_1, \quad y = x_1 - \left\lfloor\frac{A}{B}\right\rfloor \cdot y1x=y1​,y=x1​−⌊BA​⌋⋅y1

So:

Ax+By=dAx + By = dAx+By=d

which proves that the identity holds at each step.

Code (Non-Recursive Version)

def extended_gcd(a, b):
    # Initialize: (r, s, t) ← (a, 1, 0), (r', s', t') ← (b, 0, 1)
    r_prev, r = a, b
    s_prev, s = 1, 0
    t_prev, t = 0, 1

    while r != 0:
        q = r_prev // r  # quotient

        # Update (r, s, t)
        r_prev, r = r, r_prev - q * r
        s_prev, s = s, s_prev - q * s
        t_prev, t = t, t_prev - q * t

    # At termination: r_prev = gcd(a, b), s_prev = x, t_prev = y
    return r_prev, s_prev, t_prev

Written by from

Bézout coefficients
modular inverse
A41
ryan Kim