Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated TreeHash to spec - added padding #238

Merged
merged 3 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions eth2/types/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ impl Decodable for Attestation {
}

impl TreeHash for Attestation {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.aggregation_bitfield.hash_tree_root());
result.append(&mut self.data.hash_tree_root());
result.append(&mut self.custody_bitfield.hash_tree_root());
result.append(&mut self.aggregate_signature.hash_tree_root());
result.append(&mut self.aggregation_bitfield.hash_tree_root_internal());
result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.custody_bitfield.hash_tree_root_internal());
result.append(&mut self.aggregate_signature.hash_tree_root_internal());
hash(&result)
}
}
Expand Down Expand Up @@ -99,11 +99,11 @@ mod tests {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Attestation::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
22 changes: 11 additions & 11 deletions eth2/types/src/attestation_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ impl Decodable for AttestationData {
}

impl TreeHash for AttestationData {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = 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.hash_tree_root());
result.append(&mut self.justified_epoch.hash_tree_root());
result.append(&mut self.justified_block_root.hash_tree_root());
result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.shard.hash_tree_root_internal());
result.append(&mut self.beacon_block_root.hash_tree_root_internal());
result.append(&mut self.epoch_boundary_root.hash_tree_root_internal());
result.append(&mut self.shard_block_root.hash_tree_root_internal());
result.append(&mut self.latest_crosslink.hash_tree_root_internal());
result.append(&mut self.justified_epoch.hash_tree_root_internal());
result.append(&mut self.justified_block_root.hash_tree_root_internal());
hash(&result)
}
}
Expand Down Expand Up @@ -129,11 +129,11 @@ mod tests {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = AttestationData::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
10 changes: 5 additions & 5 deletions eth2/types/src/attestation_data_and_custody_bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ impl Decodable for AttestationDataAndCustodyBit {
}

impl TreeHash for AttestationDataAndCustodyBit {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.data.hash_tree_root());
result.append(&mut self.data.hash_tree_root_internal());
// TODO: add bool ssz
// result.append(custody_bit.hash_tree_root());
// result.append(custody_bit.hash_tree_root_internal());
ssz::hash(&result)
}
}
Expand Down Expand Up @@ -68,11 +68,11 @@ mod test {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = AttestationDataAndCustodyBit::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
10 changes: 5 additions & 5 deletions eth2/types/src/attester_slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ impl Decodable for AttesterSlashing {
}

impl TreeHash for AttesterSlashing {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.slashable_attestation_1.hash_tree_root());
result.append(&mut self.slashable_attestation_2.hash_tree_root());
result.append(&mut self.slashable_attestation_1.hash_tree_root_internal());
result.append(&mut self.slashable_attestation_2.hash_tree_root_internal());
hash(&result)
}
}
Expand Down Expand Up @@ -67,11 +67,11 @@ mod tests {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = AttesterSlashing::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
20 changes: 10 additions & 10 deletions eth2/types/src/beacon_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ impl Decodable for BeaconBlock {
}

impl TreeHash for BeaconBlock {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = 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());
result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.parent_root.hash_tree_root_internal());
result.append(&mut self.state_root.hash_tree_root_internal());
result.append(&mut self.randao_reveal.hash_tree_root_internal());
result.append(&mut self.eth1_data.hash_tree_root_internal());
result.append(&mut self.signature.hash_tree_root_internal());
result.append(&mut self.body.hash_tree_root_internal());
hash(&result)
}
}
Expand Down Expand Up @@ -142,11 +142,11 @@ mod tests {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = BeaconBlock::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
16 changes: 8 additions & 8 deletions eth2/types/src/beacon_block_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ impl Decodable for BeaconBlockBody {
}

impl TreeHash for BeaconBlockBody {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.proposer_slashings.hash_tree_root());
result.append(&mut self.attester_slashings.hash_tree_root());
result.append(&mut self.attestations.hash_tree_root());
result.append(&mut self.deposits.hash_tree_root());
result.append(&mut self.exits.hash_tree_root());
result.append(&mut self.proposer_slashings.hash_tree_root_internal());
result.append(&mut self.attester_slashings.hash_tree_root_internal());
result.append(&mut self.attestations.hash_tree_root_internal());
result.append(&mut self.deposits.hash_tree_root_internal());
result.append(&mut self.exits.hash_tree_root_internal());
hash(&result)
}
}
Expand Down Expand Up @@ -86,11 +86,11 @@ mod tests {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = BeaconBlockBody::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
56 changes: 30 additions & 26 deletions eth2/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,33 +1059,37 @@ impl Decodable for BeaconState {
}

impl TreeHash for BeaconState {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.slot.hash_tree_root());
result.append(&mut self.genesis_time.hash_tree_root());
result.append(&mut self.fork.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_epoch.hash_tree_root());
result.append(&mut self.latest_randao_mixes.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_calculation_epoch.hash_tree_root());
result.append(&mut self.current_calculation_epoch.hash_tree_root());
result.append(&mut self.previous_epoch_seed.hash_tree_root());
result.append(&mut self.current_epoch_seed.hash_tree_root());
result.append(&mut self.previous_justified_epoch.hash_tree_root());
result.append(&mut self.justified_epoch.hash_tree_root());
result.append(&mut self.justification_bitfield.hash_tree_root());
result.append(&mut self.finalized_epoch.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_index_roots.hash_tree_root());
result.append(&mut self.latest_penalized_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());
result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.genesis_time.hash_tree_root_internal());
result.append(&mut self.fork.hash_tree_root_internal());
result.append(&mut self.validator_registry.hash_tree_root_internal());
result.append(&mut self.validator_balances.hash_tree_root_internal());
result.append(
&mut self
.validator_registry_update_epoch
.hash_tree_root_internal(),
);
result.append(&mut self.latest_randao_mixes.hash_tree_root_internal());
result.append(&mut self.previous_epoch_start_shard.hash_tree_root_internal());
result.append(&mut self.current_epoch_start_shard.hash_tree_root_internal());
result.append(&mut self.previous_calculation_epoch.hash_tree_root_internal());
result.append(&mut self.current_calculation_epoch.hash_tree_root_internal());
result.append(&mut self.previous_epoch_seed.hash_tree_root_internal());
result.append(&mut self.current_epoch_seed.hash_tree_root_internal());
result.append(&mut self.previous_justified_epoch.hash_tree_root_internal());
result.append(&mut self.justified_epoch.hash_tree_root_internal());
result.append(&mut self.justification_bitfield.hash_tree_root_internal());
result.append(&mut self.finalized_epoch.hash_tree_root_internal());
result.append(&mut self.latest_crosslinks.hash_tree_root_internal());
result.append(&mut self.latest_block_roots.hash_tree_root_internal());
result.append(&mut self.latest_index_roots.hash_tree_root_internal());
result.append(&mut self.latest_penalized_balances.hash_tree_root_internal());
result.append(&mut self.latest_attestations.hash_tree_root_internal());
result.append(&mut self.batched_block_roots.hash_tree_root_internal());
result.append(&mut self.latest_eth1_data.hash_tree_root_internal());
result.append(&mut self.eth1_data_votes.hash_tree_root_internal());
hash(&result)
}
}
Expand Down
4 changes: 2 additions & 2 deletions eth2/types/src/beacon_state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ pub fn test_ssz_round_trip() {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = BeaconState::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
10 changes: 5 additions & 5 deletions eth2/types/src/casper_slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ impl Decodable for CasperSlashing {
}

impl TreeHash for CasperSlashing {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.slashable_vote_data_1.hash_tree_root());
result.append(&mut self.slashable_vote_data_2.hash_tree_root());
result.append(&mut self.slashable_vote_data_1.hash_tree_root_internal());
result.append(&mut self.slashable_vote_data_2.hash_tree_root_internal());
hash(&result)
}
}
Expand Down Expand Up @@ -68,11 +68,11 @@ mod tests {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = CasperSlashing::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
10 changes: 5 additions & 5 deletions eth2/types/src/crosslink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ impl Decodable for Crosslink {
}

impl TreeHash for Crosslink {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.epoch.hash_tree_root());
result.append(&mut self.shard_block_root.hash_tree_root());
result.append(&mut self.epoch.hash_tree_root_internal());
result.append(&mut self.shard_block_root.hash_tree_root_internal());
hash(&result)
}
}
Expand Down Expand Up @@ -78,11 +78,11 @@ mod tests {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Crosslink::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
12 changes: 6 additions & 6 deletions eth2/types/src/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ impl Decodable for Deposit {
}

impl TreeHash for Deposit {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.branch.hash_tree_root());
result.append(&mut self.index.hash_tree_root());
result.append(&mut self.deposit_data.hash_tree_root());
result.append(&mut self.branch.hash_tree_root_internal());
result.append(&mut self.index.hash_tree_root_internal());
result.append(&mut self.deposit_data.hash_tree_root_internal());
hash(&result)
}
}
Expand Down Expand Up @@ -74,11 +74,11 @@ mod tests {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Deposit::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
12 changes: 6 additions & 6 deletions eth2/types/src/deposit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ impl Decodable for DepositData {
}

impl TreeHash for DepositData {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = 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());
result.append(&mut self.amount.hash_tree_root_internal());
result.append(&mut self.timestamp.hash_tree_root_internal());
result.append(&mut self.deposit_input.hash_tree_root_internal());
hash(&result)
}
}
Expand Down Expand Up @@ -74,11 +74,11 @@ mod tests {
}

#[test]
pub fn test_hash_tree_root() {
pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = DepositData::random_for_test(&mut rng);

let result = original.hash_tree_root();
let result = original.hash_tree_root_internal();

assert_eq!(result.len(), 32);
// TODO: Add further tests
Expand Down
Loading