TExcelTAD
Home › Number Theory & Cryptography › Fermat's Little Theorem

Fermat's Little Theorem

Raise any number to the power p−1p - 1 modulo a prime pp and you get 1. This one fact gives fast modular inverses, the first primality tests, Pollard's p−1p - 1 factoring method and, generalized by Euler, the correctness of RSA.

Statement

If pp is prime and p∤ap \nmid a, then

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

Equivalently, ap≡a(modp)a^p \equiv a \pmod{p} for every integer aa (this form also covers p∣ap \mid a).

python · try it yourself
p = 13
print([pow(a, p - 1, p) for a in range(1, p)])
print([pow(a, p, p) == a % p for a in range(-3, 20)].count(False))
print("composite 15:", [pow(a, 14, 15) for a in range(1, 15)])
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
0
composite 15: [1, 4, 9, 1, 10, 6, 4, 4, 6, 10, 1, 9, 4, 1]

For the composite 15 the pattern breaks: most bases do not give 1. That is the idea of the Fermat primality test.

Proof by rearrangement

Multiply each of 1,2,…,p−11, 2, \ldots, p - 1 by aa modulo pp. The results are again 1,…,p−11, \ldots, p - 1 in a different order: none is 0 (as p∤ap \nmid a, p∤kp \nmid k) and no two coincide (if ak≡ajak \equiv aj then k≡jk \equiv j, since aa is invertible). So the products agree:

∏k=1p−1(ak)≡∏k=1p−1k(modp)⟹ap−1(p−1)!≡(p−1)!(modp)\prod_{k=1}^{p-1} (ak) \equiv \prod_{k=1}^{p-1} k \pmod{p} \quad\Longrightarrow\quad a^{p-1}(p - 1)! \equiv (p - 1)! \pmod{p}

(p−1)!(p - 1)! is invertible modulo pp, so cancel it: ap−1≡1a^{p-1} \equiv 1.

python · try it yourself
p, a = 11, 7
print(sorted(a * k % p for k in range(1, p)))      # a permutation of 1..10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Proof by counting necklaces

A second proof shows why ap−aa^p - a is divisible by pp: there are apa^p strings of length pp over an alphabet of aa letters. Remove the aa constant strings. The remaining ap−aa^p - a fall into groups of exactly pp rotations each (because pp is prime, no non-constant string equals a nontrivial rotation of itself). So p∣ap−ap \mid a^p - a.

Uses

Reducing exponents. Modulo a prime pp, exponents can be reduced modulo p−1p - 1: ak≡ak mod (p−1)a^k \equiv a^{k \bmod (p-1)} when p∤ap \nmid a. Example: 31000 mod 73^{1000} \bmod 7. Since 36≡13^6 \equiv 1 and 1000=6⋅166+41000 = 6 \cdot 166 + 4, it equals 34=81≡43^4 = 81 \equiv 4.

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

Wilson's theorem, a cousin: pp is prime iff (p−1)!≡−1(modp)(p - 1)! \equiv -1 \pmod{p}. It is a correct but useless primality test, since computing the factorial takes pp steps.

python · try it yourself
from math import factorial

print(pow(3, 1000, 7), pow(3, 1000 % 6, 7))
print([(n, factorial(n - 1) % n) for n in range(2, 14)])
4 4
[(2, 1), (3, 2), (4, 2), (5, 4), (6, 0), (7, 6), (8, 0), (9, 0), (10, 0), (11, 10), (12, 0), (13, 12)]

In software

  • Elliptic-curve libraries invert field elements as ap−2a^{p-2} because the exponent is public and fixed, so the computation is naturally constant-time.
  • Fermat's theorem underlies the fast probable-prime checks that every key generator runs (Miller-Rabin).

Exercises

Exercise 1: Reduce the exponent

Compute 2100 mod 132^{100} \bmod 13 by hand.

Answer
Solution

212≡1(mod13)2^{12} \equiv 1 \pmod{13} and 100=12⋅8+4100 = 12 \cdot 8 + 4, so 2100≡24=16≡32^{100} \equiv 2^4 = 16 \equiv 3.

Exercise 2: A divisibility fact

Show that n7−nn^7 - n is divisible by 7 for every integer nn, and also by 42.

Answer

Fermat for p=7p = 7, 3 and 2.

Solution

n7≡n(mod7)n^7 \equiv n \pmod 7 is Fermat. Also n7−n=n(n6−1)n^7 - n = n(n^6 - 1); modulo 3, n3≡nn^3 \equiv n gives n7=n3⋅n3⋅n≡n3≡nn^7 = n^3 \cdot n^3 \cdot n \equiv n^3 \equiv n; modulo 2, n7≡nn^7 \equiv n since parity is preserved. So 2, 3 and 7 all divide it, hence 42.

Exercise 3: Check numerically (code)

Verify n7−n≡0(mod42)n^7 - n \equiv 0 \pmod{42} for nn from −100-100 to 100100, and find the largest mm dividing n7−nn^7 - n for all those nn.

Answer

m=42m = 42.

Solution
python · try it yourself
from math import gcd
from functools import reduce
vals = [n**7 - n for n in range(-100, 101)]
print(all(v % 42 == 0 for v in vals), reduce(gcd, vals))
True 42

Test yourself

For a prime pp not dividing aa, Fermat's little theorem gives…

  1. ap≡1a^p \equiv 1
  2. ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}
  3. ap+1≡aa^{p+1} \equiv a
  4. pa≡1p^a \equiv 1

Modulo the prime 11, a57a^{57} equals (for 11∤a11 \nmid a)…

  1. a7a^7
  2. a57−11a^{57 - 11}
  3. a5a^{5}
  4. aa

In the rearrangement proof, multiplying 1,…,p−11, \ldots, p - 1 by aa modulo pp…

  1. gives all zeros
  2. permutes them
  3. doubles them
  4. sorts them