scripts
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime
|
||||
{
|
||||
// Token: 0x02000009 RID: 9
|
||||
public enum ConfidenceFactor
|
||||
{
|
||||
// Token: 0x0400002B RID: 43
|
||||
ExtraLow,
|
||||
// Token: 0x0400002C RID: 44
|
||||
Low,
|
||||
// Token: 0x0400002D RID: 45
|
||||
Medium,
|
||||
// Token: 0x0400002E RID: 46
|
||||
High,
|
||||
// Token: 0x0400002F RID: 47
|
||||
ExtraHigh,
|
||||
// Token: 0x04000030 RID: 48
|
||||
Provable
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime.Generator
|
||||
{
|
||||
// Token: 0x0200000B RID: 11
|
||||
public class NextPrimeFinder : SequentialSearchPrimeGeneratorBase
|
||||
{
|
||||
// Token: 0x0600006D RID: 109 RVA: 0x00004AD4 File Offset: 0x00002CD4
|
||||
protected override BigInteger GenerateSearchBase(int bits, object Context)
|
||||
{
|
||||
if (Context == null)
|
||||
{
|
||||
throw new ArgumentNullException("Context");
|
||||
}
|
||||
BigInteger bigInteger = new BigInteger((BigInteger)Context);
|
||||
bigInteger.SetBit(0U);
|
||||
return bigInteger;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime.Generator
|
||||
{
|
||||
// Token: 0x0200000C RID: 12
|
||||
public abstract class PrimeGeneratorBase
|
||||
{
|
||||
// Token: 0x17000002 RID: 2
|
||||
// (get) Token: 0x0600006F RID: 111 RVA: 0x00004B10 File Offset: 0x00002D10
|
||||
public virtual ConfidenceFactor Confidence
|
||||
{
|
||||
get
|
||||
{
|
||||
return ConfidenceFactor.Medium;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000003 RID: 3
|
||||
// (get) Token: 0x06000070 RID: 112 RVA: 0x00004B14 File Offset: 0x00002D14
|
||||
public virtual PrimalityTest PrimalityTest
|
||||
{
|
||||
get
|
||||
{
|
||||
return new PrimalityTest(PrimalityTests.RabinMillerTest);
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x17000004 RID: 4
|
||||
// (get) Token: 0x06000071 RID: 113 RVA: 0x00004B24 File Offset: 0x00002D24
|
||||
public virtual int TrialDivisionBounds
|
||||
{
|
||||
get
|
||||
{
|
||||
return 4000;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000072 RID: 114 RVA: 0x00004B2C File Offset: 0x00002D2C
|
||||
protected bool PostTrialDivisionTests(BigInteger bi)
|
||||
{
|
||||
return this.PrimalityTest(bi, this.Confidence);
|
||||
}
|
||||
|
||||
// Token: 0x06000073 RID: 115
|
||||
public abstract BigInteger GenerateNewPrime(int bits);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime.Generator
|
||||
{
|
||||
// Token: 0x0200000D RID: 13
|
||||
public class SequentialSearchPrimeGeneratorBase : PrimeGeneratorBase
|
||||
{
|
||||
// Token: 0x06000075 RID: 117 RVA: 0x00004B48 File Offset: 0x00002D48
|
||||
protected virtual BigInteger GenerateSearchBase(int bits, object context)
|
||||
{
|
||||
BigInteger bigInteger = BigInteger.GenerateRandom(bits);
|
||||
bigInteger.SetBit(0U);
|
||||
return bigInteger;
|
||||
}
|
||||
|
||||
// Token: 0x06000076 RID: 118 RVA: 0x00004B64 File Offset: 0x00002D64
|
||||
public override BigInteger GenerateNewPrime(int bits)
|
||||
{
|
||||
return this.GenerateNewPrime(bits, null);
|
||||
}
|
||||
|
||||
// Token: 0x06000077 RID: 119 RVA: 0x00004B70 File Offset: 0x00002D70
|
||||
public virtual BigInteger GenerateNewPrime(int bits, object context)
|
||||
{
|
||||
BigInteger bigInteger = this.GenerateSearchBase(bits, context);
|
||||
uint num = bigInteger % 3234846615U;
|
||||
int trialDivisionBounds = this.TrialDivisionBounds;
|
||||
uint[] smallPrimes = BigInteger.smallPrimes;
|
||||
for (;;)
|
||||
{
|
||||
if (num % 3U != 0U)
|
||||
{
|
||||
if (num % 5U != 0U)
|
||||
{
|
||||
if (num % 7U != 0U)
|
||||
{
|
||||
if (num % 11U != 0U)
|
||||
{
|
||||
if (num % 13U != 0U)
|
||||
{
|
||||
if (num % 17U != 0U)
|
||||
{
|
||||
if (num % 19U != 0U)
|
||||
{
|
||||
if (num % 23U != 0U)
|
||||
{
|
||||
if (num % 29U != 0U)
|
||||
{
|
||||
int num2 = 10;
|
||||
while (num2 < smallPrimes.Length && (ulong)smallPrimes[num2] <= (ulong)((long)trialDivisionBounds))
|
||||
{
|
||||
if (bigInteger % smallPrimes[num2] == 0U)
|
||||
{
|
||||
goto IL_105;
|
||||
}
|
||||
num2++;
|
||||
}
|
||||
if (this.IsPrimeAcceptable(bigInteger, context))
|
||||
{
|
||||
if (this.PrimalityTest(bigInteger, this.Confidence))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
IL_105:
|
||||
num += 2U;
|
||||
if (num >= 3234846615U)
|
||||
{
|
||||
num -= 3234846615U;
|
||||
}
|
||||
bigInteger.Incr2();
|
||||
}
|
||||
return bigInteger;
|
||||
}
|
||||
|
||||
// Token: 0x06000078 RID: 120 RVA: 0x00004CA4 File Offset: 0x00002EA4
|
||||
protected virtual bool IsPrimeAcceptable(BigInteger bi, object context)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime
|
||||
{
|
||||
// Token: 0x020000CA RID: 202
|
||||
// (Invoke) Token: 0x0600070D RID: 1805
|
||||
public delegate bool PrimalityTest(BigInteger bi, ConfidenceFactor confidence);
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime
|
||||
{
|
||||
// Token: 0x0200000A RID: 10
|
||||
public sealed class PrimalityTests
|
||||
{
|
||||
// Token: 0x06000067 RID: 103 RVA: 0x00004774 File Offset: 0x00002974
|
||||
private PrimalityTests()
|
||||
{
|
||||
}
|
||||
|
||||
// Token: 0x06000068 RID: 104 RVA: 0x0000477C File Offset: 0x0000297C
|
||||
private static int GetSPPRounds(BigInteger bi, ConfidenceFactor confidence)
|
||||
{
|
||||
int num = bi.BitCount();
|
||||
int num2;
|
||||
if (num <= 100)
|
||||
{
|
||||
num2 = 27;
|
||||
}
|
||||
else if (num <= 150)
|
||||
{
|
||||
num2 = 18;
|
||||
}
|
||||
else if (num <= 200)
|
||||
{
|
||||
num2 = 15;
|
||||
}
|
||||
else if (num <= 250)
|
||||
{
|
||||
num2 = 12;
|
||||
}
|
||||
else if (num <= 300)
|
||||
{
|
||||
num2 = 9;
|
||||
}
|
||||
else if (num <= 350)
|
||||
{
|
||||
num2 = 8;
|
||||
}
|
||||
else if (num <= 400)
|
||||
{
|
||||
num2 = 7;
|
||||
}
|
||||
else if (num <= 500)
|
||||
{
|
||||
num2 = 6;
|
||||
}
|
||||
else if (num <= 600)
|
||||
{
|
||||
num2 = 5;
|
||||
}
|
||||
else if (num <= 800)
|
||||
{
|
||||
num2 = 4;
|
||||
}
|
||||
else if (num <= 1250)
|
||||
{
|
||||
num2 = 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
num2 = 2;
|
||||
}
|
||||
switch (confidence)
|
||||
{
|
||||
case ConfidenceFactor.ExtraLow:
|
||||
num2 >>= 2;
|
||||
return (num2 == 0) ? 1 : num2;
|
||||
case ConfidenceFactor.Low:
|
||||
num2 >>= 1;
|
||||
return (num2 == 0) ? 1 : num2;
|
||||
case ConfidenceFactor.Medium:
|
||||
return num2;
|
||||
case ConfidenceFactor.High:
|
||||
return num2 << 1;
|
||||
case ConfidenceFactor.ExtraHigh:
|
||||
return num2 << 2;
|
||||
case ConfidenceFactor.Provable:
|
||||
throw new Exception("The Rabin-Miller test can not be executed in a way such that its results are provable");
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("confidence");
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000069 RID: 105 RVA: 0x000048C4 File Offset: 0x00002AC4
|
||||
public static bool Test(BigInteger n, ConfidenceFactor confidence)
|
||||
{
|
||||
if (n.BitCount() < 33)
|
||||
{
|
||||
return PrimalityTests.SmallPrimeSppTest(n, confidence);
|
||||
}
|
||||
return PrimalityTests.RabinMillerTest(n, confidence);
|
||||
}
|
||||
|
||||
// Token: 0x0600006A RID: 106 RVA: 0x000048E4 File Offset: 0x00002AE4
|
||||
public static bool RabinMillerTest(BigInteger n, ConfidenceFactor confidence)
|
||||
{
|
||||
int num = n.BitCount();
|
||||
int spprounds = PrimalityTests.GetSPPRounds(num, confidence);
|
||||
BigInteger bigInteger = n - 1;
|
||||
int num2 = bigInteger.LowestSetBit();
|
||||
BigInteger bigInteger2 = bigInteger >> num2;
|
||||
BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(n);
|
||||
BigInteger bigInteger3 = null;
|
||||
if (n.BitCount() > 100)
|
||||
{
|
||||
bigInteger3 = modulusRing.Pow(2U, bigInteger2);
|
||||
}
|
||||
for (int i = 0; i < spprounds; i++)
|
||||
{
|
||||
if (i > 0 || bigInteger3 == null)
|
||||
{
|
||||
BigInteger bigInteger4;
|
||||
do
|
||||
{
|
||||
bigInteger4 = BigInteger.GenerateRandom(num);
|
||||
}
|
||||
while (bigInteger4 <= 2 && bigInteger4 >= bigInteger);
|
||||
bigInteger3 = modulusRing.Pow(bigInteger4, bigInteger2);
|
||||
}
|
||||
if (!(bigInteger3 == 1U))
|
||||
{
|
||||
int num3 = 0;
|
||||
while (num3 < num2 && bigInteger3 != bigInteger)
|
||||
{
|
||||
bigInteger3 = modulusRing.Pow(bigInteger3, 2);
|
||||
if (bigInteger3 == 1U)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
num3++;
|
||||
}
|
||||
if (bigInteger3 != bigInteger)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Token: 0x0600006B RID: 107 RVA: 0x00004A10 File Offset: 0x00002C10
|
||||
public static bool SmallPrimeSppTest(BigInteger bi, ConfidenceFactor confidence)
|
||||
{
|
||||
int spprounds = PrimalityTests.GetSPPRounds(bi, confidence);
|
||||
BigInteger bigInteger = bi - 1;
|
||||
int num = bigInteger.LowestSetBit();
|
||||
BigInteger bigInteger2 = bigInteger >> num;
|
||||
BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(bi);
|
||||
for (int i = 0; i < spprounds; i++)
|
||||
{
|
||||
BigInteger bigInteger3 = modulusRing.Pow(BigInteger.smallPrimes[i], bigInteger2);
|
||||
if (!(bigInteger3 == 1U))
|
||||
{
|
||||
bool flag = false;
|
||||
for (int j = 0; j < num; j++)
|
||||
{
|
||||
if (bigInteger3 == bigInteger)
|
||||
{
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
bigInteger3 = bigInteger3 * bigInteger3 % bi;
|
||||
}
|
||||
if (!flag)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user