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,…,p−2,p−10,1,\dots, p - 2, p - 1).
  3. For each ii from 1 to k1\ \text{to}\ k
    1. Compute aip−1 mod pa_i^{p - 1} \bmod p and reject if different from 1. This is in accordance with Fermat’s Little Theorem, which states that ap−1≡1 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 p−1p - 1 is even. Hence, we can factorize p−1=2k⋅sp - 1 = 2^k\cdot s, where ss is some odd number.
    3. We then compute the sequence ais⋅20,ais⋅21,…,ais⋅2k mod pa_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 −1 mod p-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 ap−1≡1mod  pa^{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 ais⋅20a_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≢1mod  pb \not\equiv 1 \mod p trivially, and make the assumption b≢−1mod  pb \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 b2≡1mod  p  ⟹  b2−1≡0mod  pb^2 \equiv 1 \mod p \implies b^2 - 1 \equiv 0 \mod p. Any number that is equivalent to 0 mod p0 \bmod p implies that the number is divisible by p.p.

We can factorize b2−1b^2 - 1 as

b2−1=(b−1)(b+1)≡0mod  pb^2 - 1 = (b - 1)(b + 1) \equiv 0 \mod p

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

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

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

Hence, we arrive at a contradiction. Hence, if b≢1mod  pb \not\equiv 1 \mod p, then b≡−1mod  pb \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 hs⋅2j≡−1mod  ph^{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

t≡hmod  qt≡1mod  r\begin{split} t \equiv h \mod q\\ t \equiv 1 \mod r \end{split}

From this we can state that

ts⋅2j≡−1mod  qts⋅2j≡1mod  r\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 hs⋅2jh^{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 hs⋅2j≡−1mod  qh^{s\cdot 2^j} \equiv -1 \mod q. Since t≡hmod  qt \equiv h \mod q, we can clearly conclude that ts⋅2j≡−1mod  qt^{s \cdot 2^j} \equiv -1 \mod q.

The second equivalence is trivial as t≡1mod  rt \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 ts⋅2j≡1mod  pt^{s\cdot 2^j} \equiv 1 \mod p, Then we can write ts⋅2j=kp+1=(kr)q+1t^{s\cdot 2^j} = kp + 1 = (kr)q + 1. This implies ts⋅2j≡1mod  qt^{s \cdot 2^j} \equiv 1 \mod q, which is incorrect. Hence ts⋅2j≢1mod  pt^{s \cdot 2^j} \not\equiv 1 \mod p

If ts⋅2j≡−1mod  pt^{s \cdot 2^j} \equiv -1 \mod p, Then we can write ts⋅2j=kp−1=(kq)r−1t^{s\cdot 2^j} = kp - 1 = (kq)r - 1. This implies that ts⋅2j≡−1mod  rt^{s\cdot 2^j} \equiv -1 \mod r, which is incorrect.

Hence ts⋅2j≢±1mod  pt^{s\cdot 2^j} \not\equiv \pm 1 \mod p.

However, since ts⋅2j+1≡1mod  qt^{s \cdot 2^{j + 1}} \equiv 1 \mod q, and ts⋅2j+1≡1mod  rt^{s \cdot 2^{j + 1}} \equiv 1 \mod r, this implies that ts⋅2j+1−1t^{s \cdot 2^{j + 1}} - 1 is divisible by both qq and rr. Since qq and rr are relatively prime, we know that ts⋅2j+1≡1mod  pt^{s \cdot 2^{j + 1}} \equiv 1 \mod p

Since ts⋅2j≢±1mod  pt^{s \cdot 2^j} \not\equiv \pm 1 \mod p and ts⋅2j+1≡1mod  pt^{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 dt mod pdt \bmod p is a unique witness for each unique non-witness dd.
Since dd is a non-witness, it implies that ds⋅2j≡±1mod  pd^{s \cdot 2^j} \equiv \pm 1 \mod p. However, ts⋅2j≢±1mod  pt^{s \cdot 2^j} \not \equiv \pm 1 \mod p as tt is a witness.
Hence, (dt)s⋅2j≡ds⋅2j⋅ts⋅2j≡±1⋅ts⋅2j≢±1mod  p(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 d1t≡d2tmod  p  ⟹  d1≡d2mod  pd_1t \equiv d_2t \mod p \implies d_1 \equiv d_2 \mod p

If d1t≡d2tmod  pd_1t \equiv d_2t \mod p, then

d1t⋅ts2j+1−1≡d2t⋅ts⋅2j+1−1mod  pd1ts⋅2j+1≡d2ts⋅2j+1mod  pd1⋅1≡d2⋅1mod  p \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 ∣Witnesses∣≥∣Non-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 (2k−1)/2k(2^k - 1)/2^k.

Published Nov 24, 2022