Overview
Primality testing is a significant problem in mathematics and computer science, playing a crucial role in fields like cryptography. Determining whether a large number is prime is computationally expensive with traditional methods, making efficient algorithms essential.
The Miller-Rabin Primality Test is a probabilistic algorithm that can quickly test the primality of large numbers. For certain ranges, it can even be used in a deterministic manner. This algorithm has a time complexity of and is considered important both theoretically and practically.
Background: Fermat’s Little Theorem and Its Limitations
The Miller-Rabin algorithm builds upon Fermat’s Little Theorem.
Fermat's Little Theorem
For a prime number and an integer that is coprime to ,
Using Fermat’s Little Theorem, we can infer the following:
If , then is not prime.
However, there exist composite numbers that satisfy Fermat’s Little Theorem. These are called Carmichael numbers, such as . This means that Fermat’s primality test alone cannot filter out all composite numbers, highlighting its limitations.
Miller-Rabin Primality Test
The Miller-Rabin test addresses the weaknesses of Fermat’s test by identifying strong probable primes.
Algorithm Steps
- Factorize
- For an odd integer , write , where is odd and is the greatest power of dividing .
- Choose a base
- Select a random integer such that .
- Check primality conditions
- If satisfies one of the following conditions, it is considered a probable prime for the chosen base :
- for some
- If satisfies one of the following conditions, it is considered a probable prime for the chosen base :
- Composite determination
- If none of the conditions are satisfied, is definitively composite.
This algorithm is probabilistic, and repeating the test times can reduce the error probability to . In practice, most composite numbers fail the test, making the error probability even lower.
Probabilistic Nature
The Miller-Rabin test is probabilistic, with an error probability of after iterations.
- Increasing the number of iterations () reduces the error probability exponentially.
- In practice, most composite numbers fail the test, resulting in a lower error rate than the theoretical calculation.
Deterministic Testing: Specific Base Combinations
The Miller-Rabin test can be used deterministically by selecting specific base combinations for certain ranges.
| RANGE () | REQUIRED BASE COMBINATION | REFERENCE |
|---|---|---|
| 1,373,653 | 2, 3 | (Carl Pomerance & Samuel S. Wagstaff, 1980) |
| 9,080,191 | 31, 73 | (Carl Pomerance & Samuel S. Wagstaff, 1980) |
| 25,326,001 | 2, 3, 5 | (Carl Pomerance & Samuel S. Wagstaff, 1980) |
| 3,215,031,751 | 2, 3, 5, 7 | |
| 4,759,123,141 | 2, 7, 61 | |
| 2,152,302,898,747 | 2, 3, 5, 7, 11 | (Jaeschke, 1993) |
| 3,474,749,660,383 | 2, 3, 5, 7, 11, 13 | (Jaeschke, 1993) |
| 341,550,071,728,321 | 2, 3, 5, 7, 11, 13, 17 | (Jaeschke, 1993) |
These base combinations ensure that all composite numbers within the specified range are detected. This deterministic approach is often used in cryptography.
Implementation Example (C++)
The Miller-Rabin algorithm can be implemented in various programming languages. Below is a simple implementation in C++:
inline long long mulmod(__int128 a, __int128 b, __int128 mod) {
return a * b % mod;
}
long long power(long long base, long long exp, long long mod) {
long long ret = 1;
while (exp) {
if (exp & 1) ret = mulmod(ret, base, mod);
base = mulmod(base, base, mod);
exp >>= 1;
}
return ret;
}
bool miller_rabin(long long n, long long a) {
int r = 0;
long long d = n-1;
while (!(d & 1)) {
r++;
d >>= 1;
}
long long x = power(a, d, n);
if (x == 1 || x == n-1) return true;
for (int i=0; i<r-1; i++) {
x = mulmod(x, x, n);
if (x == n-1) return true;
}
return false;
}
bool prime(long long n) {
long long a[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
if (n <= 1) return false;
if (n == 2 || n == 3) return true;
if (!(n & 1)) return false;
for (int i=0; i<9; i++) {
if (n == a[i]) return true;
if (!miller_rabin(n, a[i])) return false;
}
return true;
}Additional Resources
- The PrimePages - Strong probable-primality and a practical test
- Wikipedia - Miller–Rabin primality test
- Lecture Notes: Miller–Rabin Test - Purdue University
- Improving the Speed and Accuracy of the Miller-Rabin Primality Test