logo

Miller-Rabin Primality Test 📂Number Theory

Miller-Rabin Primality Test

Theorem 1

Let’s express an odd number $n,q$ as $n-1 = 2^{k} q$. $$ 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} $$ if there exists a $ a$ that satisfies the above, then $n$ is a composite number.

Explanation

With the increased amount of computation, there’s a possibility to filter out composite numbers that pass the Fermat’s Little Theorem.

For example, a Carmichael number such as $561$, since $n-1 = 560 = 2^4 \cdot 35$ is $2^{35} \equiv 263 \pmod{561}$, we can quickly verify that $561$ is not a prime number. The number used in this judgment, such as $a=2$, is called a Miller-Rabin Witness. However, even if all $a$ pass through the test, it doesn’t mean $n$ is a prime.

Proof

Let’s consider an odd prime number $p$ as $p-1 = 2^{k} q$. By Fermat’s Little Theorem, $$ a^{p-1} \equiv a^{2^{k} q} \equiv 1 \pmod{p} $$

If not $a^{2^{k-1} q} \equiv -1 \pmod{p}$, it might be $a^{2^{k-2} q} \equiv -1 \pmod{p}$.

If not even $a^{2^{k-2} q} \equiv -1 \pmod{p}$, it might be $a^{2^{k-3} q} \equiv -1 \pmod{p}$.

Similarly, at least one of the $a^{q} , a^{2q} , \cdots , a^{2^{k-2} q} , a^{2^{k-1} q}$ must be congruent to $(-1)$ in $\pmod{p}$ or initially, $$ a^{ q} \equiv 1 \pmod{p} $$ Therefore, if $n = 2^{k} q +1 $ is a prime number, either $a^{q} \equiv 1 \pmod{n}$ or at least one of the $0 \le i \le (k-1)$ must $$ a^{2^{i} q} \equiv -1 \pmod{n} $$ Finally, the proof concludes by contraposition.

Code

Below is the Miller-Rabin test implemented in R, using the method of successive squaring and Euclidean algorithm for calculation.

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

Below is the result of running the code.

20190304\_112954.png Unlike the Fermat Test, the Miller-Rabin test can also catch Carmichael numbers such as $561$, $1105$, $1729$, $41041$.


  1. Silverman. (2012). A Friendly Introduction to Number Theory (4th Edition): p137. ↩︎