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 nn, we write

[n]={1,2,…,n}. [n] = \{1,2,\ldots,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 nn and mm, if

gcd⁡(n,m)=k, \gcd(n,m) = k,

then there exist integers a,b∈Za,b \in \mathbb{Z} such that

an+bm=k. an + bm = k.

Proof. Consider the set

E={an+bm:a,b∈Z}. E = \{ an + bm : a,b \in \mathbb{Z} \}.

Let

c=min⁡{e∈E:e>0}. c = \min \{ e \in E : e > 0 \}.

The value cc is well-defined because n,m>0n,m > 0, and hence n∈En \in E and m∈Em \in E. In particular, we have

c≤nandc≤m. c \le n \qquad \text{and} \qquad c \le m.

We first show that cc divides both nn and mm. By the division algorithm, there exist integers qq and rr such that

n=qc+r,0≤r<c. n = qc + r, \qquad 0 \le r < c.

Since n∈En \in E and c∈Ec \in E, we also have

r=n−qc∈E. r = n - qc \in E.

If r>0r > 0, then rr is a positive element of EE smaller than cc, contradicting the definition of cc. Therefore, r=0r = 0, and hence c∣nc \mid n. The same argument shows that c∣mc \mid m.

Thus, cc is a common divisor of nn and mm. Moreover, every common divisor of nn and mm divides every element of EE, and in particular divides cc. Therefore, cc is the greatest common divisor of nn and mm. Hence,

c=gcd⁡(n,m)=k. c = \gcd(n,m) = k.

Since c∈Ec \in E, there exist integers a,b∈Za,b \in \mathbb{Z} such that

an+bm=k. an + bm = k.

■\blacksquare

Fermat's Little Theorem

Theorem. If nn is prime, then for every x∈[n−1]x \in [n-1],

xn−1≡1(modn). x^{n-1} \equiv 1 \pmod n.

Proof. Consider the sequence

x,2x,3x,…,(n−1)x. x, 2x, 3x, \ldots, (n-1)x.

We claim that these numbers are pairwise distinct modulo nn.

Indeed, suppose that

ix≡jx(modn) ix \equiv jx \pmod n

for some i,j∈[n−1]i,j \in [n-1]. Then

(i−j)x≡0(modn). (i-j)x \equiv 0 \pmod n.

Since nn is prime and x≢0(modn)x \not\equiv 0 \pmod n, it follows that

i−j≡0(modn). i-j \equiv 0 \pmod n.

But i,j∈[n−1]i,j \in [n-1], so this implies i=ji=j.

Therefore, multiplication by xx permutes the nonzero residues modulo nn. Hence,

x⋅2x⋅3x⋯(n−1)x≡1⋅2⋅3⋯(n−1)(modn). x \cdot 2x \cdot 3x \cdots (n-1)x \equiv 1 \cdot 2 \cdot 3 \cdots (n-1) \pmod n.

Equivalently,

xn−1(n−1)!≡(n−1)!(modn). x^{n-1}(n-1)! \equiv (n-1)! \pmod n.

Since nn is prime, none of 1,2,…,n−11,2,\ldots,n-1 is divisible by nn. Thus, (n−1)!(n-1)! has a multiplicative inverse modulo nn. Canceling (n−1)!(n-1)! from both sides gives

xn−1≡1(modn). x^{n-1} \equiv 1 \pmod n.

■\blacksquare

Lagrange's Theorem

Theorem. If ZZ is a finite group and SS is a subgroup of ZZ, then ∣S∣|S| divides ∣Z∣|Z|. Equivalently,

∣Z∣≡0(mod∣S∣). |Z| \equiv 0 \pmod{|S|}.

Proof. For each z∈Zz \in Z, define the left coset of SS generated by zz as

zS={zs:s∈S}. zS = \{ zs : s \in S \}.

We first show that every coset has the same size as SS. Fix z∈Zz \in Z. Consider the map

ϕz:S→zS,ϕz(s)=zs. \phi_z : S \to zS, \qquad \phi_z(s) = zs.

This map is injective: if zx=zyzx = zy, then multiplying both sides on the left by z−1z^{-1} gives x=yx = y. It is also surjective by the definition of zSzS. Therefore, ϕz\phi_z is a bijection, and hence

∣zS∣=∣S∣. |zS| = |S|.

Now consider the collection of all left cosets of SS in ZZ. We claim that distinct cosets are disjoint. Suppose that two cosets zSzS and wSwS have a common element. Then there exist x,y∈Sx,y \in S such that

zx=wy. zx = wy.

Rearranging gives

z=wyx−1. z = wyx^{-1}.

Since y,x−1∈Sy,x^{-1} \in S, we have yx−1∈Syx^{-1} \in S. Thus, z∈wSz \in wS.

We now show that zS=wSzS = wS. For any s∈Ss \in S,

zs=w(yx−1)s. zs = w(yx^{-1})s.

Since (yx−1)s∈S(yx^{-1})s \in S, it follows that zs∈wSzs \in wS. Hence,

zS⊆wS. zS \subseteq wS.

By symmetry,

wS⊆zS. wS \subseteq zS.

Therefore,

zS=wS. zS = wS.

Thus, the distinct left cosets of SS form a partition of ZZ. If there are tt distinct cosets, then each has size ∣S∣|S|, and therefore

∣Z∣=t∣S∣. |Z| = t |S|.

Hence, ∣S∣|S| divides ∣Z∣|Z|.

■\blacksquare

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 nn is definitely composite; if it outputs YES, then nn 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 nn and a repetition parameter TT.

Output: NO if nn is composite; YES if nn is probably prime.

  1. If n≤1n \le 1, output NO.
  2. If n=2n = 2, output YES.
  3. Repeat the following steps for t=1,2,…,Tt = 1,2,\ldots,T:
    1. Choose x∈[n−1]x \in [n-1] uniformly at random.
    2. If gcd⁡(x,n)≠1\gcd(x,n) \neq 1, output NO.
    3. Check whetherxn−1≡1(modn). x^{n-1} \equiv 1 \pmod n. If not, output NO.
  4. If all TT iterations pass, output YES.

We now analyze the algorithm. For an integer nn, define

Zn∗={x∈[n−1]:gcd⁡(x,n)=1}. \mathbb{Z}_n^* = \{ x \in [n-1] : \gcd(x,n)=1 \}.

That is, Zn∗\mathbb{Z}_n^* is the set of nonzero residues modulo nn that are coprime to nn.

The set Zn∗\mathbb{Z}_n^* forms a group under multiplication modulo nn:

  • The identity element 11 belongs to Zn∗\mathbb{Z}_n^*.
  • If x∈Zn∗x \in \mathbb{Z}_n^*, then gcd⁡(x,n)=1\gcd(x,n)=1. By Bézout's Identity, there exist integers a,b∈Za,b \in \mathbb{Z} such thatax+bn=1. ax + bn = 1. Henceax≡1(modn). ax \equiv 1 \pmod n. Therefore, a mod na \bmod n is the multiplicative inverse of xx modulo nn.
  • If x,y∈Zn∗x,y \in \mathbb{Z}_n^*, then gcd⁡(xy,n)=1\gcd(xy,n)=1, so xy∈Zn∗xy \in \mathbb{Z}_n^*.
  • Multiplication modulo nn is associative.

Now consider the set of elements that satisfy Fermat's congruence:

S={x∈Zn∗:xn−1≡1(modn)}. S = \{ x \in \mathbb{Z}_n^* : x^{n-1} \equiv 1 \pmod n \}.

The set SS is a subgroup of Zn∗\mathbb{Z}_n^*.

Indeed:

  • The identity element 11 belongs to SS.
  • If x,y∈Sx,y \in S, then(xy)n−1≡xn−1yn−1≡1(modn). (xy)^{n-1} \equiv x^{n-1} y^{n-1} \equiv 1 \pmod n. Hence xy∈Sxy \in S.
  • If x∈Sx \in S, thenxn−1≡1(modn). x^{n-1} \equiv 1 \pmod n. Taking inverses on both sides gives(x−1)n−1≡1(modn). (x^{-1})^{n-1} \equiv 1 \pmod n. Hence x−1∈Sx^{-1} \in S.

Therefore, SS is a subgroup of Zn∗\mathbb{Z}_n^*. Since Zn∗\mathbb{Z}_n^* is finite, Lagrange's Theorem implies that

∣S∣∣∣Zn∗∣. |S| \mid |\mathbb{Z}_n^*|.

Suppose there exists some x∈Zn∗x \in \mathbb{Z}_n^* such that

xn−1≢1(modn). x^{n-1} \not\equiv 1 \pmod n.

Then SS is a proper subgroup of Zn∗\mathbb{Z}_n^*. By Lagrange's Theorem, this implies

∣S∣≤∣Zn∗∣2. |S| \le \frac{|\mathbb{Z}_n^*|}{2}.

Thus, at least half of the elements in Zn∗\mathbb{Z}_n^* fail Fermat's congruence.

Moreover, in the algorithm above, every x∉Zn∗x \notin \mathbb{Z}_n^* is detected immediately by the gcd check. Therefore, if SS is a proper subgroup of Zn∗\mathbb{Z}_n^*, then one iteration of Fermat's Test outputs NO with probability at least 1/21/2.

However, a systematic source of error remains. There exist composite numbers nn such that every element coprime to nn satisfies Fermat's congruence:

xn−1≡1(modn)for every x∈Zn∗. x^{n-1} \equiv 1 \pmod n \qquad \text{for every } x \in \mathbb{Z}_n^*.

Equivalently, for such numbers,

S=Zn∗. S = \mathbb{Z}_n^*.

These composite numbers are called Carmichael numbers. The smallest example is

561=3⋅11⋅17. 561 = 3 \cdot 11 \cdot 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 11 modulo nn.

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), x^{n-1} \equiv 1 \pmod n,

