Miller-Rabin Primality Test

Miller-Rabin is a co-RP algorithm to test if a given number pp is in PRIMES\text{PRIMES} or not.

What is co-RP?

A problem is said to be in co-RP if there exists an algorithm such that

  • In runs in polynomial time with respect to input size
  • If the correct answer is Yes, it outputs Yes with probability 11
  • If the correct answer is No, then it returns No with probability 1/2\geq 1/2.

Hence PRIMES\text{PRIMES} is in co-RP as Miller-Rabin will output that a number pp is prime with probability 11 if the number is prime, and if composite, will output that pp is composite with probability 1/2\geq 1/2.

Algorithm

Input: An integer pp.

  1. If p=2p = 2, then accept (claim number is prime). Else, if pp is even, then reject (claim the number of composite).
  2. Pick kk numbers, a1,a2,,aka_1, a_2,\dots, a_k from Zp+\mathbb{Z}^+_p (Numbers 0,1,,p2,p10,1,\dots, p - 2, p - 1).
  3. For each ii from 1 to k1\ \text{to}\ k
    1. Compute aip1modpa_i^{p - 1} \bmod p and reject if different from 1. This is in accordance with Fermat’s Little Theorem, which states that ap11 mod pa^{p - 1} \equiv 1\ \text{mod}\ p if pp is prime.
    2. Since we are only dealing with odd values of pp (we reach a verdict for all even numbers in step 1) we can say that p1p - 1 is even. Hence, we can factorize p1=2ksp - 1 = 2^k\cdot s, where ss is some odd number.
    3. We then compute the sequence ais20,ais21,,ais2kmodpa_i^{s\cdot 2^0}, a_i^{s\cdot 2^1},\dots,a_i^{s\cdot2^k} \bmod p.
    4. We read from right to left, and try to find an element in this sequence that is not 11. If the first number we come across a number which is not 11, and that number isn’t 1modp-1 \bmod p, then we reject pp.
  4. If we haven’t rejected the number pp till now, it implies that it’s passed all the tests, hence we accept the number pp.

Proof: A prime number is always accepted with probability 11

If the prime number is even and 2, then we automatically accept it in the first step itself.

Else the prime number is odd.

In step 3.13.1, primes will always pass this test as ap11modpa^{p - 1} \equiv 1 \mod p according to Fermat’s Little Theorem.

In step 3.43.4, we need to prove that the first number we come across from right to left that’s not 11 has to be 1-1 if the given number is prime.

Proof By Contradiction:

We can notice that the sequence that we calculate is just ais20a_i^{s \cdot 2^0} squared repeatedly. Hence an element in the sequence is just the square of the previous term (except for the first term).

Take the first term that is not 11 as bb. We’ll formally state that b≢1modpb \not\equiv 1 \mod p trivially, and make the assumption b≢1modpb \not\equiv -1 \mod p to arrive at a contradiction later.

Since bb is the first term from the right that is not 11, we know the term to its right is 11.

Hence b21modp    b210modpb^2 \equiv 1 \mod p \implies b^2 - 1 \equiv 0 \mod p. Any number that is equivalent to 0modp0 \bmod p implies that the number is divisible by p.p.

We can factorize b21b^2 - 1 as

b21=(b1)(b+1)0modpb^2 - 1 = (b - 1)(b + 1) \equiv 0 \mod p

Since (b1)(b+1)0modp(b - 1)(b + 1) \equiv 0 \mod p, this implies that at least one of the two, (b1) or (b+1)(b - 1)\ \text{or}\ (b + 1) are divisible by pp, as pp is prime.

If (b1)(b - 1) is divisible by pp, it implies that (b1)0modp(b - 1) \equiv 0 \mod p. Hence, b1modpb \equiv 1 \mod p. However, this goes against our earlier assumption that b≢1modpb \not\equiv 1 \mod p.

If (b+1)(b + 1) is divisible by pp, it implies that (b+1)0modp(b + 1) \equiv 0 \mod p. Hence, b1modpb \equiv -1 \mod p. However, this goes against our other assumption that b≢1modpb \not\equiv -1 \mod p

Hence, we arrive at a contradiction. Hence, if b≢1modpb \not\equiv 1 \mod p, then b1modpb \equiv -1 \mod p when pp is an odd prime number.

Proof: An Odd Composite Number will be rejected with Probability 12\geq \frac{1}{2}

We define witnesses to be numbers that fail the tests at steps 3.13.1 or 3.43.4\

There are two trivial non-witnesses, 11 and 1-1. There are two types of non-witnesses as well, those which generate a sequence of only 11s, and those which have some numbers which aren’t 11. 1-1 falls into the second kind.

