← Back to section · Foundations of Cryptography

Extended Euclid and Modular Inverses: The Universal Wrench of Cryptographic Engineering

Computing modular inverses is the most frequent low-level operation in key generation and signature verification. This article explains the Euclidean algorithm, its extension and Bézout's identity, and compares inversion via Fermat's little theorem in terms of applicability and engineering trade-offs.

Published 22 November 2026 · 5 min read · PQ Frontier

Extended Euclid and Modular Inverses: The Universal Wrench of Cryptographic Engineering
AI-generated site artwork
Video explainer · Number theory: primes and modular arithmetic

The modular inverse is the least glamorous and hardest-working component in cryptography: RSA key generation derives dd from ee with one, every elliptic-curve signature inverts in the scalar field, and countless normalisations, point operations and key derivations inside protocols depend on it. The question is: given aa and a modulus nn, how do you efficiently find a−1a^{-1} with a⋅a−1≡1(modn)a \cdot a^{-1} \equiv 1 \pmod n? The answer is a natural extension of one of the oldest algorithms in mathematics — the Euclidean algorithm, whose history reaches back to Euclid’s Elements in the third century BCE.

From the gcd to Bézout’s identity

The Euclidean algorithm computes the greatest common divisor from one simple observation: gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b); iterate the remainder operation, and when the remainder hits zero the other number is the gcd. Its efficiency is remarkable: Lamé’s theorem bounds the number of steps at roughly five times the number of decimal digits of the smaller input, with the worst case attained by consecutive Fibonacci numbers — even so, a 2048-bit input needs only on the order of ten thousand elementary steps. Bézout’s identity says the gcd can always be written as an integer combination of the two numbers: there exist integers x,yx, y with ax+by=gcd⁡(a,b)ax + by = \gcd(a, b). The extended Euclidean algorithm computes exactly this: while running the division chain it tracks the quotients and maintains the combination coefficients, so that at the end it has both the gcd and a pair of Bézout coefficients.

How the inverse drops out of Bézout’s identity

When gcd⁡(a,n)=1\gcd(a, n) = 1, Bézout’s identity gives ax+ny=1ax + ny = 1. Read this equation modulo nn: the nyny term vanishes, leaving ax≡1(modn)ax \equiv 1 \pmod n — the coefficient xx (taken modulo nn as a positive value) is precisely the inverse of aa. The argument is seamless and doubles as an existence criterion: an inverse exists if and only if gcd⁡(a,n)=1\gcd(a, n) = 1; if the gcd exceeds 1, the algorithm still runs to completion and tells you plainly that no inverse exists instead of returning a wrong answer. A small example: the inverse of 17 modulo 3120 (the parameters from the classic RSA textbook example) comes from 17×(−367)+3120×2=117 \times (-367) + 3120 \times 2 = 1, so the inverse is −367 mod 3120=2753-367 \bmod 3120 = 2753 — exactly the private exponent dd of that famous key pair. The final step of RSA key generation is one such computation.

The binary version: built for hardware and constant time

The textbook extended Euclidean algorithm performs a full division with remainder at every step, and division is among the slowest arithmetic operations on a processor. The binary gcd algorithm, proposed by Stein in the 1960s, achieves the same with subtraction and shifts: if both numbers are even, halve both; if one is even, halve it; if both are odd, subtract the smaller from the larger. It too has an extended version maintaining Bézout coefficients, and on hardware good only at addition, subtraction and shifting it is markedly faster — the actual choice inside many low-level bignum libraries. In modern cryptographic implementations the focus has moved one step further: in both the classical and binary versions, the branch pattern and step count depend on the actual input values, and when the input is a secret scalar those differences leak through execution time. Inversion code that handles private-key material therefore either switches to the fixed-length Fermat route described below, or uses specially designed constant-time variants of extended Euclid that fix the operation sequence at every step. An algorithm from before the common era is still being polished, line by line, to meet the demands of the side-channel era — the vitality of a foundational algorithm lies in exactly this capacity to be reinvented.

Beyond inversion, extended Euclid’s output also appears in the CRT parameters of RSA private keys: the coefficient qinv=q−1 mod pq_{inv} = q^{-1} \bmod p is precisely the inverse needed when stitching the two branch results together, produced in passing by the same extended Euclidean run at key generation and stored in the private-key file. From a key’s birth to every signature it makes, this wrench is on the scene.

One more trick that earns compound interest from inversion deserves its own mention: Montgomery’s batch inversion. Elliptic-curve work often needs the inverses of a whole batch of values at once (converting a batch of points to affine coordinates, for instance), and inverting them one by one is expensive. The trick computes the prefix products of all the values, inverts only the total product once, then walks backwards, extracting one inverse per multiplication — the cost of nn inverses collapses to “one inversion plus about 3n3n multiplications”. Batch signature verification and batch point operations lean on it to turn inversion from a bottleneck into a rounding error. The example underlines a broader point: the value of a foundational algorithm like extended Euclid lies not only in the speed of a single call but in its role as a composable primitive that higher-level techniques can invoke and amortise again and again.

From a representational angle, extended Euclid has an equivalent matrix face: each step of the division chain can be written as a small 2×22 \times 2 matrix, and multiplying all steps together, one row of the final matrix yields the gcd and a pair of Bézout coefficients at once. The viewpoint is more than aesthetic — it turns the correctness proof into an exercise in associativity of matrix multiplication, and it opens the door to accelerated versions for very large inputs (the divide-and-conquer “half-gcd” algorithms that process only half the digits at a time). When inputs grow so large that even plain repeated division feels slow, industrial bignum libraries rely on this route to keep inversion near linearithmic time.

The other road: inversion via Fermat’s little theorem

If the modulus is a prime pp, Fermat’s little theorem offers another route: ap−2≡a−1(modp)a^{p-2} \equiv a^{-1} \pmod p, a single modular exponentiation. This route is common in elliptic-curve implementations, because modular exponentiation is a ready-made, heavily optimised component in finite-field libraries, and a fixed-length computation is easier to make constant-time, avoiding the timing side channel that extended Euclid’s uneven branches and step counts can leak. Its limitations are equally clear: it works only for prime moduli (or, via Euler’s theorem, when φ(n)\varphi(n) is known), and it costs a full exponentiation, whereas extended Euclid accepts any modulus, runs faster, and reports nonexistence for free. The practical division of labour is roughly this: key generation, parameter computation and other non-secret paths use extended Euclid; inversion over secret data in finite fields tends to use the fixed-length Fermat route, hardened to constant time. Knowing where each road comes from and what it costs is what lets an implementer use this universal wrench in the right place — small as it is, it is the most frequently turned screw in the entire edifice of public-key cryptography.

Tags: number theoryEuclidean algorithmmodular inverseBézout identity