/// <summary> /// Probabilistic prime test based on Rabin-Miller's test /// </summary> /// <param name="n" type="BigInteger.BigInteger"> /// <para> /// The number to test. /// </para> /// </param> /// <param name="confidence" type="int"> /// <para> /// The number of chosen bases. The test has at least a /// 1/4^confidence chance of falsely returning True. /// </para> /// </param> /// <returns> /// <para> /// True if "this" is a strong pseudoprime to randomly chosen bases. /// </para> /// <para> /// False if "this" is definitely NOT prime. /// </para> /// </returns> public static bool RabinMillerTest(BigInteger n, ConfidenceFactor confidence) { int bits = n.BitCount(); int t = GetSPPRounds(bits, confidence); // n - 1 == 2^s * r, r is odd BigInteger n_minus_1 = n - 1; int s = n_minus_1.LowestSetBit(); BigInteger r = n_minus_1 >> s; BigInteger.ModulusRing mr = new BigInteger.ModulusRing(n); // Applying optimization from HAC section 4.50 (base == 2) // not a really random base but an interesting (and speedy) one BigInteger y = null; // FIXME - optimization disable for small primes due to bug #81857 if (n.BitCount() > 100) { y = mr.Pow(2, r); } // still here ? start at round 1 (round 0 was a == 2) for (int round = 0; round < t; round++) { if ((round > 0) || (y == null)) { BigInteger a = null; // check for 2 <= a <= n - 2 // ...but we already did a == 2 previously as an optimization do { a = BigInteger.GenerateRandom(bits); } while ((a <= 2) && (a >= n_minus_1)); y = mr.Pow(a, r); } if (y == 1) { continue; } for (int j = 0; ((j < s) && (y != n_minus_1)); j++) { y = mr.Pow(y, 2); if (y == 1) { return(false); } } if (y != n_minus_1) { return(false); } } return(true); }
public static bool RabinMillerTest(BigInteger n, ConfidenceFactor confidence) { int num = n.BitCount(); int sPPRounds = GetSPPRounds(num, confidence); BigInteger bigInteger = n - 1; int num2 = bigInteger.LowestSetBit(); BigInteger bigInteger2 = bigInteger >> num2; BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(n); BigInteger bigInteger3 = null; if (n.BitCount() > 100) { bigInteger3 = modulusRing.Pow(2u, bigInteger2); } for (int i = 0; i < sPPRounds; i++) { if (i > 0 || bigInteger3 == null) { BigInteger bigInteger4 = null; do { bigInteger4 = BigInteger.GenerateRandom(num); }while (bigInteger4 <= 2 && bigInteger4 >= bigInteger); bigInteger3 = modulusRing.Pow(bigInteger4, bigInteger2); } if (bigInteger3 == 1u) { continue; } for (int j = 0; j < num2; j++) { if (!(bigInteger3 != bigInteger)) { break; } bigInteger3 = modulusRing.Pow(bigInteger3, 2); if (bigInteger3 == 1u) { return(false); } } if (bigInteger3 != bigInteger) { return(false); } } return(true); }
public static bool SmallPrimeSppTest(BigInteger bi, ConfidenceFactor confidence) { int sppRounds = PrimalityTests.GetSPPRounds(bi, confidence); BigInteger bigInteger1 = bi - (BigInteger)1; int num = bigInteger1.LowestSetBit(); BigInteger exp = bigInteger1 >> num; BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(bi); for (int index1 = 0; index1 < sppRounds; ++index1) { BigInteger bigInteger2 = modulusRing.Pow(BigInteger.smallPrimes[index1], exp); if (!(bigInteger2 == 1U)) { bool flag = false; for (int index2 = 0; index2 < num; ++index2) { if (bigInteger2 == bigInteger1) { flag = true; break; } bigInteger2 = bigInteger2 * bigInteger2 % bi; } if (!flag) { return(false); } } } return(true); }
public static bool SmallPrimeSppTest(BigInteger bi, ConfidenceFactor confidence) { int spprounds = PrimalityTests.GetSPPRounds(bi, confidence); BigInteger bigInteger = bi - 1; int num = bigInteger.LowestSetBit(); BigInteger exp = bigInteger >> num; BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(bi); for (int i = 0; i < spprounds; i++) { BigInteger bigInteger2 = modulusRing.Pow(BigInteger.smallPrimes[i], exp); if (!(bigInteger2 == 1u)) { bool flag = false; for (int j = 0; j < num; j++) { if (bigInteger2 == bigInteger) { flag = true; break; } bigInteger2 = bigInteger2 * bigInteger2 % bi; } if (!flag) { return(false); } } } return(true); }
/// <summary> /// Probabilistic prime test based on Rabin-Miller's test /// </summary> /// <param name="bi" type="BigInteger.BigInteger"> /// <para> /// The number to test. /// </para> /// </param> /// <param name="confidence" type="int"> /// <para> /// The number of chosen bases. The test has at least a /// 1/4^confidence chance of falsely returning True. /// </para> /// </param> /// <returns> /// <para> /// True if "this" is a strong pseudoprime to randomly chosen bases. /// </para> /// <para> /// False if "this" is definitely NOT prime. /// </para> /// </returns> public static bool RabinMillerTest(BigInteger bi, ConfidenceFactor confidence) { int Rounds = GetSPPRounds(bi, confidence); // calculate values of s and t BigInteger p_sub1 = bi - 1; int s = p_sub1.LowestSetBit(); BigInteger t = p_sub1 >> s; int bits = bi.bitCount(); BigInteger a = null; RandomNumberGenerator rng = RandomNumberGenerator.Create(); var mr = new BigInteger.ModulusRing(bi); for (int round = 0; round < Rounds; round++) { while (true) { // generate a < n a = BigInteger.genRandom(bits, rng); // make sure "a" is not 0 if (a > 1 && a < bi) { break; } } if (a.gcd(bi) != 1) { return(false); } BigInteger b = mr.Pow(a, t); if (b == 1) { continue; // a^t mod p = 1 } bool result = false; for (int j = 0; j < s; j++) { if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1 result = true; break; } b = (b * b) % bi; } if (result == false) { return(false); } } return(true); }
public static bool RabinMillerTest(BigInteger n, ConfidenceFactor confidence) { int bits = n.BitCount(); int sppRounds = PrimalityTests.GetSPPRounds((BigInteger)bits, confidence); BigInteger bigInteger1 = n - (BigInteger)1; int num = bigInteger1.LowestSetBit(); BigInteger bigInteger2 = bigInteger1 >> num; BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(n); BigInteger a = (BigInteger)null; if (n.BitCount() > 100) { a = modulusRing.Pow(2U, bigInteger2); } for (int index1 = 0; index1 < sppRounds; ++index1) { if (index1 > 0 || a == (BigInteger)null) { BigInteger random; do { random = BigInteger.GenerateRandom(bits); }while (random <= (BigInteger)2 && random >= bigInteger1); a = modulusRing.Pow(random, bigInteger2); } if (!(a == 1U)) { for (int index2 = 0; index2 < num && a != bigInteger1; ++index2) { a = modulusRing.Pow(a, (BigInteger)2); if (a == 1U) { return(false); } } if (a != bigInteger1) { return(false); } } } return(true); }
/// <summary> /// Probabilistic prime test based on Rabin-Miller's test /// </summary> /// <param name="bi" type="BigInteger.BigInteger"> /// <para> /// The number to test. /// </para> /// </param> /// <param name="confidence" type="int"> /// <para> /// The number of chosen bases. The test has at least a /// 1/4^confidence chance of falsely returning True. /// </para> /// </param> /// <returns> /// <para> /// True if "this" is a strong pseudoprime to randomly chosen bases. /// </para> /// <para> /// False if "this" is definitely NOT prime. /// </para> /// </returns> public static bool RabinMillerTest(BigInteger bi, ConfidenceFactor confidence) { int Rounds = GetSPPRounds(bi, confidence); // calculate values of s and t BigInteger p_sub1 = bi - 1; int s = p_sub1.LowestSetBit(); BigInteger t = p_sub1 >> s; int bits = bi.bitCount(); BigInteger a = null; RandomNumberGenerator rng = RandomNumberGenerator.Create(); BigInteger.ModulusRing mr = new BigInteger.ModulusRing(bi); for (int round = 0; round < Rounds; round++) { while (true) { // generate a < n a = BigInteger.genRandom(bits, rng); // make sure "a" is not 0 if (a > 1 && a < bi) break; } if (a.gcd(bi) != 1) return false; BigInteger b = mr.Pow(a, t); if (b == 1) continue; // a^t mod p = 1 bool result = false; for (int j = 0; j < s; j++) { if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1 result = true; break; } b = (b * b) % bi; } if (result == false) return false; } return true; }
public static bool RabinMillerTest(BigInteger bi, ConfidenceFactor confidence) { int sPPRounds = GetSPPRounds(bi, confidence); BigInteger bigInteger = bi - 1; int num = bigInteger.LowestSetBit(); BigInteger exp = bigInteger >> num; int bits = bi.bitCount(); BigInteger bigInteger2 = null; RandomNumberGenerator rng = RandomNumberGenerator.Create(); BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(bi); for (int i = 0; i < sPPRounds; i++) { do { bigInteger2 = BigInteger.genRandom(bits, rng); }while (!(bigInteger2 > 1) || !(bigInteger2 < bi)); if (bigInteger2.gcd(bi) != 1u) { return(false); } BigInteger bigInteger3 = modulusRing.Pow(bigInteger2, exp); if (bigInteger3 == 1u) { continue; } bool flag = false; for (int j = 0; j < num; j++) { if (bigInteger3 == bigInteger) { flag = true; break; } bigInteger3 = bigInteger3 * bigInteger3 % bi; } if (!flag) { return(false); } } return(true); }
public static bool SmallPrimeSppTest(BigInteger bi, ConfidenceFactor confidence) { int Rounds = GetSPPRounds(bi, confidence); // calculate values of s and t BigInteger p_sub1 = bi - 1; int s = p_sub1.LowestSetBit(); BigInteger t = p_sub1 >> s; var mr = new BigInteger.ModulusRing(bi); for (int round = 0; round < Rounds; round++) { BigInteger b = mr.Pow(BigInteger.smallPrimes[round], t); if (b == 1) { continue; // a^t mod p = 1 } bool result = false; for (int j = 0; j < s; j++) { if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1 result = true; break; } b = (b * b) % bi; } if (result == false) { return(false); } } return(true); }
public static bool SmallPrimeSppTest (BigInteger bi, ConfidenceFactor confidence) { int Rounds = GetSPPRounds (bi, confidence); // calculate values of s and t BigInteger p_sub1 = bi - 1; int s = p_sub1.LowestSetBit (); BigInteger t = p_sub1 >> s; BigInteger.ModulusRing mr = new BigInteger.ModulusRing (bi); for (int round = 0; round < Rounds; round++) { BigInteger b = mr.Pow (BigInteger.smallPrimes [round], t); if (b == 1) continue; // a^t mod p = 1 bool result = false; for (int j = 0; j < s; j++) { if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1 result = true; break; } b = (b * b) % bi; } if (result == false) return false; } return true; }
/// <summary> /// Probabilistic prime test based on Rabin-Miller's test /// </summary> /// <param name="n" type="BigInteger.BigInteger"> /// <para> /// The number to test. /// </para> /// </param> /// <param name="confidence" type="int"> /// <para> /// The number of chosen bases. The test has at least a /// 1/4^confidence chance of falsely returning True. /// </para> /// </param> /// <returns> /// <para> /// True if "this" is a strong pseudoprime to randomly chosen bases. /// </para> /// <para> /// False if "this" is definitely NOT prime. /// </para> /// </returns> public static bool RabinMillerTest (BigInteger n, ConfidenceFactor confidence) { int bits = n.BitCount (); int t = GetSPPRounds (bits, confidence); // n - 1 == 2^s * r, r is odd BigInteger n_minus_1 = n - 1; int s = n_minus_1.LowestSetBit (); BigInteger r = n_minus_1 >> s; BigInteger.ModulusRing mr = new BigInteger.ModulusRing (n); // Applying optimization from HAC section 4.50 (base == 2) // not a really random base but an interesting (and speedy) one BigInteger y = null; // FIXME - optimization disable for small primes due to bug #81857 if (n.BitCount () > 100) y = mr.Pow (2, r); // still here ? start at round 1 (round 0 was a == 2) for (int round = 0; round < t; round++) { if ((round > 0) || (y == null)) { BigInteger a = null; // check for 2 <= a <= n - 2 // ...but we already did a == 2 previously as an optimization do { a = BigInteger.GenerateRandom (bits); } while ((a <= 2) && (a >= n_minus_1)); y = mr.Pow (a, r); } if (y == 1) continue; for (int j = 0; ((j < s) && (y != n_minus_1)); j++) { y = mr.Pow (y, 2); if (y == 1) return false; } if (y != n_minus_1) return false; } return true; }
/// <summary> /// Probabilistic prime test based on Rabin-Miller's test /// </summary> /// <param name="bi" type="BigInteger.BigInteger"> /// <para> /// The number to test. /// </para> /// </param> /// <param name="confidence" type="int"> /// <para> /// The number of chosen bases. The test has at least a /// 1/4^confidence chance of falsely returning True. /// </para> /// </param> /// <returns> /// <para> /// True if "this" is a strong pseudoprime to randomly chosen bases. /// </para> /// <para> /// False if "this" is definitely NOT prime. /// </para> /// </returns> public static bool RabinMillerTest (BigInteger bi, ConfidenceFactor confidence) { int Rounds = GetSPPRounds (bi, confidence); // calculate values of s and t BigInteger p_sub1 = bi - 1; int s = p_sub1.LowestSetBit (); BigInteger t = p_sub1 >> s; int bits = bi.BitCount (); BigInteger a = null; BigInteger.ModulusRing mr = new BigInteger.ModulusRing (bi); // Applying optimization from HAC section 4.50 (base == 2) // not a really random base but an interesting (and speedy) one BigInteger b = mr.Pow (2, t); if (b != 1) { bool result = false; for (int j=0; j < s; j++) { if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1 result = true; break; } b = (b * b) % bi; } if (!result) return false; } // still here ? start at round 1 (round 0 was a == 2) for (int round = 1; round < Rounds; round++) { while (true) { // generate a < n a = BigInteger.GenerateRandom (bits); // make sure "a" is not 0 (and not 2 as we have already tested that) if (a > 2 && a < bi) break; } if (a.GCD (bi) != 1) return false; b = mr.Pow (a, t); if (b == 1) continue; // a^t mod p = 1 bool result = false; for (int j = 0; j < s; j++) { if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1 result = true; break; } b = (b * b) % bi; } if (!result) return false; } return true; }
public void ApowBmodN() { Expect(mr.Pow(A, B), aPowBmodN); }
/// <summary> /// Probabilistic prime test based on Rabin-Miller's test /// </summary> /// <param name="bi" type="BigInteger.BigInteger"> /// <para> /// The number to test. /// </para> /// </param> /// <param name="confidence" type="int"> /// <para> /// The number of chosen bases. The test has at least a /// 1/4^confidence chance of falsely returning True. /// </para> /// </param> /// <returns> /// <para> /// True if "this" is a strong pseudoprime to randomly chosen bases. /// </para> /// <para> /// False if "this" is definitely NOT prime. /// </para> /// </returns> public static bool RabinMillerTest(BigInteger bi, ConfidenceFactor confidence) { int Rounds = GetSPPRounds(bi, confidence); // calculate values of s and t BigInteger p_sub1 = bi - 1; int s = p_sub1.LowestSetBit(); BigInteger t = p_sub1 >> s; int bits = bi.BitCount(); BigInteger a = null; BigInteger.ModulusRing mr = new BigInteger.ModulusRing(bi); // Applying optimization from HAC section 4.50 (base == 2) // not a really random base but an interesting (and speedy) one BigInteger b = mr.Pow(2, t); if (b != 1) { bool result = false; for (int j = 0; j < s; j++) { if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1 result = true; break; } b = b * b % bi; } if (!result) { return(false); } } // still here ? start at round 1 (round 0 was a == 2) for (int round = 1; round < Rounds; round++) { while (true) { // generate a < n a = BigInteger.GenerateRandom(bits); // make sure "a" is not 0 (and not 2 as we have already tested that) if (a > 2 && a < bi) { break; } } if (a.GCD(bi) != 1) { return(false); } b = mr.Pow(a, t); if (b == 1) { continue; // a^t mod p = 1 } bool result = false; for (int j = 0; j < s; j++) { if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1 result = true; break; } b = b * b % bi; } if (!result) { return(false); } } return(true); }