Apex › Cryptography

Dividing mod m (extended Euclid & the modular inverse)

You can’t divide mod m, but you can multiply by an inverse — and the extended Euclidean algorithm computes it. Euclid’s gcd runs dividend = q·divisor + remainder down to the gcd; the extended version carries coefficients with the same quotients so it also returns x, y with a·x + m·y = gcd (Bézout). When gcd(a,m)=1, a·x + m·y = 1 ⇒ a·x ≡ 1, so x is a⁻¹ mod m — exactly how RSA computes d = e⁻¹ mod φ(n). Verified in node: a·x + m·y = gcd for all pairs, and a·a⁻¹ ≡ 1 across 12230 inverses. Invert a number mod m and watch the chain run.

More in Cryptography