Intro to Fermat's Little Theorem

Prologue

Fermat’s Little Theorem is quite often seen in the CTF contests, so let’s dive in to this article to know more about it! Math is powerful!

Required Knowledge

  • ab(modk)k(ab)a\equiv{b}\pmod{k}\Leftrightarrow{k}\mid(a-b)
  • ab(modk)andcd(modk)a+cb+c(modk)a\equiv{b}\pmod{k}\quad\text{and}\quad c\equiv{d}\pmod{k}\Leftrightarrow a+c\equiv b+c\pmod{k}
  • ab(modk)andcd(modk)acbd(modk)a\equiv{b}\pmod{k}\quad\text{and}\quad c\equiv{d}\pmod{k}\Leftrightarrow ac\equiv bd\pmod{k}
    • ab(modk)anbn(modk)a\equiv b\pmod{k}\Leftrightarrow a^n\equiv b^n\pmod{k}
    • ab(modk)ambm(modk)a\equiv b\pmod{k}\Leftrightarrow am\equiv bm\pmod{k}

Statement

If pp is a prime, and aa is an arbitrary integer which is coprime with pp, then the following relationship is satisfied.

ap11(modp)a^{p-1}\equiv1\pmod{p}

This means, if we devided ap1a^{p-1} by pp, the remainder will be 1. Mathematically, if we repeatedly multiply an integer aa, which is coprime to a prime pp, until reaching ap1a^{p-1}, the result will be congruent to 1 modulo pp.

Example

Suppose p=7p=7, and a=3a=3. Since 33 and 77 are coprime, so we can apply the Fermat’s Little Theorem.

371=36=7293^{7-1} =3^{6}=729

Then calculate 729mod7729\mod{7}

729÷7=1041729\div{7}=104\dots{1}

The remainder is 11 so,

361(mod7)3^{6}\equiv1\pmod{7}

Generalization & Application

  • Fast Exponentiation
    • Fermat’s Little Theorem is often used to simplify modular exponention. For large exponents, applying this Theorem can significantly reduce the computational complexity.
    • For example, to compute a1000modpa^{1000}\mod{p}, one can first reduce 10001000 modulo p1p-1, transforming it into a smaller exponent before calculating.
  • RSA Encryption & Decryption
  • Verifying Congruence Relations
    • Fermat’s Little Theorem is also frequently used to verify specific congruence properties in modular arithmetic, making it an essential tool in number theory proofs.

The Generalization of Euler’s Theorem

Fermat’s Little Theorem is a special case of Euler’s Theorem. Euler’s Theorem applies to all integers aa that are coprime with nn, and its form is as follows.

aϕ(n)1(modn)a^{\phi(n)}\equiv1\pmod{n}

Where ϕ(n)\phi(n) is Euler’s function, representing the numbers of positive integers less than nn that are coprime to nn. When n=pn=p is a prime number, ϕ(p)=p1\phi(p)=p-1, and Eular’s Theorem simplifies to Fermat’s Little Theorem.