Fermat's Little Theorem
Statement
If is a prime number, then for any integer :
Equivalently, if :
Proof via Necklace Counting
Consider the set of all strings of length over an alphabet of symbols. There are such strings total. Exactly of them are constant (all symbols the same). The remaining strings can be grouped into equivalence classes under cyclic rotation, each of size exactly (since is primeInfinitude of PrimesEuclid's classic proof that there are infinitely many prime numbersRead more → and the string is non-constant). Therefore , giving us .
Applications
- Primality testing: Fermat's test checks whether
- RSA cryptography: The RSA algorithm relies on Euler's generalization of this theorem
- Modular inverses: When , the inverse of modulo is
Connections
This theorem builds on the Fundamental Theorem of ArithmeticFundamental Theorem of ArithmeticEvery integer greater than 1 has a unique prime factorizationRead more → and is a key ingredient in Quadratic ReciprocityQuadratic ReciprocityRelationship between solvability of quadratic congruences for two odd primesRead more →.
Lean4 Proof
/-- Fermat's little theorem: in `ZMod p`, every element raised to the
p-th power is itself. Mathlib bundles the necklace-counting argument
(and the polynomial-identity argument) into `ZMod.pow_card`. -/
theorem fermat_little (p : ℕ) [Fact p.Prime] (a : ZMod p) : a ^ p = a :=
ZMod.pow_card a
/-- Equivalent form for nonzero residues: `a^(p-1) = 1`. -/
theorem fermat_little_nonzero (p : ℕ) [Fact p.Prime] {a : ZMod p}
(ha : a ≠ 0) : a ^ (p - 1) = 1 :=
ZMod.pow_card_sub_one_eq_one haReferenced by
- Diffie–HellmanCryptography
- RSA CorrectnessCryptography
- Miller–Rabin Primality TestCryptography
- One-Way FunctionCryptography
- ElGamal EncryptionCryptography
- Fundamental Theorem of ArithmeticNumber Theory
- Order Modulo nNumber Theory
- Sum of Two SquaresNumber Theory
- Bezout's IdentityNumber Theory
- Carmichael NumbersNumber Theory
- Carmichael NumbersNumber Theory
- Euler's Totient FunctionNumber Theory
- Euler's Totient FunctionNumber Theory
- Wilson's TheoremNumber Theory
- Quadratic ResiduesNumber Theory
- Lucas's TheoremNumber Theory
- Quadratic ReciprocityNumber Theory
- Inclusion-Exclusion PrincipleCombinatorics
- Pigeonhole PrincipleCombinatorics
- Finite FieldsField Theory