-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelPrime.cs
199 lines (171 loc) · 6.53 KB
/
ParallelPrime.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
FiatShamirIdentification
Copyright 2015 Ivan Sarno
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//version V.2.2
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading;
namespace FiatShamirIdentification
{
/// <summary>
/// Utility for prime numbers.
/// parallel version.
/// for internal use.
/// </summary>
internal class ParallelPrime : IPrime
{
private readonly Random _generator;
private readonly uint _precision; //precision of Miller-Rabin primality test
private readonly uint _size;
private readonly int _threads;
private readonly AutoResetEvent _wait;
private bool _continue;
private BigInteger _current;
private int _pass;
/// <summary>
/// </summary>
/// <param name="seed">seed of random number generator</param>
/// <param name="precision">precision of Miller-Rabin test, error = 1/2^(2*precision)</param>
/// <param name="wordSize">length in bytes of number generated</param>
/// <param name="threads">number of threads to use</param>
public ParallelPrime(int seed, uint precision = 20, uint wordSize = 128, int threads = 2)
{
if (precision < 5 || wordSize < 8 || threads < 2)
throw new ArgumentException("precision < 5 or wordSize < 8 or threads < 2");
_precision = precision;
_generator = new Random(seed);
_size = wordSize;
_wait = new AutoResetEvent(false);
_threads = threads;
}
/// <summary>
/// </summary>
/// <param name="precision">precision of Miller-Rabin test, error = 1/2^(2*precision)</param>
/// <param name="wordSize">length in bytes of number generated</param>
/// <param name="threads">number of threads to use</param>
public ParallelPrime(uint precision = 20, uint wordSize = 128, int threads = 2)
{
if (precision < 5 || wordSize < 8 || threads < 2)
throw new ArgumentException("precision < 5 or wordSize < 8 or threads < 2");
_precision = precision;
_generator = new Random();
_size = wordSize;
_wait = new AutoResetEvent(false);
_threads = threads;
}
/// <summary>
/// Primality test.
/// </summary>
/// <param name="number">number to test</param>
/// <returns>true if number is prime</returns>
public bool IsPrime(ref BigInteger number)
{
if (number == 2)
return true;
if (number.IsEven)
return false;
return number > 2 && MRtest(ref number, new byte[_size], _generator);
}
/// <summary>
/// Return the first prime number following the argument.
/// </summary>
/// <param name="number">current number</param>
/// <returns>next prime number</returns>
public BigInteger NextPrime(BigInteger number)
{
if (number < 2)
return 2;
if (number.IsEven)
number++;
_current = number;
_continue = true;
_pass = 1;
for (var i = 0; i < _threads; i++) ThreadPool.QueueUserWorkItem(Routine, i);
_wait.WaitOne();
return _current;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool MRpredicate1(ref BigInteger y, ref BigInteger z, ref BigInteger number)
{
return _continue && BigInteger.ModPow(y, z, number) == 1;
}
private bool MRpredicate2(ref BigInteger y, ref BigInteger number, ref BigInteger z, uint w)
{
if (!_continue)
return false;
uint i = 0;
BigInteger pow2 = 1;
var cond = BigInteger.ModPow(y, z, number) == number - 1;
while (!cond && i < w)
{
i++;
pow2 <<= 1;
cond = BigInteger.ModPow(y, pow2 * z, number) == number - 1;
}
return i != w;
}
private bool MRtest(ref BigInteger number, byte[] buffer, Random generator)
{
MRscomposition(ref number, out var w, out var z);
var ris = true;
uint i = 0;
while (_continue && ris && i < _precision)
{
//extract a random number
generator.NextBytes(buffer);
buffer[buffer.Length - 1] &= 127; //forces a positive number
var y = new BigInteger(buffer);
////
y = y % number;
while (y < 2) //avoids extraction of 0 and 1
{
y += generator.Next();
y = y % number;
}
//test
ris = BigInteger.GreatestCommonDivisor(y, number) == 1 &&
(MRpredicate1(ref y, ref z, ref number) || MRpredicate2(ref y, ref number, ref z, w));
i++;
}
return ris;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void MRscomposition(ref BigInteger number, out uint w, out BigInteger z)
{
z = number - 1;
w = 0;
while (z.IsEven)
{
w++;
z >>= 1;
}
}
//thread's routine
private void Routine(object threadId)
{
var id = (int) threadId;
var increment = _threads * 2;
var number = _current + 2 * id;
var buffer = new byte[_size];
var generator = new Random(_generator.Next()); //local generator
while (_continue && !MRtest(ref number, buffer, generator))
number += increment;
var pass = Interlocked.CompareExchange(ref _pass, 0, 1);
if (pass == 0) return;
_continue = false;
_current = number;
_wait.Set();
}
}
}