2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-12-07 17:20:15 +01:00
|
|
|
//! General hash types, a fixed-size raw-data type used as the output of hash functions.
|
|
|
|
|
2016-07-06 11:23:29 +02:00
|
|
|
use rustc_serialize::hex::FromHex;
|
|
|
|
use std::{ops, fmt, cmp};
|
|
|
|
use std::cmp::*;
|
|
|
|
use std::ops::*;
|
2016-08-03 22:03:40 +02:00
|
|
|
use std::hash::{Hash, Hasher, BuildHasherDefault};
|
|
|
|
use std::collections::{HashMap, HashSet};
|
2016-07-06 11:23:29 +02:00
|
|
|
use std::str::FromStr;
|
2016-01-14 21:24:03 +01:00
|
|
|
use math::log2;
|
2016-01-10 12:53:55 +01:00
|
|
|
use error::UtilError;
|
2015-11-26 23:24:01 +01:00
|
|
|
use rand::Rng;
|
|
|
|
use rand::os::OsRng;
|
2016-01-08 11:52:46 +01:00
|
|
|
use bytes::{BytesConvertable,Populatable};
|
2016-02-29 22:21:15 +01:00
|
|
|
use bigint::uint::{Uint, U256};
|
2015-11-25 19:26:40 +01:00
|
|
|
|
2015-12-07 17:20:15 +01:00
|
|
|
/// Trait for a fixed-size byte array to be used as the output of hash functions.
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 17:20:15 +01:00
|
|
|
/// Note: types implementing `FixedHash` must be also `BytesConvertable`.
|
2016-01-14 21:24:03 +01:00
|
|
|
pub trait FixedHash: Sized + BytesConvertable + Populatable + FromStr + Default {
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Create a new, zero-initialised, instance.
|
2015-11-28 01:38:00 +01:00
|
|
|
fn new() -> Self;
|
2016-01-09 22:30:13 +01:00
|
|
|
/// Synonym for `new()`. Prefer to new as it's more readable.
|
|
|
|
fn zero() -> Self;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Create a new, cryptographically random, instance.
|
2015-11-27 20:10:31 +01:00
|
|
|
fn random() -> Self;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Assign self have a cryptographically random value.
|
2015-11-27 20:10:31 +01:00
|
|
|
fn randomize(&mut self);
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Get the size of this object in bytes.
|
|
|
|
fn len() -> usize;
|
|
|
|
/// Convert a slice of bytes of length `len()` to an instance of this type.
|
2015-11-29 02:11:56 +01:00
|
|
|
fn from_slice(src: &[u8]) -> Self;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Assign self to be of the same value as a slice of bytes of length `len()`.
|
2015-11-29 02:11:56 +01:00
|
|
|
fn clone_from_slice(&mut self, src: &[u8]) -> usize;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Copy the data of this object into some mutable slice of length `len()`.
|
2015-11-30 16:38:55 +01:00
|
|
|
fn copy_to(&self, dest: &mut [u8]);
|
2016-02-03 14:51:45 +01:00
|
|
|
/// When interpreting self as a bloom output, augment (bit-wise OR) with the a bloomed version of `b`.
|
2016-01-09 23:47:46 +01:00
|
|
|
fn shift_bloomed<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: FixedHash;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Same as `shift_bloomed` except that `self` is consumed and a new value returned.
|
2016-01-09 23:47:46 +01:00
|
|
|
fn with_bloomed<T>(mut self, b: &T) -> Self where T: FixedHash { self.shift_bloomed(b); self }
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Bloom the current value using the bloom parameter `m`.
|
2015-11-28 03:02:06 +01:00
|
|
|
fn bloom_part<T>(&self, m: usize) -> T where T: FixedHash;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Check to see whether this hash, interpreted as a bloom, contains the value `b` when bloomed.
|
2016-01-09 23:47:46 +01:00
|
|
|
fn contains_bloomed<T>(&self, b: &T) -> bool where T: FixedHash;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Returns `true` if all bits set in `b` are also set in `self`.
|
2015-11-28 19:11:04 +01:00
|
|
|
fn contains<'a>(&'a self, b: &'a Self) -> bool;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Returns `true` if no bits are set.
|
2016-01-09 10:20:07 +01:00
|
|
|
fn is_zero(&self) -> bool;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Returns the lowest 8 bytes interpreted as a BigEndian integer.
|
2016-01-27 14:44:02 +01:00
|
|
|
fn low_u64(&self) -> u64;
|
2015-11-27 20:10:31 +01:00
|
|
|
}
|
|
|
|
|
2016-03-19 23:51:24 +01:00
|
|
|
/// Return `s` without the `0x` at the beginning of it, if any.
|
|
|
|
pub fn clean_0x(s: &str) -> &str {
|
2016-07-25 16:09:47 +02:00
|
|
|
if s.starts_with("0x") {
|
2016-01-13 18:58:57 +01:00
|
|
|
&s[2..]
|
|
|
|
} else {
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 19:26:40 +01:00
|
|
|
macro_rules! impl_hash {
|
|
|
|
($from: ident, $size: expr) => {
|
2016-01-04 13:49:32 +01:00
|
|
|
#[derive(Eq)]
|
2016-05-28 21:48:42 +02:00
|
|
|
#[repr(C)]
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Unformatted binary data of fixed length.
|
2015-11-29 02:11:56 +01:00
|
|
|
pub struct $from (pub [u8; $size]);
|
2015-11-25 19:26:40 +01:00
|
|
|
|
2016-04-30 17:41:24 +02:00
|
|
|
impl From<[u8; $size]> for $from {
|
|
|
|
fn from(bytes: [u8; $size]) -> Self {
|
|
|
|
$from(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
impl Deref for $from {
|
|
|
|
type Target = [u8];
|
2015-12-13 22:44:28 +01:00
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
#[inline]
|
|
|
|
fn deref(&self) -> &[u8] {
|
2015-12-13 22:44:28 +01:00
|
|
|
&self.0
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-13 22:44:28 +01:00
|
|
|
|
2016-02-15 11:54:38 +01:00
|
|
|
impl AsRef<[u8]> for $from {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 14:44:02 +01:00
|
|
|
impl DerefMut for $from {
|
2015-11-29 02:11:56 +01:00
|
|
|
#[inline]
|
|
|
|
fn deref_mut(&mut self) -> &mut [u8] {
|
2015-12-13 22:44:28 +01:00
|
|
|
&mut self.0
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 20:10:31 +01:00
|
|
|
impl FixedHash for $from {
|
2015-11-28 01:38:00 +01:00
|
|
|
fn new() -> $from {
|
|
|
|
$from([0; $size])
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:30:13 +01:00
|
|
|
fn zero() -> $from {
|
|
|
|
$from([0; $size])
|
|
|
|
}
|
|
|
|
|
2015-11-27 20:10:31 +01:00
|
|
|
fn random() -> $from {
|
2015-11-26 23:24:01 +01:00
|
|
|
let mut hash = $from::new();
|
|
|
|
hash.randomize();
|
|
|
|
hash
|
|
|
|
}
|
2015-11-27 20:10:31 +01:00
|
|
|
|
|
|
|
fn randomize(&mut self) {
|
2015-11-26 23:24:01 +01:00
|
|
|
let mut rng = OsRng::new().unwrap();
|
|
|
|
rng.fill_bytes(&mut self.0);
|
|
|
|
}
|
2015-11-27 17:54:33 +01:00
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
fn len() -> usize {
|
2015-11-28 11:25:00 +01:00
|
|
|
$size
|
|
|
|
}
|
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
#[inline]
|
|
|
|
fn clone_from_slice(&mut self, src: &[u8]) -> usize {
|
2016-06-28 11:52:59 +02:00
|
|
|
let min = cmp::min($size, src.len());
|
|
|
|
self.0[..min].copy_from_slice(&src[..min]);
|
2015-11-29 02:11:56 +01:00
|
|
|
min
|
|
|
|
}
|
2016-06-07 16:15:17 +02:00
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
fn from_slice(src: &[u8]) -> Self {
|
|
|
|
let mut r = Self::new();
|
|
|
|
r.clone_from_slice(src);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2015-11-30 16:38:55 +01:00
|
|
|
fn copy_to(&self, dest: &mut[u8]) {
|
2016-06-28 11:52:59 +02:00
|
|
|
let min = cmp::min($size, dest.len());
|
2016-06-07 16:11:34 +02:00
|
|
|
dest[..min].copy_from_slice(&self.0[..min]);
|
2015-11-30 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 23:47:46 +01:00
|
|
|
fn shift_bloomed<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: FixedHash {
|
2015-11-28 03:02:06 +01:00
|
|
|
let bp: Self = b.bloom_part($size);
|
2015-11-28 01:38:00 +01:00
|
|
|
let new_self = &bp | self;
|
|
|
|
|
2016-06-07 16:04:26 +02:00
|
|
|
self.0 = new_self.0;
|
2015-11-28 01:38:00 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-11-28 03:02:06 +01:00
|
|
|
fn bloom_part<T>(&self, m: usize) -> T where T: FixedHash {
|
|
|
|
// numbers of bits
|
|
|
|
// TODO: move it to some constant
|
|
|
|
let p = 3;
|
|
|
|
|
|
|
|
let bloom_bits = m * 8;
|
|
|
|
let mask = bloom_bits - 1;
|
|
|
|
let bloom_bytes = (log2(bloom_bits) + 7) / 8;
|
|
|
|
|
|
|
|
// must be a power of 2
|
|
|
|
assert_eq!(m & (m - 1), 0);
|
|
|
|
// out of range
|
|
|
|
assert!(p * bloom_bytes <= $size);
|
|
|
|
|
|
|
|
// return type
|
|
|
|
let mut ret = T::new();
|
|
|
|
|
|
|
|
// 'ptr' to out slice
|
|
|
|
let mut ptr = 0;
|
|
|
|
|
|
|
|
// set p number of bits,
|
|
|
|
// p is equal 3 according to yellowpaper
|
|
|
|
for _ in 0..p {
|
|
|
|
let mut index = 0 as usize;
|
|
|
|
for _ in 0..bloom_bytes {
|
|
|
|
index = (index << 8) | self.0[ptr] as usize;
|
|
|
|
ptr += 1;
|
|
|
|
}
|
|
|
|
index &= mask;
|
2016-01-07 23:59:50 +01:00
|
|
|
ret.as_slice_mut()[m - 1 - index / 8] |= 1 << (index % 8);
|
2015-11-28 03:02:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2016-01-09 23:47:46 +01:00
|
|
|
fn contains_bloomed<T>(&self, b: &T) -> bool where T: FixedHash {
|
2015-11-28 03:02:06 +01:00
|
|
|
let bp: Self = b.bloom_part($size);
|
2015-11-28 19:11:04 +01:00
|
|
|
self.contains(&bp)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contains<'a>(&'a self, b: &'a Self) -> bool {
|
|
|
|
&(b & self) == b
|
2015-11-28 01:38:00 +01:00
|
|
|
}
|
2016-01-09 10:20:07 +01:00
|
|
|
|
|
|
|
fn is_zero(&self) -> bool {
|
|
|
|
self.eq(&Self::new())
|
|
|
|
}
|
2016-01-27 14:44:02 +01:00
|
|
|
|
|
|
|
fn low_u64(&self) -> u64 {
|
|
|
|
let mut ret = 0u64;
|
|
|
|
for i in 0..min($size, 8) {
|
|
|
|
ret |= (self.0[$size - 1 - i] as u64) << (i * 8);
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
2015-11-27 17:54:33 +01:00
|
|
|
}
|
|
|
|
|
2015-11-25 19:26:40 +01:00
|
|
|
impl FromStr for $from {
|
2016-01-10 12:53:55 +01:00
|
|
|
type Err = UtilError;
|
|
|
|
fn from_str(s: &str) -> Result<$from, UtilError> {
|
2015-11-25 19:26:40 +01:00
|
|
|
let a = try!(s.from_hex());
|
2016-01-10 12:53:55 +01:00
|
|
|
if a.len() != $size { return Err(UtilError::BadSize); }
|
2015-11-25 19:26:40 +01:00
|
|
|
let mut ret = $from([0;$size]);
|
|
|
|
for i in 0..$size {
|
|
|
|
ret.0[i] = a[i];
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 00:02:43 +01:00
|
|
|
|
|
|
|
impl fmt::Debug for $from {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-01-19 12:14:29 +01:00
|
|
|
for i in &self.0[..] {
|
2015-11-26 00:02:43 +01:00
|
|
|
try!(write!(f, "{:02x}", i));
|
|
|
|
}
|
|
|
|
Ok(())
|
2015-11-26 23:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-06 11:23:29 +02:00
|
|
|
|
2015-11-26 00:02:43 +01:00
|
|
|
impl fmt::Display for $from {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-01-19 12:14:29 +01:00
|
|
|
for i in &self.0[0..2] {
|
2016-01-14 12:27:41 +01:00
|
|
|
try!(write!(f, "{:02x}", i));
|
|
|
|
}
|
2016-01-18 00:51:55 +01:00
|
|
|
try!(write!(f, "…"));
|
2016-02-14 12:54:27 +01:00
|
|
|
for i in &self.0[$size - 2..$size] {
|
2016-01-18 00:51:55 +01:00
|
|
|
try!(write!(f, "{:02x}", i));
|
|
|
|
}
|
|
|
|
Ok(())
|
2015-11-26 23:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 23:01:35 +01:00
|
|
|
impl Copy for $from {}
|
2016-03-11 11:16:49 +01:00
|
|
|
#[cfg_attr(feature="dev", allow(expl_impl_clone_on_copy))]
|
2015-11-26 23:24:01 +01:00
|
|
|
impl Clone for $from {
|
|
|
|
fn clone(&self) -> $from {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret = $from::new();
|
|
|
|
ret.0.copy_from_slice(&self.0);
|
|
|
|
ret
|
2015-11-26 23:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:02:43 +01:00
|
|
|
impl PartialEq for $from {
|
2015-11-26 23:24:01 +01:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
2015-11-26 00:02:43 +01:00
|
|
|
for i in 0..$size {
|
2015-11-26 23:24:01 +01:00
|
|
|
if self.0[i] != other.0[i] {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-26 00:02:43 +01:00
|
|
|
}
|
|
|
|
true
|
2015-11-26 23:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 12:16:10 +01:00
|
|
|
impl Ord for $from {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
2016-01-07 23:59:50 +01:00
|
|
|
for i in 0..$size {
|
|
|
|
if self.0[i] > other.0[i] {
|
2016-01-13 12:16:10 +01:00
|
|
|
return Ordering::Greater;
|
2016-01-07 23:59:50 +01:00
|
|
|
} else if self.0[i] < other.0[i] {
|
2016-01-13 12:16:10 +01:00
|
|
|
return Ordering::Less;
|
2016-01-07 23:59:50 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-13 12:16:10 +01:00
|
|
|
Ordering::Equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for $from {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
2016-01-07 23:59:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 23:24:01 +01:00
|
|
|
impl Hash for $from {
|
|
|
|
fn hash<H>(&self, state: &mut H) where H: Hasher {
|
|
|
|
state.write(&self.0);
|
|
|
|
state.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<usize> for $from {
|
|
|
|
type Output = u8;
|
|
|
|
|
2016-01-19 12:14:29 +01:00
|
|
|
fn index(&self, index: usize) -> &u8 {
|
2015-11-26 23:24:01 +01:00
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl IndexMut<usize> for $from {
|
2016-01-19 12:14:29 +01:00
|
|
|
fn index_mut(&mut self, index: usize) -> &mut u8 {
|
2015-11-26 23:24:01 +01:00
|
|
|
&mut self.0[index]
|
|
|
|
}
|
2015-11-26 00:02:43 +01:00
|
|
|
}
|
2015-11-29 02:11:56 +01:00
|
|
|
impl Index<ops::Range<usize>> for $from {
|
|
|
|
type Output = [u8];
|
|
|
|
|
2016-01-19 12:14:29 +01:00
|
|
|
fn index(&self, index: ops::Range<usize>) -> &[u8] {
|
2015-11-29 02:11:56 +01:00
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl IndexMut<ops::Range<usize>> for $from {
|
2016-01-19 12:14:29 +01:00
|
|
|
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [u8] {
|
2015-11-29 02:11:56 +01:00
|
|
|
&mut self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Index<ops::RangeFull> for $from {
|
|
|
|
type Output = [u8];
|
|
|
|
|
2016-01-19 12:14:29 +01:00
|
|
|
fn index(&self, _index: ops::RangeFull) -> &[u8] {
|
2015-11-29 02:11:56 +01:00
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl IndexMut<ops::RangeFull> for $from {
|
2016-01-19 12:14:29 +01:00
|
|
|
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut [u8] {
|
2015-11-29 02:11:56 +01:00
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
2015-11-27 23:24:01 +01:00
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// `BitOr` on references
|
2015-11-27 23:24:01 +01:00
|
|
|
impl<'a> BitOr for &'a $from {
|
|
|
|
type Output = $from;
|
|
|
|
|
|
|
|
fn bitor(self, rhs: Self) -> Self::Output {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret: $from = $from::default();
|
|
|
|
for i in 0..$size {
|
|
|
|
ret.0[i] = self.0[i] | rhs.0[i];
|
2015-11-27 23:24:01 +01:00
|
|
|
}
|
2016-06-07 16:04:26 +02:00
|
|
|
ret
|
2015-11-27 23:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Moving `BitOr`
|
2015-11-27 23:24:01 +01:00
|
|
|
impl BitOr for $from {
|
|
|
|
type Output = $from;
|
|
|
|
|
|
|
|
fn bitor(self, rhs: Self) -> Self::Output {
|
|
|
|
&self | &rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// `BitAnd` on references
|
2015-11-28 03:02:06 +01:00
|
|
|
impl <'a> BitAnd for &'a $from {
|
|
|
|
type Output = $from;
|
|
|
|
|
|
|
|
fn bitand(self, rhs: Self) -> Self::Output {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret: $from = $from::default();
|
|
|
|
for i in 0..$size {
|
|
|
|
ret.0[i] = self.0[i] & rhs.0[i];
|
2015-11-28 03:02:06 +01:00
|
|
|
}
|
2016-06-07 16:04:26 +02:00
|
|
|
ret
|
2015-11-28 03:02:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Moving `BitAnd`
|
2015-11-28 03:02:06 +01:00
|
|
|
impl BitAnd for $from {
|
|
|
|
type Output = $from;
|
|
|
|
|
|
|
|
fn bitand(self, rhs: Self) -> Self::Output {
|
|
|
|
&self & &rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// `BitXor` on references
|
2015-11-30 16:38:55 +01:00
|
|
|
impl <'a> BitXor for &'a $from {
|
|
|
|
type Output = $from;
|
|
|
|
|
|
|
|
fn bitxor(self, rhs: Self) -> Self::Output {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret: $from = $from::default();
|
|
|
|
for i in 0..$size {
|
|
|
|
ret.0[i] = self.0[i] ^ rhs.0[i];
|
2015-11-30 16:38:55 +01:00
|
|
|
}
|
2016-06-07 16:04:26 +02:00
|
|
|
ret
|
2015-11-30 16:38:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Moving `BitXor`
|
2015-11-30 16:38:55 +01:00
|
|
|
impl BitXor for $from {
|
|
|
|
type Output = $from;
|
|
|
|
|
|
|
|
fn bitxor(self, rhs: Self) -> Self::Output {
|
|
|
|
&self ^ &rhs
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 21:24:03 +01:00
|
|
|
|
2015-11-28 22:54:12 +01:00
|
|
|
impl $from {
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Get a hex representation.
|
2015-11-28 22:54:12 +01:00
|
|
|
pub fn hex(&self) -> String {
|
2016-01-14 13:10:18 +01:00
|
|
|
format!("{:?}", self)
|
2015-11-28 22:54:12 +01:00
|
|
|
}
|
2016-01-09 23:47:46 +01:00
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Construct new instance equal to the bloomed value of `b`.
|
2016-01-09 23:47:46 +01:00
|
|
|
pub fn from_bloomed<T>(b: &T) -> Self where T: FixedHash { b.bloom_part($size) }
|
2015-11-28 22:54:12 +01:00
|
|
|
}
|
2016-01-13 18:41:33 +01:00
|
|
|
|
2016-01-14 21:24:03 +01:00
|
|
|
impl Default for $from {
|
|
|
|
fn default() -> Self { $from::new() }
|
|
|
|
}
|
|
|
|
|
2016-01-13 18:41:33 +01:00
|
|
|
impl From<u64> for $from {
|
|
|
|
fn from(mut value: u64) -> $from {
|
|
|
|
let mut ret = $from::new();
|
|
|
|
for i in 0..8 {
|
|
|
|
if i < $size {
|
|
|
|
ret.0[$size - i - 1] = (value & 0xff) as u8;
|
|
|
|
value >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
2016-01-13 18:58:57 +01:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
impl From<&'static str> for $from {
|
|
|
|
fn from(s: &'static str) -> $from {
|
|
|
|
let s = clean_0x(s);
|
2016-01-13 18:58:57 +01:00
|
|
|
if s.len() % 2 == 1 {
|
2016-07-25 16:09:47 +02:00
|
|
|
$from::from_str(&("0".to_owned() + s)).unwrap()
|
2016-01-13 18:58:57 +01:00
|
|
|
} else {
|
2016-07-25 16:09:47 +02:00
|
|
|
$from::from_str(s).unwrap()
|
2016-01-13 18:58:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 19:26:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 01:27:02 +01:00
|
|
|
impl From<U256> for H256 {
|
|
|
|
fn from(value: U256) -> H256 {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret = H256::new();
|
2016-07-10 20:18:23 +02:00
|
|
|
value.to_big_endian(&mut ret);
|
2016-06-07 16:04:26 +02:00
|
|
|
ret
|
2016-01-14 01:27:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 11:36:30 +02:00
|
|
|
impl<'a> From<&'a U256> for H256 {
|
|
|
|
fn from(value: &'a U256) -> H256 {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret: H256 = H256::new();
|
2016-07-10 20:18:23 +02:00
|
|
|
value.to_big_endian(&mut ret);
|
2016-06-07 16:04:26 +02:00
|
|
|
ret
|
2016-01-13 18:41:33 +01:00
|
|
|
}
|
2015-12-03 19:07:08 +01:00
|
|
|
}
|
|
|
|
|
2016-01-25 13:39:15 +01:00
|
|
|
impl From<H256> for U256 {
|
|
|
|
fn from(value: H256) -> U256 {
|
2016-07-06 11:23:29 +02:00
|
|
|
U256::from(value.as_slice())
|
2016-01-25 13:39:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 11:36:30 +02:00
|
|
|
impl<'a> From<&'a H256> for U256 {
|
|
|
|
fn from(value: &'a H256) -> U256 {
|
2016-07-06 11:23:29 +02:00
|
|
|
U256::from(value.as_slice())
|
2016-01-25 13:39:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 00:31:43 +01:00
|
|
|
impl From<H256> for Address {
|
|
|
|
fn from(value: H256) -> Address {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret = Address::new();
|
|
|
|
ret.0.copy_from_slice(&value[12..32]);
|
|
|
|
ret
|
2015-12-24 00:31:43 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-14 16:56:59 +01:00
|
|
|
|
|
|
|
impl From<H256> for H64 {
|
|
|
|
fn from(value: H256) -> H64 {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret = H64::new();
|
|
|
|
ret.0.copy_from_slice(&value[20..28]);
|
|
|
|
ret
|
2016-01-14 16:56:59 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-07 16:04:26 +02:00
|
|
|
|
2015-12-24 00:31:43 +01:00
|
|
|
impl From<Address> for H256 {
|
|
|
|
fn from(value: Address) -> H256 {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret = H256::new();
|
|
|
|
ret.0[12..32].copy_from_slice(&value);
|
|
|
|
ret
|
2015-12-24 00:31:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 11:36:30 +02:00
|
|
|
impl<'a> From<&'a Address> for H256 {
|
|
|
|
fn from(value: &'a Address) -> H256 {
|
2016-06-07 16:04:26 +02:00
|
|
|
let mut ret = H256::new();
|
2016-06-07 16:18:50 +02:00
|
|
|
ret.0[12..32].copy_from_slice(value);
|
2016-06-07 16:04:26 +02:00
|
|
|
ret
|
2016-01-14 01:27:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Convert string `s` to an `H256`. Will panic if `s` is not 64 characters long or if any of
|
|
|
|
/// those characters are not 0-9, a-z or A-Z.
|
2016-01-07 23:59:50 +01:00
|
|
|
pub fn h256_from_hex(s: &str) -> H256 {
|
|
|
|
use std::str::FromStr;
|
|
|
|
H256::from_str(s).unwrap()
|
|
|
|
}
|
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Convert `n` to an `H256`, setting the rightmost 8 bytes.
|
2016-01-07 23:59:50 +01:00
|
|
|
pub fn h256_from_u64(n: u64) -> H256 {
|
2016-02-29 22:21:15 +01:00
|
|
|
use bigint::uint::U256;
|
2016-01-07 23:59:50 +01:00
|
|
|
H256::from(&U256::from(n))
|
|
|
|
}
|
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Convert string `s` to an `Address`. Will panic if `s` is not 40 characters long or if any of
|
|
|
|
/// those characters are not 0-9, a-z or A-Z.
|
2016-01-07 23:59:50 +01:00
|
|
|
pub fn address_from_hex(s: &str) -> Address {
|
|
|
|
use std::str::FromStr;
|
|
|
|
Address::from_str(s).unwrap()
|
|
|
|
}
|
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Convert `n` to an `Address`, setting the rightmost 8 bytes.
|
2016-01-07 23:59:50 +01:00
|
|
|
pub fn address_from_u64(n: u64) -> Address {
|
|
|
|
let h256 = h256_from_u64(n);
|
|
|
|
From::from(h256)
|
|
|
|
}
|
|
|
|
|
2015-11-28 22:54:12 +01:00
|
|
|
impl_hash!(H32, 4);
|
2015-11-27 13:16:32 +01:00
|
|
|
impl_hash!(H64, 8);
|
|
|
|
impl_hash!(H128, 16);
|
2015-11-25 19:26:40 +01:00
|
|
|
impl_hash!(Address, 20);
|
2015-11-27 13:16:32 +01:00
|
|
|
impl_hash!(H256, 32);
|
2015-12-13 22:44:28 +01:00
|
|
|
impl_hash!(H264, 33);
|
2015-11-27 13:16:32 +01:00
|
|
|
impl_hash!(H512, 64);
|
|
|
|
impl_hash!(H520, 65);
|
|
|
|
impl_hash!(H1024, 128);
|
|
|
|
impl_hash!(H2048, 256);
|
2015-11-25 19:26:40 +01:00
|
|
|
|
2016-08-03 22:03:40 +02:00
|
|
|
// Specialized HashMap and HashSet
|
|
|
|
|
|
|
|
/// Hasher that just takes 8 bytes of the provided value.
|
2016-08-05 09:15:55 +02:00
|
|
|
/// May only be used for keys which are 32 bytes.
|
|
|
|
pub struct PlainHasher {
|
|
|
|
prefix: [u8; 8],
|
|
|
|
_marker: [u64; 0], // for alignment
|
|
|
|
}
|
2016-08-03 22:03:40 +02:00
|
|
|
|
|
|
|
impl Default for PlainHasher {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> PlainHasher {
|
2016-08-05 09:15:55 +02:00
|
|
|
PlainHasher {
|
|
|
|
prefix: [0; 8],
|
|
|
|
_marker: [0; 0],
|
|
|
|
}
|
2016-08-03 22:03:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hasher for PlainHasher {
|
|
|
|
#[inline]
|
|
|
|
fn finish(&self) -> u64 {
|
2016-08-05 09:15:55 +02:00
|
|
|
unsafe { ::std::mem::transmute(self.prefix) }
|
2016-08-03 22:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, bytes: &[u8]) {
|
|
|
|
debug_assert!(bytes.len() == 32);
|
2016-08-05 09:15:55 +02:00
|
|
|
|
|
|
|
for quarter in bytes.chunks(8) {
|
|
|
|
for (x, y) in self.prefix.iter_mut().zip(quarter) {
|
|
|
|
*x ^= *y
|
|
|
|
}
|
|
|
|
}
|
2016-08-03 22:03:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Specialized version of HashMap with H256 keys and fast hashing function.
|
|
|
|
pub type H256FastMap<T> = HashMap<H256, T, BuildHasherDefault<PlainHasher>>;
|
|
|
|
/// Specialized version of HashSet with H256 keys and fast hashing function.
|
|
|
|
pub type H256FastSet = HashSet<H256, BuildHasherDefault<PlainHasher>>;
|
|
|
|
|
2015-11-28 03:02:06 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use hash::*;
|
2016-02-29 22:21:15 +01:00
|
|
|
use bigint::uint::*;
|
2015-11-28 03:02:06 +01:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2016-08-05 09:15:55 +02:00
|
|
|
#[test]
|
|
|
|
fn hasher_alignment() {
|
|
|
|
use std::mem::align_of;
|
|
|
|
assert_eq!(align_of::<u64>(), align_of::<PlainHasher>());
|
|
|
|
}
|
|
|
|
|
2015-11-28 03:02:06 +01:00
|
|
|
#[test]
|
2016-03-11 11:16:49 +01:00
|
|
|
#[cfg_attr(feature="dev", allow(eq_op))]
|
2015-11-28 03:02:06 +01:00
|
|
|
fn hash() {
|
|
|
|
let h = H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]);
|
|
|
|
assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h);
|
2016-02-14 12:54:27 +01:00
|
|
|
assert_eq!(format!("{}", h), "0123…cdef");
|
2015-11-28 03:02:06 +01:00
|
|
|
assert_eq!(format!("{:?}", h), "0123456789abcdef");
|
2015-11-28 22:54:12 +01:00
|
|
|
assert_eq!(h.hex(), "0123456789abcdef");
|
2015-11-28 03:02:06 +01:00
|
|
|
assert!(h == h);
|
|
|
|
assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee]));
|
|
|
|
assert!(h != H64([0; 8]));
|
|
|
|
}
|
2015-11-27 23:24:01 +01:00
|
|
|
|
2015-11-28 03:02:06 +01:00
|
|
|
#[test]
|
|
|
|
fn hash_bitor() {
|
|
|
|
let a = H64([1; 8]);
|
|
|
|
let b = H64([2; 8]);
|
|
|
|
let c = H64([3; 8]);
|
2015-11-27 23:24:01 +01:00
|
|
|
|
2015-11-28 03:02:06 +01:00
|
|
|
// borrow
|
|
|
|
assert_eq!(&a | &b, c);
|
|
|
|
|
|
|
|
// move
|
|
|
|
assert_eq!(a | b, c);
|
|
|
|
}
|
2015-11-27 23:24:01 +01:00
|
|
|
|
2015-11-28 03:02:06 +01:00
|
|
|
#[test]
|
2016-01-09 23:47:46 +01:00
|
|
|
fn shift_bloomed() {
|
2015-11-28 03:02:06 +01:00
|
|
|
use sha3::Hashable;
|
2015-12-03 19:07:08 +01:00
|
|
|
|
2015-11-28 03:02:06 +01:00
|
|
|
let bloom = H2048::from_str("00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
|
|
|
|
let address = Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap();
|
|
|
|
let topic = H256::from_str("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc").unwrap();
|
|
|
|
|
|
|
|
let mut my_bloom = H2048::new();
|
2016-01-09 23:47:46 +01:00
|
|
|
assert!(!my_bloom.contains_bloomed(&address.sha3()));
|
|
|
|
assert!(!my_bloom.contains_bloomed(&topic.sha3()));
|
2015-11-28 03:02:06 +01:00
|
|
|
|
2016-01-09 23:47:46 +01:00
|
|
|
my_bloom.shift_bloomed(&address.sha3());
|
|
|
|
assert!(my_bloom.contains_bloomed(&address.sha3()));
|
|
|
|
assert!(!my_bloom.contains_bloomed(&topic.sha3()));
|
2015-12-03 19:07:08 +01:00
|
|
|
|
2016-01-09 23:47:46 +01:00
|
|
|
my_bloom.shift_bloomed(&topic.sha3());
|
2015-11-28 03:02:06 +01:00
|
|
|
assert_eq!(my_bloom, bloom);
|
2016-01-09 23:47:46 +01:00
|
|
|
assert!(my_bloom.contains_bloomed(&address.sha3()));
|
|
|
|
assert!(my_bloom.contains_bloomed(&topic.sha3()));
|
2015-11-28 03:02:06 +01:00
|
|
|
}
|
2015-12-24 00:31:43 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_and_to_address() {
|
|
|
|
let address = Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap();
|
|
|
|
let h = H256::from(address.clone());
|
|
|
|
let a = Address::from(h);
|
|
|
|
assert_eq!(address, a);
|
|
|
|
}
|
2016-01-13 18:58:57 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_u64() {
|
|
|
|
assert_eq!(H128::from(0x1234567890abcdef), H128::from_str("00000000000000001234567890abcdef").unwrap());
|
|
|
|
assert_eq!(H64::from(0x1234567890abcdef), H64::from_str("1234567890abcdef").unwrap());
|
|
|
|
assert_eq!(H32::from(0x1234567890abcdef), H32::from_str("90abcdef").unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_str() {
|
|
|
|
assert_eq!(H64::from(0x1234567890abcdef), H64::from("0x1234567890abcdef"));
|
|
|
|
assert_eq!(H64::from(0x1234567890abcdef), H64::from("1234567890abcdef"));
|
|
|
|
assert_eq!(H64::from(0x234567890abcdef), H64::from("0x234567890abcdef"));
|
|
|
|
}
|
2016-01-25 13:39:15 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_and_to_u256() {
|
2016-05-31 16:40:48 +02:00
|
|
|
let u: U256 = 0x123456789abcdef0u64.into();
|
2016-01-25 13:39:15 +01:00
|
|
|
let h = H256::from(u);
|
|
|
|
assert_eq!(H256::from(u), H256::from("000000000000000000000000000000000000000000000000123456789abcdef0"));
|
|
|
|
let h_ref = H256::from(&u);
|
|
|
|
assert_eq!(h, h_ref);
|
|
|
|
let r_ref: U256 = From::from(&h);
|
|
|
|
assert_eq!(r_ref, u);
|
|
|
|
let r: U256 = From::from(h);
|
|
|
|
assert_eq!(r, u);
|
|
|
|
}
|
2015-11-27 23:24:01 +01:00
|
|
|
}
|