Euler's Totient Function
How many numbers between 1 and are coprime to ? That count, , is the size of the group of units modulo . It is the exponent in Euler's theorem and the number RSA's private key is computed from.
Definition
For example (the numbers 1, 5, 7, 11), , and .
Formulas
- For a prime: .
- For a prime power: , because the non-coprime numbers are the multiples of .
- Multiplicative: if then . This is the Chinese remainder theorem: is coprime to exactly when its residues mod and mod are both units.
Putting these together, for :
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
For an RSA modulus , . Knowing is equivalent to knowing the factorization: from and you get , and then are the roots of .
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 for an RSA modulus is as hard as factoring it.
Gauss's sum
A pretty identity: . Group the fractions by their reduced denominator ; exactly of them have denominator .
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 , the Carmichael function: the smallest exponent with for all units . It divides and gives a smaller private exponent that works just as well.
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 ; OpenSSL's older keys used . Both decrypt correctly.
- The totient also counts the generators of a cyclic group of order , which matters when choosing Diffie-Hellman groups (Primitive Roots).
Exercises
Exercise 1: By hand
Compute and .
Answer
24 and 96.
Solution
, so . 97 is prime, so .
Exercise 2: Factor from
An RSA modulus is with . Find and .
Answer
, .
Solution
. Solve : the discriminant is , so or .
Exercise 3: Which have ? (code)
Find all with . (Hint: , so suffices.)
Answer
13, 21, 26, 28, 36, 42.
Solution
from sympy import totient
print([n for n in range(1, 289) if totient(n) == 12])[13, 21, 26, 28, 36, 42]
Test yourself
for a prime equals…
- 1
For an RSA modulus , is…
Knowing and lets you…
- nothing more
- factor by solving a quadratic
- only find
- compute discrete logs