This primality check is significantly quick with an accuracy of 4^(-k) - Java java.lang

Java examples for java.lang:int prime

Description

This primality check is significantly quick with an accuracy of 4^(-k)

Demo Code


//package com.java2s;

import java.math.BigInteger;
import java.security.SecureRandom;

public class Main {
    /** Convienence variable for the BigInteger Zero */
    private static BigInteger ZERO_INT = new BigInteger("0");
    /** Convienence variable for the BigInteger One */
    private static BigInteger ONE_INT = new BigInteger("1");
    /** Convienence variable for the BigInteger Two */
    private static BigInteger TWO_INT = new BigInteger("2");

    /**//  w  w w .j a v a2 s . c om
     * This primality check is significantly quick with an accuracy of 4^(-k)
     * 
     * <p>
     * This function is a direct transcription of the following psuedo code into
     * native java as found at this address
     * </P>
     * 
     * <p>
     * <a href="http://www.cryptomathic.com/labs/rabinprimalitytest.html">
     * http://www.cryptomathic.com/labs/rabinprimalitytest.html </a>
     * </p>
     * <br>
     * Algorithm for the Miller-Rabin probabilistic primality test<br>
     * Miller-Rabin(n,t)<br>
     * <dd><b>Input</b> : <i>n</i> : An odd integer greater than 1, <i>t</i> : the number of witnesses</dd>
     * <dd><b>Output</b> : the answer <u>composite</u> or <u>prime</u></dd>
     * <dd>Write <i>n</i>-1 = 2<sup><i>s</i></sup> * <i>r</i> such that <i>r</i> is odd</dd>
     * <dd>Repeat from 1 to  <i>t</i></dd>
     * <dd>Choose a random integer <i>a</i> which satisfies 1 < <i>a</i> < <i>n</i>-1</dd>
     * <dd>Compute <i>y</i> = <i>a</i><sup><i>r</i></sup> mod <i>n</i></dd>
     * <dd>if <i>y</i> <> 1 and <i>y</i> <> <i>n</i>-1 then</dd>
     * <dl>
     * <dd><i>j</i> := 1</dd>
     * <dd>while <i>j</i> < <i>s</i> and <i>y</i> <> <i>n</i>-1 then</dd>
     * <dl>
     * <dd><i>y</i> := <i>y</i><sup>2</sup> mod <i>n</i></dd>
     * <dd>if <i>y</i> = 1 then return <u>composite</u></dd>
     * <dd><i>j</i> := <i>j</i> + 1</dd>
     * </dl>
     * <dd>if <i>y</i> <> <i>n</i>-1 then return <u>composite</u></dd>
     * </dl>
     * <dd>return <u>prime</u></dd>
     * 
     * @param n
     *            The number in question
     * @param t
     *            The number of witnesses.
     * @return true if the number is most likely prime given the number of
     *         witnesses t.
     */
    public static boolean checkMillerRabin(BigInteger n, int t) {
        boolean rval = true;
        SecureRandom srnd = new SecureRandom();
        BigInteger r = n.subtract(ONE_INT);
        BigInteger s = ZERO_INT;
        BigInteger a = ZERO_INT;
        BigInteger y = ZERO_INT;

        while (r.mod(TWO_INT).compareTo(ZERO_INT) == 0) {
            r = r.divide(TWO_INT);
            s = s.add(ONE_INT);
        }

        for (int i = 1; i <= t && rval; i++) {
            a = new BigInteger(n.subtract(TWO_INT).bitCount(), srnd)
                    .add(TWO_INT);
            y = a.modPow(r, n);
            if (y.compareTo(ONE_INT) != 0
                    && (y.compareTo(n.subtract(ONE_INT)) != 0)) {
                BigInteger j = ONE_INT;
                while (j.compareTo(s.subtract(ONE_INT)) <= 0
                        && y.compareTo(n.subtract(ONE_INT)) != 0 && rval) {
                    y = y.modPow(TWO_INT, n);
                    if (y.compareTo(ONE_INT) == 0) {
                        rval = false;
                    }
                    j = j.add(ONE_INT);
                }
                if (y.compareTo(n.subtract(ONE_INT)) != 0) {
                    rval = false;
                }
            }
        }
        return rval;
    }
}

Related Tutorials