Miller-Rabin is a co-RP algorithm to test if a given number p is in 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 1
If the correct answer is No, then it returns No with probability ≥1/2.
Hence PRIMES is in co-RP as Miller-Rabin will output that a number p is prime with probability 1 if the number is prime, and if composite, will output that p is composite with probability ≥1/2.
Algorithm
Input: An integer p.
If p=2, then accept (claim number is prime). Else, if p is even, then reject (claim the number of composite).
Pick k numbers, a1,a2,…,ak from Zp+ (Numbers 0,1,…,p−2,p−1).
For each i from 1tok
Compute aip−1modp and reject if different from 1. This is in accordance with Fermat’s Little Theorem, which states that ap−1≡1modp if p is prime.
Since we are only dealing with odd values of p (we reach a verdict for all even numbers in step 1) we can say that p−1 is even. Hence, we can factorize p−1=2k⋅s, where s is some odd number.
We then compute the sequence ais⋅20,ais⋅21,…,ais⋅2kmodp.
We read from right to left, and try to find an element in this sequence that is not 1. If the first number we come across a number which is not 1, and that number isn’t −1modp, then we reject p.
If we haven’t rejected the number p till now, it implies that it’s passed all the tests, hence we accept the number p.
Proof: A prime number is always accepted with probability 1
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.1, primes will always pass this test as ap−1≡1modp according to Fermat’s Little Theorem.
In step 3.4, we need to prove that the first number we come across from right to left that’s not 1 has to be −1 if the given number is prime.
Proof By Contradiction:
We can notice that the sequence that we calculate is just ais⋅20 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 1 as b. We’ll formally state that b≡1modp trivially, and make the assumption b≡−1modp to arrive at a contradiction later.
Since b is the first term from the right that is not 1, we know the term to its right is 1.
Hence b2≡1modp⟹b2−1≡0modp. Any number that is equivalent to 0modp implies that the number is divisible by p.
We can factorize b2−1 as
b2−1=(b−1)(b+1)≡0modp
Since (b−1)(b+1)≡0modp, this implies that at least one of the two, (b−1)or(b+1) are divisible by p, as p is prime.
If (b−1) is divisible by p, it implies that (b−1)≡0modp. Hence, b≡1modp. However, this goes against our earlier assumption that b≡1modp.
If (b+1) is divisible by p, it implies that (b+1)≡0modp. Hence, b≡−1modp. However, this goes against our other assumption that b≡−1modp
Hence, we arrive at a contradiction. Hence, if b≡1modp, then b≡−1modp when p is an odd prime number.
Proof: An Odd Composite Number will be rejected with Probability ≥21
We define witnesses to be numbers that fail the tests at steps 3.1 or 3.4\
There are two trivial non-witnesses, 1 and −1. There are two types of non-witnesses as well, those which generate a sequence of only 1s, and those which have some numbers which aren’t 1. −1 falls into the second kind.
We will find the non-witness of the second kind that generates a sequence with the −1 coming at the rightmost position possible. We consider that number as h, and the position of the −1 at position j, assuming 0-based indexing.
Therefore hs⋅2j≡−1modp.
Since p 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,r. We’ll just go over the latter case.
Using the Chinese Remainder Theorem, we know there exists a number t such that
t≡hmodqt≡1modr
From this we can state that
ts⋅2j≡−1modqts⋅2j≡1modr
We know that hs⋅2j can be represented in the form a(qr)−1. Therefore, we can also represent it as ar(q)−1, implying hs⋅2j≡−1modq. Since t≡hmodq, we can clearly conclude that ts⋅2j≡−1modq.
The second equivalence is trivial as t≡1modr, hence raising it to any power will give 1.
Now, we’ll make the claim that t is a witness.
If ts⋅2j≡1modp, Then we can write ts⋅2j=kp+1=(kr)q+1. This implies ts⋅2j≡1modq, which is incorrect. Hence ts⋅2j≡1modp
If ts⋅2j≡−1modp, Then we can write ts⋅2j=kp−1=(kq)r−1. This implies that ts⋅2j≡−1modr, which is incorrect.
Hence ts⋅2j≡±1modp.
However, since ts⋅2j+1≡1modq, and ts⋅2j+1≡1modr, this implies that ts⋅2j+1−1 is divisible by both q and r. Since q and r are relatively prime, we know that ts⋅2j+1≡1modp
Since ts⋅2j≡±1modp and ts⋅2j+1≡1modp, this implies that t is a witness.
Mapping each non-witness to a unique witness
We can prove that dtmodp is a unique witness for each unique non-witness d.
Since d is a non-witness, it implies that ds⋅2j≡±1modp. However, ts⋅2j≡±1modp as t is a witness.
Hence, (dt)s⋅2j≡ds⋅2j⋅ts⋅2j≡±1⋅ts⋅2j≡±1modp. Hence dt is a witness as well.
Proving uniqueness of witnesses. i.e d1t≡d2tmodp⟹d1≡d2modp
Hence we’ve proven that the witnesses are unique. Hence, we’ve proven that ∣Witnesses∣≥∣Non-witnesses∣. Hence, the probability of an odd composite number being rejected is ≥1/2.
Hence, upon running the Miller-Rabin primality test with k numbers, we get an accuracy of (2k−1)/2k.