Pham Ngoc Hai personal web site

Syndicate

Miller-Rabin prime test in Java
 
Written by Pham Ngoc Hai, on 27-11-2007 15:11

A simple Miller-Rabin prime number test I wrote in Java.

Along with this method I implemented isWitness as well as the modulo power by bit shifting method.

Feel free to comment ;)

=================

Update: 

Someone requested for myBigRanNum and FactorOfTwo, I put them here as well, enjoy.

=================

 

public static boolean MillerRabin(BigInteger n, int k){

        BigInteger a;
        BigInteger m = n.subtract(BigInteger.ONE);
        boolean isPrime = true;
       
        if (n.compareTo(BigInteger.ONE) == 0) return false;
        if (n.mod(BigInteger.valueOf(2)).intValue() == 0) return false;       
      
        for (int i = 0; i < k; i++) {
            a = myBigRanNum(MAX_BIT);
            if (a.compareTo(m) > 0) a = a.mod(m);
            if (a.compareTo(BigInteger.ZERO) == 0) a = BigInteger.ONE;
           
            if (isWitness(a, n)) return false;
        }
        return isPrime;

}

 

 

public static boolean isWitness(BigInteger a, BigInteger n) {
        int s;
        BigInteger apow;
        BigInteger firstEqua, secondEqua;
        BigInteger m = n.subtract(BigInteger.ONE);
        BigInteger d;
        s = FactorOfTwo(m);
        d = m.divide(BigInteger.valueOf((int)Math.pow(2, s)));  

        firstEqua = ModExpo(a, d, n);
        if (firstEqua.intValue() == 1 || firstEqua.compareTo(m) == 0 ) return false;      
      
        for (int r = 0; r < s; r++) {
            apow = d.multiply(BigInteger.valueOf((int)Math.pow(2, r)));
            secondEqua = ModExpo(a, apow, n);
            if (secondEqua.intValue() == -1 || secondEqua.compareTo(m) == 0) {              
                return false;
            }
        }
        return true;
}

 

public static BigInteger ModExpo(BigInteger aBigInt, BigInteger pow, BigInteger n) {
        BigInteger e = pow;
        BigInteger a = aBigInt;
        BigInteger result = BigInteger.ONE;
        while (e.compareTo(BigInteger.ZERO) > 0 ) {
            if (e.and(BigInteger.ONE).compareTo(BigInteger.ONE) == 0 ){              
                result = (result.multiply(a)).mod(n);
            }          
            e = e.shiftRight(1);
            a = (a.multiply(a)).mod(n);
        }
        return result;
}

 

 

public static int FactorOfTwo(BigInteger aNumber) {
        int i = 0;
        BigInteger myNumber = aNumber;
        BigInteger myRemainer;
        while (true) {          
            myRemainer = myNumber.mod(BigInteger.valueOf(2));
            if (myRemainer.compareTo(BigInteger.ZERO) != 0 ) break;
            myNumber = myNumber.divide(BigInteger.valueOf(2));
            i++;
        }
        return i;
}

 

 

public static BigInteger myBigRanNum(int numBits) {
      
        //We only deal with positive number here

        BigInteger aBigInterger = new BigInteger(numBits, aRand);
        aBigInterger = aBigInterger.abs();
        return aBigInterger;
}

Last update: 15-03-2008 09:14

Published in : Computer stuff, Programming
Keywords : Computer stuff, Programming, Miller Rabin prime test in JavaMiller Rabin prime number test Java, witness, modulo power, bit shifting
Quote this article in website Favoured Print Send to friend Related articles Save this to del.icio.us

Users' Comments (2) RSS feed comment
Posted by Camilo, on 16-04-2009 02:29, , Guest
1. Excelent
Excelent job bro! 
Just check that 2 it's also prime and is done!.
 
» Report this comment to administrator
» Reply to this comment...

Posted by Alex, on 18-03-2009 05:48, , Guest
2. how about 2
how about 2? It's prime.
 
» Report this comment to administrator
» Reply to this comment...

Add your comment



mXcomment 1.0.9 © 2007-2010 - visualclinic.fr
License Creative Commons - Some rights reserved
< Prev


Search

Calendar

 Aug   September 2010   Oct

SMTWTFS
   1  2  3  4
  5  6  7  8  91011
12131415161718
19202122232425
2627282930 
VLSI ESL

Random Photos






Donate

Enter Amount:

Sponsored Links

Copyright © 2007 Joomla Templates By Joomladesigns.  Modified By Pham Ngoc Hai