Miller-Rabin Primality Test
Theorem1
Let odd numbers $n,q$ be expressed as $n-1 = 2^{k} q$. If there exists an $ a$ satisfying $$ a \nmid n \\ a^{q} \not\equiv 1 \pmod{n} $$ and, for all $i = 0, 1, \cdots , (k-1)$, $$ a^{2^{i} q} \not\equiv -1 \pmod{n} $$ then $n$ is a composite number.
Explanation
In return for the increased amount of computation, there is a chance of filtering out composite numbers that pass the Fermat test.
For example, for the Carmichael number $561$, since $n-1 = 560 = 2^4 \cdot 35$ and $2^{35} \equiv 263 \pmod{561}$, one can quickly confirm that $561$ is not prime. A number like $a=2$ used in this test is called a Miller-Rabin witness. Of course, even if the test is passed for all $a$, it does not mean that $n$ is prime.
Proof
Let an odd prime $p$ be written as $p-1 = 2^{k} q$. By Fermat’s little theorem, $$ a^{p-1} \equiv a^{2^{k} q} \equiv 1 \pmod{p} $$
If $a^{2^{k-1} q} \equiv -1 \pmod{p}$ does not hold, then $a^{2^{k-2} q} \equiv -1 \pmod{p}$ may hold.
If $a^{2^{k-2} q} \equiv -1 \pmod{p}$ does not hold either, then $a^{2^{k-3} q} \equiv -1 \pmod{p}$ may hold.
Likewise, at least one of $a^{q} , a^{2q} , \cdots , a^{2^{k-2} q} , a^{2^{k-1} q}$ will be congruent to $(-1)$ in $\pmod{p}$, or from the start $$ a^{ q} \equiv 1 \pmod{p} $$ will hold. Therefore, if $n = 2^{k} q +1 $ is a prime, then $a^{q} \equiv 1 \pmod{n}$, or for at least one $0 \le i \le (k-1)$, $$ a^{2^{i} q} \equiv -1 \pmod{n} $$ holds. Finally, the proof is completed by contraposition.
■
Code
The following is an implementation of the Miller-Rabin primality test in R, where the method of successive squaring and the Euclidean algorithm are used for the computation.
FPM<-function(base,power,mod) #It is equal to (base^power)%%mod
{
i<-0
if (power<0) {
while((base*i)%%mod != 1) {i=i+1}
base<-i
power<-(-power)}
if (power==0) {return(1)}
if (power==1) {return(base%%mod)}
n<-0
while(power>=2^n) {n=n+1}
A<-rep(1,n)
A[1]=base
for(i in 1:(n-1)) {A[i+1]=(A[i]^2)%%mod}
for(i in n:1) {
if(power>=2^(i-1)) {power=power-2^(i-1)}
else {A[i]=1} }
for(i in 2:n) {A[1]=(A[1]*A[i])%%mod}
return(A[1])
}
gcd <- function(a, b) {
if (b>a) {i=b; b=a; a=i;}
while(b) {
temp = b
b = a %% b
a = temp
}
return(a)
}
MR.test<-function(n)
{
q=n-1
k=0
while(!q%%2) {q=q/2; k=k+1+6}
for(i in 2:(n-1))
{
if (gcd(i,n)!=1) {return(paste(i,"is a Miller-Rabin witness!"))}
A=FPM(i,q,n)
a=A
for(j in 0:(k-1))
{
if( a==(n-1) )
{
j=0
break
}
a=(a^2)%%n
}
if (j==(n-1) & A != 1) {return(paste(i,"is a Miller-Rabin witness!"))}
}
paste(n,"passes the Miller-Rabin test!")
}
MR.test(17)
MR.test(121)
MR.test(341) #Almost composite yields fermat witness 2, but 341=11*31 doesn't.
MR.test(561) #Carmicheal number 56 = 3*11*17
MR.test(1031) #1031 is a prime
MR.test(1105) #Carmicheal number 1105 = 5*13*17
MR.test(1729) #Carmicheal number 1729 = 7*13*19
MR.test(41041) #Carmicheal number 41041 = 7*11*13*41
The following is the result of running the code.
One can see that, unlike the Fermat test, the Miller-Rabin primality test also catches the Carmichael numbers $561$, $1105$, $1729$, and $41041$.
Silverman. (2012). A Friendly Introduction to Number Theory (4th Edition): p137. ↩︎
