Miller–Rabin primality test: Difference between revisions

Content added Content deleted
m (→‎{{header|Sidef}}: minor code updates)
Line 4,668: Line 4,668:
}
}
}
}


// is_prime() checks the passed-in number against many known small primes.
// is_prime() checks the passed-in number against many known small primes.
// If that doesn't determine if the number is prime or not, then the number
// If that doesn't determine if the number is prime or not, then the number
Line 4,678: Line 4,678:
return false
return false
}
}
let small_primes = vec![2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
let small_primes = vec![2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
Line 4,696: Line 4,696:
919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
991, 997, 1009, 1013];
991, 997, 1009, 1013];

use num::traits::Zero; // for Zero::zero()
use num::traits::Zero; // for Zero::zero()

// Check to see if our number is a small prime (which means it's prime),
// Check to see if our number is a small prime (which means it's prime),
// or a multiple of a small prime (which means it's not prime):
// or a multiple of a small prime (which means it's not prime):
for sp in small_primes {
for sp in small_primes {
let sp = sp.to_bigint().unwrap();
let sp = sp.to_bigint().unwrap();

if n.clone() == sp {
if n.clone() == sp {
return true
return true
Line 4,710: Line 4,710:
}
}
}
}
is_rabin_miller_prime(&n, None)
is_rabin_miller_prime(&n, None)
}
}


// Note: "use bigint::RandBigInt;" (which is needed for gen_bigint_range())
// Note: "use bigint::RandBigInt;" (which is needed for gen_bigint_range())
// fails to work in the Rust playground ( https://play.rust-lang.org ).
// fails to work in the Rust playground ( https://play.rust-lang.org ).
Line 4,722: Line 4,722:
return low.clone()
return low.clone()
}
}
let middle = (low.clone() + high) / 2.to_bigint().unwrap();
let middle = (low.clone() + high) / 2.to_bigint().unwrap();
let go_low: bool = rand::random();
let go_low: bool = rand::random();
if go_low {
if go_low {
return get_random_bigint(low, &middle)
return get_random_bigint(low, &middle)
Line 4,733: Line 4,733:
}
}
}
}


// k is the number of times for testing (pass in None to use 5 (the default)).
// k is the number of times for testing (pass in None to use 5 (the default)).
fn is_rabin_miller_prime<T: ToBigInt>(n: &T, k: Option<usize>) -> bool {
fn is_rabin_miller_prime<T: ToBigInt>(n: &T, k: Option<usize>) -> bool {
let n = n.to_bigint().unwrap();
let n = n.to_bigint().unwrap();
let k = k.unwrap_or(5); // number of times for testing (defaults to 5)
let k = k.unwrap_or(10); // number of times for testing (defaults to 10)

use num::traits::{Zero, One}; // for Zero::zero() and One::one()
use num::traits::{Zero, One}; // for Zero::zero() and One::one()
let zero: BigInt = Zero::zero();
let zero: BigInt = Zero::zero();
let one: BigInt = One::one();
let one: BigInt = One::one();
let two: BigInt = 2.to_bigint().unwrap();
let two: BigInt = 2.to_bigint().unwrap();

// The call to is_prime() should have already checked this,
// The call to is_prime() should have already checked this,
// but check for two, less than two, and multiples of two:
// but check for two, less than two, and multiples of two:
Line 4,755: Line 4,755:
}
}


let mut t: BigInt = zero.clone();
// The following algorithm is ported from:
// https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
// and from:
// https://inventwithpython.com/hacking/chapter23.html
let mut s: BigInt = Zero::zero();
let n_minus_one: BigInt = n.clone() - &one;
let n_minus_one: BigInt = n.clone() - &one;
let mut d = n_minus_one.clone();
let mut s = n_minus_one.clone();
while d.clone() % &two == One::one() {
while &s % &two == one {
d /= &two;
s /= &two;
s += &one;
t += &one;
}
}

let s_minus_one = s.clone() - &one;

// Try k times to test if our number is non-prime:
// Try k times to test if our number is non-prime:
for _ in 0..k {
'outer: for _ in 0..k {
let a = get_random_bigint(&two, &n_minus_one);
let a = get_random_bigint(&two, &n_minus_one);
let mut v = modular_exponentiation(&a, &s, &n);
let mut v = modular_exponentiation(&a, &s, &n);
if v == one {
if v == one {
continue
continue 'outer;
}
}
let mut i: BigInt = zero.clone();
let mut i: BigInt = zero.clone();
while v != n_minus_one {
'inner: while &i < &t {
if i == s_minus_one {
v = (v.clone() * &v) % &n;
return false
if &v == &n_minus_one {
continue 'outer;
}
}
i += &one;
i += &one;
v = (v.clone() * &v) % &n;
}
}
return false;
}
}

// If we get here, then we have a degree of certainty
// If we get here, then we have a degree of certainty
// that n really is a prime number, so return true:
// that n really is a prime number, so return true: