openethereum/util/bigint/src/hash.rs

553 lines
12 KiB
Rust
Raw Normal View History

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.
use std::{ops, fmt, cmp, str};
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};
2015-11-26 23:24:01 +01:00
use rand::Rng;
use rand::os::OsRng;
use rustc_hex::{FromHex, FromHexError};
use bigint::U256;
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 {
cli overhaul (#1600) * cli commands * cleanup parity/signer * cleanup parity/signer * remove redundant import of signer crate from main.rs * cli cleanup in progress * cli cleanup in progress * moved few commonly used functions to separate methods with tests * cleaning up blockchain import in progress * cleaning up blockchain import in progress2 * cleaning up blockchain import in progress3 * tests for database compaction profile parsing * cleaning up blockchain import in progress4 * cleaning up blockchain import in progress5 * blockchain import * export blockchain in progress * cleanup execute_export * Configuration::to_duration cleaned up * removed unused code, tests for to_duration * cleanup Configuration::mode function * parsing some of the cli params in params.rs * rpc and signer are no longer optional * move importing extern crates to main.rs file * swipe dies from rpc module * swipe dies from dapps * finding deprecated * several tests and fixes for parity * parity cleanup in progress * cleanup price parsing * parity cleanup in progress * swiped all dies * parity cleanup in progress * replace usages of from_str with parse() in parity/params.rs * removed few more from_str * split parity/params.rs into params and helpers * removed wildcard import from configuration.rs * cleanup directories/path creation * cleaning up run cmd * moved LoggerConfig * defaults for cli params * fixed indention in raise_fd_limit * tests for rpc_apis * tests for default ipc and rpc settings * ipc socket * cleanup in progress * account service * cleanup miner config * BlockChain commands use Directiores structure now * client_config * network settings and dapps configuration * removing warnings * default logger config * fixed client_path * overhaul * fixing export && import * default export DataFormat * import and export also upgrade db * fixed export && import * polishing pr * polishing pr * fixed custom bootnodes * fixed daemonize on windows * fixed setting up enable network * finished pr * fixed compiling on windows * Fixed warning; windows build * Better cache management * Fixed tests on windows * Fixed test * Restored pruning method names * --cache alias * Fixed more tests * Ensure default options actually listed as valid [ci:skip]
2016-07-25 16:09:47 +02:00
if s.starts_with("0x") {
&s[2..]
} else {
s
}
}
2015-11-25 19:26:40 +01:00
macro_rules! impl_hash {
($from: ident, $size: expr) => {
#[repr(C)]
/// 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
impl From<[u8; $size]> for $from {
fn from(bytes: [u8; $size]) -> Self {
$from(bytes)
}
}
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
}
}
impl $from {
/// Create a new, zero-initialised, instance.
pub fn new() -> $from {
2015-11-28 01:38:00 +01:00
$from([0; $size])
}
/// Synonym for `new()`. Prefer to new as it's more readable.
pub fn zero() -> $from {
$from([0; $size])
}
/// 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
/// 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();
rng.fill_bytes(&mut self.0);
}
2015-11-27 17:54:33 +01:00
/// Get the size of this object in bytes.
pub fn len() -> usize {
$size
}
2015-11-29 02:11:56 +01:00
#[inline]
/// 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
/// 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
}
/// 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]);
}
/// 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
/// 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
/// 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
}
impl str::FromStr for $from {
2016-07-25 20:19:33 +02:00
type Err = FromHexError;
fn from_str(s: &str) -> Result<$from, FromHexError> {
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[..] {
write!(f, "{:02x}", i)?;
2015-11-26 00:02:43 +01:00
}
Ok(())
2015-11-26 23:24:01 +01: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] {
write!(f, "{:02x}", i)?;
2016-01-14 12:27:41 +01:00
}
write!(f, "")?;
2016-02-14 12:54:27 +01:00
for i in &self.0[$size - 2..$size] {
write!(f, "{:02x}", i)?;
2016-01-18 00:51:55 +01:00
}
Ok(())
2015-11-26 23:24:01 +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
}
}
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 {
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 {
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))
}
}
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
}
}
2016-04-06 10:07:24 +02:00
/// `BitOr` on references
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];
}
2016-06-07 16:04:26 +02:00
ret
}
}
2016-04-06 10:07:24 +02:00
/// Moving `BitOr`
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
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];
}
2016-06-07 16:04:26 +02:00
ret
}
}
2016-04-06 10:07:24 +02:00
/// Moving `BitAnd`
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
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];
}
2016-06-07 16:04:26 +02:00
ret
}
}
2016-04-06 10:07:24 +02:00
/// Moving `BitXor`
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 {
/// 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
}
}
cli overhaul (#1600) * cli commands * cleanup parity/signer * cleanup parity/signer * remove redundant import of signer crate from main.rs * cli cleanup in progress * cli cleanup in progress * moved few commonly used functions to separate methods with tests * cleaning up blockchain import in progress * cleaning up blockchain import in progress2 * cleaning up blockchain import in progress3 * tests for database compaction profile parsing * cleaning up blockchain import in progress4 * cleaning up blockchain import in progress5 * blockchain import * export blockchain in progress * cleanup execute_export * Configuration::to_duration cleaned up * removed unused code, tests for to_duration * cleanup Configuration::mode function * parsing some of the cli params in params.rs * rpc and signer are no longer optional * move importing extern crates to main.rs file * swipe dies from rpc module * swipe dies from dapps * finding deprecated * several tests and fixes for parity * parity cleanup in progress * cleanup price parsing * parity cleanup in progress * swiped all dies * parity cleanup in progress * replace usages of from_str with parse() in parity/params.rs * removed few more from_str * split parity/params.rs into params and helpers * removed wildcard import from configuration.rs * cleanup directories/path creation * cleaning up run cmd * moved LoggerConfig * defaults for cli params * fixed indention in raise_fd_limit * tests for rpc_apis * tests for default ipc and rpc settings * ipc socket * cleanup in progress * account service * cleanup miner config * BlockChain commands use Directiores structure now * client_config * network settings and dapps configuration * removing warnings * default logger config * fixed client_path * overhaul * fixing export && import * default export DataFormat * import and export also upgrade db * fixed export && import * polishing pr * polishing pr * fixed custom bootnodes * fixed daemonize on windows * fixed setting up enable network * finished pr * fixed compiling on windows * Fixed warning; windows build * Better cache management * Fixed tests on windows * Fixed test * Restored pruning method names * --cache alias * Fixed more tests * Ensure default options actually listed as valid [ci:skip]
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);
if s.len() % 2 == 1 {
("0".to_owned() + s).parse().unwrap()
} else {
s.parse().unwrap()
}
}
}
impl<'a> From<&'a [u8]> for $from {
fn from(s: &'a [u8]) -> $from {
$from::from_slice(s)
}
}
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();
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();
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
}
}
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
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
}
}
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);
impl_hash!(H64, 8);
impl_hash!(H128, 16);
impl_hash!(H160, 20);
impl_hash!(H256, 32);
2015-12-13 22:44:28 +01:00
impl_hash!(H264, 33);
impl_hash!(H512, 64);
impl_hash!(H520, 65);
impl_hash!(H1024, 128);
impl_hash!(H2048, 256);
2015-11-25 19:26:40 +01:00
#[cfg(feature="heapsizeof")]
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.
/// May only be used for keys which are 32 bytes.
pub struct PlainHasher {
prefix: [u8; 8],
_marker: [u64; 0], // for alignment
}
impl Default for PlainHasher {
#[inline]
fn default() -> PlainHasher {
PlainHasher {
prefix: [0; 8],
_marker: [0; 0],
}
}
}
impl Hasher for PlainHasher {
#[inline]
fn finish(&self) -> u64 {
unsafe { ::std::mem::transmute(self.prefix) }
}
#[inline]
fn write(&mut self, bytes: &[u8]) {
debug_assert!(bytes.len() == 32);
for quarter in bytes.chunks(8) {
for (x, y) in self.prefix.iter_mut().zip(quarter) {
*x ^= *y
}
}
}
}
/// 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>>;
2016-07-25 20:19:33 +02:00
#[cfg(test)]
mod tests {
use hash::*;
use std::str::FromStr;
#[test]
fn hasher_alignment() {
use std::mem::align_of;
assert_eq!(align_of::<u64>(), align_of::<PlainHasher>());
}
#[test]
2016-03-11 11:16:49 +01:00
#[cfg_attr(feature="dev", allow(eq_op))]
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");
assert_eq!(format!("{:?}", h), "0123456789abcdef");
2015-11-28 22:54:12 +01:00
assert_eq!(h.hex(), "0123456789abcdef");
assert!(h == h);
assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee]));
assert!(h != H64([0; 8]));
}
#[test]
fn hash_bitor() {
let a = H64([1; 8]);
let b = H64([2; 8]);
let c = H64([3; 8]);
// borrow
assert_eq!(&a | &b, c);
// move
assert_eq!(a | b, c);
}
2015-12-24 00:31:43 +01:00
#[test]
fn from_and_to_address() {
let address: H160 = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into();
2015-12-24 00:31:43 +01:00
let h = H256::from(address.clone());
let a = H160::from(h);
2015-12-24 00:31:43 +01:00
assert_eq!(address, a);
}
#[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);
}
}