Ich versuche, den Miller-Rabin-Primzahltest gemäß der Beschreibung in FIPS 186-3 C.3.1 zu implementieren. Egal, was ich tue, ich kann es nicht zur Arbeit bringen. Die Anweisungen sind ziemlich spezifisch, und ich glaube nicht, dass ich etwas verpasst habe, und trotzdem bekomme ich true
für nicht-prime Werte.Miller-Rabin-Primalitätstest FIPS 186-3-Implementierung
Was habe ich falsch gemacht?
template <typename R, typename S, typename T>
T POW(R base, S exponent, const T mod){
T result = 1;
while (exponent){
if (exponent & 1)
result = (result * base) % mod;
exponent >>= 1;
base = (base * base) % mod;
}
return result;
}
// used uint64_t to prevent overflow, but only testing with small numbers for now
bool MillerRabin_FIPS186(uint64_t w, unsigned int iterations = 50){
srand(time(0));
unsigned int a = 0;
uint64_t W = w - 1; // dont want to keep calculating w - 1
uint64_t m = W;
while (!(m & 1)){
m >>= 1;
a++;
}
// skipped getting wlen
// when i had this function using my custom arbitrary precision integer class,
// and could get len(w), getting it and using it in an actual RBG
// made no difference
for(unsigned int i = 0; i < iterations; i++){
uint64_t b = (rand() % (W - 3)) + 2; // 2 <= b <= w - 2
uint64_t z = POW(b, m, w);
if ((z == 1) || (z == W))
continue;
else
for(unsigned int j = 1; j < a; j++){
z = POW(z, 2, w);
if (z == W)
continue;
if (z == 1)
return 0;// Composite
}
}
return 1;// Probably Prime
}
dies:
std::cout << MillerRabin_FIPS186(33) << std::endl;
std::cout << MillerRabin_FIPS186(35) << std::endl;
std::cout << MillerRabin_FIPS186(37) << std::endl;
std::cout << MillerRabin_FIPS186(39) << std::endl;
std::cout << MillerRabin_FIPS186(45) << std::endl;
std::cout << MillerRabin_FIPS186(49) << std::endl;
ist mir geben:
0
1
1
1
0
1
Wie wird 'POW()' implementiert? – sarnold
Können wir 'POW' sehen? Ich sehe einen Fehler, der einige Primzahlen als zusammengesetzt erklären könnte, aber nichts springt für den umgekehrten Fall heraus. Für welche Werte erhalten Sie falsche Ergebnisse? –
Wo ist deine Definition von POW? – Antimony