1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-02 22:17:45 +00:00

factor::miller_rabin::is_prime: Fix bug

Montgomery only works for odd n, so attempting to construct an instance
for an even number results in a panic!

The most obvious solution is to special-case even numbers.
This commit is contained in:
nicoo 2020-07-04 12:58:09 +02:00
parent 9b0f131135
commit 4cfe754551

View file

@ -84,10 +84,14 @@ pub(crate) fn test<A: Arithmetic>(m: A) -> Result {
Prime Prime
} }
// Used by build.rs' tests // Used by build.rs' tests and debug assertions
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) fn is_prime(n: u64) -> bool { pub(crate) fn is_prime(n: u64) -> bool {
test::<Montgomery>(Montgomery::new(n)).is_prime() if n % 2 == 0 {
n == 2
} else {
test::<Montgomery>(Montgomery::new(n)).is_prime()
}
} }
#[cfg(test)] #[cfg(test)]