2 // Mono.Math.Prime.PrimalityTests.cs - Test for primality
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.
35 namespace Mono.Math.Prime {
42 delegate bool PrimalityTest (BigInteger bi, ConfidenceFactor confidence);
49 sealed class PrimalityTests {
51 private PrimalityTests ()
57 private static int GetSPPRounds (BigInteger bi, ConfidenceFactor confidence)
59 int bc = bi.BitCount();
63 // Data from HAC, 4.49
64 if (bc <= 100 ) Rounds = 27;
65 else if (bc <= 150 ) Rounds = 18;
66 else if (bc <= 200 ) Rounds = 15;
67 else if (bc <= 250 ) Rounds = 12;
68 else if (bc <= 300 ) Rounds = 9;
69 else if (bc <= 350 ) Rounds = 8;
70 else if (bc <= 400 ) Rounds = 7;
71 else if (bc <= 500 ) Rounds = 6;
72 else if (bc <= 600 ) Rounds = 5;
73 else if (bc <= 800 ) Rounds = 4;
74 else if (bc <= 1250) Rounds = 3;
78 case ConfidenceFactor.ExtraLow:
80 return Rounds != 0 ? Rounds : 1;
81 case ConfidenceFactor.Low:
83 return Rounds != 0 ? Rounds : 1;
84 case ConfidenceFactor.Medium:
86 case ConfidenceFactor.High:
88 case ConfidenceFactor.ExtraHigh:
90 case ConfidenceFactor.Provable:
91 throw new Exception ("The Rabin-Miller test can not be executed in a way such that its results are provable");
93 throw new ArgumentOutOfRangeException ("confidence");
98 /// Probabilistic prime test based on Rabin-Miller's test
100 /// <param name="bi" type="BigInteger.BigInteger">
102 /// The number to test.
105 /// <param name="confidence" type="int">
107 /// The number of chosen bases. The test has at least a
108 /// 1/4^confidence chance of falsely returning True.
113 /// True if "this" is a strong pseudoprime to randomly chosen bases.
116 /// False if "this" is definitely NOT prime.
119 public static bool RabinMillerTest (BigInteger bi, ConfidenceFactor confidence)
121 int Rounds = GetSPPRounds (bi, confidence);
123 // calculate values of s and t
124 BigInteger p_sub1 = bi - 1;
125 int s = p_sub1.LowestSetBit ();
127 BigInteger t = p_sub1 >> s;
129 int bits = bi.BitCount ();
131 BigInteger.ModulusRing mr = new BigInteger.ModulusRing (bi);
133 // Applying optimization from HAC section 4.50 (base == 2)
134 // not a really random base but an interesting (and speedy) one
135 BigInteger b = mr.Pow (2, t);
138 for (int j=0; j < s; j++) {
139 if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
150 // still here ? start at round 1 (round 0 was a == 2)
151 for (int round = 1; round < Rounds; round++) {
152 while (true) { // generate a < n
153 a = BigInteger.GenerateRandom (bits);
155 // make sure "a" is not 0 (and not 2 as we have already tested that)
166 continue; // a^t mod p = 1
169 for (int j = 0; j < s; j++) {
171 if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
185 public static bool SmallPrimeSppTest (BigInteger bi, ConfidenceFactor confidence)
187 int Rounds = GetSPPRounds (bi, confidence);
189 // calculate values of s and t
190 BigInteger p_sub1 = bi - 1;
191 int s = p_sub1.LowestSetBit ();
193 BigInteger t = p_sub1 >> s;
196 BigInteger.ModulusRing mr = new BigInteger.ModulusRing (bi);
198 for (int round = 0; round < Rounds; round++) {
200 BigInteger b = mr.Pow (BigInteger.smallPrimes [round], t);
202 if (b == 1) continue; // a^t mod p = 1
205 for (int j = 0; j < s; j++) {
207 if (b == p_sub1) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
223 // TODO: Implement the Lucus test
224 // TODO: Implement other new primality tests
225 // TODO: Implement primality proving