mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-26 01:03:40 +00:00
Merge branch 'master' into lmd-ghost
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use super::{AggregatePublicKey, Signature};
|
||||
use bls_aggregates::AggregateSignature as RawAggregateSignature;
|
||||
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
/// A BLS aggregate signature.
|
||||
///
|
||||
@@ -44,6 +44,12 @@ impl Decodable for AggregateSignature {
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for AggregateSignature {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
hash(&self.0.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::{Keypair, Signature};
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use super::SecretKey;
|
||||
use bls_aggregates::PublicKey as RawPublicKey;
|
||||
use hex::encode as hex_encode;
|
||||
use ssz::{decode_ssz_list, ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{
|
||||
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
|
||||
};
|
||||
use std::default;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
@@ -53,6 +55,12 @@ impl Decodable for PublicKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for PublicKey {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
hash(&self.0.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for PublicKey {
|
||||
fn eq(&self, other: &PublicKey) -> bool {
|
||||
ssz_encode(self) == ssz_encode(other)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use bls_aggregates::{DecodeError as BlsDecodeError, SecretKey as RawSecretKey};
|
||||
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
/// A single BLS signature.
|
||||
///
|
||||
@@ -40,6 +40,12 @@ impl Decodable for SecretKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for SecretKey {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
self.0.as_bytes().clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::{PublicKey, SecretKey};
|
||||
use bls_aggregates::Signature as RawSignature;
|
||||
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
/// A single BLS signature.
|
||||
///
|
||||
@@ -57,6 +57,12 @@ impl Decodable for Signature {
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for Signature {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
hash(&self.0.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::Keypair;
|
||||
|
||||
@@ -149,6 +149,12 @@ impl ssz::Decodable for BooleanBitfield {
|
||||
}
|
||||
}
|
||||
|
||||
impl ssz::TreeHash for BooleanBitfield {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
self.to_bytes().hash_tree_root()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -6,4 +6,3 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
tiny-keccak = "1.4.2"
|
||||
ssz = { path = "../ssz" }
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use ssz::{ssz_encode, Encodable as SszEncodable};
|
||||
use tiny_keccak::Keccak;
|
||||
|
||||
pub fn canonical_hash(input: &[u8]) -> Vec<u8> {
|
||||
@@ -9,10 +8,6 @@ pub fn canonical_hash(input: &[u8]) -> Vec<u8> {
|
||||
result
|
||||
}
|
||||
|
||||
pub fn hash_tree_root<T: SszEncodable>(input: &T) -> Vec<u8> {
|
||||
canonical_hash(&ssz_encode(input))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -7,3 +7,4 @@ edition = "2018"
|
||||
[dependencies]
|
||||
bytes = "0.4.9"
|
||||
ethereum-types = "0.4.0"
|
||||
hashing = { path = "../hashing" }
|
||||
|
||||
@@ -79,6 +79,14 @@ mod tests {
|
||||
assert_eq!(ssz.drain(), vec![0; 32]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_encode_address() {
|
||||
let h = Address::zero();
|
||||
let mut ssz = SszStream::new();
|
||||
ssz.append(&h);
|
||||
assert_eq!(ssz.drain(), vec![0; 20]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_encode_u8() {
|
||||
let x: u8 = 0;
|
||||
|
||||
78
eth2/utils/ssz/src/impl_tree_hash.rs
Normal file
78
eth2/utils/ssz/src/impl_tree_hash.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use super::ethereum_types::{Address, H256};
|
||||
use super::{hash, merkle_hash, ssz_encode, TreeHash};
|
||||
|
||||
impl TreeHash for u8 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for u16 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for u32 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for u64 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for usize {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for Address {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for H256 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for [u8] {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
if self.len() > 32 {
|
||||
return hash(&self);
|
||||
}
|
||||
self.to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> TreeHash for Vec<T>
|
||||
where
|
||||
T: TreeHash,
|
||||
{
|
||||
/// Returns the merkle_hash of a list of hash_tree_root values created
|
||||
/// from the given list.
|
||||
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
|
||||
/// to be handled properly (i.e. hashed) as byte array.
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root()).collect();
|
||||
merkle_hash(&mut tree_hashes)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_impl_tree_hash_vec() {
|
||||
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root();
|
||||
assert_eq!(result.len(), 32);
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,15 @@ extern crate ethereum_types;
|
||||
|
||||
pub mod decode;
|
||||
pub mod encode;
|
||||
pub mod tree_hash;
|
||||
|
||||
mod impl_decode;
|
||||
mod impl_encode;
|
||||
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 const LENGTH_BYTES: usize = 4;
|
||||
pub const MAX_LIST_SIZE: usize = 1 << (4 * 8);
|
||||
|
||||
86
eth2/utils/ssz/src/tree_hash.rs
Normal file
86
eth2/utils/ssz/src/tree_hash.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
use hashing::canonical_hash;
|
||||
|
||||
const SSZ_CHUNK_SIZE: usize = 128;
|
||||
const HASHSIZE: usize = 32;
|
||||
|
||||
pub trait TreeHash {
|
||||
fn hash_tree_root(&self) -> Vec<u8>;
|
||||
}
|
||||
|
||||
/// Returns a 32 byte hash of 'list' - a vector of byte vectors.
|
||||
/// Note that this will consume 'list'.
|
||||
pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
||||
// flatten list
|
||||
let (mut chunk_size, mut chunkz) = list_to_blob(list);
|
||||
|
||||
// get data_len as bytes. It will hashed will the merkle root
|
||||
let datalen = list.len().to_le_bytes();
|
||||
|
||||
// Tree-hash
|
||||
while chunkz.len() > HASHSIZE {
|
||||
let mut new_chunkz: Vec<u8> = Vec::new();
|
||||
|
||||
for two_chunks in chunkz.chunks(chunk_size * 2) {
|
||||
if two_chunks.len() == chunk_size {
|
||||
// Odd number of chunks
|
||||
let mut c = two_chunks.to_vec();
|
||||
c.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
||||
new_chunkz.append(&mut hash(&c));
|
||||
} else {
|
||||
// Hash two chuncks together
|
||||
new_chunkz.append(&mut hash(two_chunks));
|
||||
}
|
||||
chunk_size = HASHSIZE;
|
||||
}
|
||||
chunkz = new_chunkz;
|
||||
}
|
||||
|
||||
chunkz.append(&mut datalen.to_vec());
|
||||
hash(&chunkz)
|
||||
}
|
||||
|
||||
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
||||
let chunk_size = if list.is_empty() {
|
||||
SSZ_CHUNK_SIZE
|
||||
} else if list[0].len() < SSZ_CHUNK_SIZE {
|
||||
let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len();
|
||||
items_per_chunk * list[0].len()
|
||||
} else {
|
||||
list[0].len()
|
||||
};
|
||||
|
||||
let mut data = Vec::new();
|
||||
if list.is_empty() {
|
||||
// handle and empty list
|
||||
data.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
||||
} else {
|
||||
// just create a blob here; we'll divide into
|
||||
// chunked slices when we merklize
|
||||
data.reserve(list[0].len() * list.len());
|
||||
for item in list.iter_mut() {
|
||||
data.append(item);
|
||||
}
|
||||
}
|
||||
(chunk_size, data)
|
||||
}
|
||||
|
||||
pub fn hash(data: &[u8]) -> Vec<u8> {
|
||||
canonical_hash(data)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_merkle_hash() {
|
||||
let data1 = vec![1; 100];
|
||||
let data2 = vec![2; 100];
|
||||
let data3 = vec![3; 100];
|
||||
let mut list = vec![data1, data2, data3];
|
||||
let result = merkle_hash(&mut list);
|
||||
|
||||
//note: should test againt a known test hash value
|
||||
assert_eq!(HASHSIZE, result.len());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user