Check platform without using #define
[mono.git] / mcs / class / Mono.Security / Mono.Math.Prime.Generator / SequentialSearchPrimeGeneratorBase.cs
1 //
2 // Mono.Math.Prime.Generator.SequentialSearchPrimeGeneratorBase.cs - Prime Generator
3 //
4 // Authors:
5 //      Ben Maurer
6 //
7 // Copyright (c) 2003 Ben Maurer. All rights reserved
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 namespace Mono.Math.Prime.Generator {
31
32 #if INSIDE_CORLIB
33         internal
34 #else
35         public
36 #endif
37         class SequentialSearchPrimeGeneratorBase : PrimeGeneratorBase {
38
39                 protected virtual BigInteger GenerateSearchBase (int bits, object context)
40                 {
41                         BigInteger ret = BigInteger.GenerateRandom (bits);
42                         ret.SetBit (0);
43                         return ret;
44                 }
45
46
47                 public override BigInteger GenerateNewPrime (int bits)
48                 {
49                         return GenerateNewPrime (bits, null);
50                 }
51
52
53                 public virtual BigInteger GenerateNewPrime (int bits, object context)
54                 {
55                         //
56                         // STEP 1. Find a place to do a sequential search
57                         //
58                         BigInteger curVal = GenerateSearchBase (bits, context);
59
60                         const uint primeProd1 = 3u* 5u * 7u * 11u * 13u * 17u * 19u * 23u * 29u;
61
62                         uint pMod1 = curVal % primeProd1;
63
64                         int DivisionBound = TrialDivisionBounds;
65                         uint[] SmallPrimes = BigInteger.smallPrimes;
66                         //
67                         // STEP 2. Search for primes
68                         //
69                         while (true) {
70
71                                 //
72                                 // STEP 2.1 Sieve out numbers divisible by the first 9 primes
73                                 //
74                                 if (pMod1 %  3 == 0) goto biNotPrime;
75                                 if (pMod1 %  5 == 0) goto biNotPrime;
76                                 if (pMod1 %  7 == 0) goto biNotPrime;
77                                 if (pMod1 % 11 == 0) goto biNotPrime;
78                                 if (pMod1 % 13 == 0) goto biNotPrime;
79                                 if (pMod1 % 17 == 0) goto biNotPrime;
80                                 if (pMod1 % 19 == 0) goto biNotPrime;
81                                 if (pMod1 % 23 == 0) goto biNotPrime;
82                                 if (pMod1 % 29 == 0) goto biNotPrime;
83
84                                 //
85                                 // STEP 2.2 Sieve out all numbers divisible by the primes <= DivisionBound
86                                 //
87                                 for (int p = 10; p < SmallPrimes.Length && SmallPrimes [p] <= DivisionBound; p++) {
88                                         if (curVal % SmallPrimes [p] == 0)
89                                                 goto biNotPrime;
90                                 }
91
92                                 //
93                                 // STEP 2.3 Is the potential prime acceptable?
94                                 //
95                                 if (!IsPrimeAcceptable (curVal, context))
96                                         goto biNotPrime;
97
98                                 //
99                                 // STEP 2.4 Filter out all primes that pass this step with a primality test
100                                 //
101                                 if (PrimalityTest (curVal, Confidence))
102                                         return curVal;
103
104                                 //
105                                 // STEP 2.4
106                                 //
107                         biNotPrime:
108                                 pMod1 += 2;
109                                 if (pMod1 >= primeProd1)
110                                         pMod1 -= primeProd1;
111                                 curVal.Incr2 ();
112                         }
113                 }
114
115                 protected virtual bool IsPrimeAcceptable (BigInteger bi, object context)
116                 {
117                         return true;
118                 }
119         }
120 }