Rename canonical_hash to `hash

This commit is contained in:
Paul Hauner 2019-01-25 17:40:22 +11:00
parent 73d86bcc3b
commit f9acc42aca
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
11 changed files with 22 additions and 27 deletions

View File

@ -12,6 +12,7 @@ db = { path = "../db" }
failure = "0.1"
failure_derive = "0.1"
genesis = { path = "../../eth2/genesis" }
hashing = { path = "../../eth2/utils/hashing" }
serde_derive = "1.0"
slot_clock = { path = "../../eth2/utils/slot_clock" }
ssz = { path = "../../eth2/utils/ssz" }

View File

@ -1,6 +1,7 @@
use super::{BeaconChain, ClientDB, DBError, SlotClock};
use bls::{AggregatePublicKey, AggregateSignature, PublicKey, Signature};
use boolean_bitfield::BooleanBitfield;
use hashing::hash;
use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError};
use ssz::ssz_encode;
use types::{
@ -127,12 +128,13 @@ where
Error::BadRandaoSignature
);
// TODO: check this is correct.
let new_mix = {
let mut mix = state.latest_randao_mixes
[(state.slot % self.spec.latest_randao_mixes_length) as usize]
.to_vec();
mix.append(&mut ssz_encode(&block.randao_reveal));
hash(&mix)
Hash256::from(&hash(&mix)[..])
};
state.latest_randao_mixes[(state.slot % self.spec.latest_randao_mixes_length) as usize] =
@ -396,11 +398,6 @@ fn penalize_validator(_state: &BeaconState, _proposer_index: usize) {
// TODO: stubbed out.
}
fn hash<T>(_input: &T) -> Hash256 {
// TODO: stubbed out.
Hash256::zero()
}
fn get_domain(_fork: &Fork, _slot: u64, _domain_type: u64) -> u64 {
// TODO: stubbed out.
0

View File

@ -1,9 +1,8 @@
use super::{BeaconBlockBody, Eth1Data, Hash256};
use crate::test_utils::TestRandom;
use bls::Signature;
use hashing::canonical_hash;
use rand::RngCore;
use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
mod signing;

View File

@ -6,9 +6,8 @@ use super::pending_attestation::PendingAttestation;
use super::validator::Validator;
use super::Hash256;
use crate::test_utils::TestRandom;
use hashing::canonical_hash;
use rand::RngCore;
use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
mod slot_advance;

View File

@ -18,7 +18,7 @@ pub use self::bls_aggregates::AggregatePublicKey;
pub const BLS_AGG_SIG_BYTE_SIZE: usize = 97;
use hashing::canonical_hash;
use hashing::hash;
use ssz::ssz_encode;
use std::default::Default;
@ -30,13 +30,13 @@ fn extend_if_needed(hash: &mut Vec<u8>) {
/// For some signature and public key, ensure that the signature message was the public key and it
/// was signed by the secret key that corresponds to that public key.
pub fn verify_proof_of_possession(sig: &Signature, pubkey: &PublicKey) -> bool {
let mut hash = canonical_hash(&ssz_encode(pubkey));
let mut hash = hash(&ssz_encode(pubkey));
extend_if_needed(&mut hash);
sig.verify_hashed(&hash, &pubkey)
}
pub fn create_proof_of_possession(keypair: &Keypair) -> Signature {
let mut hash = canonical_hash(&ssz_encode(&keypair.pk));
let mut hash = hash(&ssz_encode(&keypair.pk));
extend_if_needed(&mut hash);
Signature::new_hashed(&hash, &keypair.sk)
}

View File

@ -1,6 +1,6 @@
use tiny_keccak::Keccak;
pub fn canonical_hash(input: &[u8]) -> Vec<u8> {
pub fn hash(input: &[u8]) -> Vec<u8> {
let mut keccak = Keccak::new_keccak256();
keccak.update(input);
let mut result = vec![0; 32];
@ -17,7 +17,7 @@ mod tests {
fn test_hashing() {
let input: Vec<u8> = From::from("hello");
let output = canonical_hash(input.as_ref());
let output = hash(input.as_ref());
let expected = &[
0x1c, 0x8a, 0xff, 0x95, 0x06, 0x85, 0xc2, 0xed, 0x4b, 0xc3, 0x17, 0x4f, 0x34, 0x72,
0x28, 0x7b, 0x56, 0xd9, 0x51, 0x7b, 0x9c, 0x94, 0x81, 0x27, 0x31, 0x9a, 0x09, 0xa7,

View File

@ -1,5 +1,6 @@
use super::ethereum_types::{Address, H256};
use super::{hash, merkle_hash, ssz_encode, TreeHash};
use super::{merkle_hash, ssz_encode, TreeHash};
use hashing::hash;
impl TreeHash for u8 {
fn hash_tree_root(&self) -> Vec<u8> {

View File

@ -20,7 +20,9 @@ mod impl_tree_hash;
pub use crate::decode::{decode_ssz, decode_ssz_list, Decodable, DecodeError};
pub use crate::encode::{Encodable, SszStream};
pub use crate::tree_hash::{hash, merkle_hash, TreeHash};
pub use crate::tree_hash::{merkle_hash, TreeHash};
pub use hashing::hash;
pub const LENGTH_BYTES: usize = 4;
pub const MAX_LIST_SIZE: usize = 1 << (4 * 8);

View File

@ -1,4 +1,4 @@
use hashing::canonical_hash;
use hashing::hash;
const SSZ_CHUNK_SIZE: usize = 128;
const HASHSIZE: usize = 32;
@ -64,10 +64,6 @@ fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
(chunk_size, data)
}
pub fn hash(data: &[u8]) -> Vec<u8> {
canonical_hash(data)
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -45,7 +45,7 @@ mod tests {
use std::{fs::File, io::prelude::*, path::PathBuf};
use super::{hashing::canonical_hash, *};
use super::{hashing::hash, *};
#[test]
fn test_shuffling() {
@ -70,7 +70,7 @@ mod tests {
let seed_bytes = test_case["seed"].as_str().unwrap().as_bytes();
let seed = if seed_bytes.len() > 0 {
canonical_hash(seed_bytes)
hash(seed_bytes)
} else {
vec![]
};

View File

@ -1,4 +1,4 @@
use super::hashing::canonical_hash;
use super::hashing::hash;
const SEED_SIZE_BYTES: usize = 32;
const RAND_BYTES: usize = 3; // 24 / 8
@ -16,7 +16,7 @@ impl ShuffleRng {
/// Create a new instance given some "seed" bytes.
pub fn new(initial_seed: &[u8]) -> Self {
Self {
seed: canonical_hash(initial_seed),
seed: hash(initial_seed),
idx: 0,
rand_max: RAND_MAX,
}
@ -24,7 +24,7 @@ impl ShuffleRng {
/// "Regenerates" the seed by hashing it.
fn rehash_seed(&mut self) {
self.seed = canonical_hash(&self.seed);
self.seed = hash(&self.seed);
self.idx = 0;
}