2 // Mono.Math.Prime.PrimalityTests.cs - Test for primality
7 // Copyright (c) 2003 Ben Maurer. All rights reserved
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 namespace Mono
.Math
.Prime
{
40 delegate bool PrimalityTest (BigInteger bi
, ConfidenceFactor confidence
);
47 sealed class PrimalityTests
{
49 private PrimalityTests ()
55 private static int GetSPPRounds (BigInteger bi
, ConfidenceFactor confidence
)
57 int bc
= bi
.BitCount();
61 // Data from HAC, 4.49
62 if (bc
<= 100 ) Rounds
= 27;
63 else if (bc
<= 150 ) Rounds
= 18;
64 else if (bc
<= 200 ) Rounds
= 15;
65 else if (bc
<= 250 ) Rounds
= 12;
66 else if (bc
<= 300 ) Rounds
= 9;
67 else if (bc
<= 350 ) Rounds
= 8;
68 else if (bc
<= 400 ) Rounds
= 7;
69 else if (bc
<= 500 ) Rounds
= 6;
70 else if (bc
<= 600 ) Rounds
= 5;
71 else if (bc
<= 800 ) Rounds
= 4;
72 else if (bc
<= 1250) Rounds
= 3;
76 case ConfidenceFactor
.ExtraLow
:
78 return Rounds
!= 0 ? Rounds
: 1;
79 case ConfidenceFactor
.Low
:
81 return Rounds
!= 0 ? Rounds
: 1;
82 case ConfidenceFactor
.Medium
:
84 case ConfidenceFactor
.High
:
86 case ConfidenceFactor
.ExtraHigh
:
88 case ConfidenceFactor
.Provable
:
89 throw new Exception ("The Rabin-Miller test can not be executed in a way such that its results are provable");
91 throw new ArgumentOutOfRangeException ("confidence");
96 /// Probabilistic prime test based on Rabin-Miller's test
98 /// <param name="bi" type="BigInteger.BigInteger">
100 /// The number to test.
103 /// <param name="confidence" type="int">
105 /// The number of chosen bases. The test has at least a
106 /// 1/4^confidence chance of falsely returning True.
111 /// True if "this" is a strong pseudoprime to randomly chosen bases.
114 /// False if "this" is definitely NOT prime.
117 public static bool RabinMillerTest (BigInteger bi
, ConfidenceFactor confidence
)
119 int Rounds
= GetSPPRounds (bi
, confidence
);
121 // calculate values of s and t
122 BigInteger p_sub1
= bi
- 1;
123 int s
= p_sub1
.LowestSetBit ();
125 BigInteger t
= p_sub1
>> s
;
127 int bits
= bi
.BitCount ();
129 BigInteger
.ModulusRing mr
= new BigInteger
.ModulusRing (bi
);
131 // Applying optimization from HAC section 4.50 (base == 2)
132 // not a really random base but an interesting (and speedy) one
133 BigInteger b
= mr
.Pow (2, t
);
136 for (int j
=0; j
< s
; j
++) {
137 if (b
== p_sub1
) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
148 // still here ? start at round 1 (round 0 was a == 2)
149 for (int round
= 1; round
< Rounds
; round
++) {
150 while (true) { // generate a < n
151 a
= BigInteger
.GenerateRandom (bits
);
153 // make sure "a" is not 0 (and not 2 as we have already tested that)
164 continue; // a^t mod p = 1
167 for (int j
= 0; j
< s
; j
++) {
169 if (b
== p_sub1
) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
183 public static bool SmallPrimeSppTest (BigInteger bi
, ConfidenceFactor confidence
)
185 int Rounds
= GetSPPRounds (bi
, confidence
);
187 // calculate values of s and t
188 BigInteger p_sub1
= bi
- 1;
189 int s
= p_sub1
.LowestSetBit ();
191 BigInteger t
= p_sub1
>> s
;
194 BigInteger
.ModulusRing mr
= new BigInteger
.ModulusRing (bi
);
196 for (int round
= 0; round
< Rounds
; round
++) {
198 BigInteger b
= mr
.Pow (BigInteger
.smallPrimes
[round
], t
);
200 if (b
== 1) continue; // a^t mod p = 1
203 for (int j
= 0; j
< s
; j
++) {
205 if (b
== p_sub1
) { // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
221 // TODO: Implement the Lucus test
222 // TODO: Implement other new primality tests
223 // TODO: Implement primality proving