-
Notifications
You must be signed in to change notification settings - Fork 0
/
SequentialPrime.cs
164 lines (138 loc) · 5.16 KB
/
SequentialPrime.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
/*
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;
namespace FiatShamirIdentification
{
/// <summary>
/// Utility for prime numbers.
/// sequential version.
/// for internal use.
/// </summary>
internal class SequentialPrime : IPrime
{
private readonly byte[] _buffer; //used for random number generation
private readonly Random _generator;
private readonly uint _precision; //precision of Miller-Rabin primality test
/// <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>
public SequentialPrime(int seed, uint precision = 20, uint wordSize = 128)
{
if (precision < 5 || wordSize < 8)
throw new ArgumentException("precision < 5 or wordSize < 8");
_precision = precision;
_generator = new Random(seed);
_buffer = new byte[wordSize];
}
/// <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>
public SequentialPrime(uint precision = 20, uint wordSize = 128)
{
if (precision < 5 || wordSize < 8)
throw new ArgumentException("precision < 5 or wordSize < 8");
_precision = precision;
_generator = new Random();
_buffer = new byte[wordSize];
}
/// <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);
}
/// <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++;
while (!MRtest(ref number)) //test primality
number += 2;
return number;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool MRpredicate1(ref BigInteger y, ref BigInteger z, ref BigInteger number)
{
return BigInteger.ModPow(y, z, number) == 1;
}
private static bool MRpredicate2(ref BigInteger y, ref BigInteger number, ref BigInteger z, uint w)
{
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)
{
MRscomposition(ref number, out var w, out var z);
var ris = true;
uint i = 0;
while (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;
}
}
}
}