Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #12 from gavofyork/fixed_multiplication
Browse files Browse the repository at this point in the history
fixed multiplication, removed unused file
  • Loading branch information
debris committed Dec 9, 2015
2 parents f10e72c + 6db1240 commit 74b2269
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 0 additions & 11 deletions src/macros.rs

This file was deleted.

47 changes: 44 additions & 3 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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));
}
}

0 comments on commit 74b2269

Please sign in to comment.