From e415b17c019a15ab1924500eb14fa946b5c64e69 Mon Sep 17 00:00:00 2001 From: nicoo Date: Thu, 25 Jun 2020 05:05:31 +0200 Subject: [PATCH 1/3] factor::miller_rabin: Remove duplicated work The duplicate work was introduced in 2015. --- src/uu/factor/src/miller_rabin.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/uu/factor/src/miller_rabin.rs b/src/uu/factor/src/miller_rabin.rs index 762ee1b4c..49099d5f1 100644 --- a/src/uu/factor/src/miller_rabin.rs +++ b/src/uu/factor/src/miller_rabin.rs @@ -54,7 +54,7 @@ pub(crate) fn test(m: A) -> Result { let one = m.one(); let minus_one = m.minus_one(); - for _a in A::BASIS.iter() { + 'witness: for _a in A::BASIS.iter() { let _a = _a % n; if _a == 0 { continue; @@ -65,22 +65,11 @@ pub(crate) fn test(m: A) -> Result { // x = a^r mod n let mut x = m.pow(a, r); - { - // y = ((x²)²...)² i times = x ^ (2ⁱ) = a ^ (r 2ⁱ) = x ^ (n - 1) - let mut y = x; - for _ in 0..i { - y = m.mul(y, y) - } - if y != one { - return Pseudoprime; - }; - } - if x == one || x == minus_one { continue; } - loop { + for _ in 1..i { let y = m.mul(x, x); if y == one { return Composite(gcd(m.to_u64(x) - 1, m.modulus())); @@ -88,10 +77,12 @@ pub(crate) fn test(m: A) -> Result { if y == minus_one { // This basis element is not a witness of `n` being composite. // Keep looking. - break; + continue 'witness; } x = y; } + + return Pseudoprime; } Prime From ecc3e2db24eb7d30f1c42a2944a39aa7db3ebb92 Mon Sep 17 00:00:00 2001 From: nicoo Date: Fri, 17 Jul 2020 12:53:28 +0200 Subject: [PATCH 2/3] factor::miller_rabin::test: Minor readability improvement --- src/uu/factor/src/miller_rabin.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/uu/factor/src/miller_rabin.rs b/src/uu/factor/src/miller_rabin.rs index 49099d5f1..ad454f1ac 100644 --- a/src/uu/factor/src/miller_rabin.rs +++ b/src/uu/factor/src/miller_rabin.rs @@ -73,8 +73,7 @@ pub(crate) fn test(m: A) -> Result { let y = m.mul(x, x); if y == one { return Composite(gcd(m.to_u64(x) - 1, m.modulus())); - } - if y == minus_one { + } else if y == minus_one { // This basis element is not a witness of `n` being composite. // Keep looking. continue 'witness; From 3e55139c13a55c74b734681cac178ad195a36222 Mon Sep 17 00:00:00 2001 From: nicoo Date: Fri, 17 Jul 2020 12:54:37 +0200 Subject: [PATCH 3/3] factor::miller_rabbin::Result: Mark as #[must_use] Ignoring a value of that type is a bug: they are only produced by `miller_rabbin::test`, which is a pure, but expensive, function. As such, an ignored value is always either a mistake, or an easy optimisation opportunity (just remove the useless call to `test`). --- src/uu/factor/src/miller_rabin.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/uu/factor/src/miller_rabin.rs b/src/uu/factor/src/miller_rabin.rs index ad454f1ac..3d12ead02 100644 --- a/src/uu/factor/src/miller_rabin.rs +++ b/src/uu/factor/src/miller_rabin.rs @@ -25,6 +25,7 @@ impl Basis for Montgomery { } #[derive(Eq, PartialEq)] +#[must_use = "Ignoring the output of a primality test."] pub(crate) enum Result { Prime, Pseudoprime,