liveBook · Manning

Fermat’s Little Theorem

As explained in Hacking Cryptography

Fermat's little theorem states that for a prime p, every integer a not divisible by p satisfies a^(p−1) ≡ 1 modulo p.

For a prime p, every integer a satisfies a^p ≡ a (mod p); when p does not divide a, this becomes a^(p−1) ≡ 1 (mod p). section 8.2.2

The theorem supports probabilistic primality testing: test randomly chosen values of a against the congruence. One failure proves that p is composite; repeated successes increase confidence but cannot guarantee primality, because some composite Carmichael numbers also pass these checks. section 8.2.2

A practical testing workflow follows:

  • Choose random values of a for the candidate p. section 8.2.2
  • Reject p immediately when any value violates the congruence. section 8.2.2
  • Treat repeated successes as probabilistic evidence rather than proof. section 8.2.2
Equation 8.4
\[\begin{aligned} \label{equation:fermats-little-theorem-2} \begin{aligned} a^{p-1} - 1 &= 0 \pmod{p} \\ a^{p-1} &= 1 \pmod{p} \end{aligned} \end{aligned}\]

Use the equivalent a^(p−1) ≡ 1 (mod p) form when checking bases that are not divisible by p. section 8.2.2

How Hacking Cryptography covers it

How deep it goes
Derives equivalent congruences and explains randomized testing, but stops short of a guaranteed primality method.
Examples
Uses equations 8.3 and 8.4, random candidate bases, and Carmichael numbers as a failure case.
What you can do after
Apply it via a probabilistic primality test that rejects candidates after one failed congruence.
How it connects
Connects modular arithmetic, primality testing, composite numbers, Carmichael numbers, and asymmetric cryptography.

Also explained in

Compare how 2 books explain Fermat’s Little Theorem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.