From ceb1e9eb58860e1a697ab018e405de59f65f89a0 Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Wed, 4 Apr 2018 17:26:49 -0700 Subject: [PATCH] Fix some shifting issues. --- src/cryptonum/complete_arith.rs | 6 +++--- src/cryptonum/unsigned.rs | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/cryptonum/complete_arith.rs b/src/cryptonum/complete_arith.rs index 9b828b5..ed0c9ea 100644 --- a/src/cryptonum/complete_arith.rs +++ b/src/cryptonum/complete_arith.rs @@ -78,7 +78,7 @@ macro_rules! derive_shifts_from_shift_assign fn $fn(self, rhs: $base) -> $type { let mut copy = self.clone(); - copy.$asnfn(rhs); + copy.$asnfn(rhs as u64); copy } } @@ -88,7 +88,7 @@ macro_rules! derive_shifts_from_shift_assign fn $fn(self, rhs: $base) -> $type { let mut copy = self.clone(); - copy.$asnfn(rhs); + copy.$asnfn(rhs as u64); copy } } @@ -113,7 +113,7 @@ macro_rules! derive_signed_shift_operators if rhs < 0 { self.shl_assign(-rhs); } else { - self.shr_assign(rhs); + self.shr_assign(rhs as $base); } } } diff --git a/src/cryptonum/unsigned.rs b/src/cryptonum/unsigned.rs index fe361f3..af99f69 100644 --- a/src/cryptonum/unsigned.rs +++ b/src/cryptonum/unsigned.rs @@ -890,7 +890,7 @@ mod test { (&a << 0) == a } fn shr_identity(a: UCN) -> bool { - (&a << 0) == a + (&a >> 0) == a } fn add_identity(a: UCN) -> bool { (&a + &UCN{ contents: vec![] }) == a @@ -1049,6 +1049,17 @@ mod test { let pow2 = one << b; (&a << b) == (&a * pow2) } + fn shl_mul_equiv(a: UCN) -> bool { + (&a << 1) == (&a * UCN::from(2 as u64)) && + (&a << 3) == (&a * UCN::from(8 as u64)) && + (&a << 4) == (&a * UCN::from(16 as u64)) && + (&a << 43) == (&a * UCN::from(8796093022208 as u64)) + } + fn shr_div_equiv(a: UCN) -> bool { + (&a >> 1) == (&a / UCN::from(2 as u64)) && + (&a >> 3) == (&a / UCN::from(8 as u64)) && + (&a >> 4) == (&a / UCN::from(16 as u64)) && + (&a >> 43) == (&a / UCN::from(8796093022208 as u64)) + } } - }