2017-05-11 15:34:35 +02:00
|
|
|
// Copyright 2015-2017 Parity Technologies
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2015-12-07 17:20:15 +01:00
|
|
|
//! General hash types, a fixed-size raw-data type used as the output of hash functions.
|
|
|
|
|
2017-07-06 11:26:14 +02:00
|
|
|
use std::{ops, fmt, cmp, str};
|
2016-08-15 15:09:00 +02:00
|
|
|
use std::cmp::{min, Ordering};
|
|
|
|
use std::ops::{Deref, DerefMut, BitXor, BitAnd, BitOr, IndexMut, Index};
|
2016-08-03 22:03:40 +02:00
|
|
|
use std::hash::{Hash, Hasher, BuildHasherDefault};
|
|
|
|
use std::collections::{HashMap, HashSet};
|
2017-07-14 20:40:28 +02:00
|
|
|
use rand::{Rand, Rng};
|
2015-11-26 23:24:01 +01:00
|
|
|
use rand::os::OsRng;
|
2017-07-06 11:26:14 +02:00
|
|
|
use rustc_hex::{FromHex, FromHexError};
|
2017-08-17 20:40:10 +02:00
|
|
|
use plain_hasher::PlainHasher;
|
2017-05-24 12:31:33 +02:00
|
|
|
use bigint::U256;
|
2017-01-13 09:52:23 +01:00
|
|
|
use libc::{c_void, memcmp};
|
2015-11-25 19:26:40 +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-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-09-28 23:32:32 +02:00
|
|
|
|
2016-04-30 17:41:24 +02:00
|
|
|
impl From<[u8; $size]> for $from {
|
|
|
|
fn from(bytes: [u8; $size]) -> Self {
|
|
|
|
$from(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:09:00 +02:00
|
|
|
impl From<$from> for [u8; $size] {
|
|
|
|
fn from(s: $from) -> Self {
|
|
|
|
s.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
impl $from {
|
|
|
|
/// Create a new, zero-initialised, instance.
|
|
|
|
pub fn new() -> $from {
|
2015-11-28 01:38:00 +01:00
|
|
|
$from([0; $size])
|
|
|
|
}
|
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Synonym for `new()`. Prefer to new as it's more readable.
|
|
|
|
pub fn zero() -> $from {
|
2016-01-09 22:30:13 +01:00
|
|
|
$from([0; $size])
|
|
|
|
}
|
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Create a new, cryptographically random, instance.
|
|
|
|
pub 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
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Assign self have a cryptographically random value.
|
|
|
|
pub fn randomize(&mut self) {
|
2015-11-26 23:24:01 +01:00
|
|
|
let mut rng = OsRng::new().unwrap();
|
2017-07-14 20:40:28 +02:00
|
|
|
*self= $from::rand(&mut rng);
|
2015-11-26 23:24:01 +01:00
|
|
|
}
|
2015-11-27 17:54:33 +01:00
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Get the size of this object in bytes.
|
|
|
|
pub fn len() -> usize {
|
2015-11-28 11:25:00 +01:00
|
|
|
$size
|
|
|
|
}
|
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
#[inline]
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Assign self to be of the same value as a slice of bytes of length `len()`.
|
|
|
|
pub 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
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Convert a slice of bytes of length `len()` to an instance of this type.
|
|
|
|
pub fn from_slice(src: &[u8]) -> Self {
|
2015-11-29 02:11:56 +01:00
|
|
|
let mut r = Self::new();
|
|
|
|
r.clone_from_slice(src);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Copy the data of this object into some mutable slice of length `len()`.
|
|
|
|
pub 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
|
|
|
}
|
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Returns `true` if all bits set in `b` are also set in `self`.
|
|
|
|
pub fn contains<'a>(&'a self, b: &'a Self) -> bool {
|
2015-11-28 19:11:04 +01:00
|
|
|
&(b & self) == b
|
2015-11-28 01:38:00 +01:00
|
|
|
}
|
2016-01-09 10:20:07 +01:00
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Returns `true` if no bits are set.
|
|
|
|
pub fn is_zero(&self) -> bool {
|
2016-01-09 10:20:07 +01:00
|
|
|
self.eq(&Self::new())
|
|
|
|
}
|
2016-01-27 14:44:02 +01:00
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
/// Returns the lowest 8 bytes interpreted as a BigEndian integer.
|
|
|
|
pub fn low_u64(&self) -> u64 {
|
2016-01-27 14:44:02 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-06 11:26:14 +02:00
|
|
|
impl str::FromStr for $from {
|
2016-07-25 20:19:33 +02:00
|
|
|
type Err = FromHexError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<$from, FromHexError> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let a = s.from_hex()?;
|
2016-07-25 20:19:33 +02:00
|
|
|
if a.len() != $size {
|
|
|
|
return Err(FromHexError::InvalidHexLength);
|
2015-11-25 19:26:40 +01:00
|
|
|
}
|
2016-07-25 20:19:33 +02:00
|
|
|
|
|
|
|
let mut ret = [0;$size];
|
|
|
|
ret.copy_from_slice(&a);
|
|
|
|
Ok($from(ret))
|
2015-11-25 19:26:40 +01:00
|
|
|
}
|
|
|
|
}
|
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[..] {
|
2016-12-27 12:53:56 +01:00
|
|
|
write!(f, "{:02x}", i)?;
|
2015-11-26 00:02:43 +01:00
|
|
|
}
|
|
|
|
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-12-27 12:53:56 +01:00
|
|
|
write!(f, "{:02x}", i)?;
|
2016-01-14 12:27:41 +01:00
|
|
|
}
|
2016-12-27 12:53:56 +01:00
|
|
|
write!(f, "…")?;
|
2016-02-14 12:54:27 +01:00
|
|
|
for i in &self.0[$size - 2..$size] {
|
2016-12-27 12:53:56 +01:00
|
|
|
write!(f, "{:02x}", i)?;
|
2016-01-18 00:51:55 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 23:32:32 +02:00
|
|
|
impl Eq for $from {}
|
|
|
|
|
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 {
|
2017-01-13 09:52:23 +01:00
|
|
|
unsafe { memcmp(self.0.as_ptr() as *const c_void, other.0.as_ptr() as *const c_void, $size) == 0 }
|
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 {
|
2017-01-13 09:52:23 +01:00
|
|
|
let r = unsafe { memcmp(self.0.as_ptr() as *const c_void, other.0.as_ptr() as *const c_void, $size) };
|
|
|
|
if r < 0 { return Ordering::Less }
|
|
|
|
if r > 0 { return Ordering::Greater }
|
|
|
|
return Ordering::Equal;
|
2016-01-13 12:16:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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 {
|
2017-07-06 11:26:14 +02:00
|
|
|
("0".to_owned() + s).parse().unwrap()
|
2016-01-13 18:58:57 +01:00
|
|
|
} else {
|
2017-07-06 11:26:14 +02:00
|
|
|
s.parse().unwrap()
|
2016-01-13 18:58:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 12:05:10 +01:00
|
|
|
|
|
|
|
impl<'a> From<&'a [u8]> for $from {
|
|
|
|
fn from(s: &'a [u8]) -> $from {
|
|
|
|
$from::from_slice(s)
|
|
|
|
}
|
|
|
|
}
|
2017-07-14 20:40:28 +02:00
|
|
|
|
|
|
|
impl Rand for $from {
|
|
|
|
fn rand<R: Rng>(r: &mut R) -> Self {
|
|
|
|
let mut hash = $from::new();
|
|
|
|
r.fill_bytes(&mut hash.0);
|
|
|
|
hash
|
|
|
|
}
|
|
|
|
}
|
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-25 20:19:33 +02:00
|
|
|
U256::from(&value)
|
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-25 20:19:33 +02:00
|
|
|
U256::from(value.as_ref() as &[u8])
|
2016-01-25 13:39:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:09:00 +02:00
|
|
|
impl From<H256> for H160 {
|
|
|
|
fn from(value: H256) -> H160 {
|
|
|
|
let mut ret = H160::new();
|
2016-06-07 16:04:26 +02:00
|
|
|
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
|
|
|
|
2016-08-15 15:09:00 +02:00
|
|
|
impl From<H160> for H256 {
|
|
|
|
fn from(value: H160) -> 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-08-15 15:09:00 +02:00
|
|
|
impl<'a> From<&'a H160> for H256 {
|
|
|
|
fn from(value: &'a H160) -> 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2016-08-15 15:09:00 +02:00
|
|
|
impl_hash!(H160, 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
|
|
|
|
2017-07-06 11:26:14 +02:00
|
|
|
#[cfg(feature="heapsizeof")]
|
2016-08-15 15:09:00 +02:00
|
|
|
known_heap_size!(0, H32, H64, H128, H160, H256, H264, H512, H520, H1024, H2048);
|
2016-08-03 22:03:40 +02:00
|
|
|
// Specialized HashMap and HashSet
|
|
|
|
|
2016-08-10 16:29:40 +02:00
|
|
|
/// Specialized version of `HashMap` with H256 keys and fast hashing function.
|
2016-08-03 22:03:40 +02:00
|
|
|
pub type H256FastMap<T> = HashMap<H256, T, BuildHasherDefault<PlainHasher>>;
|
2016-08-10 16:29:40 +02:00
|
|
|
/// Specialized version of `HashSet` with H256 keys and fast hashing function.
|
2016-08-03 22:03:40 +02:00
|
|
|
pub type H256FastSet = HashSet<H256, BuildHasherDefault<PlainHasher>>;
|
2016-07-25 20:19:33 +02:00
|
|
|
|
2015-11-28 03:02:06 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use hash::*;
|
|
|
|
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-12-24 00:31:43 +01:00
|
|
|
#[test]
|
|
|
|
fn from_and_to_address() {
|
2016-08-15 15:09:00 +02:00
|
|
|
let address: H160 = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into();
|
2015-12-24 00:31:43 +01:00
|
|
|
let h = H256::from(address.clone());
|
2016-08-15 15:09:00 +02:00
|
|
|
let a = H160::from(h);
|
2015-12-24 00:31:43 +01:00
|
|
|
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
|
|
|
}
|