From d2b1b92f9eca2a5721e43e2abfa2d3bc931937b8 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 9 Dec 2015 11:05:27 +0100 Subject: [PATCH 1/2] removed unused macros.rs --- src/lib.rs | 1 - src/macros.rs | 11 ----------- 2 files changed, 12 deletions(-) delete mode 100644 src/macros.rs diff --git a/src/lib.rs b/src/lib.rs index 883cac2f274..12998529a23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,6 @@ extern crate secp256k1; extern crate arrayvec; extern crate elastic_array; -pub mod macros; pub mod error; pub mod hash; pub mod uint; diff --git a/src/macros.rs b/src/macros.rs deleted file mode 100644 index 69286a340d6..00000000000 --- a/src/macros.rs +++ /dev/null @@ -1,11 +0,0 @@ -macro_rules! map( - { $($key:expr => $value:expr),+ } => { - { - let mut m = ::std::collections::HashMap::new(); - $( - m.insert($key, $value); - )+ - m - } - }; -); From 6db12408dcf9ac01c9d91dea9b02136f97a87a50 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 9 Dec 2015 14:50:01 +0100 Subject: [PATCH 2/2] fixed uint multiplication --- src/uint.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/uint.rs b/src/uint.rs index e017de3854d..234867ea965 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -82,6 +82,15 @@ macro_rules! construct_uint { bytes[i] = (arr[pos] >> ((rev % 8) * 8)) as u8; } } + + #[inline] + pub fn exp10(n: usize) -> $name { + match n { + 0 => $name::from(1u64), + _ => $name::exp10(n - 1) * $name::from(10u64) + } + } + /// Multiplication by u32 fn mul_u32(self, other: u32) -> $name { let $name(ref arr) = self; @@ -167,12 +176,12 @@ macro_rules! construct_uint { type Output = $name; fn mul(self, other: $name) -> $name { - let mut me = self; + let mut res = $name::from(0u64); // TODO: be more efficient about this for i in 0..(2 * $n_words) { - me = (me + me.mul_u32((other >> (32 * i)).low_u32())) << (32 * i); + res = res + (self.mul_u32((other >> (32 * i)).low_u32()) << (32 * i)); } - me + res } } @@ -516,5 +525,37 @@ mod tests { assert_eq!(add >> 64, U256([0xDEADBEEFDEADBEEF, 0, 0, 0])); assert_eq!(add << 64, U256([0, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF, 0])); } + + #[test] + pub fn uint256_exp10() { + assert_eq!(U256::exp10(0), U256::from(1u64)); + println!("\none: {:?}", U256::from(1u64)); + println!("ten: {:?}", U256::from(10u64)); + assert_eq!(U256::from(2u64) * U256::from(10u64), U256::from(20u64)); + assert_eq!(U256::exp10(1), U256::from(10u64)); + assert_eq!(U256::exp10(2), U256::from(100u64)); + assert_eq!(U256::exp10(5), U256::from(100000u64)); + } + + #[test] + pub fn uint256_mul32() { + assert_eq!(U256::from(0u64).mul_u32(2), U256::from(0u64)); + assert_eq!(U256::from(1u64).mul_u32(2), U256::from(2u64)); + assert_eq!(U256::from(10u64).mul_u32(2), U256::from(20u64)); + assert_eq!(U256::from(10u64).mul_u32(5), U256::from(50u64)); + assert_eq!(U256::from(1000u64).mul_u32(50), U256::from(50000u64)); + } + + #[test] + pub fn uint256_mul() { + assert_eq!(U256::from(1u64) * U256::from(10u64), U256::from(10u64)); + } + + #[test] + fn uint256_div() { + assert_eq!(U256::from(10u64) / U256::from(1u64), U256::from(10u64)); + assert_eq!(U256::from(10u64) / U256::from(2u64), U256::from(5u64)); + assert_eq!(U256::from(10u64) / U256::from(3u64), U256::from(3u64)); + } }