ethkey and ethstore use hash structures from bigint (#1851)

* Address renamed to H160 at bigint library level

* moved uint specific test from util to bigint library

* naming

* unifing hashes in progress

* unifing hashes

* cleanup redundant unwraps in tests

* fixed compiling
This commit is contained in:
Marek Kotewicz
2016-08-15 15:09:00 +02:00
committed by Gav Wood
parent e6d9fb2ad3
commit c39761c042
32 changed files with 245 additions and 396 deletions

View File

@@ -17,8 +17,8 @@
//! General hash types, a fixed-size raw-data type used as the output of hash functions.
use std::{ops, fmt, cmp};
use std::cmp::*;
use std::ops::*;
use std::cmp::{min, Ordering};
use std::ops::{Deref, DerefMut, BitXor, BitAnd, BitOr, IndexMut, Index};
use std::hash::{Hash, Hasher, BuildHasherDefault};
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
@@ -75,6 +75,12 @@ macro_rules! impl_hash {
}
}
impl From<$from> for [u8; $size] {
fn from(s: $from) -> Self {
s.0
}
}
impl Deref for $from {
type Target = [u8];
@@ -409,9 +415,9 @@ impl<'a> From<&'a H256> for U256 {
}
}
impl From<H256> for Address {
fn from(value: H256) -> Address {
let mut ret = Address::new();
impl From<H256> for H160 {
fn from(value: H256) -> H160 {
let mut ret = H160::new();
ret.0.copy_from_slice(&value[12..32]);
ret
}
@@ -425,16 +431,16 @@ impl From<H256> for H64 {
}
}
impl From<Address> for H256 {
fn from(value: Address) -> H256 {
impl From<H160> for H256 {
fn from(value: H160) -> H256 {
let mut ret = H256::new();
ret.0[12..32].copy_from_slice(&value);
ret
}
}
impl<'a> From<&'a Address> for H256 {
fn from(value: &'a Address) -> H256 {
impl<'a> From<&'a H160> for H256 {
fn from(value: &'a H160) -> H256 {
let mut ret = H256::new();
ret.0[12..32].copy_from_slice(value);
ret
@@ -444,7 +450,7 @@ impl<'a> From<&'a Address> for H256 {
impl_hash!(H32, 4);
impl_hash!(H64, 8);
impl_hash!(H128, 16);
impl_hash!(Address, 20);
impl_hash!(H160, 20);
impl_hash!(H256, 32);
impl_hash!(H264, 33);
impl_hash!(H512, 64);
@@ -452,7 +458,7 @@ impl_hash!(H520, 65);
impl_hash!(H1024, 128);
impl_hash!(H2048, 256);
known_heap_size!(0, H32, H64, H128, Address, H256, H264, H512, H520, H1024, H2048);
known_heap_size!(0, H32, H64, H128, H160, H256, H264, H512, H520, H1024, H2048);
// Specialized HashMap and HashSet
/// Hasher that just takes 8 bytes of the provided value.
@@ -535,9 +541,9 @@ mod tests {
#[test]
fn from_and_to_address() {
let address = Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap();
let address: H160 = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into();
let h = H256::from(address.clone());
let a = Address::from(h);
let a = H160::from(h);
assert_eq!(address, a);
}

View File

@@ -36,14 +36,11 @@
//! The functions here are designed to be fast.
//!
use std::mem;
use std::fmt;
use std::{mem, fmt};
use std::str::{FromStr};
use std::convert::From;
use std::hash::Hash;
use std::ops::*;
use std::cmp::*;
use std::ops::{Shr, Shl, BitAnd, BitOr, BitXor, Not, Div, Rem, Mul, Add, Sub};
use std::cmp::Ordering;
use rustc_serialize::hex::{FromHex, FromHexError};
/// Conversion from decimal string error
@@ -2208,5 +2205,59 @@ mod tests {
let result = U256([1, 2, 3, 4]).full_mul(U256([5, 6, 7, 8]));
assert_eq!(U512([5, 16, 34, 60, 61, 52, 32, 0]), result);
}
}
#[test]
fn u256_multi_muls2() {
let (result, _) = U256([0, 0, 0, 0]).overflowing_mul(U256([0, 0, 0, 0]));
assert_eq!(U256([0, 0, 0, 0]), result);
let (result, _) = U256([1, 0, 0, 0]).overflowing_mul(U256([1, 0, 0, 0]));
assert_eq!(U256([1, 0, 0, 0]), result);
let (result, _) = U256([5, 0, 0, 0]).overflowing_mul(U256([5, 0, 0, 0]));
assert_eq!(U256([25, 0, 0, 0]), result);
let (result, _) = U256([0, 5, 0, 0]).overflowing_mul(U256([0, 5, 0, 0]));
assert_eq!(U256([0, 0, 25, 0]), result);
let (result, _) = U256([0, 0, 0, 1]).overflowing_mul(U256([1, 0, 0, 0]));
assert_eq!(U256([0, 0, 0, 1]), result);
let (result, _) = U256([0, 0, 0, 5]).overflowing_mul(U256([2, 0, 0, 0]));
assert_eq!(U256([0, 0, 0, 10]), result);
let (result, _) = U256([0, 0, 1, 0]).overflowing_mul(U256([0, 5, 0, 0]));
assert_eq!(U256([0, 0, 0, 5]), result);
let (result, _) = U256([0, 0, 8, 0]).overflowing_mul(U256([0, 0, 7, 0]));
assert_eq!(U256([0, 0, 0, 0]), result);
let (result, _) = U256([2, 0, 0, 0]).overflowing_mul(U256([0, 5, 0, 0]));
assert_eq!(U256([0, 10, 0, 0]), result);
let (result, _) = U256([1, 0, 0, 0]).overflowing_mul(U256([0, 0, 0, u64::max_value()]));
assert_eq!(U256([0, 0, 0, u64::max_value()]), result);
let x1: U256 = "0000000000000000000000000000000000000000000000000000012365124623".into();
let x2sqr_right: U256 = "000000000000000000000000000000000000000000014baeef72e0378e2328c9".into();
let x1sqr = x1 * x1;
assert_eq!(x2sqr_right, x1sqr);
let x1cube = x1sqr * x1;
let x1cube_right: U256 = "0000000000000000000000000000000001798acde139361466f712813717897b".into();
assert_eq!(x1cube_right, x1cube);
let x1quad = x1cube * x1;
let x1quad_right: U256 = "000000000000000000000001adbdd6bd6ff027485484b97f8a6a4c7129756dd1".into();
assert_eq!(x1quad_right, x1quad);
let x1penta = x1quad * x1;
let x1penta_right: U256 = "00000000000001e92875ac24be246e1c57e0507e8c46cc8d233b77f6f4c72993".into();
assert_eq!(x1penta_right, x1penta);
let x1septima = x1penta * x1;
let x1septima_right: U256 = "00022cca1da3f6e5722b7d3cc5bbfb486465ebc5a708dd293042f932d7eee119".into();
assert_eq!(x1septima_right, x1septima);
}
}