TExcelTAD
Home › Number Theory & Cryptography › Euler's Theorem

Euler's Theorem

Euler extended Fermat's theorem from primes to every modulus: raising a unit to the power φ(n)\varphi(n) gives 1. With it, RSA's correctness is a two-line proof.

Statement

If gcd⁡(a,n)=1\gcd(a, n) = 1, then

aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod{n}

For a prime n=pn = p this is Fermat's little theorem, since φ(p)=p−1\varphi(p) = p - 1.

Proof. The same rearrangement as for Fermat: let r1,…,rφ(n)r_1, \ldots, r_{\varphi(n)} be the units modulo nn. Multiplying each by the unit aa permutes them, so aφ(n)∏ri≡∏ria^{\varphi(n)} \prod r_i \equiv \prod r_i, and the product of units can be cancelled.

A group-theory view: the units form a group of order φ(n)\varphi(n), and by Lagrange's theorem the order of every element divides the order of the group.

python · try it yourself
from math import gcd
from sympy import totient

for n in (10, 12, 21, 100):
    ph = int(totient(n))
    units = [a for a in range(1, n) if gcd(a, n) == 1]
    print(n, ph, all(pow(a, ph, n) == 1 for a in units))
print("non-unit 2 mod 10:", [pow(2, k, 10) for k in range(1, 9)])
10 4 True
12 4 True
21 12 True
100 40 True
non-unit 2 mod 10: [2, 4, 8, 6, 2, 4, 8, 6]

For a non-unit the theorem fails: powers of 2 modulo 10 cycle through 2,4,8,62, 4, 8, 6 and never reach 1.

Reducing exponents

For a unit aa, ak≡ak mod φ(n)(modn)a^k \equiv a^{k \bmod \varphi(n)} \pmod{n}. Example: the last two digits of 720267^{2026}. φ(100)=40\varphi(100) = 40 and 2026=50⋅40+262026 = 50 \cdot 40 + 26, so we need 726 mod 1007^{26} \bmod 100. With 74=2401≡1(mod100)7^4 = 2401 \equiv 1 \pmod{100} it is even quicker: 726=(74)6⋅72≡497^{26} = (7^4)^6 \cdot 7^2 \equiv 49.

Towers of exponents reduce recursively: abc mod na^{b^c} \bmod n uses bc mod φ(n)b^c \bmod \varphi(n).

python · try it yourself
print(pow(7, 2026, 100), pow(7, 2026 % 40, 100))
# 3^(3^(3^3)) mod 1000: reduce the exponent modulo phi(1000) = 400
e = pow(3, 3**3, 400)
print(pow(3, e, 1000))
49 49
387

Why RSA works

RSA picks n=pqn = pq, a public exponent ee and dd with ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)}, so ed=1+kφ(n)ed = 1 + k\varphi(n). For a message mm coprime to nn:

(me)d=m1+kφ(n)=m⋅(mφ(n))k≡m⋅1k=m(modn)(m^e)^d = m^{1 + k\varphi(n)} = m \cdot \left(m^{\varphi(n)}\right)^k \equiv m \cdot 1^k = m \pmod{n}

The rare messages sharing a factor with nn also decrypt correctly; check modulo pp and qq separately (Fermat for each, then CRT).

python · try it yourself
p, q, e = 101, 113, 17
n, ph = p * q, (p - 1) * (q - 1)
d = pow(e, -1, ph)
bad = [m for m in range(n) if pow(pow(m, e, n), d, n) != m]
print("messages that fail:", bad)
print("m = 202 (a multiple of p):", pow(pow(202, e, n), d, n))
messages that fail: []
m = 202 (a multiple of p): 202

Every one of the 11413 possible messages decrypts correctly, including multiples of pp or qq.

In software

  • Exponent reduction modulo φ(n)\varphi(n) or λ(n)\lambda(n) is how RSA private keys are derived, and how CRT decryption shortens the exponents to d mod (p−1)d \bmod (p - 1) and d mod (q−1)d \bmod (q - 1).
  • Verifiable delay functions and time-lock puzzles rely on the opposite: without knowing φ(n)\varphi(n) you cannot shortcut 22T mod n2^{2^T} \bmod n, so you must do all TT squarings.

Exercises

Exercise 1: Last two digits

Find the last two digits of 320263^{2026}.

Answer
Solution

Euler gives 340≡1(mod100)3^{40} \equiv 1 \pmod{100}, but the order of 3 is even smaller: 310=59049≡493^{10} = 59049 \equiv 49 and 492=2401≡149^2 = 2401 \equiv 1, so 320≡13^{20} \equiv 1. Since 2026=20⋅101+62026 = 20 \cdot 101 + 6, 32026≡36=729≡29(mod100)3^{2026} \equiv 3^6 = 729 \equiv 29 \pmod{100}. Check with pow(3, 2026, 100).

Exercise 2: Inverse via Euler

Show that a−1≡aφ(n)−1(modn)a^{-1} \equiv a^{\varphi(n) - 1} \pmod{n} for a unit aa, and use it to invert 3 modulo 10.

Answer

3−1≡33=27≡73^{-1} \equiv 3^3 = 27 \equiv 7.

Solution

a⋅aφ(n)−1=aφ(n)≡1a \cdot a^{\varphi(n) - 1} = a^{\varphi(n)} \equiv 1. With φ(10)=4\varphi(10) = 4: 33=27≡73^3 = 27 \equiv 7, and 3⋅7=21≡13 \cdot 7 = 21 \equiv 1.

Exercise 3: A power tower (code)

Compute 22222 mod 10002^{2^{2^{2^2}}} \bmod 1000, that is 265536 mod 10002^{65536} \bmod 1000, both directly and by reducing the exponent. Why must you be careful, given that 2 is not a unit modulo 1000?

Answer

736 both ways, but the shortcut needs care.

Solution
python · try it yourself
direct = pow(2, 65536, 1000)
# 1000 = 8 * 125: modulo 8 the power is 0; modulo 125 Euler applies (phi(125) = 100)
m125 = pow(2, 65536 % 100, 125)
x = next(x for x in range(1000) if x % 8 == 0 and x % 125 == m125)
print(direct, x)
736 736

Euler's theorem needs gcd⁡(a,n)=1\gcd(a, n) = 1. Split 1000=8⋅1251000 = 8 \cdot 125: modulo 8 any high power of 2 is 0, modulo 125 the exponent reduces modulo 100, and the CRT combines them.

Test yourself

Euler's theorem requires…

  1. nn prime
  2. gcd⁡(a,n)=1\gcd(a, n) = 1
  3. a<na < n
  4. aa prime

Modulo 15, a8a^{8} equals 1 for every unit aa because…

  1. 15−7=815 - 7 = 8
  2. φ(15)=8\varphi(15) = 8
  3. 8 is a power of 2
  4. it is a coincidence

RSA decryption (me)d≡m(m^e)^d \equiv m works because…

  1. e=de = d
  2. ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)} and Euler's theorem
  3. nn is prime
  4. m<em < e