Download code

Jump to: navigation, search

Back to Miller-Rabin_primality_test_(Java)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

MillerRabin32.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Miller-Rabin_primality_test_(Java)?oldid=17585
14 */
15 
16 public class MillerRabin32
17 {
18     private static int modular_exponent_32(int base, int power, int modulus) {
19         long result = 1;
20         for (int i = 31; i >= 0; i--) {
21             result = (result*result) % modulus;
22             if ((power & (1 << i)) != 0) {
23                 result = (result*base) % modulus;
24             }
25         }
26         return (int)result; // Will not truncate since modulus is an int
27     }
28 
29 
30     private static boolean miller_rabin_pass_32(int a, int n) {
31         int d = n - 1;
32 	int s = Integer.numberOfTrailingZeros(d);
33 	d >>= s;
34         int a_to_power = modular_exponent_32(a, d, n);
35         if (a_to_power == 1) return true;
36         for (int i = 0; i < s-1; i++) {
37             if (a_to_power == n-1) return true;
38             a_to_power = modular_exponent_32(a_to_power, 2, n);
39         }
40         if (a_to_power == n-1) return true;
41         return false;
42     }
43 
44     public static boolean miller_rabin_32(int n) {
45         if (n <= 1) return false;
46         else if (n == 2) return true;
47         else if (miller_rabin_pass_32( 2, n) &&
48             (n <= 7  || miller_rabin_pass_32( 7, n)) &&
49             (n <= 61 || miller_rabin_pass_32(61, n)))
50             return true;
51         else
52             return false;
53     }
54 
55     public static void main(String[] args) {
56         int n = Integer.parseInt(args[0]);
57         System.out.println(miller_rabin_32(n) ? "PRIME" : "COMPOSITE");
58     }
59 }


hijacker
hijacker
hijacker
hijacker

MillerRabin.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Miller-Rabin_primality_test_(Java)?oldid=17585
14 */
15 
16 import java.math.BigInteger;
17 import java.util.Random;
18 
19 public class MillerRabin
20 {
21     private static final Random rnd = new Random();
22 
23     private static boolean miller_rabin_pass(BigInteger a, BigInteger n) {
24         BigInteger n_minus_one = n.subtract(BigInteger.ONE);
25         BigInteger d = n_minus_one;
26 	int s = d.getLowestSetBit();
27 	d = d.shiftRight(s);
28         BigInteger a_to_power = a.modPow(d, n);
29         if (a_to_power.equals(BigInteger.ONE)) return true;
30         for (int i = 0; i < s-1; i++) {
31             if (a_to_power.equals(n_minus_one)) return true;
32             a_to_power = a_to_power.multiply(a_to_power).mod(n);
33         }
34         if (a_to_power.equals(n_minus_one)) return true;
35         return false;
36     }
37 
38     public static boolean miller_rabin(BigInteger n) {
39         for (int repeat = 0; repeat < 20; repeat++) {
40             BigInteger a;
41             do {
42                 a = new BigInteger(n.bitLength(), rnd);
43             } while (a.equals(BigInteger.ZERO));
44             if (!miller_rabin_pass(a, n)) {
45                 return false;
46             }
47         }
48         return true;
49     }
50 
51     public static void main(String[] args) {
52         if (args[0].equals("test")) {
53             BigInteger n = new BigInteger(args[1]);
54             System.out.println(miller_rabin(n) ? "PRIME" : "COMPOSITE");
55         } else if (args[0].equals("genprime")) {
56             int nbits = Integer.parseInt(args[1]);
57             BigInteger p;
58             do {
59                 p = new BigInteger(nbits, rnd);
60                 if (p.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO)) continue;
61 		if (p.mod(BigInteger.valueOf(3)).equals(BigInteger.ZERO)) continue;
62 		if (p.mod(BigInteger.valueOf(5)).equals(BigInteger.ZERO)) continue;
63 		if (p.mod(BigInteger.valueOf(7)).equals(BigInteger.ZERO)) continue;
64             } while (!miller_rabin(p));
65             System.out.println(p);
66         }
67     }
68 }


hijacker
hijacker
hijacker
hijacker