public void NaiveTest() { const long value = 16769023; var isPrime = PrimalityTest.Naive(value); Assert.IsTrue(isPrime, "Naive Primality Test has an incorrect result"); }
public void FermatTest() { const long value = 16769023; var isPrime = PrimalityTest.Fermat(value); Assert.IsTrue(isPrime, "Fermat Primality Test has an incorrect result"); }
private long GetRandomPrime(uint[] primeNumbers, Random random) { var index = random.Next(2, primeNumbers.Length - 1); var result = primeNumbers[index]; if (!PrimalityTest.Fermat(result)) throw new InvalidOperationException($"The number is not prime: {result}"); return result; }
public void PrimeTestFirst10000000ItemsTest() { var result = SieveAtkin.GetPrimeNumbers(limit: 10000000).ToArray(); var valid = true; for (int i = 0; i < result.Length; i++) { if (!PrimalityTest.Fermat(result[i])) { valid = false; break; } } Assert.IsTrue(valid, "Not prime found"); }
public virtual BigInteger GenerateNewPrime(int bits, object Context) { // // STEP 1. Find a place to do a sequential search // BigInteger curVal = GenerateSearchBase(bits, Context); const uint primeProd1 = 3u * 5u * 7u * 11u * 13u * 17u * 19u * 23u * 29u; uint pMod1 = curVal % primeProd1; int DivisionBound = TrialDivisionBounds; uint[] SmallPrimes = BigInteger.smallPrimes; PrimalityTest PostTrialDivisionTest = this.PrimalityTest; // // STEP 2. Search for primes // while (true) { // // STEP 2.1 Sieve out numbers divisible by the first 9 primes // if (pMod1 % 3 == 0) { goto biNotPrime; } if (pMod1 % 5 == 0) { goto biNotPrime; } if (pMod1 % 7 == 0) { goto biNotPrime; } if (pMod1 % 11 == 0) { goto biNotPrime; } if (pMod1 % 13 == 0) { goto biNotPrime; } if (pMod1 % 17 == 0) { goto biNotPrime; } if (pMod1 % 19 == 0) { goto biNotPrime; } if (pMod1 % 23 == 0) { goto biNotPrime; } if (pMod1 % 29 == 0) { goto biNotPrime; } // // STEP 2.2 Sieve out all numbers divisible by the primes <= DivisionBound // for (int p = 9; p < SmallPrimes.Length && SmallPrimes[p] <= DivisionBound; p++) { if (curVal % SmallPrimes[p] == 0) { goto biNotPrime; } } // // STEP 2.3 Is the potential prime acceptable? // if (!IsPrimeAcceptable(curVal, Context)) { goto biNotPrime; } // // STEP 2.4 Filter out all primes that pass this step with a primality test // if (PrimalityTest(curVal, Confidence)) { return(curVal); } // // STEP 2.4 // biNotPrime: pMod1 += 2; if (pMod1 >= primeProd1) { pMod1 -= primeProd1; } curVal.Incr2(); } }
public RSA(PrimalityTest primalityTest, int securityToken) { this.primalityTestToUse = primalityTest; this.securityToken = securityToken; }
private void button1_Click(object sender, EventArgs e) { if (radioButton1.Checked) { primalityTest = PrimalityTests.IsProbablyPrimeFermatTest; } if (radioButton2.Checked) { primalityTest = PrimalityTests.IsProbablyPrimeSolovayStrassenTest; } if (radioButton3.Checked) { primalityTest = PrimalityTests.IsProbablyPrimeRabinMillerTest; } BigInteger n, a; label2.Text = ""; try { n = BigInteger.Parse(textBox1.Text); if (n == 0) { return; } if (n <= 3) { label2.Text = String.Format("Число {0} простое\n", n); return; } RandomBigInteger randomBigInteger = new RandomBigInteger(); bool isPrime = true; for (int i = 0; i < 5; i++) { a = randomBigInteger.Next(2, n - 1); isPrime &= primalityTest(n, a); } if (isPrime) { label2.Text = String.Format("Число {0} вероятно простое\n", n); } else { label2.Text = String.Format("Число {0} составное", n); } } catch (ArgumentOutOfRangeException aoorExc) { label2.Text = aoorExc.Message; } catch (Exception exc) { Console.WriteLine(exc); } }