2 // Mono.Math.Prime.Generator.SequentialSearchPrimeGeneratorBase.cs - Prime Generator
7 // Copyright (c) 2003 Ben Maurer. All rights reserved
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using Mono.Math.Prime;
36 namespace Mono.Math.Prime.Generator {
43 class SequentialSearchPrimeGeneratorBase : PrimeGeneratorBase {
45 protected virtual BigInteger GenerateSearchBase (int bits, object context)
47 BigInteger ret = BigInteger.GenerateRandom (bits);
53 public override BigInteger GenerateNewPrime (int bits)
55 return GenerateNewPrime (bits, null);
59 public virtual BigInteger GenerateNewPrime (int bits, object context)
62 // STEP 1. Find a place to do a sequential search
64 BigInteger curVal = GenerateSearchBase (bits, context);
66 const uint primeProd1 = 3u* 5u * 7u * 11u * 13u * 17u * 19u * 23u * 29u;
68 uint pMod1 = curVal % primeProd1;
70 int DivisionBound = TrialDivisionBounds;
71 uint[] SmallPrimes = BigInteger.smallPrimes;
72 PrimalityTest PostTrialDivisionTest = this.PrimalityTest;
74 // STEP 2. Search for primes
79 // STEP 2.1 Sieve out numbers divisible by the first 9 primes
81 if (pMod1 % 3 == 0) goto biNotPrime;
82 if (pMod1 % 5 == 0) goto biNotPrime;
83 if (pMod1 % 7 == 0) goto biNotPrime;
84 if (pMod1 % 11 == 0) goto biNotPrime;
85 if (pMod1 % 13 == 0) goto biNotPrime;
86 if (pMod1 % 17 == 0) goto biNotPrime;
87 if (pMod1 % 19 == 0) goto biNotPrime;
88 if (pMod1 % 23 == 0) goto biNotPrime;
89 if (pMod1 % 29 == 0) goto biNotPrime;
92 // STEP 2.2 Sieve out all numbers divisible by the primes <= DivisionBound
94 for (int p = 10; p < SmallPrimes.Length && SmallPrimes [p] <= DivisionBound; p++) {
95 if (curVal % SmallPrimes [p] == 0)
100 // STEP 2.3 Is the potential prime acceptable?
102 if (!IsPrimeAcceptable (curVal, context))
106 // STEP 2.4 Filter out all primes that pass this step with a primality test
108 if (PrimalityTest (curVal, Confidence))
116 if (pMod1 >= primeProd1)
122 protected virtual bool IsPrimeAcceptable (BigInteger bi, object context)