but also at the intermediate square roots of 11 that appear during the computation.

The key observation is the following.

Nontrivial Square Roots of Unity Modulo a Prime

Lemma. If nn is prime and

x2≡1(modn), x^2 \equiv 1 \pmod n,

then

x≡1(modn)orx≡−1(modn). x \equiv 1 \pmod n \qquad \text{or} \qquad x \equiv -1 \pmod n.

Here −1(modn)-1 \pmod n denotes the residue n−1n-1.

Proof. From

x2≡1(modn), x^2 \equiv 1 \pmod n,

we have

(x−1)(x+1)≡0(modn). (x-1)(x+1) \equiv 0 \pmod n.

Since nn is prime, it follows that

x−1≡0(modn)orx+1≡0(modn). x-1 \equiv 0 \pmod n \qquad \text{or} \qquad x+1 \equiv 0 \pmod n.

Therefore,

x≡1(modn)orx≡−1(modn). x \equiv 1 \pmod n \qquad \text{or} \qquad x \equiv -1 \pmod n.

■\blacksquare

Observe that when n>2n > 2, the two residues 11 and −1-1 are distinct modulo nn.

Algorithm: Miller--Rabin Test

Input: An integer nn and a repetition parameter TT.

Output: NO if nn is composite; YES if nn is probably prime.

  1. If n≤1n \le 1, output NO.
  2. If n=2n = 2, output YES.
  3. If nn is even, output NO.
  4. If nn is a perfect power, output NO.
  5. Writen−1=2km, n-1 = 2^k m, where mm is odd.
  6. Repeat the following steps for t=1,2,…,Tt = 1,2,\ldots,T:
    1. Choose x∈[n−1]x \in [n-1] uniformly at random.
    2. If gcd⁡(x,n)≠1\gcd(x,n) \neq 1, output NO.
    3. Computexm,x2m,x4m,…,x2km(modn). x^m, x^{2m}, x^{4m}, \ldots, x^{2^k m} \pmod n.
    4. Ifxm≡1(modn), x^m \equiv 1 \pmod n, then this iteration passes.
    5. Otherwise, if there exists i∈{0,1,…,k−1}i \in \{0,1,\ldots,k-1\} such thatx2im≡−1(modn), x^{2^i m} \equiv -1 \pmod n, then this iteration passes.
    6. Otherwise, output NO.
  7. If all TT iterations pass, output YES.

