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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
use std::mem;
|
||||
use std::ops::DerefMut;
|
||||
use {H64, Address, H256, H512, H520, H2048, FixedHash};
|
||||
use {H64, H160, H256, H512, H520, H2048, FixedHash};
|
||||
|
||||
/// Returns log2.
|
||||
pub fn log2(x: usize) -> u32 {
|
||||
@@ -110,7 +110,7 @@ macro_rules! impl_bloomable_for_hash {
|
||||
}
|
||||
|
||||
impl_bloomable_for_hash!(H64, 8);
|
||||
impl_bloomable_for_hash!(Address, 20);
|
||||
impl_bloomable_for_hash!(H160, 20);
|
||||
impl_bloomable_for_hash!(H256, 32);
|
||||
impl_bloomable_for_hash!(H512, 64);
|
||||
impl_bloomable_for_hash!(H520, 65);
|
||||
@@ -118,14 +118,14 @@ impl_bloomable_for_hash!(H2048, 256);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use {Address, H256, H2048};
|
||||
use {H160, H256, H2048};
|
||||
use sha3::Hashable;
|
||||
use super::Bloomable;
|
||||
|
||||
#[test]
|
||||
fn shift_bloomed() {
|
||||
let bloom: H2048 = "00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into();
|
||||
let address: Address = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into();
|
||||
let address: H160 = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into();
|
||||
let topic: H256 = "02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc".into();
|
||||
|
||||
let mut my_bloom = H2048::default();
|
||||
|
||||
@@ -232,7 +232,7 @@ fn fax_raw_dyn() {
|
||||
#[test]
|
||||
fn populate_big_types() {
|
||||
use hash::*;
|
||||
let a: Address = "ffffffffffffffffffffffffffffffffffffffff".into();
|
||||
let a: H160 = "ffffffffffffffffffffffffffffffffffffffff".into();
|
||||
let mut h: H256 = 0x69.into();
|
||||
h.populate_raw_from(&a);
|
||||
assert_eq!(h, "ffffffffffffffffffffffffffffffffffffffff000000000000000000000000".into());
|
||||
|
||||
@@ -23,6 +23,7 @@ use secp256k1::{key, Secp256k1};
|
||||
use rand::os::OsRng;
|
||||
use sha3::Hashable;
|
||||
use std::fmt;
|
||||
use Address;
|
||||
|
||||
/// Secret key for secp256k1 EC operations. 256 bit generic "hash" data.
|
||||
pub type Secret = H256;
|
||||
|
||||
@@ -156,59 +156,5 @@ pub use log::*;
|
||||
pub use kvdb::*;
|
||||
pub use timer::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::str::FromStr;
|
||||
use {U256, H256, Uint};
|
||||
pub type Address = H160;
|
||||
|
||||
#[test]
|
||||
fn u256_multi_muls() {
|
||||
|
||||
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, ::std::u64::MAX]));
|
||||
assert_eq!(U256([0, 0, 0, ::std::u64::MAX]), result);
|
||||
|
||||
let x1 = U256::from_str("0000000000000000000000000000000000000000000000000000012365124623").unwrap();
|
||||
let x2sqr_right = U256::from_str("000000000000000000000000000000000000000000014baeef72e0378e2328c9").unwrap();
|
||||
let x1sqr = x1 * x1;
|
||||
assert_eq!(H256::from(x2sqr_right), H256::from(x1sqr));
|
||||
let x1cube = x1sqr * x1;
|
||||
let x1cube_right = U256::from_str("0000000000000000000000000000000001798acde139361466f712813717897b").unwrap();
|
||||
assert_eq!(H256::from(x1cube_right), H256::from(x1cube));
|
||||
let x1quad = x1cube * x1;
|
||||
let x1quad_right = U256::from_str("000000000000000000000001adbdd6bd6ff027485484b97f8a6a4c7129756dd1").unwrap();
|
||||
assert_eq!(H256::from(x1quad_right), H256::from(x1quad));
|
||||
let x1penta = x1quad * x1;
|
||||
let x1penta_right = U256::from_str("00000000000001e92875ac24be246e1c57e0507e8c46cc8d233b77f6f4c72993").unwrap();
|
||||
assert_eq!(H256::from(x1penta_right), H256::from(x1penta));
|
||||
let x1septima = x1penta * x1;
|
||||
let x1septima_right = U256::from_str("00022cca1da3f6e5722b7d3cc5bbfb486465ebc5a708dd293042f932d7eee119").unwrap();
|
||||
assert_eq!(H256::from(x1septima_right), H256::from(x1septima));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ use std::fmt;
|
||||
use std::cmp::Ordering;
|
||||
use std::error::Error as StdError;
|
||||
use bigint::uint::{Uint, U128, U256};
|
||||
use hash::{H64, H128, Address, H256, H512, H520, H2048};
|
||||
use hash::{H64, H128, H160, H256, H512, H520, H2048};
|
||||
use elastic_array::*;
|
||||
|
||||
/// Vector like object
|
||||
@@ -159,7 +159,7 @@ macro_rules! impl_hash_to_bytes {
|
||||
|
||||
impl_hash_to_bytes!(H64);
|
||||
impl_hash_to_bytes!(H128);
|
||||
impl_hash_to_bytes!(Address);
|
||||
impl_hash_to_bytes!(H160);
|
||||
impl_hash_to_bytes!(H256);
|
||||
impl_hash_to_bytes!(H512);
|
||||
impl_hash_to_bytes!(H520);
|
||||
@@ -282,7 +282,7 @@ macro_rules! impl_hash_from_bytes {
|
||||
|
||||
impl_hash_from_bytes!(H64, 8);
|
||||
impl_hash_from_bytes!(H128, 16);
|
||||
impl_hash_from_bytes!(Address, 20);
|
||||
impl_hash_from_bytes!(H160, 20);
|
||||
impl_hash_from_bytes!(H256, 32);
|
||||
impl_hash_from_bytes!(H512, 64);
|
||||
impl_hash_from_bytes!(H520, 65);
|
||||
|
||||
@@ -168,7 +168,7 @@ fn encode_address() {
|
||||
use hash::*;
|
||||
|
||||
let tests = vec![
|
||||
ETestPair(Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap(),
|
||||
ETestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"),
|
||||
vec![0x94, 0xef, 0x2d, 0x6d, 0x19, 0x40, 0x84, 0xc2, 0xde,
|
||||
0x36, 0xe0, 0xda, 0xbf, 0xce, 0x45, 0xd0, 0x46,
|
||||
0xb3, 0x7d, 0x11, 0x06])
|
||||
@@ -304,7 +304,7 @@ fn decode_untrusted_address() {
|
||||
use hash::*;
|
||||
|
||||
let tests = vec![
|
||||
DTestPair(Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap(),
|
||||
DTestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"),
|
||||
vec![0x94, 0xef, 0x2d, 0x6d, 0x19, 0x40, 0x84, 0xc2, 0xde,
|
||||
0x36, 0xe0, 0xda, 0xbf, 0xce, 0x45, 0xd0, 0x46,
|
||||
0xb3, 0x7d, 0x11, 0x06])
|
||||
|
||||
Reference in New Issue
Block a user