The last lesson covered addition, subtraction, and multiplication inside a modulus, and quietly skipped the fourth operation. That gap gets fixed here — division turns out to be the one operation that doesn’t survive the trip into modular arithmetic unchanged, and patching it back in is what this lesson is about.
Why division breaks
In ordinary arithmetic, dividing by a number is identical to multiplying by its reciprocal: and land on the same . The trouble is that a modulus only has whole numbers to work with — there’s no slot in for a fraction like to occupy.
What stands in for the reciprocal instead is called a modular inverse: a whole number that, multiplied by the original, lands on exactly .
is the modular inverse of — the number that undoes multiplying by , the same job a reciprocal does everywhere else.
Finding one by brute force
For small numbers, there’s nothing cleverer to do than try every candidate. Take : run through and check which one lands on .
Why not every number has one
Brute force doesn’t always end in success. Try the same search for :
No candidate works, and the reason is a single number: the greatest common divisor of and .
An inverse exists only when and share no common factor besides . , which is exactly why the search above never found one. It’s also why ZK systems build their modulus out of a prime number: every value from to automatically shares no factor with a prime , so every one of them is guaranteed an inverse.
Fermat’s little theorem: a shortcut for huge primes
Brute force works fine for , and falls apart completely once the modulus is a 254-bit prime — no computer is checking that many candidates one at a time. Fermat’s little theorem is the shortcut that replaces the search with a single formula.
Holds whenever is prime and isn’t a multiple of — true for every number from to , no exceptions.
Three lines turn that statement into a formula for the inverse itself, just by splitting the exponent apart and lining the result up against the definition from the start of this lesson:
Raise to the power of , reduce by , and whatever comes out is the inverse — no search required.
Checking the formula against the brute-force answer
The whole point of a formula is that it has to agree with the slow method it’s replacing. Plug , back in:
Matches the brute-force answer from a few sections back exactly — , both times.
Why Circom secretly computes this
There’s no division gate inside a finite field. A Circom circuit can’t compute directly, because — same as everywhere else in this lesson — that operation doesn’t exist there. What it computes instead is times ‘s modular inverse.
Every constraint in a circuit that looks like division is, underneath, running exactly the formula from this lesson. The next lesson gives this whole structure — a modulus, closed under addition, subtraction, multiplication, and now division too — its proper name: a finite field.