We will find the non-witness of the second kind that generates a sequence with the 1-1 coming at the rightmost position possible. We consider that number as hh, and the position of the 1-1 at position jj, assuming 00-based indexing.

Therefore hs2j1modph^{s\cdot2^j} \equiv -1 \mod p.

Since pp is composite, it must either be a power of a prime, or it can be represented as a product of two numbers that are relatively prime, q,rq,r. We’ll just go over the latter case.

Using the Chinese Remainder Theorem, we know there exists a number tt such that

thmodqt1modr\begin{split} t \equiv h \mod q\\ t \equiv 1 \mod r \end{split}

From this we can state that

ts2j1modqts2j1modr\begin{split} t^{s\cdot 2^j} \equiv -1 \mod q\\ t^{s \cdot 2^j} \equiv 1 \mod r \end{split}

We know that hs2jh^{s \cdot 2^j} can be represented in the form a(qr)1a(qr) - 1. Therefore, we can also represent it as ar(q)1ar(q) - 1, implying hs2j1modqh^{s\cdot 2^j} \equiv -1 \mod q. Since thmodqt \equiv h \mod q, we can clearly conclude that ts2j1modqt^{s \cdot 2^j} \equiv -1 \mod q.

The second equivalence is trivial as t1modrt \equiv 1 \mod r, hence raising it to any power will give 11.

Now, we’ll make the claim that tt is a witness.

If ts2j1modpt^{s\cdot 2^j} \equiv 1 \mod p, Then we can write ts2j=kp+1=(kr)q+1t^{s\cdot 2^j} = kp + 1 = (kr)q + 1. This implies ts2j1modqt^{s \cdot 2^j} \equiv 1 \mod q, which is incorrect. Hence ts2j≢1modpt^{s \cdot 2^j} \not\equiv 1 \mod p

If ts2j1modpt^{s \cdot 2^j} \equiv -1 \mod p, Then we can write ts2j=kp1=(kq)r1t^{s\cdot 2^j} = kp - 1 = (kq)r - 1. This implies that ts2j1modrt^{s\cdot 2^j} \equiv -1 \mod r, which is incorrect.

Hence ts2j≢±1modpt^{s\cdot 2^j} \not\equiv \pm 1 \mod p.

However, since ts2j+11modqt^{s \cdot 2^{j + 1}} \equiv 1 \mod q, and ts2j+11modrt^{s \cdot 2^{j + 1}} \equiv 1 \mod r, this implies that ts2j+11t^{s \cdot 2^{j + 1}} - 1 is divisible by both qq and rr. Since qq and rr are relatively prime, we know that ts2j+11modpt^{s \cdot 2^{j + 1}} \equiv 1 \mod p

Since ts2j≢±1modpt^{s \cdot 2^j} \not\equiv \pm 1 \mod p and ts2j+11modpt^{s \cdot 2^{j +1}} \equiv 1 \mod p, this implies that tt is a witness.

Mapping each non-witness to a unique witness

We can prove that dtmodpdt \bmod p is a unique witness for each unique non-witness dd.
Since dd is a non-witness, it implies that ds2j±1modpd^{s \cdot 2^j} \equiv \pm 1 \mod p. However, ts2j≢±1modpt^{s \cdot 2^j} \not \equiv \pm 1 \mod p as tt is a witness.
Hence, (dt)s2jds2jts2j±1ts2j≢±1modp(dt)^{s \cdot 2^j} \equiv d^{s \cdot 2^j} \cdot t^{s \cdot 2^j} \equiv \pm 1 \cdot t^{s \cdot 2^j} \not \equiv \pm 1 \mod p. Hence dtdt is a witness as well.

Proving uniqueness of witnesses. i.e d1td2tmodp    d1d2modpd_1t \equiv d_2t \mod p \implies d_1 \equiv d_2 \mod p

If d1td2tmodpd_1t \equiv d_2t \mod p, then

d1tts2j+11d2tts2j+11modpd1ts2j+1d2ts2j+1modpd11d21modp \begin{split} d_1 t \cdot t^{s 2^{j + 1} - 1} &\equiv d_2 t \cdot t ^{s \cdot 2^{j + 1} - 1} \mod p \\ d_1t^{s \cdot 2^{j + 1}} & \equiv d_2t^{s \cdot 2^{j + 1}} \mod p\\ d_1 \cdot 1 &\equiv d_2 \cdot 1 \mod p \end{split}

Hence we’ve proven that the witnesses are unique. Hence, we’ve proven that WitnessesNon-witnesses|\text{Witnesses}| \ge | \text{Non-witnesses}|. Hence, the probability of an odd composite number being rejected is 1/2\geq 1/2.

Hence, upon running the Miller-Rabin primality test with kk numbers, we get an accuracy of (2k1)/2k(2^k - 1)/2^k.

Published Nov 24, 2022