Merge pull request #1236 from rphmeier/hash_unsafety
remove unsafety from util/hash.rs and util/bigint/uint.rs
This commit is contained in:
commit
3e7d8bf1a8
@ -92,8 +92,8 @@ macro_rules! uint_overflowing_add_reg {
|
||||
macro_rules! uint_overflowing_add {
|
||||
(U256, $n_words: expr, $self_expr: expr, $other: expr) => ({
|
||||
let mut result: [u64; 4] = unsafe { mem::uninitialized() };
|
||||
let self_t: &[u64; 4] = unsafe { &mem::transmute($self_expr) };
|
||||
let other_t: &[u64; 4] = unsafe { &mem::transmute($other) };
|
||||
let self_t: &[u64; 4] = &self.0;
|
||||
let other_t: &[u64; 4] = &other.0;
|
||||
|
||||
let overflow: u8;
|
||||
unsafe {
|
||||
@ -115,8 +115,8 @@ macro_rules! uint_overflowing_add {
|
||||
});
|
||||
(U512, $n_words: expr, $self_expr: expr, $other: expr) => ({
|
||||
let mut result: [u64; 8] = unsafe { mem::uninitialized() };
|
||||
let self_t: &[u64; 8] = unsafe { &mem::transmute($self_expr) };
|
||||
let other_t: &[u64; 8] = unsafe { &mem::transmute($other) };
|
||||
let self_t: &[u64; 8] = &self.0;
|
||||
let other_t: &[u64; 8] = &other.0;
|
||||
|
||||
let overflow: u8;
|
||||
|
||||
@ -196,8 +196,8 @@ macro_rules! uint_overflowing_sub_reg {
|
||||
macro_rules! uint_overflowing_sub {
|
||||
(U256, $n_words: expr, $self_expr: expr, $other: expr) => ({
|
||||
let mut result: [u64; 4] = unsafe { mem::uninitialized() };
|
||||
let self_t: &[u64; 4] = unsafe { &mem::transmute($self_expr) };
|
||||
let other_t: &[u64; 4] = unsafe { &mem::transmute($other) };
|
||||
let self_t: &[u64; 4] = &self.0;
|
||||
let other_t: &[u64; 4] = &other.0;
|
||||
|
||||
let overflow: u8;
|
||||
unsafe {
|
||||
@ -218,8 +218,8 @@ macro_rules! uint_overflowing_sub {
|
||||
});
|
||||
(U512, $n_words: expr, $self_expr: expr, $other: expr) => ({
|
||||
let mut result: [u64; 8] = unsafe { mem::uninitialized() };
|
||||
let self_t: &[u64; 8] = unsafe { &mem::transmute($self_expr) };
|
||||
let other_t: &[u64; 8] = unsafe { &mem::transmute($other) };
|
||||
let self_t: &[u64; 8] = &self.0;
|
||||
let other_t: &[u64; 8] = &other.0;
|
||||
|
||||
let overflow: u8;
|
||||
|
||||
@ -270,8 +270,8 @@ macro_rules! uint_overflowing_sub {
|
||||
macro_rules! uint_overflowing_mul {
|
||||
(U256, $n_words: expr, $self_expr: expr, $other: expr) => ({
|
||||
let mut result: [u64; 4] = unsafe { mem::uninitialized() };
|
||||
let self_t: &[u64; 4] = unsafe { &mem::transmute($self_expr) };
|
||||
let other_t: &[u64; 4] = unsafe { &mem::transmute($other) };
|
||||
let self_t: &[u64; 4] = &self.0;
|
||||
let other_t: &[u64; 4] = &self.0;
|
||||
|
||||
let overflow: u64;
|
||||
unsafe {
|
||||
@ -548,6 +548,7 @@ pub trait Uint: Sized + Default + FromStr + From<u64> + fmt::Debug + fmt::Displa
|
||||
macro_rules! construct_uint {
|
||||
($name:ident, $n_words:expr) => (
|
||||
/// Little-endian large integer type
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
pub struct $name(pub [u64; $n_words]);
|
||||
|
||||
@ -1132,8 +1133,8 @@ impl U256 {
|
||||
/// No overflow possible
|
||||
#[cfg(all(asm_available, target_arch="x86_64"))]
|
||||
pub fn full_mul(self, other: U256) -> U512 {
|
||||
let self_t: &[u64; 4] = unsafe { &mem::transmute(self) };
|
||||
let other_t: &[u64; 4] = unsafe { &mem::transmute(other) };
|
||||
let self_t: &[u64; 4] = &self.0;
|
||||
let other_t: &[u64; 4] = &other.0;
|
||||
let mut result: [u64; 8] = unsafe { mem::uninitialized() };
|
||||
unsafe {
|
||||
asm!("
|
||||
|
114
util/src/hash.rs
114
util/src/hash.rs
@ -143,6 +143,7 @@ macro_rules! impl_hash {
|
||||
}
|
||||
min
|
||||
}
|
||||
|
||||
fn from_slice(src: &[u8]) -> Self {
|
||||
let mut r = Self::new();
|
||||
r.clone_from_slice(src);
|
||||
@ -150,24 +151,15 @@ macro_rules! impl_hash {
|
||||
}
|
||||
|
||||
fn copy_to(&self, dest: &mut[u8]) {
|
||||
unsafe {
|
||||
let min = ::std::cmp::min($size, dest.len());
|
||||
::std::ptr::copy(self.0.as_ptr(), dest.as_mut_ptr(), min);
|
||||
}
|
||||
let min = ::std::cmp::min($size, dest.len());
|
||||
dest[..min].copy_from_slice(&self.0[..min]);
|
||||
}
|
||||
|
||||
fn shift_bloomed<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: FixedHash {
|
||||
let bp: Self = b.bloom_part($size);
|
||||
let new_self = &bp | self;
|
||||
|
||||
// impl |= instead
|
||||
// TODO: that's done now!
|
||||
|
||||
unsafe {
|
||||
use std::{mem, ptr};
|
||||
ptr::copy(new_self.0.as_ptr(), self.0.as_mut_ptr(), mem::size_of::<Self>());
|
||||
}
|
||||
|
||||
self.0 = new_self.0;
|
||||
self
|
||||
}
|
||||
|
||||
@ -179,7 +171,6 @@ macro_rules! impl_hash {
|
||||
let bloom_bits = m * 8;
|
||||
let mask = bloom_bits - 1;
|
||||
let bloom_bytes = (log2(bloom_bits) + 7) / 8;
|
||||
//println!("bb: {}", bloom_bytes);
|
||||
|
||||
// must be a power of 2
|
||||
assert_eq!(m & (m - 1), 0);
|
||||
@ -316,12 +307,9 @@ macro_rules! impl_hash {
|
||||
#[cfg_attr(feature="dev", allow(expl_impl_clone_on_copy))]
|
||||
impl Clone for $from {
|
||||
fn clone(&self) -> $from {
|
||||
unsafe {
|
||||
use std::{mem, ptr};
|
||||
let mut ret: $from = mem::uninitialized();
|
||||
ptr::copy(self.0.as_ptr(), ret.0.as_mut_ptr(), mem::size_of::<$from>());
|
||||
ret
|
||||
}
|
||||
let mut ret = $from::new();
|
||||
ret.0.copy_from_slice(&self.0);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
@ -404,14 +392,11 @@ macro_rules! impl_hash {
|
||||
type Output = $from;
|
||||
|
||||
fn bitor(self, rhs: Self) -> Self::Output {
|
||||
unsafe {
|
||||
use std::mem;
|
||||
let mut ret: $from = mem::uninitialized();
|
||||
for i in 0..$size {
|
||||
ret.0[i] = self.0[i] | rhs.0[i];
|
||||
}
|
||||
ret
|
||||
let mut ret: $from = $from::default();
|
||||
for i in 0..$size {
|
||||
ret.0[i] = self.0[i] | rhs.0[i];
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
@ -429,14 +414,11 @@ macro_rules! impl_hash {
|
||||
type Output = $from;
|
||||
|
||||
fn bitand(self, rhs: Self) -> Self::Output {
|
||||
unsafe {
|
||||
use std::mem;
|
||||
let mut ret: $from = mem::uninitialized();
|
||||
for i in 0..$size {
|
||||
ret.0[i] = self.0[i] & rhs.0[i];
|
||||
}
|
||||
ret
|
||||
let mut ret: $from = $from::default();
|
||||
for i in 0..$size {
|
||||
ret.0[i] = self.0[i] & rhs.0[i];
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
@ -454,14 +436,11 @@ macro_rules! impl_hash {
|
||||
type Output = $from;
|
||||
|
||||
fn bitxor(self, rhs: Self) -> Self::Output {
|
||||
unsafe {
|
||||
use std::mem;
|
||||
let mut ret: $from = mem::uninitialized();
|
||||
for i in 0..$size {
|
||||
ret.0[i] = self.0[i] ^ rhs.0[i];
|
||||
}
|
||||
ret
|
||||
let mut ret: $from = $from::default();
|
||||
for i in 0..$size {
|
||||
ret.0[i] = self.0[i] ^ rhs.0[i];
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
@ -516,21 +495,17 @@ macro_rules! impl_hash {
|
||||
|
||||
impl From<U256> for H256 {
|
||||
fn from(value: U256) -> H256 {
|
||||
unsafe {
|
||||
let mut ret: H256 = ::std::mem::uninitialized();
|
||||
value.to_raw_bytes(&mut ret);
|
||||
ret
|
||||
}
|
||||
let mut ret = H256::new();
|
||||
value.to_raw_bytes(&mut ret);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a U256> for H256 {
|
||||
fn from(value: &'a U256) -> H256 {
|
||||
unsafe {
|
||||
let mut ret: H256 = ::std::mem::uninitialized();
|
||||
value.to_raw_bytes(&mut ret);
|
||||
ret
|
||||
}
|
||||
let mut ret: H256 = H256::new();
|
||||
value.to_raw_bytes(&mut ret);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
@ -548,51 +523,44 @@ impl<'a> From<&'a H256> for U256 {
|
||||
|
||||
impl From<H256> for Address {
|
||||
fn from(value: H256) -> Address {
|
||||
unsafe {
|
||||
let mut ret: Address = ::std::mem::uninitialized();
|
||||
::std::ptr::copy(value.as_ptr().offset(12), ret.as_mut_ptr(), 20);
|
||||
ret
|
||||
}
|
||||
let mut ret = Address::new();
|
||||
ret.0.copy_from_slice(&value[12..32]);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl From<H256> for H64 {
|
||||
fn from(value: H256) -> H64 {
|
||||
unsafe {
|
||||
let mut ret: H64 = ::std::mem::uninitialized();
|
||||
::std::ptr::copy(value.as_ptr().offset(20), ret.as_mut_ptr(), 8);
|
||||
ret
|
||||
}
|
||||
let mut ret = H64::new();
|
||||
ret.0.copy_from_slice(&value[20..28]);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
impl<'a> From<&'a H256> for Address {
|
||||
fn from(value: &'a H256) -> Address {
|
||||
unsafe {
|
||||
let mut ret: Address = ::std::mem::uninitialized();
|
||||
::std::ptr::copy(value.as_ptr().offset(12), ret.as_mut_ptr(), 20);
|
||||
let mut ret = Address::new();
|
||||
ret.0.copy_from_slice(&value[12..32]);
|
||||
ret
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
impl From<Address> for H256 {
|
||||
fn from(value: Address) -> H256 {
|
||||
unsafe {
|
||||
let mut ret = H256::new();
|
||||
::std::ptr::copy(value.as_ptr(), ret.as_mut_ptr().offset(12), 20);
|
||||
ret
|
||||
}
|
||||
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 {
|
||||
unsafe {
|
||||
let mut ret = H256::new();
|
||||
::std::ptr::copy(value.as_ptr(), ret.as_mut_ptr().offset(12), 20);
|
||||
ret
|
||||
}
|
||||
let mut ret = H256::new();
|
||||
ret.0[12..32].copy_from_slice(value);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user