|
Miller-Rabin test (Michael Rabin and Gary Miller)
Below is the C++ code, this is only one simple implementation which only handle 32-bits value, remember to put more randomWitness if the "prime" value to be tested is bigger when using big number integer like in OpenSLL or other or mine (yes, I have the big integer class for C/C++ on my web site as well): BOOL MillerRabinPrimeTestDWORD(DWORD prime)
// 1 < randomWitness < prime-1 DWORD totalWitness = 15; DWORD randomWitness[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; DWORD primeMinusOne = prime - 1; DWORD oddMultiplier; DWORD bitLength; DWORD i, j; DWORD result; // find oddMultiplier, defined as "prime - 1 = (2^B) * M" oddMultiplier >>= 1; // right shift for(i = 0; i < totalWitness; i++)
// loop to get AT LEAST one "result = primeMinusOne"
result = ExpModDWORD(result, 2, prime); // is result = primeMinusOne ? if(result == primeMinusOne)
if(result != primeMinusOne)
// it may be pseudoprime/prime return(FALSE); DWORD ExpModDWORD(DWORD value, DWORD exp, DWORD mod)
ULONGLONG ullResult = 1; ULONGLONG ullValue = value; while(exp) {
{ // odd
ullResult *= ullValue; ullResult %= mod; // value = (value * value) % mod ullValue *= ullValue; ullValue %= mod; // exp = exp / 2 return((DWORD) ullResult); The odds of a composite passing decreases faster with this test than with previous ones. Three-quarters of the possible values of a are guaranteed to be witnesses. This means that a composite number will slip through t tests no more than (1/4 * T) of the time, where T is the number of iterations. Actually, these numbers are very pessimistic. For most random numbers, something like 99.9 percent of the possible a values are witnesses. |
|||||||||||||||||||||
|
Author
Site Map Disclaimer
HMaxF Ultimate Recursive Lossless Compression Research 2001 - 2003 (c) All Rights Reserved. |
|||||||||||||||||||||