Merge pull request #12 from gavofyork/fixed_multiplication

fixed multiplication, removed unused file
This commit is contained in:
Marek Kotewicz 2015-12-09 14:51:50 +01:00
commit 74b22691b7
3 changed files with 44 additions and 15 deletions

View File

@ -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;

View File

@ -1,11 +0,0 @@
macro_rules! map(
{ $($key:expr => $value:expr),+ } => {
{
let mut m = ::std::collections::HashMap::new();
$(
m.insert($key, $value);
)+
m
}
};
);

View File

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