Fermat's theorem shrinks exponents for a prime modulus. Euler found the version for any modulus: replace $p - 1$ by $\varphi(n)$, the number of units. It is the theorem that makes RSA work.
Euler's theorem. If $\gcd(a, n) = 1$, then $$a^{\varphi(n)} \equiv 1 \pmod{n}.$$ For a prime, $\varphi(p) = p - 1$ and this is Fermat's theorem. As before, exponents may be reduced mod $\varphi(n)$: if $e \equiv r \pmod{\varphi(n)}$, then $a^{e} \equiv a^{r}$.
Why it works
Worked example: Last digit
- We want $7^{222} \bmod 10$. $\gcd(7, 10) = 1$ and $\varphi(10) = 4$.
- $222 = 4 \cdot 55 + 2$, so $7^{222} \equiv 7^{2} = 49 \equiv 9$.
Answer: $9$.
Worked example: Last two digits
- Work mod 100. $\varphi(100) = \varphi(4)\varphi(25) = 2 \cdot 20 = 40$, and $\gcd(3, 100) = 1$.
- $1000 = 40 \cdot 25$, so $3^{1000} = (3^{40})^{25} \equiv 1 \pmod{100}$.
Answer: The last two digits are $01$.
Worked example: An exponent tower
- The exponent is $3^{3} = 27$. Reduce it mod $\varphi(10) = 4$: $27 \equiv 3$.
- $3^{27} \equiv 3^{3} = 27 \equiv 7 \pmod{10}$.
Answer: $7$.
Worked example: Inverses from Euler
- $5 \cdot 5^{\varphi(12) - 1} = 5^{\varphi(12)} \equiv 1$, so $5^{-1} \equiv 5^{\varphi(12) - 1} = 5^{3}$. In general $a^{-1} \equiv a^{\varphi(n) - 1}$ for a unit $a$.
- $5^{3} = 125 = 12 \cdot 10 + 5$, so $5^{-1} \equiv 5$. Check: $5 \cdot 5 = 25 \equiv 1 \pmod{12}$.
Answer: $5$.
Common mistake
This theorem is the engine of RSA. If $ed \equiv 1 \pmod{\varphi(n)}$, then $ed = 1 + k\varphi(n)$, and for every unit $m$, $(m^{e})^{d} = m \cdot (m^{\varphi(n)})^{k} \equiv m$. Raising to the power $e$ scrambles $m$; raising to the power $d$ unscrambles it.
Key idea