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
  • Core Idea
  • Code
Export as PDF
  1. Primitives
  2. Euclidean Algorithm

Binary Euclidean Algorithm

Objective

The Binary Euclidean Algorithm, also known as Stein’s algorithm, is an efficient method for computing the greatest common divisor (GCD) of two integers. Unlike the standard Euclidean algorithm, which relies on division, the binary version replaces division with more efficient bitwise operations such as shifts and subtractions. This makes it highly suitable for implementation in both hardware and low-level software.

Core Idea

gcd⁡(A,B)={A if B=0B if A=02×gcd⁡(A2,B2) if A≡0(mod2)∧ B≡0(mod2)gcd⁡(A2,B) if A≡0(mod2)∧ B≡1(mod2)gcd⁡(A,B2) if A≡1(mod2)∧ B≡0(mod2)gcd⁡(∣A−B∣2,min⁡(A,B)) if A≡1(mod2)∧ B≡1(mod2)\gcd(A, B) = \begin{cases} A & \text{ if } B = 0 \\ B & \text{ if } A = 0 \\ 2 \times \gcd \left(\frac{A}{2}, \frac{B}{2} \right) & \text{ if } A \equiv 0 \pmod 2 \land \ B \equiv 0 \pmod 2\\ \gcd \left(\frac{A}{2}, B \right) & \text{ if } A \equiv 0 \pmod 2 \land \ B \equiv 1 \pmod 2 \\ \gcd \left(A, \frac{B}{2} \right) & \text{ if } A \equiv 1 \pmod 2 \land \ B \equiv 0 \pmod 2 \\ \gcd \left(\frac{|A - B|}{2}, \min(A, B) \right) & \text{ if } A \equiv 1 \pmod 2 \land \ B \equiv 1 \pmod 2 \\ \end{cases}gcd(A,B)=⎩⎨⎧​AB2×gcd(2A​,2B​)gcd(2A​,B)gcd(A,2B​)gcd(2∣A−B∣​,min(A,B))​ if B=0 if A=0 if A≡0(mod2)∧ B≡0(mod2) if A≡0(mod2)∧ B≡1(mod2) if A≡1(mod2)∧ B≡0(mod2) if A≡1(mod2)∧ B≡1(mod2)​

gcd⁡(A,B)\gcd(A,B)gcd(A,B) is transformed according to the following rules:

  1. If B=0B = 0B=0, then gcd⁡(A,B)=A\gcd(A, B) = Agcd(A,B)=A

  2. If A=0A = 0A=0, then gcd⁡(A,B)=B\gcd(A, B) = Bgcd(A,B)=B

  3. If both AAA and BBB are even, then gcd⁡(A,B)=2×gcd⁡(A2,B2)\gcd(A, B) = 2 \times \gcd\left(\frac{A}{2}, \frac{B}{2}\right)gcd(A,B)=2×gcd(2A​,2B​) because 2 is a common factor

  4. If only AAA is even, then gcd⁡(A,B)=gcd⁡(A2,B)\gcd(A, B) = \gcd\left(\frac{A}{2}, B\right)gcd(A,B)=gcd(2A​,B), because BBB does not have a factor of 2

  5. If only BBB is even, then gcd⁡(A,B)=gcd⁡(A,B2)\gcd(A, B) = \gcd\left(A, \frac{B}{2}\right)gcd(A,B)=gcd(A,2B​), because AAA does not have a factor of 2

  6. If both AAA and BBB are odd, then gcd⁡(A,B)=gcd⁡(∣A−B∣2,min⁡(A,B))\gcd(A, B) = \gcd\left(\frac{|A - B|}{2}, \min(A, B)\right)gcd(A,B)=gcd(2∣A−B∣​,min(A,B)) because gcd⁡(A,B)=gcd⁡(B,A)\gcd(A, B) = \gcd(B, A)gcd(A,B)=gcd(B,A), and if A>BA > BA>B, then gcd⁡(A,B)=gcd⁡(A−B,B)\gcd(A, B) = \gcd(A - B, B)gcd(A,B)=gcd(A−B,B). Since A−BA−BA−B is even, we divide it by 2.

Code

def binary_gcd(a, b):
    # Base case
    if a == 0: return b
    if b == 0: return a

    # Case: both even
    if a % 2 == 0 and b % 2 == 0:
        return 2 * binary_gcd(a // 2, b // 2)
    # Case: a even, b odd
    elif a % 2 == 0:
        return binary_gcd(a // 2, b)
    # Case: a odd, b even
    elif b % 2 == 0:
        return binary_gcd(a, b // 2)
    # Case: both odd
    elif a >= b:
        return binary_gcd((a - b) // 2, b)
    else:
        return binary_gcd((b - a) // 2, a)
PreviousExtended Euclidean AlgorithmNextExtended Binary Euclidean Algorithm

Last updated 1 month ago

Written by from

A41
ryan Kim