Let us briefly justify the perfect-power check. If nn is a perfect power, then

n=ab n = a^b

for some integers a≥2a \ge 2 and b≥2b \ge 2. Since a≥2a \ge 2, we have

b≤log⁡2n. b \le \log_2 n.

Therefore, it suffices to check every exponent

b∈{2,3,…,⌊log⁡2n⌋} b \in \{2,3,\ldots,\lfloor \log_2 n \rfloor\}

and test whether n1/bn^{1/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 nn is prime. By Fermat's Little Theorem,

xn−1≡1(modn) x^{n-1} \equiv 1 \pmod n

for every x∈[n−1]x \in [n-1]. Since

n−1=2km, n-1 = 2^k m,

we have

x2km≡1(modn). x^{2^k m} \equiv 1 \pmod n.

Consider the sequence

xm,x2m,x4m,…,x2km(modn). x^m, x^{2m}, x^{4m}, \ldots, x^{2^k m} \pmod n.

Each term is the square of the previous term. Since the last term is 11, the sequence eventually reaches 11. If the first term is already 11, then the iteration passes. Otherwise, let x2jmx^{2^j m} be the first term in the sequence that equals 11. Then j≥1j \ge 1, and the previous term satisfies

(x2j−1m)2=x2jm≡1(modn). \left(x^{2^{j-1}m}\right)^2 = x^{2^j m} \equiv 1 \pmod n.

Since x2j−1m≢1(modn)x^{2^{j-1}m} \not\equiv 1 \pmod n, the previous lemma implies

x2j−1m≡−1(modn). x^{2^{j-1}m} \equiv -1 \pmod n.

Thus, the algorithm never outputs NO when nn is prime.

The main point is that when nn is composite, a random choice of xx is likely to reveal this.

Error Probability of the Miller--Rabin Test

Theorem. If nn is composite, then one iteration of the Miller--Rabin Test outputs YES with probability at most 1/21/2. Therefore, after TT independent repetitions, the probability of incorrectly outputting YES is at most

2−T. 2^{-T}.

Proof. Since the algorithm already rejects even numbers and perfect powers, we may assume that nn is odd and is not a perfect power.

Let

n−1=2km, n-1 = 2^k m,

where mm is odd.

We call an element x∈[n−1]x \in [n-1] a strong liar if one iteration of the Miller--Rabin Test outputs YES when using this value of xx. Equivalently, xx is a strong liar if

xm≡1(modn) x^m \equiv 1 \pmod n

or there exists some i∈{0,1,…,k−1}i \in \{0,1,\ldots,k-1\} such that

x2im≡−1(modn). x^{2^i m} \equiv -1 \pmod n.

Any strong liar must belong to Zn∗\mathbb{Z}_n^*, because otherwise the gcd check would output NO.

We will show that all strong liars are contained in a proper subgroup of Zn∗\mathbb{Z}_n^*. Then the desired probability bound follows from Lagrange's Theorem.

For each

r∈{m,2m,4m,…,2k−1m}, r \in \{m,2m,4m,\ldots,2^{k-1}m\},

define

Sr={x∈Zn∗:xr≡±1(modn)}. S_r = \{ x \in \mathbb{Z}_n^* : x^r \equiv \pm 1 \pmod n \}.

Choose the largest value of rr among

m,2m,4m,…,2k−1m m,2m,4m,\ldots,2^{k-1}m

such that there exists some x∈Zn∗x \in \mathbb{Z}_n^* satisfying

xr≡−1(modn). x^r \equiv -1 \pmod n.

Such an rr exists because −1∈Zn∗-1 \in \mathbb{Z}_n^* and, since mm is odd,

(−1)m≡−1(modn). (-1)^m \equiv -1 \pmod n.

For this choice of rr, define

S=Sr. S = S_r.

We claim that SS has the following three properties:

  1. Every strong liar belongs to SS.
  2. The set SS is a subgroup of Zn∗\mathbb{Z}_n^*.
  3. The subgroup SS is proper, i.e.,S≠Zn∗. S \neq \mathbb{Z}_n^*.

Once these three properties are proved, the theorem follows immediately. Indeed, since SS is a proper subgroup of the finite group Zn∗\mathbb{Z}_n^*, Lagrange's Theorem implies that

∣S∣≤12∣Zn∗∣. |S| \le \frac{1}{2} |\mathbb{Z}_n^*|.

Since every strong liar belongs to SS, we have

∣{strong liars}∣≤∣S∣≤12∣Zn∗∣≤n−12. |\{ \text{strong liars} \}| \le |S| \le \frac{1}{2} |\mathbb{Z}_n^*| \le \frac{n-1}{2}.

Therefore, when xx is chosen uniformly at random from [n−1][n-1], one iteration outputs YES with probability at most 1/21/2. Repeating the test independently TT times, the probability that all iterations output YES is at most

2−T. 2^{-T}.

It remains to prove the three properties.

Property 1: Every strong liar belongs to SS

Let xx be a strong liar. Then either

xm≡1(modn), x^m \equiv 1 \pmod n,

or there exists i∈{0,1,…,k−1}i \in \{0,1,\ldots,k-1\} such that

x2im≡−1(modn). x^{2^i m} \equiv -1 \pmod n.

If xm≡1(modn)x^m \equiv 1 \pmod n, then since rr is a multiple of mm,

xr≡1(modn), x^r \equiv 1 \pmod n,

so x∈Sx \in S.

Otherwise, suppose

x2im≡−1(modn). x^{2^i m} \equiv -1 \pmod n.

By the maximality of rr, we have 2im≤r2^i m \le r. Since both 2im2^i m and rr are powers of two times mm, r/(2im)r/(2^i m) is a power of two. Hence

xr=(x2im)r/(2im)≡±1(modn). x^r = \left(x^{2^i m}\right)^{r/(2^i m)} \equiv \pm 1 \pmod n.

Thus x∈Sx \in S.

Therefore, every strong liar belongs to SS.

Property 2: SS is a subgroup of Zn∗\mathbb{Z}_n^*

We show that SS satisfies the subgroup conditions.

First, the identity element belongs to SS because

1r≡1(modn). 1^r \equiv 1 \pmod n.

Second, SS is closed under multiplication. If x,y∈Sx,y \in S, then

xr≡±1(modn)andyr≡±1(modn). x^r \equiv \pm 1 \pmod n \qquad \text{and} \qquad y^r \equiv \pm 1 \pmod n.

Therefore,

(xy)r≡xryr≡±1(modn). (xy)^r \equiv x^r y^r \equiv \pm 1 \pmod n.

Thus xy∈Sxy \in S.

Third, SS is closed under inverses. If x∈Sx \in S, then

xr≡±1(modn). x^r \equiv \pm 1 \pmod n.

Taking inverses on both sides gives

(x−1)r=(xr)−1≡±1(modn). (x^{-1})^r = (x^r)^{-1} \equiv \pm 1 \pmod n.

Hence x−1∈Sx^{-1} \in S.

Therefore, SS is a subgroup of Zn∗\mathbb{Z}_n^*.

Property 3: SS is a proper subgroup of Zn∗\mathbb{Z}_n^*

It remains to show that

S≠Zn∗. S \neq \mathbb{Z}_n^*.

We will construct an element yy such that

y∉Sandy∈Zn∗. y \notin S \qquad \text{and} \qquad y \in \mathbb{Z}_n^*.

Since nn is composite, odd, and not a perfect power, nn has at least two distinct prime factors. Write

n=p1e1p2e2⋯ptet, n = p_1^{e_1} p_2^{e_2} \cdots p_t^{e_t},

where t≥2t \ge 2. Let

a=p1e1andb=p2e2⋯ptet. a = p_1^{e_1} \qquad \text{and} \qquad b = p_2^{e_2} \cdots p_t^{e_t}.

Then

n=ab,a>1,b>1,gcd⁡(a,b)=1. n = ab, \qquad a>1, \qquad b>1, \qquad \gcd(a,b)=1.

Moreover, since nn is odd, both aa and bb are odd, and hence

1≢−1(moda)and1≢−1(modb). 1 \not\equiv -1 \pmod a \qquad \text{and} \qquad 1 \not\equiv -1 \pmod b.

By the choice of rr, there exists some x∈Zn∗x \in \mathbb{Z}_n^* such that

xr≡−1(modn). x^r \equiv -1 \pmod n.

Equivalently,

xr≡−1(moda)andxr≡−1(modb). x^r \equiv -1 \pmod a \qquad \text{and} \qquad x^r \equiv -1 \pmod b.

By the Chinese Remainder Theorem, there exists some y∈[0,…,n−1]y \in [0, \ldots, n-1] such that

y≡x(moda)andy≡1(modb). y \equiv x \pmod a \qquad \text{and} \qquad y \equiv 1 \pmod b.

We first show that y∉Sy \notin S. From the definition of yy, we have

yr≡xr≡−1(moda),andyr≡1(modb). y^r \equiv x^r \equiv -1 \pmod a, \qquad \text{and} \qquad y^r \equiv 1 \pmod b.

Thus yry^r cannot be congruent to 11 modulo nn, because modulo aa it is congruent to −1-1, not 11. Similarly, yry^r cannot be congruent to −1-1 modulo nn, because modulo bb it is congruent to 11, not −1-1. Therefore,

yr≢±1(modn). y^r \not\equiv \pm 1 \pmod n.

Hence

y∉S. y \notin S.

We next show that y∈Zn∗y \in \mathbb{Z}_n^*. Since x∈Zn∗x \in \mathbb{Z}_n^*, we have

gcd⁡(x,n)=1andgcd⁡(x,a)=1. \gcd(x,n)=1 \qquad \text{and} \qquad \gcd(x,a)=1.

Because

y≡x(moda), y \equiv x \pmod a,

we get

gcd⁡(y,a)=1. \gcd(y,a)=1.

Also, since

y≡1(modb), y \equiv 1 \pmod b,

we have

gcd⁡(y,b)=1. \gcd(y,b)=1.

Since n=abn=ab and gcd⁡(a,b)=1\gcd(a,b)=1, it follows that

gcd⁡(y,n)=1. \gcd(y,n)=1.

Therefore,

y∈Zn∗. y \in \mathbb{Z}_n^*.

We have found an element

y∈Zn∗andy∉S. y \in \mathbb{Z}_n^* \qquad \text{and} \qquad y \notin S.

Therefore,

S≠Zn∗. S \neq \mathbb{Z}_n^*.

So SS is a proper subgroup of Zn∗\mathbb{Z}_n^*.

■\blacksquare

Reference

  1. Primality Testing, CS265/CME309, Fall 2017, Gregory Valiant