コード例 #1
0
        public static bool SmallPrimeSppTest(BigInteger bi, ConfidenceFactor confidence)
        {
            int        sppRounds   = PrimalityTests.GetSPPRounds(bi, confidence);
            BigInteger bigInteger1 = bi - (BigInteger)1;
            int        num         = bigInteger1.LowestSetBit();
            BigInteger exp         = bigInteger1 >> num;

            BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(bi);
            for (int index1 = 0; index1 < sppRounds; ++index1)
            {
                BigInteger bigInteger2 = modulusRing.Pow(BigInteger.smallPrimes[index1], exp);
                if (!(bigInteger2 == 1U))
                {
                    bool flag = false;
                    for (int index2 = 0; index2 < num; ++index2)
                    {
                        if (bigInteger2 == bigInteger1)
                        {
                            flag = true;
                            break;
                        }
                        bigInteger2 = bigInteger2 * bigInteger2 % bi;
                    }
                    if (!flag)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
コード例 #2
0
        public static bool SmallPrimeSppTest(BigInteger bi, ConfidenceFactor confidence)
        {
            int        spprounds  = PrimalityTests.GetSPPRounds(bi, confidence);
            BigInteger bigInteger = bi - 1;
            int        num        = bigInteger.LowestSetBit();
            BigInteger exp        = bigInteger >> num;

            BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(bi);
            for (int i = 0; i < spprounds; i++)
            {
                BigInteger bigInteger2 = modulusRing.Pow(BigInteger.smallPrimes[i], exp);
                if (!(bigInteger2 == 1u))
                {
                    bool flag = false;
                    for (int j = 0; j < num; j++)
                    {
                        if (bigInteger2 == bigInteger)
                        {
                            flag = true;
                            break;
                        }
                        bigInteger2 = bigInteger2 * bigInteger2 % bi;
                    }
                    if (!flag)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
コード例 #3
0
 public static bool Test(BigInteger n, ConfidenceFactor confidence)
 {
     if (n.BitCount() < 33)
     {
         return(PrimalityTests.SmallPrimeSppTest(n, confidence));
     }
     return(PrimalityTests.RabinMillerTest(n, confidence));
 }
コード例 #4
0
        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);
        }
コード例 #5
0
        public static bool RabinMillerTest(BigInteger n, ConfidenceFactor confidence)
        {
            int        bits        = n.BitCount();
            int        sppRounds   = PrimalityTests.GetSPPRounds((BigInteger)bits, confidence);
            BigInteger bigInteger1 = n - (BigInteger)1;
            int        num         = bigInteger1.LowestSetBit();
            BigInteger bigInteger2 = bigInteger1 >> num;

            BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(n);
            BigInteger             a           = (BigInteger)null;

            if (n.BitCount() > 100)
            {
                a = modulusRing.Pow(2U, bigInteger2);
            }
            for (int index1 = 0; index1 < sppRounds; ++index1)
            {
                if (index1 > 0 || a == (BigInteger)null)
                {
                    BigInteger random;
                    do
                    {
                        random = BigInteger.GenerateRandom(bits);
                    }while (random <= (BigInteger)2 && random >= bigInteger1);
                    a = modulusRing.Pow(random, bigInteger2);
                }
                if (!(a == 1U))
                {
                    for (int index2 = 0; index2 < num && a != bigInteger1; ++index2)
                    {
                        a = modulusRing.Pow(a, (BigInteger)2);
                        if (a == 1U)
                        {
                            return(false);
                        }
                    }
                    if (a != bigInteger1)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }