diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index e53c91fbd9..9720472b89 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -2,7 +2,7 @@ use super::{AttestationData, Bitfield}; use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, Clone, PartialEq)] pub struct Attestation { @@ -49,6 +49,17 @@ impl Attestation { } } +impl TreeHash for Attestation { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.data.hash_tree_root()); + result.append(&mut self.aggregation_bitfield.hash_tree_root()); + result.append(&mut self.custody_bitfield.hash_tree_root()); + result.append(&mut self.aggregate_signature.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for Attestation { fn random_for_test(rng: &mut T) -> Self { Self { @@ -76,4 +87,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Attestation::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/attestation_data.rs b/eth2/types/src/attestation_data.rs index 9a035472af..5639099f06 100644 --- a/eth2/types/src/attestation_data.rs +++ b/eth2/types/src/attestation_data.rs @@ -1,7 +1,7 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 8 + // slot @@ -85,6 +85,21 @@ impl Decodable for AttestationData { } } +impl TreeHash for AttestationData { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.slot.hash_tree_root()); + result.append(&mut self.shard.hash_tree_root()); + result.append(&mut self.beacon_block_root.hash_tree_root()); + result.append(&mut self.epoch_boundary_root.hash_tree_root()); + result.append(&mut self.shard_block_root.hash_tree_root()); + result.append(&mut self.latest_crosslink_root.hash_tree_root()); + result.append(&mut self.justified_slot.hash_tree_root()); + result.append(&mut self.justified_block_root.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for AttestationData { fn random_for_test(rng: &mut T) -> Self { Self { @@ -116,4 +131,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = AttestationData::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/attestation_data_and_custody_bit.rs b/eth2/types/src/attestation_data_and_custody_bit.rs index b6a160189e..1b3b2ea3b1 100644 --- a/eth2/types/src/attestation_data_and_custody_bit.rs +++ b/eth2/types/src/attestation_data_and_custody_bit.rs @@ -1,7 +1,7 @@ use super::AttestationData; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, Clone, PartialEq, Default)] pub struct AttestationDataAndCustodyBit { @@ -27,6 +27,16 @@ impl Decodable for AttestationDataAndCustodyBit { } } +impl TreeHash for AttestationDataAndCustodyBit { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.data.hash_tree_root()); + // TODO: add bool ssz + // result.append(custody_bit.hash_tree_root()); + ssz::hash(&result) + } +} + impl TestRandom for AttestationDataAndCustodyBit { fn random_for_test(rng: &mut T) -> Self { Self { @@ -55,4 +65,16 @@ mod test { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = AttestationDataAndCustodyBit::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/beacon_block/mod.rs b/eth2/types/src/beacon_block/mod.rs index 685658c078..9b58b27ee2 100644 --- a/eth2/types/src/beacon_block/mod.rs +++ b/eth2/types/src/beacon_block/mod.rs @@ -3,7 +3,7 @@ use crate::test_utils::TestRandom; use bls::Signature; use hashing::canonical_hash; use rand::RngCore; -use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash}; mod signing; @@ -63,6 +63,20 @@ impl Decodable for BeaconBlock { } } +impl TreeHash for BeaconBlock { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.slot.hash_tree_root()); + result.append(&mut self.parent_root.hash_tree_root()); + result.append(&mut self.state_root.hash_tree_root()); + result.append(&mut self.randao_reveal.hash_tree_root()); + result.append(&mut self.eth1_data.hash_tree_root()); + result.append(&mut self.signature.hash_tree_root()); + result.append(&mut self.body.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for BeaconBlock { fn random_for_test(rng: &mut T) -> Self { Self { @@ -93,4 +107,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = BeaconBlock::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/beacon_block/signing.rs b/eth2/types/src/beacon_block/signing.rs index c3470217f8..e3317b920f 100644 --- a/eth2/types/src/beacon_block/signing.rs +++ b/eth2/types/src/beacon_block/signing.rs @@ -1,5 +1,5 @@ use crate::{BeaconBlock, ChainSpec, Hash256, ProposalSignedData}; -use hashing::hash_tree_root; +use ssz::TreeHash; impl BeaconBlock { pub fn proposal_root(&self, spec: &ChainSpec) -> Hash256 { @@ -14,6 +14,6 @@ impl BeaconBlock { shard: spec.beacon_chain_shard_number, block_root: block_without_signature_root, }; - Hash256::from_slice(&hash_tree_root(&proposal)[..]) + Hash256::from_slice(&proposal.hash_tree_root()[..]) } } diff --git a/eth2/types/src/beacon_block_body.rs b/eth2/types/src/beacon_block_body.rs index 722264b129..8084c38d99 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -1,7 +1,7 @@ use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing}; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; // The following types are just dummy classes as they will not be defined until // Phase 1 (Sharding phase) @@ -61,6 +61,21 @@ impl Decodable for BeaconBlockBody { } } +impl TreeHash for BeaconBlockBody { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.proposer_slashings.hash_tree_root()); + result.append(&mut self.casper_slashings.hash_tree_root()); + result.append(&mut self.attestations.hash_tree_root()); + result.append(&mut self.custody_reseeds.hash_tree_root()); + result.append(&mut self.custody_challenges.hash_tree_root()); + result.append(&mut self.custody_responses.hash_tree_root()); + result.append(&mut self.deposits.hash_tree_root()); + result.append(&mut self.exits.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for BeaconBlockBody { fn random_for_test(rng: &mut T) -> Self { Self { @@ -92,4 +107,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = BeaconBlockBody::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/beacon_state/mod.rs b/eth2/types/src/beacon_state/mod.rs index 57fff2c530..32cc7d829c 100644 --- a/eth2/types/src/beacon_state/mod.rs +++ b/eth2/types/src/beacon_state/mod.rs @@ -8,7 +8,7 @@ use super::Hash256; use crate::test_utils::TestRandom; use hashing::canonical_hash; use rand::RngCore; -use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash}; mod slot_advance; @@ -170,6 +170,41 @@ impl Decodable for BeaconState { } } +impl TreeHash for BeaconState { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.slot.hash_tree_root()); + result.append(&mut self.genesis_time.hash_tree_root()); + result.append(&mut self.fork_data.hash_tree_root()); + result.append(&mut self.validator_registry.hash_tree_root()); + result.append(&mut self.validator_balances.hash_tree_root()); + result.append(&mut self.validator_registry_update_slot.hash_tree_root()); + result.append(&mut self.validator_registry_exit_count.hash_tree_root()); + result.append(&mut self.validator_registry_delta_chain_tip.hash_tree_root()); + result.append(&mut self.latest_randao_mixes.hash_tree_root()); + result.append(&mut self.latest_vdf_outputs.hash_tree_root()); + result.append(&mut self.previous_epoch_start_shard.hash_tree_root()); + result.append(&mut self.current_epoch_start_shard.hash_tree_root()); + result.append(&mut self.previous_epoch_calculation_slot.hash_tree_root()); + result.append(&mut self.current_epoch_calculation_slot.hash_tree_root()); + result.append(&mut self.previous_epoch_randao_mix.hash_tree_root()); + result.append(&mut self.current_epoch_randao_mix.hash_tree_root()); + result.append(&mut self.custody_challenges.hash_tree_root()); + result.append(&mut self.previous_justified_slot.hash_tree_root()); + result.append(&mut self.justified_slot.hash_tree_root()); + result.append(&mut self.justification_bitfield.hash_tree_root()); + result.append(&mut self.finalized_slot.hash_tree_root()); + result.append(&mut self.latest_crosslinks.hash_tree_root()); + result.append(&mut self.latest_block_roots.hash_tree_root()); + result.append(&mut self.latest_penalized_exit_balances.hash_tree_root()); + result.append(&mut self.latest_attestations.hash_tree_root()); + result.append(&mut self.batched_block_roots.hash_tree_root()); + result.append(&mut self.latest_eth1_data.hash_tree_root()); + result.append(&mut self.eth1_data_votes.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for BeaconState { fn random_for_test(rng: &mut T) -> Self { Self { @@ -221,4 +256,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = BeaconState::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/casper_slashing.rs b/eth2/types/src/casper_slashing.rs index 8ea0cf5f16..6b10988d6c 100644 --- a/eth2/types/src/casper_slashing.rs +++ b/eth2/types/src/casper_slashing.rs @@ -1,7 +1,7 @@ use super::SlashableVoteData; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, PartialEq, Clone)] pub struct CasperSlashing { @@ -31,6 +31,15 @@ impl Decodable for CasperSlashing { } } +impl TreeHash for CasperSlashing { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.slashable_vote_data_1.hash_tree_root()); + result.append(&mut self.slashable_vote_data_2.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for CasperSlashing { fn random_for_test(rng: &mut T) -> Self { Self { @@ -56,4 +65,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = CasperSlashing::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/crosslink.rs b/eth2/types/src/crosslink.rs index f7ba66d5e3..5fbaa33425 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -1,7 +1,7 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Clone, Debug, PartialEq)] pub struct Crosslink { @@ -41,6 +41,15 @@ impl Decodable for Crosslink { } } +impl TreeHash for Crosslink { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.slot.hash_tree_root()); + result.append(&mut self.shard_block_root.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for Crosslink { fn random_for_test(rng: &mut T) -> Self { Self { @@ -66,4 +75,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Crosslink::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/deposit.rs b/eth2/types/src/deposit.rs index 3fbbd817eb..cc0c3c524c 100644 --- a/eth2/types/src/deposit.rs +++ b/eth2/types/src/deposit.rs @@ -1,7 +1,7 @@ use super::{DepositData, Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, PartialEq, Clone)] pub struct Deposit { @@ -35,6 +35,16 @@ impl Decodable for Deposit { } } +impl TreeHash for Deposit { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.merkle_branch.hash_tree_root()); + result.append(&mut self.merkle_tree_index.hash_tree_root()); + result.append(&mut self.deposit_data.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for Deposit { fn random_for_test(rng: &mut T) -> Self { Self { @@ -61,4 +71,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Deposit::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/deposit_data.rs b/eth2/types/src/deposit_data.rs index 00e553a46c..bdc068db3c 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -1,7 +1,7 @@ use super::DepositInput; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, PartialEq, Clone)] pub struct DepositData { @@ -35,6 +35,16 @@ impl Decodable for DepositData { } } +impl TreeHash for DepositData { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.amount.hash_tree_root()); + result.append(&mut self.timestamp.hash_tree_root()); + result.append(&mut self.deposit_input.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for DepositData { fn random_for_test(rng: &mut T) -> Self { Self { @@ -61,4 +71,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = DepositData::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/deposit_input.rs b/eth2/types/src/deposit_input.rs index 47018419e1..bb7bdf5eb8 100644 --- a/eth2/types/src/deposit_input.rs +++ b/eth2/types/src/deposit_input.rs @@ -2,7 +2,7 @@ use super::Hash256; use crate::test_utils::TestRandom; use bls::{PublicKey, Signature}; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, PartialEq, Clone)] pub struct DepositInput { @@ -36,6 +36,16 @@ impl Decodable for DepositInput { } } +impl TreeHash for DepositInput { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.pubkey.hash_tree_root()); + result.append(&mut self.withdrawal_credentials.hash_tree_root()); + result.append(&mut self.proof_of_possession.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for DepositInput { fn random_for_test(rng: &mut T) -> Self { Self { @@ -62,4 +72,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = DepositInput::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/eth1_data.rs b/eth2/types/src/eth1_data.rs index d239372d8a..e8d30109e0 100644 --- a/eth2/types/src/eth1_data.rs +++ b/eth2/types/src/eth1_data.rs @@ -1,7 +1,7 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; // Note: this is refer to as DepositRootVote in specs #[derive(Debug, PartialEq, Clone, Default)] @@ -32,6 +32,15 @@ impl Decodable for Eth1Data { } } +impl TreeHash for Eth1Data { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.deposit_root.hash_tree_root()); + result.append(&mut self.block_hash.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for Eth1Data { fn random_for_test(rng: &mut T) -> Self { Self { @@ -57,4 +66,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Eth1Data::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/eth1_data_vote.rs b/eth2/types/src/eth1_data_vote.rs index 0dc6b90ddb..b6917d5165 100644 --- a/eth2/types/src/eth1_data_vote.rs +++ b/eth2/types/src/eth1_data_vote.rs @@ -1,7 +1,7 @@ use super::Eth1Data; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; // Note: this is refer to as DepositRootVote in specs #[derive(Debug, PartialEq, Clone, Default)] @@ -32,6 +32,15 @@ impl Decodable for Eth1DataVote { } } +impl TreeHash for Eth1DataVote { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.eth1_data.hash_tree_root()); + result.append(&mut self.vote_count.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for Eth1DataVote { fn random_for_test(rng: &mut T) -> Self { Self { @@ -57,4 +66,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Eth1DataVote::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/exit.rs b/eth2/types/src/exit.rs index cc99718b2f..284a180e77 100644 --- a/eth2/types/src/exit.rs +++ b/eth2/types/src/exit.rs @@ -1,7 +1,7 @@ use crate::test_utils::TestRandom; use bls::Signature; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, PartialEq, Clone)] pub struct Exit { @@ -35,6 +35,16 @@ impl Decodable for Exit { } } +impl TreeHash for Exit { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.slot.hash_tree_root()); + result.append(&mut self.validator_index.hash_tree_root()); + result.append(&mut self.signature.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for Exit { fn random_for_test(rng: &mut T) -> Self { Self { @@ -61,4 +71,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Exit::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index 5026dc621d..4c6a7c6a44 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -1,6 +1,6 @@ use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, Clone, PartialEq, Default)] pub struct Fork { @@ -34,6 +34,16 @@ impl Decodable for Fork { } } +impl TreeHash for Fork { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.pre_fork_version.hash_tree_root()); + result.append(&mut self.post_fork_version.hash_tree_root()); + result.append(&mut self.fork_slot.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for Fork { fn random_for_test(rng: &mut T) -> Self { Self { @@ -60,4 +70,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Fork::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/pending_attestation.rs b/eth2/types/src/pending_attestation.rs index 3cb6110dec..13990a014a 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -1,7 +1,7 @@ use super::{AttestationData, Bitfield}; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, Clone, PartialEq)] pub struct PendingAttestation { @@ -39,6 +39,17 @@ impl Decodable for PendingAttestation { } } +impl TreeHash for PendingAttestation { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.data.hash_tree_root()); + result.append(&mut self.aggregation_bitfield.hash_tree_root()); + result.append(&mut self.custody_bitfield.hash_tree_root()); + result.append(&mut self.custody_bitfield.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for PendingAttestation { fn random_for_test(rng: &mut T) -> Self { Self { @@ -66,4 +77,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = PendingAttestation::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/proposal_signed_data.rs b/eth2/types/src/proposal_signed_data.rs index 500c0079f6..1a433fd5c3 100644 --- a/eth2/types/src/proposal_signed_data.rs +++ b/eth2/types/src/proposal_signed_data.rs @@ -1,7 +1,7 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, PartialEq, Clone, Default)] pub struct ProposalSignedData { @@ -35,6 +35,16 @@ impl Decodable for ProposalSignedData { } } +impl TreeHash for ProposalSignedData { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.slot.hash_tree_root()); + result.append(&mut self.shard.hash_tree_root()); + result.append(&mut self.block_root.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for ProposalSignedData { fn random_for_test(rng: &mut T) -> Self { Self { @@ -61,4 +71,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = ProposalSignedData::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/proposer_slashing.rs b/eth2/types/src/proposer_slashing.rs index eb72ef0b2e..08a165665b 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -2,7 +2,7 @@ use super::ProposalSignedData; use crate::test_utils::TestRandom; use bls::Signature; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, PartialEq, Clone)] pub struct ProposerSlashing { @@ -44,6 +44,18 @@ impl Decodable for ProposerSlashing { } } +impl TreeHash for ProposerSlashing { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.proposer_index.hash_tree_root()); + result.append(&mut self.proposal_data_1.hash_tree_root()); + result.append(&mut self.proposal_signature_1.hash_tree_root()); + result.append(&mut self.proposal_data_2.hash_tree_root()); + result.append(&mut self.proposal_signature_2.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for ProposerSlashing { fn random_for_test(rng: &mut T) -> Self { Self { @@ -72,4 +84,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = ProposerSlashing::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/shard_committee.rs b/eth2/types/src/shard_committee.rs index 5d37ffe4cf..943149230b 100644 --- a/eth2/types/src/shard_committee.rs +++ b/eth2/types/src/shard_committee.rs @@ -1,6 +1,6 @@ use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Clone, Debug, PartialEq)] pub struct ShardCommittee { @@ -24,6 +24,15 @@ impl Decodable for ShardCommittee { } } +impl TreeHash for ShardCommittee { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.shard.hash_tree_root()); + result.append(&mut self.committee.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for ShardCommittee { fn random_for_test(rng: &mut T) -> Self { Self { @@ -49,4 +58,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = ShardCommittee::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/shard_reassignment_record.rs b/eth2/types/src/shard_reassignment_record.rs index f4c976bda8..5528a92f11 100644 --- a/eth2/types/src/shard_reassignment_record.rs +++ b/eth2/types/src/shard_reassignment_record.rs @@ -1,6 +1,6 @@ use crate::test_utils::TestRandom; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, PartialEq, Clone)] pub struct ShardReassignmentRecord { @@ -34,6 +34,16 @@ impl Decodable for ShardReassignmentRecord { } } +impl TreeHash for ShardReassignmentRecord { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.validator_index.hash_tree_root()); + result.append(&mut self.shard.hash_tree_root()); + result.append(&mut self.slot.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for ShardReassignmentRecord { fn random_for_test(rng: &mut T) -> Self { Self { @@ -60,4 +70,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = ShardReassignmentRecord::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index ba5258d111..d153da2b96 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -2,7 +2,7 @@ use super::AttestationData; use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; #[derive(Debug, PartialEq, Clone)] pub struct SlashableVoteData { @@ -40,6 +40,17 @@ impl Decodable for SlashableVoteData { } } +impl TreeHash for SlashableVoteData { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.custody_bit_0_indices.hash_tree_root()); + result.append(&mut self.custody_bit_1_indices.hash_tree_root()); + result.append(&mut self.data.hash_tree_root()); + result.append(&mut self.aggregate_signature.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for SlashableVoteData { fn random_for_test(rng: &mut T) -> Self { Self { @@ -67,4 +78,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = SlashableVoteData::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/special_record.rs b/eth2/types/src/special_record.rs index 47ee3de36b..21e9c88f65 100644 --- a/eth2/types/src/special_record.rs +++ b/eth2/types/src/special_record.rs @@ -1,4 +1,4 @@ -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; /// The value of the "type" field of SpecialRecord. /// @@ -71,6 +71,15 @@ impl Decodable for SpecialRecord { } } +impl TreeHash for SpecialRecord { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.kind.hash_tree_root()); + result.append(&mut self.data.as_slice().hash_tree_root()); + hash(&result) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/validator.rs b/eth2/types/src/validator.rs index 446b4d58c9..eaf3308b9c 100644 --- a/eth2/types/src/validator.rs +++ b/eth2/types/src/validator.rs @@ -2,7 +2,7 @@ use super::Hash256; use crate::test_utils::TestRandom; use bls::PublicKey; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; const STATUS_FLAG_INITIATED_EXIT: u8 = 1; const STATUS_FLAG_WITHDRAWABLE: u8 = 2; @@ -142,6 +142,24 @@ impl Decodable for Validator { } } +impl TreeHash for Validator { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.pubkey.hash_tree_root()); + result.append(&mut self.withdrawal_credentials.hash_tree_root()); + result.append(&mut self.proposer_slots.hash_tree_root()); + result.append(&mut self.activation_slot.hash_tree_root()); + result.append(&mut self.exit_slot.hash_tree_root()); + result.append(&mut self.withdrawal_slot.hash_tree_root()); + result.append(&mut self.penalized_slot.hash_tree_root()); + result.append(&mut self.exit_count.hash_tree_root()); + result.append(&mut (status_flag_to_byte(self.status_flags) as u64).hash_tree_root()); + result.append(&mut self.latest_custody_reseed_slot.hash_tree_root()); + result.append(&mut self.penultimate_custody_reseed_slot.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for Validator { fn random_for_test(rng: &mut T) -> Self { Self { @@ -198,4 +216,16 @@ mod tests { } } } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = Validator::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/types/src/validator_registry_delta_block.rs b/eth2/types/src/validator_registry_delta_block.rs index 796b2fc9eb..f673f2a51e 100644 --- a/eth2/types/src/validator_registry_delta_block.rs +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -2,7 +2,7 @@ use super::Hash256; use crate::test_utils::TestRandom; use bls::PublicKey; use rand::RngCore; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; // The information gathered from the PoW chain validator registration function. #[derive(Debug, Clone, PartialEq)] @@ -58,6 +58,18 @@ impl Decodable for ValidatorRegistryDeltaBlock { } } +impl TreeHash for ValidatorRegistryDeltaBlock { + fn hash_tree_root(&self) -> Vec { + let mut result: Vec = vec![]; + result.append(&mut self.latest_registry_delta_root.hash_tree_root()); + result.append(&mut self.validator_index.hash_tree_root()); + result.append(&mut self.pubkey.hash_tree_root()); + result.append(&mut self.slot.hash_tree_root()); + result.append(&mut self.flag.hash_tree_root()); + hash(&result) + } +} + impl TestRandom for ValidatorRegistryDeltaBlock { fn random_for_test(rng: &mut T) -> Self { Self { @@ -86,4 +98,16 @@ mod tests { assert_eq!(original, decoded); } + + #[test] + pub fn test_hash_tree_root() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = ValidatorRegistryDeltaBlock::random_for_test(&mut rng); + + let result = original.hash_tree_root(); + + assert_eq!(result.len(), 32); + // TODO: Add further tests + // https://github.com/sigp/lighthouse/issues/170 + } } diff --git a/eth2/utils/bls/src/aggregate_signature.rs b/eth2/utils/bls/src/aggregate_signature.rs index 5d5d92251f..505620f4a2 100644 --- a/eth2/utils/bls/src/aggregate_signature.rs +++ b/eth2/utils/bls/src/aggregate_signature.rs @@ -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 { + hash(&self.0.as_bytes()) + } +} + #[cfg(test)] mod tests { use super::super::{Keypair, Signature}; diff --git a/eth2/utils/bls/src/public_key.rs b/eth2/utils/bls/src/public_key.rs index eb8419dd90..5a6bb3ed1a 100644 --- a/eth2/utils/bls/src/public_key.rs +++ b/eth2/utils/bls/src/public_key.rs @@ -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 { + hash(&self.0.as_bytes()) + } +} + impl PartialEq for PublicKey { fn eq(&self, other: &PublicKey) -> bool { ssz_encode(self) == ssz_encode(other) diff --git a/eth2/utils/bls/src/secret_key.rs b/eth2/utils/bls/src/secret_key.rs index 54dfa2f242..4ff9f8684d 100644 --- a/eth2/utils/bls/src/secret_key.rs +++ b/eth2/utils/bls/src/secret_key.rs @@ -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 { + self.0.as_bytes().clone() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/eth2/utils/bls/src/signature.rs b/eth2/utils/bls/src/signature.rs index 99790fa37a..59f4555234 100644 --- a/eth2/utils/bls/src/signature.rs +++ b/eth2/utils/bls/src/signature.rs @@ -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 { + hash(&self.0.as_bytes()) + } +} + #[cfg(test)] mod tests { use super::super::Keypair; diff --git a/eth2/utils/boolean-bitfield/src/lib.rs b/eth2/utils/boolean-bitfield/src/lib.rs index 98518d70ca..33c461361c 100644 --- a/eth2/utils/boolean-bitfield/src/lib.rs +++ b/eth2/utils/boolean-bitfield/src/lib.rs @@ -149,6 +149,12 @@ impl ssz::Decodable for BooleanBitfield { } } +impl ssz::TreeHash for BooleanBitfield { + fn hash_tree_root(&self) -> Vec { + self.to_bytes().hash_tree_root() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/eth2/utils/hashing/Cargo.toml b/eth2/utils/hashing/Cargo.toml index 6abd9cb0ca..1527bceba1 100644 --- a/eth2/utils/hashing/Cargo.toml +++ b/eth2/utils/hashing/Cargo.toml @@ -6,4 +6,3 @@ edition = "2018" [dependencies] tiny-keccak = "1.4.2" -ssz = { path = "../ssz" } diff --git a/eth2/utils/hashing/src/lib.rs b/eth2/utils/hashing/src/lib.rs index 3d388fd921..c263c91e04 100644 --- a/eth2/utils/hashing/src/lib.rs +++ b/eth2/utils/hashing/src/lib.rs @@ -1,4 +1,3 @@ -use ssz::{ssz_encode, Encodable as SszEncodable}; use tiny_keccak::Keccak; pub fn canonical_hash(input: &[u8]) -> Vec { @@ -9,10 +8,6 @@ pub fn canonical_hash(input: &[u8]) -> Vec { result } -pub fn hash_tree_root(input: &T) -> Vec { - canonical_hash(&ssz_encode(input)) -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/utils/ssz/Cargo.toml b/eth2/utils/ssz/Cargo.toml index e28e92c232..25326cb5bb 100644 --- a/eth2/utils/ssz/Cargo.toml +++ b/eth2/utils/ssz/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" [dependencies] bytes = "0.4.9" ethereum-types = "0.4.0" +hashing = { path = "../hashing" } diff --git a/eth2/utils/ssz/src/impl_encode.rs b/eth2/utils/ssz/src/impl_encode.rs index 63de18058e..8714cf75fe 100644 --- a/eth2/utils/ssz/src/impl_encode.rs +++ b/eth2/utils/ssz/src/impl_encode.rs @@ -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; diff --git a/eth2/utils/ssz/src/impl_tree_hash.rs b/eth2/utils/ssz/src/impl_tree_hash.rs new file mode 100644 index 0000000000..9463283cb5 --- /dev/null +++ b/eth2/utils/ssz/src/impl_tree_hash.rs @@ -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 { + ssz_encode(self) + } +} + +impl TreeHash for u16 { + fn hash_tree_root(&self) -> Vec { + ssz_encode(self) + } +} + +impl TreeHash for u32 { + fn hash_tree_root(&self) -> Vec { + ssz_encode(self) + } +} + +impl TreeHash for u64 { + fn hash_tree_root(&self) -> Vec { + ssz_encode(self) + } +} + +impl TreeHash for usize { + fn hash_tree_root(&self) -> Vec { + ssz_encode(self) + } +} + +impl TreeHash for Address { + fn hash_tree_root(&self) -> Vec { + ssz_encode(self) + } +} + +impl TreeHash for H256 { + fn hash_tree_root(&self) -> Vec { + ssz_encode(self) + } +} + +impl TreeHash for [u8] { + fn hash_tree_root(&self) -> Vec { + if self.len() > 32 { + return hash(&self); + } + self.to_vec() + } +} + +impl TreeHash for Vec +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, must be converted to a slice (as_slice()) + /// to be handled properly (i.e. hashed) as byte array. + fn hash_tree_root(&self) -> Vec { + 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); + } +} diff --git a/eth2/utils/ssz/src/lib.rs b/eth2/utils/ssz/src/lib.rs index ccfcb7f5b1..206040c2d2 100644 --- a/eth2/utils/ssz/src/lib.rs +++ b/eth2/utils/ssz/src/lib.rs @@ -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); diff --git a/eth2/utils/ssz/src/tree_hash.rs b/eth2/utils/ssz/src/tree_hash.rs new file mode 100644 index 0000000000..497d626291 --- /dev/null +++ b/eth2/utils/ssz/src/tree_hash.rs @@ -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; +} + +/// 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 { + // 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 = 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>) -> (usize, Vec) { + 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 { + 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()); + } +}