From 308290325a416539236d6c0d06234924d0dff10e Mon Sep 17 00:00:00 2001 From: nicoo Date: Sat, 4 Jul 2020 12:58:09 +0200 Subject: [PATCH] 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. --- src/uu/factor/src/miller_rabin.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/uu/factor/src/miller_rabin.rs b/src/uu/factor/src/miller_rabin.rs index 330c8f127..f909f1ec1 100644 --- a/src/uu/factor/src/miller_rabin.rs +++ b/src/uu/factor/src/miller_rabin.rs @@ -101,10 +101,14 @@ pub(crate) fn test(m: A) -> Result { Prime } -// Used by build.rs' tests +// Used by build.rs' tests and debug assertions #[allow(dead_code)] pub(crate) fn is_prime(n: u64) -> bool { - test::>(Montgomery::new(n)).is_prime() + if n % 2 == 0 { + n == 2 + } else { + test::>(Montgomery::new(n)).is_prime() + } } #[cfg(test)]