Euclid's Algorithm
1. GCD and Why Crypto Needs It
gcd(a,b) = largest d dividing both. Crypto uses it to test invertibility (a−1modn exists ⟺ gcd(a,n)=1), to compute the inverse itself, and inside RSA key generation (gcd(e,ϕ(n))=1), primality helpers, and polynomial fields.
Worked intuition: gcd(48,18): divisors of 48: 1,2,3,4,6,8,12,16,24,48; of 18: 1,2,3,6,9,18 → common max 6.
2. Basic Euclid (Worked)
Theorem: gcd(a,b)=gcd(b,amodb). Repeatedly replace until remainder 0:
Only 4 divisions for 3-digit numbers — complexity O(logmin(a,b)) (Lamé: steps ≤5× digits). Second trace (exam-size): gcd(1970,1066): 1970=1×1066+904; 1066=1×904+162; 904=5×162+94; 162=1×94+68; 94=1×68+26; 68=2×26+16; 26=1×16+10; 16=1×10+6; 10=1×6+4; 6=1×4+2; 4=2×2+0 → gcd=2.
3. Extended Euclid: Bézout + Inverses (The Money Section)
Extended Euclid finds x,y with Bézout identity:
If gcd(a,n)=1: ax+ny=1⇒ax≡1(modn) → xmodn=a−1.
Worked inverse: 7−1mod40 (RSA-style: e=7, ϕ=40). Euclid: 40=5×7+5; 7=1×5+2; 5=2×2+1; 2=2×1+0. Back-substitute: 1=5−2×2; 2=7−1×5 → 1=5−2(7−5)=3×5−2×7; 5=40−5×7 → 1=3(40−5×7)−2×7=3×40−17×7. So x=−17≡23(mod40): 7−1=23. Check: 7×23=161=4×40+1 ✓.
Tabular shortcut (fewer errors): track (r,x) rows: (40,0), (7,1); quotient q=5 → row (5,0−5×1=−5); q=1 → (2,1−1(−5)=6); q=2 → (1,−5−2×6=−17) → inverse −17≡23 ✓. Same answer, mechanical.
4. Uses and Pitfalls
- RSA: d=e−1modϕ(n) exactly as above; gcd(e,ϕ)=1 check first — if $
e 1,picknewe$.
- Polynomial version (same algorithm, degree replaces size) gives AES GF(28) inverses (Topic 4).
- Binary/GCDвлека: never "divide" mod n without gcd=1; negative x always reduced +n; Euclid on (0,n) returns n.
Exam Points
- Remainder-chain trace (gcd(252,198)=18) — 2–3 mark opener.
- Extended back-substitution to 7−1=23mod40 with check 7×23=161≡1 — favourite 5-marker.
- Bézout statement + "inverse ⟺ gcd 1" condition.
- Table method as safe alternative; RSA d link.