Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Miller Rabin Primality test accuracy

I know the Miller–Rabin primality test is probabilistic. However I want to use it for a programming task that leaves no room for error.

Can we assume that it is correct with very high probability if the input numbers are 64-bit integers (i.e. long long in C)?

like image 239
user3717225 Avatar asked Jun 07 '14 10:06

user3717225


People also ask

How accurate is the Miller-Rabin test?

The Miller-Rabin Primality Test is significantly more accurate than the Fermat Primality Test. There exist an infinite number of composite integers known as Carmichael numbers, which satisfy the property that ∀n, where n is a Carmichael number, if (a, n) = 1, then an−1 ≡ 1 (mod n) [4].

What is the time complexity of Miller Rabin primality test?

The time complexity of the Miller-Rabin Primality Test is O ( K ∗ l o g 3 ( N ) O(K*log^3(N) O(K∗log3(N), where N is the number to be checked for primality, and K is the number of iterations performed for accuracy as per the requirement.

What is the fastest known deterministic method in the world for primality testing?

For large integers, the most efficient primality tests are pro- babilistic. However, for integers with a small fixed number of bits the best tests in practice are deterministic. Currently the best known tests of this type involve 3 rounds of the Miller-Rabin test for 32-bit integers and 7 rounds for 64-bit integers.

Does the number 561 pass the Miller-Rabin test?

Therefore 561 does not satisfy the Miller-Rabin test with a = 2, and hence is not prime. Thus our new test finds composite numbers which are missed by Fermat's test.


2 Answers

Miller–Rabin is indeed probabilistic, but you can trade accuracy for computation time arbitrarily. If the number you test is prime, it will always give the correct answer. The problematic case is when a number is composite, but is reported to be prime. We can bound the probability of this error using the formula on Wikipedia: If you select k different bases randomly and test them, the error probability is less than 4-k. So even with k = 9, you only get a 3 in a million chance of being wrong. And with k = 40 or so it becomes ridiculously unlikely.

That said, there is a deterministic version of Miller–Rabin, relying on the correctness of the generalized Riemann hypothesis. For the range u up to 264, it is enough to check a = 2, 3, 5, 7, 11, 13, 17, 19, 23. I have a C++ implementation online which was field-tested in lots of programming contests. Here's an instantiation of the template for unsigned 64-bit ints:

bool isprime(uint64_t n) { //determines if n is a prime number
    const int pn = 9, p[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23 };
    for (int i = 0; i < pn; ++i)
        if (n % p[i] == 0) return n == p[i];
    if (n < p[pn - 1]) return 0;
    uint64_t s = 0, t = n - 1;
    while (~t & 1)
        t >>= 1, ++s;
    for (int i = 0; i < pn; ++i) {
        uint64_t pt = PowerMod(p[i], t, n);
        if (pt == 1) continue;
        bool ok = 0;
        for (int j = 0; j < s && !ok; ++j) {
            if (pt == n - 1) ok = 1;
            pt = MultiplyMod(pt, pt, n);
        }
        if (!ok) return 0;
    }
    return 1;
}

PowerMod and MultiplyMod are just primitives to multiply and exponentiate under a given modulus, using square-and-{multiply,add}.

like image 132
Niklas B. Avatar answered Sep 21 '22 23:09

Niklas B.


For n < 2^64, it is possible to perform strong-pseudoprime tests to the seven bases 2, 325, 9375, 28178, 450775, 9780504, and 1795265022 and completely determine the primality of n; see http://miller-rabin.appspot.com/.

A faster primality test performs a strong-pseudoprime test to base 2 followed by a Lucas pseudoprime test. It takes about 3 times as long as a single strong-pseudoprime test, so is more than twice as fast as the 7-base Miller-Rabin test. The code is more complex, but not dauntingly so.

I can post code if you're interested; let me know in the comments.

like image 21
user448810 Avatar answered Sep 20 '22 23:09

user448810