scripts
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime
|
||||
{
|
||||
// Token: 0x02000097 RID: 151
|
||||
internal enum ConfidenceFactor
|
||||
{
|
||||
// Token: 0x040001AC RID: 428
|
||||
ExtraLow,
|
||||
// Token: 0x040001AD RID: 429
|
||||
Low,
|
||||
// Token: 0x040001AE RID: 430
|
||||
Medium,
|
||||
// Token: 0x040001AF RID: 431
|
||||
High,
|
||||
// Token: 0x040001B0 RID: 432
|
||||
ExtraHigh,
|
||||
// Token: 0x040001B1 RID: 433
|
||||
Provable
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime.Generator
|
||||
{
|
||||
// Token: 0x02000094 RID: 148
|
||||
internal class NextPrimeFinder : SequentialSearchPrimeGeneratorBase
|
||||
{
|
||||
// Token: 0x0600083E RID: 2110 RVA: 0x0001E2E0 File Offset: 0x0001C4E0
|
||||
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: 0x02000095 RID: 149
|
||||
internal abstract class PrimeGeneratorBase
|
||||
{
|
||||
// Token: 0x170000E3 RID: 227
|
||||
// (get) Token: 0x06000840 RID: 2112 RVA: 0x0001E31C File Offset: 0x0001C51C
|
||||
public virtual ConfidenceFactor Confidence
|
||||
{
|
||||
get
|
||||
{
|
||||
return ConfidenceFactor.Medium;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x170000E4 RID: 228
|
||||
// (get) Token: 0x06000841 RID: 2113 RVA: 0x0001E320 File Offset: 0x0001C520
|
||||
public virtual PrimalityTest PrimalityTest
|
||||
{
|
||||
get
|
||||
{
|
||||
return new PrimalityTest(PrimalityTests.RabinMillerTest);
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x170000E5 RID: 229
|
||||
// (get) Token: 0x06000842 RID: 2114 RVA: 0x0001E330 File Offset: 0x0001C530
|
||||
public virtual int TrialDivisionBounds
|
||||
{
|
||||
get
|
||||
{
|
||||
return 4000;
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x06000843 RID: 2115 RVA: 0x0001E338 File Offset: 0x0001C538
|
||||
protected bool PostTrialDivisionTests(BigInteger bi)
|
||||
{
|
||||
return this.PrimalityTest(bi, this.Confidence);
|
||||
}
|
||||
|
||||
// Token: 0x06000844 RID: 2116
|
||||
public abstract BigInteger GenerateNewPrime(int bits);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime.Generator
|
||||
{
|
||||
// Token: 0x02000096 RID: 150
|
||||
internal class SequentialSearchPrimeGeneratorBase : PrimeGeneratorBase
|
||||
{
|
||||
// Token: 0x06000846 RID: 2118 RVA: 0x0001E354 File Offset: 0x0001C554
|
||||
protected virtual BigInteger GenerateSearchBase(int bits, object context)
|
||||
{
|
||||
BigInteger bigInteger = BigInteger.GenerateRandom(bits);
|
||||
bigInteger.SetBit(0U);
|
||||
return bigInteger;
|
||||
}
|
||||
|
||||
// Token: 0x06000847 RID: 2119 RVA: 0x0001E370 File Offset: 0x0001C570
|
||||
public override BigInteger GenerateNewPrime(int bits)
|
||||
{
|
||||
return this.GenerateNewPrime(bits, null);
|
||||
}
|
||||
|
||||
// Token: 0x06000848 RID: 2120 RVA: 0x0001E37C File Offset: 0x0001C57C
|
||||
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: 0x06000849 RID: 2121 RVA: 0x0001E4B0 File Offset: 0x0001C6B0
|
||||
protected virtual bool IsPrimeAcceptable(BigInteger bi, object context)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime
|
||||
{
|
||||
// Token: 0x020006D2 RID: 1746
|
||||
// (Invoke) Token: 0x060041D0 RID: 16848
|
||||
internal delegate bool PrimalityTest(BigInteger bi, ConfidenceFactor confidence);
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
using System;
|
||||
|
||||
namespace Mono.Math.Prime
|
||||
{
|
||||
// Token: 0x02000098 RID: 152
|
||||
internal sealed class PrimalityTests
|
||||
{
|
||||
// Token: 0x0600084A RID: 2122 RVA: 0x0001E4B4 File Offset: 0x0001C6B4
|
||||
private PrimalityTests()
|
||||
{
|
||||
}
|
||||
|
||||
// Token: 0x0600084B RID: 2123 RVA: 0x0001E4BC File Offset: 0x0001C6BC
|
||||
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: 0x0600084C RID: 2124 RVA: 0x0001E604 File Offset: 0x0001C804
|
||||
public static bool Test(BigInteger n, ConfidenceFactor confidence)
|
||||
{
|
||||
if (n.BitCount() < 33)
|
||||
{
|
||||
return PrimalityTests.SmallPrimeSppTest(n, confidence);
|
||||
}
|
||||
return PrimalityTests.RabinMillerTest(n, confidence);
|
||||
}
|
||||
|
||||
// Token: 0x0600084D RID: 2125 RVA: 0x0001E624 File Offset: 0x0001C824
|
||||
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: 0x0600084E RID: 2126 RVA: 0x0001E750 File Offset: 0x0001C950
|
||||
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