From a4e65fa35fe7dd34f2aed893c894fafc2e2e5f63 Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Sun, 1 Apr 2018 20:42:49 -0700 Subject: [PATCH] Fix multiplication when either argument is zero. --- src/cryptonum/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/cryptonum/mod.rs b/src/cryptonum/mod.rs index 7b24d26..81c8184 100644 --- a/src/cryptonum/mod.rs +++ b/src/cryptonum/mod.rs @@ -513,6 +513,16 @@ impl<'a> SubAssign<&'a UCN> for UCN { impl<'a> MulAssign<&'a UCN> for UCN { fn mul_assign(&mut self, rhs: &UCN) { + // Handle the quick and easy multiplication by zero cases first. + if self.contents.len() == 0 { + return; + } + if rhs.contents.len() == 0 { + self.contents.resize(0,0); + return; + } + + // OK, do some real multiplication let x = self.contents.clone(); let y = rhs.contents.clone(); let outlen = x.len() + y.len(); @@ -759,6 +769,13 @@ mod test { fn sub_annihilation(a: UCN) -> bool { (&a - &a) == UCN{ contents: vec![] } } + fn mul_annihilation(a: UCN) -> bool { + let zero = UCN{ contents: vec![] }; + (&a * &zero) == zero + } + } + + quickcheck! { fn xor_inverse(a: UCN, b: UCN) -> bool { ((&a ^ &b) ^ &b) == a }