Learn / Math / Modular Inverses & Fermat's
05. Modular Inverses & Fermat's
Modular Arithmetic · Lesson 6 of 18 · 4:31
What it means to divide inside a modulus, where that operation quietly breaks, and the one theorem that turns a slow brute-force search into a single exponentiation — the same trick every Circom division constraint runs underneath.

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: 6÷26 \div 2 and 6×126 \times \tfrac{1}{2} land on the same 33. The trouble is that a modulus only has whole numbers to work with — there’s no slot in {0,1,…,n−1}\{0, 1, \ldots, n-1\} for a fraction like 12\tfrac{1}{2} to occupy.

6 ÷ 2 = 3 normal division 6 × ½ = 3 same thing — multiply by the reciprocal 6 × ½ mod 7 ? ½ is not a whole number ✗ this doesn't exist here
Fig. 1 — the same division, rewritten as a reciprocal, breaks the moment it's asked to live inside mod 7.

What stands in for the reciprocal instead is called a modular inverse: a whole number that, multiplied by the original, lands on exactly 11.

Modular inverse

a×x≡1(modn)a \times x \equiv 1 \pmod{n}

xx is the modular inverse of aa — the number that undoes multiplying by aa, 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 3 mod 73 \bmod 7: run x=1x = 1 through x=5x = 5 and check which one lands on 11.

x = 1 3 × 1 = 3 → mod 7 = 3 ✗ not 1 x = 2 3 × 2 = 6 → mod 7 = 6 ✗ not 1 x = 3 3 × 3 = 9 → mod 7 = 2 ✗ not 1 x = 4 3 × 4 = 12 → mod 7 = 5 ✗ not 1 x = 5 3 × 5 = 15 → mod 7 = 1 ✓ found it!
Fig. 2 — checking 3 × x against every x from 1 to 5. x = 5 is the first one that lands on 1.
Verifying the result

3×5=15,15 mod 7=1  ⟹  3−1≡5(mod7)3 \times 5 = 15, \quad 15 \bmod 7 = 1 \implies 3^{-1} \equiv 5 \pmod{7}

Why not every number has one

Brute force doesn’t always end in success. Try the same search for 2 mod 62 \bmod 6:

x = 1 2 × 1 = 2 → mod 6 = 2 ✗ not 1 x = 2 2 × 2 = 4 → mod 6 = 4 ✗ not 1 x = 3 2 × 3 = 6 → mod 6 = 0 ✗ not 1 x = 4 2 × 4 = 8 → mod 6 = 2 ✗ not 1 x = 5 2 × 5 = 10 → mod 6 = 4 ✗ not 1 no x ever gives 1 — 2 has no inverse mod 6, because gcd(2, 6) = 2, not 1
Fig. 3 — every x from 1 to 5, checked against 2 × x ≡ 1 (mod 6). None of them land on 1.

No candidate works, and the reason is a single number: the greatest common divisor of aa and nn.

When an inverse exists

gcd⁡(a,n)=1\gcd(a, n) = 1

An inverse exists only when aa and nn share no common factor besides 11. gcd⁡(2,6)=2\gcd(2, 6) = 2, 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 11 to p−1p-1 automatically shares no factor with a prime pp, so every one of them is guaranteed an inverse.

Fermat’s little theorem: a shortcut for huge primes

Brute force works fine for 6 mod 76 \bmod 7, 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.

Fermat's little theorem

ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}

Holds whenever pp is prime and aa isn’t a multiple of pp — true for every number from 11 to p−1p-1, 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:

start from Fermat ap-1 ≡ 1 (mod p) split the exponent a × ap-2 ≡ 1 (mod p) compare to the definition a × x ≡ 1 (mod p) so: a-1 ≡ ap-2 (mod p)
Fig. 4 — splitting Fermat's exponent in two turns the theorem directly into the inverse formula.
The inverse formula

a−1≡ap−2(modp)a^{-1} \equiv a^{p-2} \pmod{p}

Raise aa to the power of p−2p - 2, reduce by pp, 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 a=3a = 3, p=7p = 7 back in:

Worked example
3−1≡37−2(mod7)≡35(mod7)≡243 mod 7≡5(mod7)\begin{aligned} 3^{-1} &\equiv 3^{7-2} \pmod{7} \\ &\equiv 3^5 \pmod{7} \\ &\equiv 243 \bmod 7 \\ &\equiv 5 \pmod{7} \end{aligned}

Matches the brute-force answer from a few sections back exactly — 55, both times.

Why Circom secretly computes this

There’s no division gate inside a finite field. A Circom circuit can’t compute a÷b(modp)a \div b \pmod{p} directly, because — same as everywhere else in this lesson — that operation doesn’t exist there. What it computes instead is aa times bb‘s modular inverse.

a ÷ b (mod p) this operation does not exist directly a × b-1 (mod p) multiply by b's modular inverse instead
Fig. 5 — every division written in a Circom circuit compiles down to a multiplication by a 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.