1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
//! Provide a method to determine whether an unsigned integer is a prime number through the `IsPrime` Trait.
//!
//! In the current implementation, this crate uses the Miller-Rabin primality test.
//! The Miller-Rabin primality test is known to have witnesses that can conclusively determine unsigned integers of at most 64 bits.
//! In this crate, the following information is used to select the witnesses.
//!
//! [Deterministic variants of the Miller-Rabin primality test](https://miller-rabin.appspot.com/)
mod montgomery;
use montgomery::Montgomery;
pub trait IsPrime {
fn is_prime(&self) -> bool;
}
macro_rules! impl_is_prime {
( $t:ty, $witness_ty:ty, [ $( $witness:expr ),* ] ) => {
impl IsPrime for $t {
/// Determine an unsigned integer is prime number or not.
///
/// # Examples
/// ```rust
/// use primality_test::IsPrime;
///
/// assert!(998244353u32.is_prime());
/// assert!(!561u16.is_prime());
///
/// let primes = (1..20u16).filter(IsPrime::is_prime).collect::<Vec<_>>();
/// assert_eq!(primes, vec![2, 3, 5, 7, 11, 13, 17, 19]);
/// ```
fn is_prime(&self) -> bool {
let p = *self as $witness_ty;
if p == 1 || p & 1 == 0 {
return p == 2;
}
let mont = Montgomery::<$witness_ty>::new(p);
let s = (p - 1).trailing_zeros();
let t = (p - 1) >> s;
[$( $witness ),*]
.iter()
.map(|&a| a % p)
.filter(|&a| a != 0)
.all(|a| {
let a = mont.convert(a);
let at = mont.pow(a, t);
// a^t = 1 (mod p) or a^t = -1 (mod p)
if at == mont.r || at == p - mont.r {
return true;
}
// found i satisfying a^((2^i)*t) = -1 (mod p)
(1..s)
.scan(at, |at, _| {
*at = mont.multiply(*at, *at);
Some(*at)
})
.any(|at| at == p - mont.r)
})
}
}
};
}
impl_is_prime!(u8, u8, [2, 7, 61]);
impl_is_prime!(u16, u16, [2, 7, 61]);
impl_is_prime!(u32, u32, [2, 7, 61]);
impl_is_prime!(u64, u64, [2, 325, 9375, 28178, 450775, 9780504, 1795265022]);
#[cfg(target_pointer_width = "64")]
impl_is_prime!(
usize,
u64,
[2, 325, 9375, 28178, 450775, 9780504, 1795265022]
);
#[cfg(target_pointer_width = "32")]
impl_is_prime!(usize, u32, [2, 7, 61]);
#[cfg(test)]
mod tests {
use super::*;
#[rustfmt::skip]
const PRIME: &[u64] = &[
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 47, 53, 59, 61, 67, 73, 79, 83, 89, 97,
998244353, 1000000007,
];
const NO_PRIME: &[u64] = &[
1,
57,
5329,
49141,
4759123141,
1122004669633,
21652684502221,
31858317218647,
47636622961201,
55245642489451,
3071837692357849,
3770579582154547,
7999252175582851,
585226005592931977,
];
#[rustfmt::skip]
const CARMICHAEL: &[u64] = &[
561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341, 41041, 46657, 52633, 62745,
63973, 75361, 101101, 115921, 126217, 162401, 172081, 188461, 252601, 278545, 294409,
314821, 334153, 340561, 399001, 410041, 449065, 488881, 512461,
];
#[test]
fn primality_test() {
for &p in PRIME {
if p <= u8::MAX as u64 {
assert!((p as u8).is_prime());
}
if p <= u16::MAX as u64 {
assert!((p as u16).is_prime());
}
if p <= u32::MAX as u64 {
assert!((p as u32).is_prime());
}
assert!(p.is_prime());
}
}
#[test]
fn non_primality_test() {
for &p in NO_PRIME {
if p <= u8::MAX as u64 {
assert!(!(p as u8).is_prime());
}
if p <= u16::MAX as u64 {
assert!(!(p as u16).is_prime());
}
if p <= u32::MAX as u64 {
assert!(!(p as u32).is_prime());
}
assert!(!p.is_prime());
}
for &p in CARMICHAEL {
if p <= u8::MAX as u64 {
assert!(!(p as u8).is_prime());
}
if p <= u16::MAX as u64 {
assert!(!(p as u16).is_prime());
}
if p <= u32::MAX as u64 {
assert!(!(p as u32).is_prime());
}
assert!(!p.is_prime());
}
}
}