Extended Euclidean Algorithm, Euler's Theorem & Cryptological Applications
Personal Reference1. The Euclidean Algorithm — GCD Computation
The Greatest Common Divisor (GCD) of two integers a and b is the largest integer that divides both without remainder. Euclid's algorithm (c. 300 BC) computes it in O(log min(a,b)) steps using the key identity:
Core Identity gcd(a, b) = gcd(b, a mod b) gcd(a, 0) = a ← base caseWorked Example — gcd(252, 105)
| Step | a | b | a = q·b + r | r (next b) |
|---|---|---|---|---|
| 1 | 252 | 105 | 252 = 2·105 + 42 | 42 |
| 2 | 105 | 42 | 105 = 2·42 + 21 | 21 |
| 3 | 42 | 21 | 42 = 2·21 + 0 | 0 → done |
# Python
from math import gcd
print(gcd(252, 105)) # → 212. The Extended Euclidean Algorithm
The Extended Euclidean Algorithm not only finds gcd(a, b) but also computes the Bézout coefficients s and t such that:
When gcd(a, b) = 1 (coprime), this gives us s·a ≡ 1 (mod b) — meaning s is the modular inverse of a modulo b. This is the foundation of RSA.
Algorithm — Back-Substitution Method
Run the standard Euclidean algorithm forward, then back-substitute the remainders to express the GCD as a linear combination of the originals.
Worked Example — Extended gcd(35, 15)
Step 1 — Forward pass (Euclidean divisions):
| Step | Equation | Remainder |
|---|---|---|
| 1 | 35 = 2·15 + 5 | 5 |
| 2 | 15 = 3·5 + 0 | 0 → gcd = 5 |
Step 2 — Back-substitute:
5 = 35 − 2·15 ∴ s = 1, t = −2 Check: 1·35 + (−2)·15 = 35 − 30 = 5 ✓Worked Example — Modular Inverse: gcd(17, 60)
Find s such that 17·s ≡ 1 (mod 60) — i.e., the modular inverse of 17 mod 60.
Forward pass:
| Step | Equation |
|---|---|
| 1 | 60 = 3·17 + 9 |
| 2 | 17 = 1·9 + 8 |
| 3 | 9 = 1·8 + 1 |
| 4 | 8 = 8·1 + 0 → gcd = 1 |
Back-substitution:
1 = 9 − 1·8 1 = 9 − 1·(17 − 1·9) = 2·9 − 1·17 1 = 2·(60 − 3·17) − 1·17 = 2·60 − 7·17 ∴ s = −7 ≡ 53 (mod 60) Check: 17 × 53 = 901 = 15·60 + 1 ✓ 17⁻¹ ≡ 53 (mod 60) — the modular inverse.Iterative Algorithm (no back-substitution needed)
def extended_gcd(a, b):
old_r, r = a, b
old_s, s = 1, 0
old_t, t = 0, 1
while r != 0:
q = old_r // r
old_r, r = r, old_r - q * r
old_s, s = s, old_s - q * s
old_t, t = t, old_t - q * t
# old_r = gcd, old_s = s, old_t = t
return old_r, old_s, old_t
gcd, s, t = extended_gcd(17, 60)
print(f"gcd={gcd} s={s} t={t}") # gcd=1 s=-7 t=2
print(f"inverse = {s % 60}") # 533. Euler's Totient Function φ(n)
Euler's totient function φ(n) counts integers from 1 to n that are coprime to n (i.e., gcd(k, n) = 1).
Key Formulas φ(p) = p − 1 ← p prime φ(p·q) = (p−1)(q−1) ← p, q distinct primes φ(pᵏ) = pᵏ − pᵏ⁻¹ = pᵏ⁻¹(p−1) φ(mn) = φ(m)·φ(n) ← if gcd(m,n)=1| n | Coprimes to n | φ(n) |
|---|---|---|
| 7 | 1,2,3,4,5,6 | 6 |
| 8 | 1,3,5,7 | 4 |
| 12 | 1,5,7,11 | 4 |
| 15 | 1,2,4,7,8,11,13,14 | 8 |
| pq (RSA) | (p−1)(q−1) values | (p−1)(q−1) |
def totient(n):
count = 0
for k in range(1, n + 1):
if gcd(k, n) == 1:
count += 1
return count
# Efficient version using prime factorization
def totient_fast(n):
result = n
p = 2
while p * p <= n:
if n % p == 0:
while n % p == 0:
n //= p
result -= result // p
p += 1
if n > 1:
result -= result // n
return result4. Euler's Theorem
If gcd(a, n) = 1, then aφ(n) ≡ 1 (mod n)
This is one of the most powerful results in number theory. It says that raising a to the power of φ(n) always lands on 1 in modular arithmetic — as long as a and n share no common factor.
Special Case — Fermat's Little Theorem
When n = p is prime, φ(p) = p−1, giving Fermat's Little Theorem:
Fermat's Little Theorem ap−1 ≡ 1 (mod p) when p is prime, gcd(a,p)=1 Equivalently: ap ≡ a (mod p) for all aVerified Example — a=3, n=7
φ(7) = 6 (7 is prime) 3¹ = 3 3² = 9 ≡ 2 (mod 7) 3³ = 27 ≡ 6 (mod 7) 3⁴ = 81 ≡ 4 (mod 7) 3⁵ = 243 ≡ 5 (mod 7) 3⁶ = 729 ≡ 1 (mod 7) ✓ ← Euler's theorem confirmedConsequence — Modular Exponentiation Shortcut
Because aφ(n) ≡ 1, exponents can be reduced modulo φ(n):
# Fast modular exponentiation (built into Python)
pow(base, exp, mod) # Python's built-in uses square-and-multiply
# Example: 3^100 mod 7
# φ(7) = 6, 100 mod 6 = 4
# So 3^100 ≡ 3^4 ≡ 81 ≡ 4 (mod 7)
print(pow(3, 100, 7)) # → 4
print(pow(3, 4, 7)) # → 4 ✓5. Cryptological Applications
5.1 RSA Key Generation
RSA is built entirely on these two theorems. The security relies on the difficulty of factoring large n = p·q.
| Step | Operation | Math Used |
|---|---|---|
| 1 | Choose two large primes p, q | Primality testing |
| 2 | n = p · q | Modular arithmetic |
| 3 | φ(n) = (p−1)(q−1) | Euler's Totient |
| 4 | Choose e: gcd(e, φ(n)) = 1 | GCD / Extended Euclidean |
| 5 | d = e⁻¹ mod φ(n) | Extended Euclidean Algorithm |
| 6 | Public key: (e, n) — Private key: (d, n) |
Minimal RSA Example (small numbers)
from math import gcd
def extended_gcd(a, b):
old_r, r = a, b
old_s, s = 1, 0
while r:
q = old_r // r
old_r, r = r, old_r - q * r
old_s, s = s, old_s - q * s
return old_r, old_s
# Key generation
p, q = 61, 53
n = p * q # 3233
phi_n = (p - 1) * (q - 1) # 3120
e = 17 # public exponent, gcd(17, 3120) = 1
_, d = extended_gcd(e, phi_n)
d = d % phi_n # private exponent = 2753
print(f"n={n} φ(n)={phi_n} e={e} d={d}")
# Encrypt M=65
M = 65
C = pow(M, e, n) # 2790
print(f"Ciphertext: {C}")
# Decrypt
M2 = pow(C, d, n) # 65
print(f"Decrypted: {M2}") # → 65 ✓5.2 Diffie-Hellman Key Exchange
Fermat's Little Theorem ensures that discrete exponentiation in ℤp is a one-way function when p is a large prime and g is a primitive root.
DH Protocol Public parameters: prime p, generator g Alice: A = ga mod p (keeps a secret) Bob: B = gb mod p (keeps b secret) Shared secret: K = Ba mod p = Ab mod p = gab mod p Security: knowing g, p, A — finding a requires solving the Discrete Logarithm Problem (computationally infeasible for large p)# Diffie-Hellman demonstration
p = 23 # prime (use 2048-bit in production)
g = 5 # primitive root mod 23
a = 6 # Alice's private key
b = 15 # Bob's private key
A = pow(g, a, p) # Alice's public: 8
B = pow(g, b, p) # Bob's public: 19
Ka = pow(B, a, p) # Alice computes shared secret: 2
Kb = pow(A, b, p) # Bob computes shared secret: 2
print(Ka == Kb) # True ✓ — same shared secret5.3 Modular Inverse in Elliptic Curve Cryptography
In ECC, the Extended Euclidean Algorithm computes modular inverses needed for point addition and scalar multiplication over a finite field 𝔽p.
ECC Point Addition — slope λ λ = (y₂ − y₁) · (x₂ − x₁)⁻¹ mod p (x₂ − x₁)⁻¹ computed via Extended Euclidean Algorithm secp256k1 (Bitcoin/Ethereum): p = 2²⁵⁶ − 2³² − 977def mod_inverse(a, p):
"""Extended Euclidean — modular inverse of a mod p"""
_, s, _ = extended_gcd(a % p, p)
return s % p
# ECC point addition over Fp
def point_add(P, Q, p):
if P is None: return Q
if Q is None: return P
x1, y1 = P
x2, y2 = Q
if x1 == x2 and y1 != y2:
return None # point at infinity
if P == Q:
lam = (3 * x1 * x1) * mod_inverse(2 * y1, p) % p
else:
lam = (y2 - y1) * mod_inverse(x2 - x1, p) % p
x3 = (lam * lam - x1 - x2) % p
y3 = (lam * (x1 - x3) - y1) % p
return (x3, y3)6. Quick Reference — Key Identities
| Identity | Formula | Used In |
|---|---|---|
| GCD recursion | gcd(a,b) = gcd(b, a mod b) | All modular arithmetic |
| Bézout's identity | s·a + t·b = gcd(a,b) | Modular inverse |
| Modular inverse | a·a⁻¹ ≡ 1 (mod n), gcd(a,n)=1 | RSA, ECC, DH |
| Euler's totient (prime) | φ(p) = p − 1 | RSA, Fermat |
| Euler's totient (RSA) | φ(pq) = (p−1)(q−1) | RSA key gen |
| Euler's theorem | aφ(n) ≡ 1 (mod n) | RSA correctness proof |
| Fermat's little theorem | ap−1 ≡ 1 (mod p) | Primality, DH, RSA |
| Exponent reduction | ak ≡ ak mod φ(n) (mod n) | Fast exponentiation |
| RSA private key | d = e⁻¹ mod φ(n) | Extended Euclidean |
| RSA correctness | (Me)d ≡ M (mod n) | Euler's theorem |
7. The Mathematical Chain
From Euclid to RSA — One Unbroken Chain gcd(a,b) ← Euclidean Algorithm (300 BC) ↓ s·a + t·b = 1 ← Extended Euclidean → Bézout (1779) ↓ a⁻¹ mod n (when gcd=1) ← Modular Inverse ↓ φ(n), aφ(n) ≡ 1 ← Euler's Theorem (1763) ↓ d·e ≡ 1 (mod φ(n)) ← RSA Private Key (Rivest, Shamir, Adleman — 1977) ↓ Secure internet communications todayMathematics doesn't become obsolete. Euler's 1763 theorem is inside every TLS handshake, every SSH session, every encrypted email — running billions of times per second on the global internet.Ioannis (John) Alexander Konstas · Miami, FL · · Personal Reference · 2026