Although no polynomial-time classical algorithm is known for computing the prime factorization of an integer, primality testing can be done in polynomial time.
The central idea behind randomized primality testing is to search for a certificate of compositeness that can be verified efficiently.
In this note, we first review several number-theoretic primitives.
Then we discuss Fermat's Test and explain why it fails on Carmichael numbers.
Finally, we introduce the Miller--Rabin Test, which strengthens Fermat's Test by searching for nontrivial square roots of unity.
Throughout this note, for a positive integer n, we write
[n]={1,2,…,n}.
Number Theory Primitives
This section reviews several basic number-theoretic primitives that will be used later, including Bézout's Identity, Fermat's Little Theorem, and Lagrange's Theorem.
Bézout's Identity
Theorem.
Given two positive integers n and m, if
gcd(n,m)=k,
then there exist integers a,b∈Z such that
an+bm=k.
Proof.
Consider the set
E={an+bm:a,b∈Z}.
Let
c=min{e∈E:e>0}.
The value c is well-defined because n,m>0, and hence n∈E and m∈E.
In particular, we have
c≤nandc≤m.
We first show that c divides both n and m.
By the division algorithm, there exist integers q and r such that
n=qc+r,0≤r<c.
Since n∈E and c∈E, we also have
r=n−qc∈E.
If r>0, then r is a positive element of E smaller than c, contradicting the definition of c.
Therefore, r=0, and hence c∣n.
The same argument shows that c∣m.
Thus, c is a common divisor of n and m.
Moreover, every common divisor of n and m divides every element of E, and in particular divides c.
Therefore, c is the greatest common divisor of n and m.
Hence,
c=gcd(n,m)=k.
Since c∈E, there exist integers a,b∈Z such that
an+bm=k.
■
Fermat's Little Theorem
Theorem.
If n is prime, then for every x∈[n−1],
xn−1≡1(modn).
Proof.
Consider the sequence
x,2x,3x,…,(n−1)x.
We claim that these numbers are pairwise distinct modulo n.
Indeed, suppose that
ix≡jx(modn)
for some i,j∈[n−1].
Then
(i−j)x≡0(modn).
Since n is prime and x≡0(modn), it follows that
i−j≡0(modn).
But i,j∈[n−1], so this implies i=j.
Therefore, multiplication by x permutes the nonzero residues modulo n.
Hence,
x⋅2x⋅3x⋯(n−1)x≡1⋅2⋅3⋯(n−1)(modn).
Equivalently,
xn−1(n−1)!≡(n−1)!(modn).
Since n is prime, none of 1,2,…,n−1 is divisible by n.
Thus, (n−1)! has a multiplicative inverse modulo n.
Canceling (n−1)! from both sides gives
xn−1≡1(modn).
■
Lagrange's Theorem
Theorem.
If Z is a finite group and S is a subgroup of Z, then ∣S∣ divides ∣Z∣.
Equivalently,
∣Z∣≡0(mod∣S∣).
Proof.
For each z∈Z, define the left coset of S generated by z as
zS={zs:s∈S}.
We first show that every coset has the same size as S.
Fix z∈Z.
Consider the map
ϕz:S→zS,ϕz(s)=zs.
This map is injective: if zx=zy, then multiplying both sides on the left by z−1 gives x=y.
It is also surjective by the definition of zS.
Therefore, ϕz is a bijection, and hence
∣zS∣=∣S∣.
Now consider the collection of all left cosets of S in Z.
We claim that distinct cosets are disjoint.
Suppose that two cosets zS and wS have a common element.
Then there exist x,y∈S such that
zx=wy.
Rearranging gives
z=wyx−1.
Since y,x−1∈S, we have yx−1∈S.
Thus, z∈wS.
We now show that zS=wS.
For any s∈S,
zs=w(yx−1)s.
Since (yx−1)s∈S, it follows that zs∈wS.
Hence,
zS⊆wS.
By symmetry,
wS⊆zS.
Therefore,
zS=wS.
Thus, the distinct left cosets of S form a partition of Z.
If there are t distinct cosets, then each has size ∣S∣, and therefore
∣Z∣=t∣S∣.
Hence, ∣S∣ divides ∣Z∣.
■
Fermat's Test
We begin with Fermat's Test, a simple randomized primality test.
The test has one-sided error: if it outputs NO, then n is definitely composite; if it outputs YES, then n is only probably prime.
The test can have false positives, but it works well for many composite numbers.
Algorithm: Fermat's Test
Input: An integer n and a repetition parameter T.
Output:NO if n is composite; YES if n is probably prime.
If n≤1, output NO.
If n=2, output YES.
Repeat the following steps for t=1,2,…,T:
Choose x∈[n−1] uniformly at random.
If gcd(x,n)=1, output NO.
Check whetherxn−1≡1(modn).If not, output NO.
If all T iterations pass, output YES.
We now analyze the algorithm.
For an integer n, define
Zn∗={x∈[n−1]:gcd(x,n)=1}.
That is, Zn∗ is the set of nonzero residues modulo n that are coprime to n.
The set Zn∗ forms a group under multiplication modulo n:
The identity element 1 belongs to Zn∗.
If x∈Zn∗, then gcd(x,n)=1.
By Bézout's Identity, there exist integers a,b∈Z such thatax+bn=1.Henceax≡1(modn).Therefore, amodn is the multiplicative inverse of x modulo n.
If x,y∈Zn∗, then gcd(xy,n)=1, so xy∈Zn∗.
Multiplication modulo n is associative.
Now consider the set of elements that satisfy Fermat's congruence:
S={x∈Zn∗:xn−1≡1(modn)}.
The set S is a subgroup of Zn∗.
Indeed:
The identity element 1 belongs to S.
If x,y∈S, then(xy)n−1≡xn−1yn−1≡1(modn).Hence xy∈S.
If x∈S, thenxn−1≡1(modn).Taking inverses on both sides gives(x−1)n−1≡1(modn).Hence x−1∈S.
Therefore, S is a subgroup of Zn∗.
Since Zn∗ is finite, Lagrange's Theorem implies that
∣S∣∣∣Zn∗∣.
Suppose there exists some x∈Zn∗ such that
xn−1≡1(modn).
Then S is a proper subgroup of Zn∗.
By Lagrange's Theorem, this implies
∣S∣≤2∣Zn∗∣.
Thus, at least half of the elements in Zn∗ fail Fermat's congruence.
Moreover, in the algorithm above, every x∈/Zn∗ is detected immediately by the gcd check.
Therefore, if S is a proper subgroup of Zn∗, then one iteration of Fermat's Test outputs NO with probability at least 1/2.
However, a systematic source of error remains.
There exist composite numbers n such that every element coprime to n satisfies Fermat's congruence:
xn−1≡1(modn)for every x∈Zn∗.
Equivalently, for such numbers,
S=Zn∗.
These composite numbers are called Carmichael numbers.
The smallest example is
561=3⋅11⋅17.
Thus, Fermat's Test can incorrectly output YES on Carmichael numbers.
This motivates the Miller--Rabin Test, whose key idea is to search for a nontrivial square root of 1 modulo n.
Miller--Rabin Test
Fermat's Test can fail on Carmichael numbers.
The Miller--Rabin Test strengthens Fermat's Test by looking not only at whether
xn−1≡1(modn),
but also at the intermediate square roots of 1 that appear during the computation.
The key observation is the following.
Nontrivial Square Roots of Unity Modulo a Prime
Lemma.
If n is prime and
x2≡1(modn),
then
x≡1(modn)orx≡−1(modn).
Here −1(modn) denotes the residue n−1.
Proof.
From
x2≡1(modn),
we have
(x−1)(x+1)≡0(modn).
Since n is prime, it follows that
x−1≡0(modn)orx+1≡0(modn).
Therefore,
x≡1(modn)orx≡−1(modn).
■
Observe that when n>2, the two residues 1 and −1 are distinct modulo n.
Algorithm: Miller--Rabin Test
Input: An integer n and a repetition parameter T.
Output:NO if n is composite; YES if n is probably prime.
If n≤1, output NO.
If n=2, output YES.
If n is even, output NO.
If n is a perfect power, output NO.
Writen−1=2km,where m is odd.
Repeat the following steps for t=1,2,…,T:
Choose x∈[n−1] uniformly at random.
If gcd(x,n)=1, output NO.
Computexm,x2m,x4m,…,x2km(modn).
Ifxm≡1(modn),then this iteration passes.
Otherwise, if there exists i∈{0,1,…,k−1} such thatx2im≡−1(modn),then this iteration passes.
Otherwise, output NO.
If all T iterations pass, output YES.
Let us briefly justify the perfect-power check.
If n is a perfect power, then
n=ab
for some integers a≥2 and b≥2.
Since a≥2, we have
b≤log2n.
Therefore, it suffices to check every exponent
b∈{2,3,…,⌊log2n⌋}
and test whether n1/b is an integer.
This can be done in polynomial time, for example by binary search or integer root computation.
We now analyze the correctness of the test.
First, suppose n is prime.
By Fermat's Little Theorem,
xn−1≡1(modn)
for every x∈[n−1].
Since
n−1=2km,
we have
x2km≡1(modn).
Consider the sequence
xm,x2m,x4m,…,x2km(modn).
Each term is the square of the previous term.
Since the last term is 1, the sequence eventually reaches 1.
If the first term is already 1, then the iteration passes.
Otherwise, let x2jm be the first term in the sequence that equals 1.
Then j≥1, and the previous term satisfies
(x2j−1m)2=x2jm≡1(modn).
Since x2j−1m≡1(modn), the previous lemma implies
x2j−1m≡−1(modn).
Thus, the algorithm never outputs NO when n is prime.
The main point is that when n is composite, a random choice of x is likely to reveal this.
Error Probability of the Miller--Rabin Test
Theorem.
If n is composite, then one iteration of the Miller--Rabin Test outputs YES with probability at most 1/2.
Therefore, after T independent repetitions, the probability of incorrectly outputting YES is at most
2−T.
Proof.
Since the algorithm already rejects even numbers and perfect powers, we may assume that n is odd and is not a perfect power.
Let
n−1=2km,
where m is odd.
We call an element x∈[n−1] a strong liar if one iteration of the Miller--Rabin Test outputs YES when using this value of x.
Equivalently, x is a strong liar if
xm≡1(modn)
or there exists some i∈{0,1,…,k−1} such that
x2im≡−1(modn).
Any strong liar must belong to Zn∗, because otherwise the gcd check would output NO.
We will show that all strong liars are contained in a proper subgroup of Zn∗.
Then the desired probability bound follows from Lagrange's Theorem.
For each
r∈{m,2m,4m,…,2k−1m},
define
Sr={x∈Zn∗:xr≡±1(modn)}.
Choose the largest value of r among
m,2m,4m,…,2k−1m
such that there exists some x∈Zn∗ satisfying
xr≡−1(modn).
Such an r exists because −1∈Zn∗ and, since m is odd,
(−1)m≡−1(modn).
For this choice of r, define
S=Sr.
We claim that S has the following three properties:
Every strong liar belongs to S.
The set S is a subgroup of Zn∗.
The subgroup S is proper, i.e.,S=Zn∗.
Once these three properties are proved, the theorem follows immediately.
Indeed, since S is a proper subgroup of the finite group Zn∗, Lagrange's Theorem implies that
∣S∣≤21∣Zn∗∣.
Since every strong liar belongs to S, we have
∣{strong liars}∣≤∣S∣≤21∣Zn∗∣≤2n−1.
Therefore, when x is chosen uniformly at random from [n−1], one iteration outputs YES with probability at most 1/2.
Repeating the test independently T times, the probability that all iterations output YES is at most
2−T.
It remains to prove the three properties.
Property 1: Every strong liar belongs to S
Let x be a strong liar.
Then either
xm≡1(modn),
or there exists i∈{0,1,…,k−1} such that
x2im≡−1(modn).
If xm≡1(modn), then since r is a multiple of m,
xr≡1(modn),
so x∈S.
Otherwise, suppose
x2im≡−1(modn).
By the maximality of r, we have 2im≤r.
Since both 2im and r are powers of two times m, r/(2im) is a power of two.
Hence
xr=(x2im)r/(2im)≡±1(modn).
Thus x∈S.
Therefore, every strong liar belongs to S.
Property 2: S is a subgroup of Zn∗
We show that S satisfies the subgroup conditions.
First, the identity element belongs to S because
1r≡1(modn).
Second, S is closed under multiplication.
If x,y∈S, then
xr≡±1(modn)andyr≡±1(modn).
Therefore,
(xy)r≡xryr≡±1(modn).
Thus xy∈S.
Third, S is closed under inverses.
If x∈S, then
xr≡±1(modn).
Taking inverses on both sides gives
(x−1)r=(xr)−1≡±1(modn).
Hence x−1∈S.
Therefore, S is a subgroup of Zn∗.
Property 3: S is a proper subgroup of Zn∗
It remains to show that
S=Zn∗.
We will construct an element y such that
y∈/Sandy∈Zn∗.
Since n is composite, odd, and not a perfect power, n has at least two distinct prime factors.
Write
n=p1e1p2e2⋯ptet,
where t≥2.
Let
a=p1e1andb=p2e2⋯ptet.
Then
n=ab,a>1,b>1,gcd(a,b)=1.
Moreover, since n is odd, both a and b are odd, and hence
1≡−1(moda)and1≡−1(modb).
By the choice of r, there exists some x∈Zn∗ such that
xr≡−1(modn).
Equivalently,
xr≡−1(moda)andxr≡−1(modb).
By the Chinese Remainder Theorem, there exists some y∈[0,…,n−1] such that
y≡x(moda)andy≡1(modb).
We first show that y∈/S.
From the definition of y, we have
yr≡xr≡−1(moda),andyr≡1(modb).
Thus yr cannot be congruent to 1 modulo n, because modulo a it is congruent to −1, not 1.
Similarly, yr cannot be congruent to −1 modulo n, because modulo b it is congruent to 1, not −1.
Therefore,