Number TheoryTopic #33 of 40

Fermat's Little Theorem

Fermat's theorem, Euler's theorem, and applications to cryptography.

Fermat's Little Theorem

If pp is prime and aa is not divisible by pp, then: ap11(modp)a^{p-1} \equiv 1 \pmod{p}

Equivalently, for any integer aa: apa(modp)a^p \equiv a \pmod{p}

Examples

  • 261(mod7)2^6 \equiv 1 \pmod{7} (since 7 is prime, gcd(2,7)=1\gcd(2,7)=1)
  • 3101(mod11)3^{10} \equiv 1 \pmod{11}
  • 541(mod5)5^4 \equiv 1 \pmod{5}? No! gcd(5,5)1\gcd(5,5) \neq 1

Proof Sketch

Consider the set {a,2a,3a,,(p1)a}(modp)\{a, 2a, 3a, \ldots, (p-1)a\} \pmod{p}.

These are all distinct and non-zero (since gcd(a,p)=1\gcd(a,p) = 1).

So they're a permutation of {1,2,,p1}\{1, 2, \ldots, p-1\}.

a2a3a(p1)a123(p1)(modp)a \cdot 2a \cdot 3a \cdots (p-1)a \equiv 1 \cdot 2 \cdot 3 \cdots (p-1) \pmod{p} ap1(p1)!(p1)!(modp)a^{p-1} (p-1)! \equiv (p-1)! \pmod{p} ap11(modp)a^{p-1} \equiv 1 \pmod{p}

(We can divide by (p1)!(p-1)! since gcd((p1)!,p)=1\gcd((p-1)!, p) = 1.)

Applications

Computing Modular Inverses

If pp is prime and gcd(a,p)=1\gcd(a,p) = 1: a1ap2(modp)a^{-1} \equiv a^{p-2} \pmod{p}

Reducing Large Exponents

ak(modp)a^k \pmod{p} where kk is huge: akakmod(p1)(modp)a^k \equiv a^{k \mod (p-1)} \pmod{p}

Example

Compute 3100(mod7)3^{100} \pmod{7}: 100=6×16+4100 = 6 \times 16 + 4 3100=(36)163411681814(mod7)3^{100} = (3^6)^{16} \cdot 3^4 \equiv 1^{16} \cdot 81 \equiv 81 \equiv 4 \pmod{7}

Euler's Theorem (Generalization)

For any positive integer nn and integer aa with gcd(a,n)=1\gcd(a, n) = 1: aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n}

where ϕ(n)\phi(n) is Euler's totient function.

Euler's Totient Function

ϕ(n)\phi(n) = count of integers from 1 to nn coprime to nn.

Computing ϕ(n)\phi(n)

For prime pp: ϕ(p)=p1\phi(p) = p - 1

For prime power pkp^k: ϕ(pk)=pkpk1=pk1(p1)\phi(p^k) = p^k - p^{k-1} = p^{k-1}(p - 1)

Multiplicative property: If gcd(m,n)=1\gcd(m, n) = 1: ϕ(mn)=ϕ(m)ϕ(n)\phi(mn) = \phi(m)\phi(n)

General formula: For n=p1a1p2a2pkakn = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}: ϕ(n)=npn(11p)=n(11p1)(11p2)(11pk)\phi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right) = n \left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right) \cdots \left(1 - \frac{1}{p_k}\right)

Examples

ϕ(10)=10(112)(115)=101245=4\phi(10) = 10 \cdot \left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{5}\right) = 10 \cdot \frac{1}{2} \cdot \frac{4}{5} = 4 The numbers coprime to 10: 1, 3, 7, 9.

ϕ(12)=12(112)(113)=121223=4\phi(12) = 12 \cdot \left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{3}\right) = 12 \cdot \frac{1}{2} \cdot \frac{2}{3} = 4

Properties of ϕ(n)\phi(n)

dnϕ(d)=n\sum_{d \mid n} \phi(d) = n

The divisors of nn partition {1,2,,n}\{1, 2, \ldots, n\} by gcd with nn.

Fermat as Special Case of Euler

When n=pn = p (prime): ϕ(p)=p1\phi(p) = p - 1

So Euler's theorem becomes Fermat's theorem.

Carmichael Numbers

A Carmichael number is a composite nn such that: an11(modn)a^{n-1} \equiv 1 \pmod{n} for all aa coprime to nn.

Examples

561, 1105, 1729, ...

These "fool" the Fermat primality test.

Detection

Carmichael numbers are square-free with at least 3 prime factors.

Order of an Element

The order of aa modulo nn is the smallest positive kk such that: ak1(modn)a^k \equiv 1 \pmod{n}

Written ordn(a)\text{ord}_n(a).

Properties

  • ordn(a)\text{ord}_n(a) divides ϕ(n)\phi(n)
  • am1(modn)a^m \equiv 1 \pmod{n} iff ordn(a)m\text{ord}_n(a) \mid m

Primitive Roots

gg is a primitive root mod nn if ordn(g)=ϕ(n)\text{ord}_n(g) = \phi(n).

Primitive roots exist for: n=1,2,4,pk,2pkn = 1, 2, 4, p^k, 2p^k (where pp is odd prime).