/* * A random number is generated until a probable prime number is found. * * @see BigInteger#BigInteger(int,int,Random) * @see BigInteger#probablePrime(int,Random) * @see #isProbablePrime(BigInteger, int) */ internal static BigInteger consBigInteger(int bitLength, int certainty, java.util.Random rnd) { // PRE: bitLength >= 2; // For small numbers get a random prime from the prime table if (bitLength <= 10) { int[] rp = offsetPrimes[bitLength]; return(BIprimes[rp[0] + rnd.nextInt(rp[1])]); } int shiftCount = (-bitLength) & 31; int last = (bitLength + 31) >> 5; BigInteger n = new BigInteger(1, last, new int[last]); last--; do // To fill the array with random integers { for (int i = 0; i < n.numberLength; i++) { n.digits[i] = rnd.nextInt(); } // To fix to the correct bitLength n.digits[last] = (int)((long)n.digits[last] | (long)(0x80000000)); n.digits[last] = java.dotnet.lang.Operator.shiftRightUnsignet(n.digits[last], shiftCount); // To create an odd number n.digits[0] |= 1; } while (!isProbablePrime(n, certainty)); return(n); }
/* * Returns a pseudo-random number between 0.0 (inclusive) and 1.0 * (exclusive). * * @return a pseudo-random number. */ public static double random() { if (randomJ == null) { randomJ = new java.util.Random(); } return(randomJ.nextDouble()); }
/* * The Miller-Rabin primality test. * * @param n the input number to be tested. * @param t the number of trials. * @return {@code false} if the number is definitely compose, otherwise * {@code true} with probability {@code 1 - 4<sup>(-t)</sup>}. * @ar.org.fitc.ref "D. Knuth, The Art of Computer Programming Vo.2, Section * 4.5.4., Algorithm P" */ private static bool millerRabin(BigInteger n, int t) { // PRE: n >= 0, t >= 0 BigInteger x; // x := UNIFORM{2...n-1} BigInteger y; // y := x^(q * 2^j) mod n BigInteger n_minus_1 = n.subtract(BigInteger.ONE); // n-1 int bitLength = n_minus_1.bitLength(); // ~ log2(n-1) // (q,k) such that: n-1 = q * 2^k and q is odd int k = n_minus_1.getLowestSetBit(); BigInteger q = n_minus_1.shiftRight(k); java.util.Random rnd = new java.util.Random(); for (int i = 0; i < t; i++) { // To generate a witness 'x', first it use the primes of table if (i < primes.Length) { x = BIprimes[i]; } else /* * It generates random witness only if it's necesssary. Note * that all methods would call Miller-Rabin with t <= 50 so * this part is only to do more robust the algorithm */ { do { x = new BigInteger(bitLength, rnd); } while ((x.compareTo(n) >= BigInteger.EQUALS) || (x.sign == 0) || x.isOne()); } y = x.modPow(q, n); if (y.isOne() || y.equals(n_minus_1)) { continue; } for (int j = 1; j < k; j++) { if (y.equals(n_minus_1)) { continue; } y = y.multiply(y).mod(n); if (y.isOne()) { return(false); } } if (!y.equals(n_minus_1)) { return(false); } } return(true); }
/** * Returns a pseudo-random number between 0.0 (inclusive) and 1.0 * (exclusive). * * @return a pseudo-random number. */ public static double random() { if (randomJ == null) randomJ = new java.util.Random(); return randomJ.nextDouble(); }
/** * The Miller-Rabin primality test. * * @param n the input number to be tested. * @param t the number of trials. * @return {@code false} if the number is definitely compose, otherwise * {@code true} with probability {@code 1 - 4<sup>(-t)</sup>}. * @ar.org.fitc.ref "D. Knuth, The Art of Computer Programming Vo.2, Section * 4.5.4., Algorithm P" */ private static bool millerRabin(BigInteger n, int t) { // PRE: n >= 0, t >= 0 BigInteger x; // x := UNIFORM{2...n-1} BigInteger y; // y := x^(q * 2^j) mod n BigInteger n_minus_1 = n.subtract(BigInteger.ONE); // n-1 int bitLength = n_minus_1.bitLength(); // ~ log2(n-1) // (q,k) such that: n-1 = q * 2^k and q is odd int k = n_minus_1.getLowestSetBit(); BigInteger q = n_minus_1.shiftRight(k); java.util.Random rnd = new java.util.Random(); for (int i = 0; i < t; i++) { // To generate a witness 'x', first it use the primes of table if (i < primes.Length) { x = BIprimes[i]; } else {/* * It generates random witness only if it's necesssary. Note * that all methods would call Miller-Rabin with t <= 50 so * this part is only to do more robust the algorithm */ do { x = new BigInteger(bitLength, rnd); } while ((x.compareTo(n) >= BigInteger.EQUALS) || (x.sign == 0) || x.isOne()); } y = x.modPow(q, n); if (y.isOne() || y.equals(n_minus_1)) { continue; } for (int j = 1; j < k; j++) { if (y.equals(n_minus_1)) { continue; } y = y.multiply(y).mod(n); if (y.isOne()) { return false; } } if (!y.equals(n_minus_1)) { return false; } } return true; }