Skip to content

Commit

Permalink
Hashable::sha3 -> fn keccak for ethcore
Browse files Browse the repository at this point in the history
  • Loading branch information
debris committed Aug 30, 2017
1 parent e120c75 commit f0e8abb
Show file tree
Hide file tree
Showing 69 changed files with 432 additions and 401 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions ethash/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use primal::is_prime;
use std::cell::Cell;
use std::mem;
use std::ptr;
use sha3;
use hash;
use std::slice;
use std::path::{Path, PathBuf};
use std::io::{self, Read, Write};
Expand Down Expand Up @@ -200,7 +200,7 @@ impl SeedHashCompute {
#[inline]
pub fn resume_compute_seedhash(mut hash: H256, start_epoch: u64, end_epoch: u64) -> H256 {
for _ in start_epoch..end_epoch {
unsafe { sha3::sha3_256(hash[..].as_mut_ptr(), 32, hash[..].as_ptr(), 32) };
unsafe { hash::keccak_256(hash[..].as_mut_ptr(), 32, hash[..].as_ptr(), 32) };
}
hash
}
Expand All @@ -214,14 +214,14 @@ fn fnv_hash(x: u32, y: u32) -> u32 {
return x.wrapping_mul(FNV_PRIME) ^ y;
}

fn sha3_512(input: &[u8], output: &mut [u8]) {
unsafe { sha3::sha3_512(output.as_mut_ptr(), output.len(), input.as_ptr(), input.len()) };
fn keccak_512(input: &[u8], output: &mut [u8]) {
unsafe { hash::keccak_512(output.as_mut_ptr(), output.len(), input.as_ptr(), input.len()) };
}

fn sha3_512_inplace(input: &mut [u8]) {
fn keccak_512_inplace(input: &mut [u8]) {
// This is safe since `sha3_*` uses an internal buffer and copies the result to the output. This
// means that we can reuse the input buffer for both input and output.
unsafe { sha3::sha3_512(input.as_mut_ptr(), input.len(), input.as_ptr(), input.len()) };
unsafe { hash::keccak_512(input.as_mut_ptr(), input.len(), input.as_ptr(), input.len()) };
}

fn get_cache_size(block_number: u64) -> usize {
Expand Down Expand Up @@ -250,23 +250,23 @@ fn get_data_size(block_number: u64) -> usize {
/// Boundary recovered from mix hash
pub fn quick_get_difficulty(header_hash: &H256, nonce: u64, mix_hash: &H256) -> H256 {
unsafe {
// This is safe - the `sha3_512` call below reads the first 40 bytes (which we explicitly set
// This is safe - the `keccak_512` call below reads the first 40 bytes (which we explicitly set
// with two `copy_nonoverlapping` calls) but writes the first 64, and then we explicitly write
// the next 32 bytes before we read the whole thing with `sha3_256`.
// the next 32 bytes before we read the whole thing with `keccak_256`.
//
// This cannot be elided by the compiler as it doesn't know the implementation of
// `sha3_512`.
// `keccak_512`.
let mut buf: [u8; 64 + 32] = mem::uninitialized();

ptr::copy_nonoverlapping(header_hash.as_ptr(), buf.as_mut_ptr(), 32);
ptr::copy_nonoverlapping(mem::transmute(&nonce), buf[32..].as_mut_ptr(), 8);

sha3::sha3_512(buf.as_mut_ptr(), 64, buf.as_ptr(), 40);
hash::keccak_512(buf.as_mut_ptr(), 64, buf.as_ptr(), 40);
ptr::copy_nonoverlapping(mix_hash.as_ptr(), buf[64..].as_mut_ptr(), 32);

// This is initialized in `sha3_256`
// This is initialized in `keccak_256`
let mut hash: [u8; 32] = mem::uninitialized();
sha3::sha3_256(hash.as_mut_ptr(), hash.len(), buf.as_ptr(), buf.len());
hash::keccak_256(hash.as_mut_ptr(), hash.len(), buf.as_ptr(), buf.len());

hash
}
Expand Down Expand Up @@ -320,7 +320,7 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64)
half_mix: unsafe {
// Pack `header_hash` and `nonce` together
// We explicitly write the first 40 bytes, leaving the last 24 as uninitialized. Then
// `sha3_512` reads the first 40 bytes (4th parameter) and overwrites the entire array,
// `keccak_512` reads the first 40 bytes (4th parameter) and overwrites the entire array,
// leaving it fully initialized.
let mut out: [u8; NODE_BYTES] = mem::uninitialized();

Expand All @@ -336,7 +336,7 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64)
);

// compute sha3-512 hash and replicate across mix
sha3::sha3_512(
hash::keccak_512(
out.as_mut_ptr(),
NODE_BYTES,
out.as_ptr(),
Expand Down Expand Up @@ -427,10 +427,10 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64)
let value: H256 = unsafe {
// We can interpret the buffer as an array of `u8`s, since it's `repr(C)`.
let read_ptr: *const u8 = mem::transmute(&buf);
// We overwrite the second half since `sha3_256` has an internal buffer and so allows
// We overwrite the second half since `keccak_256` has an internal buffer and so allows
// overlapping arrays as input.
let write_ptr: *mut u8 = mem::transmute(&mut buf.compress_bytes);
sha3::sha3_256(
hash::keccak_256(
write_ptr,
buf.compress_bytes.len(),
read_ptr,
Expand All @@ -450,7 +450,7 @@ fn calculate_dag_item(node_index: u32, cache: &[Node]) -> Node {
let mut ret = cache[node_index as usize % num_parent_nodes].clone();
ret.as_words_mut()[0] ^= node_index;

sha3_512_inplace(&mut ret.bytes);
keccak_512_inplace(&mut ret.bytes);

debug_assert_eq!(NODE_WORDS, 16);
for i in 0..ETHASH_DATASET_PARENTS as u32 {
Expand All @@ -467,7 +467,7 @@ fn calculate_dag_item(node_index: u32, cache: &[Node]) -> Node {
}
}

sha3_512_inplace(&mut ret.bytes);
keccak_512_inplace(&mut ret.bytes);

ret
}
Expand All @@ -485,9 +485,9 @@ fn light_new<T: AsRef<Path>>(cache_dir: T, block_number: u64) -> Light {
// Use uninit instead of unnecessarily writing `size_of::<Node>() * num_nodes` 0s
nodes.set_len(num_nodes);

sha3_512(&seedhash[0..32], &mut nodes.get_unchecked_mut(0).bytes);
keccak_512(&seedhash[0..32], &mut nodes.get_unchecked_mut(0).bytes);
for i in 1..num_nodes {
sha3::sha3_512(nodes.get_unchecked_mut(i).bytes.as_mut_ptr(), NODE_BYTES, nodes.get_unchecked(i - 1).bytes.as_ptr(), NODE_BYTES);
hash::keccak_512(nodes.get_unchecked_mut(i).bytes.as_mut_ptr(), NODE_BYTES, nodes.get_unchecked(i - 1).bytes.as_ptr(), NODE_BYTES);
}

debug_assert_eq!(NODE_WORDS, 16);
Expand All @@ -504,7 +504,7 @@ fn light_new<T: AsRef<Path>>(cache_dir: T, block_number: u64) -> Light {
}
}

sha3_512(&data.bytes, &mut nodes.get_unchecked_mut(i).bytes);
keccak_512(&data.bytes, &mut nodes.get_unchecked_mut(i).bytes);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ethash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#![cfg_attr(feature = "benches", feature(test))]

extern crate primal;
extern crate sha3;
extern crate hash;
extern crate parking_lot;

#[macro_use]
Expand Down
1 change: 1 addition & 0 deletions ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ table = { path = "../util/table" }
bloomable = { path = "../util/bloomable" }
vm = { path = "vm" }
wasm = { path = "wasm" }
hash = { path = "../util/hash" }

[dev-dependencies]
native-contracts = { path = "native_contracts", features = ["test_contracts"] }
Expand Down
1 change: 1 addition & 0 deletions ethcore/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ vm = { path = "../vm" }
parity-wasm = "0.12"
ethcore-logger = { path = "../../logger" }
wasm-utils = { git = "https://github.com/paritytech/wasm-utils" }
hash = { path = "../../util/hash" }

[dev-dependencies]
rustc-hex = "1.0"
Expand Down
9 changes: 5 additions & 4 deletions ethcore/evm/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod shared_cache;
use std::marker::PhantomData;
use std::{cmp, mem};
use std::sync::Arc;
use hash::keccak;

use vm::{
self, ActionParams, ActionValue, CallType, MessageCallResult,
Expand Down Expand Up @@ -180,7 +181,7 @@ impl<Cost: CostType> vm::Vm for Interpreter<Cost> {
match result {
InstructionResult::JumpToPosition(position) => {
if valid_jump_destinations.is_none() {
let code_hash = params.code_hash.clone().unwrap_or_else(|| code.sha3());
let code_hash = params.code_hash.clone().unwrap_or_else(|| keccak(code.as_ref()));
valid_jump_destinations = Some(self.cache.jump_destinations(&code_hash, code));
}
let jump_destinations = valid_jump_destinations.as_ref().expect("jump_destinations are initialized on first jump; qed");
Expand Down Expand Up @@ -464,8 +465,8 @@ impl<Cost: CostType> Interpreter<Cost> {
instructions::SHA3 => {
let offset = stack.pop_back();
let size = stack.pop_back();
let sha3 = self.mem.read_slice(offset, size).sha3();
stack.push(U256::from(&*sha3));
let k = keccak(self.mem.read_slice(offset, size));
stack.push(U256::from(&*k));
},
instructions::SLOAD => {
let key = H256::from(&stack.pop_back());
Expand Down Expand Up @@ -880,7 +881,7 @@ mod tests {
use rustc_hex::FromHex;
use vmtype::VMType;
use factory::Factory;
use vm::{self, ActionParams, ActionValue};
use vm::{ActionParams, ActionValue};
use vm::tests::{FakeExt, test_finalize};

#[test]
Expand Down
4 changes: 2 additions & 2 deletions ethcore/evm/src/interpreter/shared_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use std::sync::Arc;
use hash::KECCAK_EMPTY;
use util::{H256, HeapSizeOf, Mutex};
use util::sha3::*;
use util::cache::MemoryLruCache;
use bit_set::BitSet;
use super::super::instructions;
Expand Down Expand Up @@ -49,7 +49,7 @@ impl SharedCache {

/// Get jump destinations bitmap for a contract.
pub fn jump_destinations(&self, code_hash: &H256, code: &[u8]) -> Arc<BitSet> {
if code_hash == &SHA3_EMPTY {
if code_hash == &KECCAK_EMPTY {
return Self::find_jump_destinations(code);
}

Expand Down
1 change: 1 addition & 0 deletions ethcore/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern crate parity_wasm;
extern crate wasm_utils;
extern crate ethcore_logger;
extern crate vm;
extern crate hash;

#[macro_use]
extern crate lazy_static;
Expand Down
Loading

0 comments on commit f0e8abb

Please sign in to comment.