C# 中的 MillerRabin 素性测试

Dag*_*ago 4 c# biginteger primality-test

欢迎。我正在尝试实施 MillerRabin 测试来检查给定的大数是否为素数。这是我的代码:

 public static bool MillerRabinTest(BigInteger number)
        {

            BigInteger d;
            var n = number - 1;
            var s = FindK(n, out d);

            BigInteger a = 2;
            BigInteger y = Calc(a, d, number);  //a^d mod number
            if (y != BigInteger.One && y != n)
            {
                for (var r = 1; r <= s - 1; r++)
                {
                    y = Calc(y, 2, number);
                    if (y == 1)
                        return false;  
                }

                if (y != n)
                    return false;
            }
            return true; //it is probably prime
        }
Run Code Online (Sandbox Code Playgroud)

它适用于小型 Bigintegers。但是如果我的程序需要计算包含超过 16 位的数字,程序就会冻结。例如,在成功检查数字是否为素数后,程序突然没有响应。我不明白这怎么可能。如果它检查了一个大数字,再次检查另一个应该没有问题。甚至调试器也没有帮助,因为 step options消失了。如果需要,我可以分享更多的函数代码。以上功能适用于小数字。

编辑。更改 BigInteger.ModPow 的模函数有帮助。不幸的是,现在对于更大的数字,超过 3000 位,它永远不会返回质数,这是不可能的。或者真的很难找到原始数字?

Dmi*_*nko 8

好吧,在我的工作站(Core i5 3.2GHz,IA64 .Net 4.5)上测试是否为质数需要大约5 秒的时间等于2**3000

  public static class PrimeExtensions {
    // Random generator (thread safe)
    private static ThreadLocal<Random> s_Gen = new ThreadLocal<Random>(
      () => {
        return new Random();
      }
    );

    // Random generator (thread safe)
    private static Random Gen {
      get {
        return s_Gen.Value;
      }
    }

    public static Boolean IsProbablyPrime(this BigInteger value, int witnesses = 10) {
      if (value <= 1)
        return false;

      if (witnesses <= 0)
        witnesses = 10;

      BigInteger d = value - 1;
      int s = 0;

      while (d % 2 == 0) {
        d /= 2;
        s += 1;
      }

      Byte[] bytes = new Byte[value.ToByteArray().LongLength];
      BigInteger a;

      for (int i = 0; i < witnesses; i++) {
        do {
          Gen.NextBytes(bytes);

          a = new BigInteger(bytes);
        }
        while (a < 2 || a >= value - 2);

        BigInteger x = BigInteger.ModPow(a, d, value);
        if (x == 1 || x == value - 1)
          continue;

        for (int r = 1; r < s; r++) {
          x = BigInteger.ModPow(x, 2, value);

          if (x == 1)
            return false;
          if (x == value - 1)
            break;
        }

        if (x != value - 1)
          return false;
      }

      return true;
    }
  }
Run Code Online (Sandbox Code Playgroud)

测试和基准

  BigInteger value = BigInteger.Pow(2, 3217) - 1; // Mersenne prime number (2.5e968)

  Stopwatch sw = new Stopwatch();

  sw.Start();

  Boolean isPrime = value.IsProbablyPrime(10);

  sw.Stop();

  Console.Write(isPrime ? "probably prime" : "not prime");
  Console.WriteLine();
  Console.Write(sw.ElapsedMilliseconds);
Run Code Online (Sandbox Code Playgroud)