Topic 5 found modular inverses by brute force, and promised a shortcut for huge primes in Fermat’s little theorem. This lesson delivers the other half of that promise — a fast way to find the gcd and the inverse for any modulus, not only a prime one — and then builds two more tools, the Chinese Remainder Theorem and Euler’s theorem, that lean on exactly that machinery.
Euclid’s algorithm finds the gcd
Checking every number up to for a shared factor works, but it’s slow. Euclid’s algorithm finds the greatest common divisor in a handful of steps, no matter how big the numbers are, by repeatedly replacing a pair with a smaller one that has the same gcd.
Divide by , keep the remainder, and repeat with the smaller pair. The last nonzero remainder is the gcd. Each step at least halves the numbers, so the whole chain finishes in roughly steps — fast enough for the 254-bit numbers this series actually uses.
Does 7 have an inverse mod 26?
The same division chain answers the question Topic 5 could only answer by brute force: does an inverse exist at all? Run it on and keep every line — they’re needed again in a moment.
The last nonzero remainder is , so and has an inverse mod — exactly the condition from Topic 5, reached in three steps instead of twenty-five candidates.
Working backwards to find the inverse
Running Euclid forward finds that an inverse exists. Running the same three lines backwards — substituting each remainder back into the equation before it — finds the inverse itself, with no searching at all.
Back-substitution finds integers and satisfying this equation — called Bézout’s identity. When , that equation reads , which is exactly : is the inverse, read straight off the last line.
When an inverse does not exist
Not every pair cooperates. Check against every from to , and the pattern Euclid’s algorithm already predicts shows up directly.
Same condition as Topic 5, now checkable in three division steps instead of a brute-force search of size . With a prime modulus this is automatic — every number from to shares no factor with — which is exactly why ZK systems build their field out of a prime.
Two clues about one number
The Chinese Remainder Theorem answers a different question: given two separate clues about a number — its remainder mod and its remainder mod — is there a number that satisfies both, and how many are there?
There’s exactly one answer in every run of consecutive numbers, because and share no factor. That’s the condition the Chinese Remainder Theorem needs: the moduli have to be pairwise coprime.
Building x without searching
Searching works for small moduli, but the CRT formula builds the same answer directly, using the inverses this lesson just made fast.
, and . Because is a multiple of every modulus except , the term keeps remainder under and vanishes to under every other modulus — so the sum satisfies every congruence at once.
φ(n) counts the numbers coprime to n
Euler’s theorem — the next and last tool in this lesson — needs one more idea first: how many numbers from to share no factor with at all.
The first formula holds for any prime — every nonzero number below it qualifies. The second holds whenever and are distinct primes, which is exactly the structure behind an RSA modulus .
Euler’s theorem
With in hand, Euler’s theorem generalizes Fermat’s little theorem from a prime modulus to any modulus — as long as the base and the modulus share no factor.
Set to a prime : , and the statement collapses to — Fermat’s little theorem from Topic 5, now visibly just the prime special case of this more general rule.
Faster inverses, bigger numbers, one big idea
Three tools, each building on the last: Euclid’s algorithm for a fast gcd and inverse at any modulus, the Chinese Remainder Theorem for combining several small moduli into one big answer, and Euler’s theorem for the exponent rule that holds everywhere Fermat’s little theorem used to need a prime.
Every one of these is really a statement about the structure hiding underneath modular arithmetic — the sets of numbers that behave consistently under multiplication. Making that structure explicit, with groups, subgroups, cosets, and roots of unity, is exactly where the next lessons go.
- Euclid’s algorithm finds the gcd in roughly steps by repeatedly replacing a pair of numbers with a smaller pair sharing the same gcd.
- Running Euclid’s steps backwards — the extended Euclidean algorithm — finds the modular inverse directly, with no brute-force search.
- An inverse exists exactly when , which is automatic for every nonzero value whenever is prime.
- The Chinese Remainder Theorem combines two congruences with pairwise-coprime moduli into one answer, using a direct formula built from these same fast inverses.
- Euler’s theorem () generalizes Fermat’s little theorem to any modulus — Fermat’s is just its prime special case.