-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimes.cs
More file actions
38 lines (35 loc) · 1.09 KB
/
Primes.cs
File metadata and controls
38 lines (35 loc) · 1.09 KB
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
using System;
using System.Numerics;
using System.Security.Cryptography;
namespace MetalWorker.Cryptography
{
/// <summary>
/// Class for working with primes
/// </summary>
public static class Primes
{
public static BigInteger GetRandom(RandomNumberGenerator rng, uint bytesCount)
{
//Certainty 4 gives
const int certainty = 4;
BigInteger upperBound = rng.NextUnsignedBigInteger(bytesCount, 3);
if (upperBound % 2 == 0)
upperBound--;
for (; upperBound >= 2; upperBound -= 2)
{
if (new MillerRabinPT().IsPrime(upperBound, certainty))
return upperBound;
}
throw new Exception("Primes not found");
}
public static BigInteger GetRandom(RandomNumberGenerator rng, uint bytesCount, BigInteger maxValue)
{
BigInteger result;
do
{
result = GetRandom(rng, bytesCount);
} while (result > maxValue);
return result;
}
}
}