How to resolve the algorithm Miller–Rabin primality test step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Miller–Rabin primality test step by step in the Rust programming language
Table of Contents
Problem Statement
The Miller–Rabin primality test or Rabin–Miller primality test is a primality test: an algorithm which determines whether a given number is prime or not. The algorithm, as modified by Michael O. Rabin to avoid the generalized Riemann hypothesis, is a probabilistic algorithm. The pseudocode, from Wikipedia is:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Miller–Rabin primality test step by step in the Rust programming language
Source code in the rust programming language
/* Add these lines to the [dependencies] section of your Cargo.toml file:
num = "0.2.0"
rand = "0.6.5"
*/
use num::bigint::BigInt;
use num::bigint::ToBigInt;
// The modular_exponentiation() function takes three identical types
// (which get cast to BigInt), and returns a BigInt:
fn modular_exponentiation<T: ToBigInt>(n: &T, e: &T, m: &T) -> BigInt {
// Convert n, e, and m to BigInt:
let n = n.to_bigint().unwrap();
let e = e.to_bigint().unwrap();
let m = m.to_bigint().unwrap();
// Sanity check: Verify that the exponent is not negative:
assert!(e >= Zero::zero());
use num::traits::{Zero, One};
// As most modular exponentiations do, return 1 if the exponent is 0:
if e == Zero::zero() {
return One::one()
}
// Now do the modular exponentiation algorithm:
let mut result: BigInt = One::one();
let mut base = n % &m;
let mut exp = e;
loop { // Loop until we can return our result.
if &exp % 2 == One::one() {
result *= &base;
result %= &m;
}
if exp == One::one() {
return result
}
exp /= 2;
base *= base.clone();
base %= &m;
}
}
// 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
// will be passed to the is_rabin_miller_prime() function:
fn is_prime<T: ToBigInt>(n: &T) -> bool {
let n = n.to_bigint().unwrap();
if n.clone() < 2.to_bigint().unwrap() {
return false
}
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,
103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
331, 337, 347, 349, 353, 359, 367, 373, 379, 383,
389, 397, 401, 409, 419, 421, 431, 433, 439, 443,
449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
509, 521, 523, 541, 547, 557, 563, 569, 571, 577,
587, 593, 599, 601, 607, 613, 617, 619, 631, 641,
643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
709, 719, 727, 733, 739, 743, 751, 757, 761, 769,
773, 787, 797, 809, 811, 821, 823, 827, 829, 839,
853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
991, 997, 1009, 1013];
use num::traits::Zero; // for Zero::zero()
// 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):
for sp in small_primes {
let sp = sp.to_bigint().unwrap();
if n.clone() == sp {
return true
} else if n.clone() % sp == Zero::zero() {
return false
}
}
is_rabin_miller_prime(&n, None)
}
// Note: "use bigint::RandBigInt;" (which is needed for gen_bigint_range())
// fails to work in the Rust playground ( https://play.rust-lang.org ).
// Therefore, I'll create my own here:
fn get_random_bigint(low: &BigInt, high: &BigInt) -> BigInt {
if low == high { // base case
return low.clone()
}
let middle = (low.clone() + high) / 2.to_bigint().unwrap();
let go_low: bool = rand::random();
if go_low {
return get_random_bigint(low, &middle)
} else {
return get_random_bigint(&middle, high)
}
}
// 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 {
let n = n.to_bigint().unwrap();
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()
let zero: BigInt = Zero::zero();
let one: BigInt = One::one();
let two: BigInt = 2.to_bigint().unwrap();
// The call to is_prime() should have already checked this,
// but check for two, less than two, and multiples of two:
if n <= one {
return false
} else if n == two {
return true // 2 is prime
} else if n.clone() % &two == Zero::zero() {
return false // even number (that's not 2) is not prime
}
let mut t: BigInt = zero.clone();
let n_minus_one: BigInt = n.clone() - &one;
let mut s = n_minus_one.clone();
while &s % &two == one {
s /= &two;
t += &one;
}
// Try k times to test if our number is non-prime:
'outer: for _ in 0..k {
let a = get_random_bigint(&two, &n_minus_one);
let mut v = modular_exponentiation(&a, &s, &n);
if v == one {
continue 'outer;
}
let mut i: BigInt = zero.clone();
'inner: while &i < &t {
v = (v.clone() * &v) % &n;
if &v == &n_minus_one {
continue 'outer;
}
i += &one;
}
return false;
}
// If we get here, then we have a degree of certainty
// that n really is a prime number, so return true:
true
}
fn main() {
let n = 1234687;
let result = is_prime(&n);
println!("Q: Is {} prime? A: {}", n, result);
let n = 1234689;
let result = is_prime(&n);
println!("Q: Is {} prime? A: {}", n, result);
let n = BigInt::parse_bytes("123123423463".as_bytes(), 10).unwrap();
let result = is_prime(&n);
println!("Q: Is {} prime? A: {}", n, result);
let n = BigInt::parse_bytes("123123423465".as_bytes(), 10).unwrap();
let result = is_prime(&n);
println!("Q: Is {} prime? A: {}", n, result);
let n = BigInt::parse_bytes("123123423467".as_bytes(), 10).unwrap();
let result = is_prime(&n);
println!("Q: Is {} prime? A: {}", n, result);
let n = BigInt::parse_bytes("123123423469".as_bytes(), 10).unwrap();
let result = is_prime(&n);
println!("Q: Is {} prime? A: {}", n, result);
}
You may also check:How to resolve the algorithm Compiler/virtual machine interpreter step by step in the Phix programming language
You may also check:How to resolve the algorithm Fermat numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Golden ratio/Convergence step by step in the Java programming language
You may also check:How to resolve the algorithm Combinations and permutations step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Deepcopy step by step in the PARI/GP programming language