What are the Miller-Rabin Algorithm for testing the primality of a given number?

Miller Rabin is a fast approach to test primality of the large numbers. This algorithm is called a Rabin-miller primality test and this algorithm decides whether number is prime which is same to other tests including Fermat primality Test and Solovay- Strassen primality test.

This test is based on equality or group of equalities that hold the true for prime values, thus checks whether they hold for the number, that it is required to test for primality.

This algorithm is most useful known primality testing algorithm and can be used in different software libraries that based on RSA encryption and best instance is OpenSSL.

Miller Rabin validate that the number is composite. So this is called compositeness test rather than primality test. The miller Rabin test identify all composites. For each composite number n, there can be at least 3/4 (Miller Rabin) of numbers a are witnesses of compositeness of n.

Miller Rabin is associatively simple extension of Fermats little Theorem that enable us to test for primality with a much larger probability than Fermats little theorem.

Algorithm : Pseudocode for Miller-Rabin test −

Miller-Rabin-Test (n, a) // n is the number; a is the base{
   Find m and k such that n − 1 = m x 2k
   T ← amod n
   If (T = ±1)return "a prime"
   for (i ← 1 to k − 1) // k – 1 is the maximum number of steps{
      T ← T2 mod n
      if (T = ±1) return "a composite"
      if (T = −1) return "a prime"
   return "a composite"

There exists a proof that each time a number passes a Miller-Rabin test, the probability that it is not a prime is ¼. If the number passes m tests (with m different passes), the probability that is not a prime is (1/4)m.

Example: Apply Miller-Rabin Algorithm using base 2 to test whether the number 341 is composite or not.

Solution: Using Miller-Rabin Algorithm, we can test the number 341 as follows −

Step1: 341 − 1 = 22 x 85. Thus p = 341, k = 2 and q = 85

Step2: x = 2 (given)

Step3: S = xq mod p

         = 285 mod 341 = (210) x 25 mod 341 8

         = 210 mod 341 x 213 mod 341

         = 1 x 8192 mod 341 = 8192 mod 341

         = 8

Step4: As 8 ≠ 1, we move to the next step.

Step5: For j = 1, S = x2q mod p

         = 2170 mod 341 = (220)8 x 210 mod 341

         = 220 mod 341 x 28 mod 341 x 210 mod 341

         = 1 x 256 x 1 = 256

Now, = 256 ≠ 1

and result is inconclusive

So, 341 is not a composite number.


  • This Algorithm can be used to test high numbers for primality.

  • Because of its advantage in speed when compared to other primality tests, Miller Rabin test will be the test of choice for several cryptographic applications.

  • When compared to Euler and Solovay-Strassen tests, Miller Rabin is more dynamic and essential aspect is that the probability of failure is decreased.

  • According to the fermat test there are too many liars for all Carmichael numbers n, the error probability is near to 1, this disadvantage is prevented in Miller Rabin.