TExcelTAD
Home › Number Theory & Cryptography › Euler's Totient Function

Euler's Totient Function

How many numbers between 1 and nn are coprime to nn? That count, φ(n)\varphi(n), is the size of the group of units modulo nn. It is the exponent in Euler's theorem and the number RSA's private key is computed from.

Definition

φ(n)=#{ k:1≤k≤n, gcd⁡(k,n)=1 }\varphi(n) = \#\{\, k : 1 \le k \le n,\ \gcd(k, n) = 1 \,\}

For example φ(12)=4\varphi(12) = 4 (the numbers 1, 5, 7, 11), φ(7)=6\varphi(7) = 6, and φ(1)=1\varphi(1) = 1.

Formulas

  • For a prime: φ(p)=p−1\varphi(p) = p - 1.
  • For a prime power: φ(pk)=pk−pk−1=pk−1(p−1)\varphi(p^k) = p^k - p^{k-1} = p^{k-1}(p - 1), because the non-coprime numbers are the pk−1p^{k-1} multiples of pp.
  • Multiplicative: if gcd⁡(m,n)=1\gcd(m, n) = 1 then φ(mn)=φ(m)φ(n)\varphi(mn) = \varphi(m)\varphi(n). This is the Chinese remainder theorem: kk is coprime to mnmn exactly when its residues mod mm and mod nn are both units.

Putting these together, for n=∏piein = \prod p_i^{e_i}:

φ(n)=n∏p∣n(1−1p)\varphi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right)
python · try it yourself
from math import gcd

def phi_count(n):
    return sum(1 for k in range(1, n + 1) if gcd(k, n) == 1)

def phi(n):
    result, m, p = n, n, 2
    while p * p <= m:
        if m % p == 0:
            while m % p == 0:
                m //= p
            result -= result // p
        p += 1
    if m > 1:
        result -= result // m
    return result

print([phi(n) for n in range(1, 21)])
print(all(phi(n) == phi_count(n) for n in range(1, 500)))
print(phi(2**10), phi(3**5), phi(1001), phi(101 * 113))
[1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6, 18, 8]
True
512 162 720 11200

RSA's φ(n)\varphi(n)

For an RSA modulus n=pqn = pq, φ(n)=(p−1)(q−1)\varphi(n) = (p - 1)(q - 1). Knowing φ(n)\varphi(n) is equivalent to knowing the factorization: from nn and φ(n)\varphi(n) you get p+q=n−φ(n)+1p + q = n - \varphi(n) + 1, and then p,qp, q are the roots of x2−(p+q)x+nx^2 - (p + q)x + n.

python · try it yourself
from math import isqrt

n, ph = 11413, 11200                 # n = 101 * 113
s = n - ph + 1                       # p + q
disc = s * s - 4 * n
p = (s - isqrt(disc)) // 2
print("p + q =", s, " p, q =", p, n // p)
p + q = 214  p, q = 101 113

So computing φ(n)\varphi(n) for an RSA modulus is as hard as factoring it.

Gauss's sum

A pretty identity: ∑d∣nφ(d)=n\sum_{d \mid n} \varphi(d) = n. Group the fractions 1n,2n,…,nn\frac{1}{n}, \frac{2}{n}, \ldots, \frac{n}{n} by their reduced denominator dd; exactly φ(d)\varphi(d) of them have denominator dd.

python · try it yourself
from sympy import totient, divisors

for n in (12, 30, 97, 360):
    print(n, sum(totient(d) for d in divisors(n)))
12 12
30 30
97 97
360 360

Carmichael's function

RSA standards actually use λ(n)=lcm⁡(p−1,q−1)\lambda(n) = \operatorname{lcm}(p - 1, q - 1), the Carmichael function: the smallest exponent with aλ(n)≡1a^{\lambda(n)} \equiv 1 for all units aa. It divides φ(n)\varphi(n) and gives a smaller private exponent dd that works just as well.

python · try it yourself
from math import lcm

p, q = 101, 113
print("phi =", (p - 1) * (q - 1), " lambda =", lcm(p - 1, q - 1))
e = 17
print("d via phi:", pow(e, -1, (p - 1) * (q - 1)), " d via lambda:", pow(e, -1, lcm(p - 1, q - 1)))
phi = 11200  lambda = 2800
d via phi: 3953  d via lambda: 1153

In software

  • PKCS #1 and FIPS 186 define the RSA private exponent with λ(n)\lambda(n); OpenSSL's older keys used φ(n)\varphi(n). Both decrypt correctly.
  • The totient also counts the generators of a cyclic group of order nn, which matters when choosing Diffie-Hellman groups (Primitive Roots).

Exercises

Exercise 1: By hand

Compute φ(72)\varphi(72) and φ(97)\varphi(97).

Answer

24 and 96.

Solution

72=23⋅3272 = 2^3 \cdot 3^2, so φ(72)=72⋅12⋅23=24\varphi(72) = 72 \cdot \frac{1}{2} \cdot \frac{2}{3} = 24. 97 is prime, so φ(97)=96\varphi(97) = 96.

Exercise 2: Factor from φ\varphi

An RSA modulus is n=3233n = 3233 with φ(n)=3120\varphi(n) = 3120. Find pp and qq.

Answer

p=53p = 53, q=61q = 61.

Solution

p+q=3233−3120+1=114p + q = 3233 - 3120 + 1 = 114. Solve x2−114x+3233=0x^2 - 114x + 3233 = 0: the discriminant is 12996−12932=6412996 - 12932 = 64, so x=(114±8)/2=61x = (114 \pm 8)/2 = 61 or 5353.

Exercise 3: Which nn have φ(n)=12\varphi(n) = 12? (code)

Find all nn with φ(n)=12\varphi(n) = 12. (Hint: φ(n)≥n/2\varphi(n) \ge \sqrt{n/2}, so n≤288n \le 288 suffices.)

Answer

13, 21, 26, 28, 36, 42.

Solution
python · try it yourself
from sympy import totient
print([n for n in range(1, 289) if totient(n) == 12])
[13, 21, 26, 28, 36, 42]

Test yourself

φ(p)\varphi(p) for a prime pp equals…

  1. pp
  2. p−1p - 1
  3. p+1p + 1
  4. 1

For an RSA modulus n=pqn = pq, φ(n)\varphi(n) is…

  1. pq−1pq - 1
  2. (p−1)(q−1)(p - 1)(q - 1)
  3. p+qp + q
  4. lcm⁡(p,q)\operatorname{lcm}(p, q)

Knowing n=pqn = pq and φ(n)\varphi(n) lets you…

  1. nothing more
  2. factor nn by solving a quadratic
  3. only find pp
  4. compute discrete logs