
Complexity of Primality Testing 91
have an order modulo n, but not so if n is composite. For example, all
powers of 5 are 5 modulo 10.
In general, a positive integer a has an order modulo n iff a is relatively
prime to n (that is, if gcd(a, n)=1)iffa is invertible modulo n;thatis,
if there exists b,1≤ b ≤ n − 1, such that ab ≡ 1(modn) (Miscellaneous
Exercise 43).
Lemma C.1 (Fermat’s Theorem) Anumberp ≥ 2 is prime if and only if some element
of {1,... ,p− 1} has order p −1 modulo p.
To prove Fermat’s theorem, we need a few basic facts of number theory
and finite fields.
Define
Z
∗
n
def
= {a | 1 ≤ a ≤ n − 1, gcd(a, n)=1}.
For example, Z
∗
10
= {1, 3, 7, 9}. Because Z
∗
n
consists of the invertible el-
ements of Z
n
, it forms a group under multiplication modulo n.Thisis
known as the group of units modulo n.ThesizeofZ
∗
n
is denoted ϕ(n). The
function ϕ is known as the Euler ϕ function.
We can calculate ϕ(n) for any n if we know the prime factorization of n.
For a prime power p
k
, ϕ(p
k
)=p
k−1
(p −1), because a number is relatively
prime to p
k
iff it is not a multiple of p,andtherearep
k−1
− 1 multiples
of p in {1,... ,p
k
−1}. By the Chinese remainder theorem (Theorem B.1),
if m and n are relatively prime, then the map x → (x mod m, x mod n)
is a ring isomorphism Z
mn
→ Z
m
× Z
n
, therefore a group isomorphism
Z
∗
mn
→ Z
∗
m
× Z
∗
n
,thusϕ(mn)=ϕ(m)ϕ(n). Combining these facts, it
follows that if n = p
k
1
1
···p
k
m
m
is the prime factorization of n,then
ϕ(n)=
m
i=1
p
k
i
−1
i
(p
i
− 1). (C.1)
We must show that for prime p, the group Z
∗
p
has an element of order
p−1. Because ϕ(p)=p−1 is the size of the whole group Z
∗
p
, that is the same
as saying that Z
∗
p
is cyclic; that is, {1, 2,... ,p− 1} = {1,a,a
2
,... ,a
p−2
}
(modulo p)forsome1≤ a ≤ p − 1.Suchanelementa is called a cyclic
generator of Z
∗
p
.
It is actually true that the multiplicative group of any finite field is
cyclic. This is no harder to show for arbitrary finite fields than for the
fields Z
p
, so that is what we do.
Recall that the characteristic of a finite field F is the smallest number
p such that 1 + ···+1
p
= 0. This must be a prime, because otherwise F
would contain zero divisors. The prime subfield of F is the smallest subfield
of F and is isomorphic to Z
p
,wherep is the characteristic. The number of