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

[WIP] Tree hash update - added padding #198

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 8 additions & 8 deletions eth2/types/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Attestation {

impl Attestation {
pub fn canonical_root(&self) -> Hash256 {
Hash256::from(&self.hash_tree_root()[..])
Hash256::from(&self.hash_tree_root_internal()[..])
}

pub fn signable_message(&self, custody_bit: bool) -> Vec<u8> {
Expand Down Expand Up @@ -61,12 +61,12 @@ impl 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.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());
result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.aggregation_bitfield.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 @@ -100,11 +100,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
26 changes: 13 additions & 13 deletions eth2/types/src/attestation_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ impl AttestationData {
}

pub fn canonical_root(&self) -> Hash256 {
Hash256::from(&self.hash_tree_root()[..])
Hash256::from(&self.hash_tree_root_internal()[..])
}

pub fn signable_message(&self, custody_bit: bool) -> Vec<u8> {
let attestation_data_and_custody_bit = AttestationDataAndCustodyBit {
data: self.clone(),
custody_bit,
};
attestation_data_and_custody_bit.hash_tree_root()
attestation_data_and_custody_bit.hash_tree_root_internal()
}
}

Expand Down Expand Up @@ -95,16 +95,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_root.hash_tree_root());
result.append(&mut self.justified_slot.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_root.hash_tree_root_internal());
result.append(&mut self.justified_slot.hash_tree_root_internal());
result.append(&mut self.justified_block_root.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 = 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
24 changes: 12 additions & 12 deletions eth2/types/src/beacon_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct BeaconBlock {

impl BeaconBlock {
pub fn canonical_root(&self) -> Hash256 {
Hash256::from(&self.hash_tree_root()[..])
Hash256::from(&self.hash_tree_root_internal()[..])
}

pub fn proposal_root(&self, spec: &ChainSpec) -> Hash256 {
Expand All @@ -33,7 +33,7 @@ impl BeaconBlock {
shard: spec.beacon_chain_shard_number,
block_root: block_without_signature_root,
};
Hash256::from_slice(&proposal.hash_tree_root()[..])
Hash256::from_slice(&proposal.hash_tree_root_internal()[..])
}
}

Expand Down Expand Up @@ -75,15 +75,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 @@ -120,11 +120,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
22 changes: 11 additions & 11 deletions eth2/types/src/beacon_block_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ 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.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());
result.append(&mut self.proposer_slashings.hash_tree_root_internal());
result.append(&mut self.casper_slashings.hash_tree_root_internal());
result.append(&mut self.attestations.hash_tree_root_internal());
result.append(&mut self.custody_reseeds.hash_tree_root_internal());
result.append(&mut self.custody_challenges.hash_tree_root_internal());
result.append(&mut self.custody_responses.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 @@ -110,11 +110,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
86 changes: 51 additions & 35 deletions eth2/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub struct BeaconState {

impl BeaconState {
pub fn canonical_root(&self) -> Hash256 {
Hash256::from(&self.hash_tree_root()[..])
Hash256::from(&self.hash_tree_root_internal()[..])
}

pub fn current_epoch(&self, spec: &ChainSpec) -> Epoch {
Expand Down Expand Up @@ -456,7 +456,7 @@ impl BeaconState {
ensure!(
bls_verify(
&proposer.pubkey,
&proposer_slashing.proposal_data_1.hash_tree_root(),
&proposer_slashing.proposal_data_1.hash_tree_root_internal(),
&proposer_slashing.proposal_signature_1,
get_domain(
&self.fork_data,
Expand All @@ -469,7 +469,7 @@ impl BeaconState {
ensure!(
bls_verify(
&proposer.pubkey,
&proposer_slashing.proposal_data_2.hash_tree_root(),
&proposer_slashing.proposal_data_2.hash_tree_root_internal(),
&proposer_slashing.proposal_signature_2,
get_domain(
&self.fork_data,
Expand Down Expand Up @@ -542,7 +542,7 @@ impl BeaconState {
validator_index: exit.validator_index,
signature: spec.empty_signature.clone(),
};
exit_struct.hash_tree_root()
exit_struct.hash_tree_root_internal()
};
ensure!(
bls_verify(
Expand Down Expand Up @@ -1765,36 +1765,52 @@ 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_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_seed.hash_tree_root());
result.append(&mut self.current_epoch_seed.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_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_data.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_slot
.hash_tree_root_internal(),
);
result.append(&mut self.validator_registry_exit_count.hash_tree_root_internal());
result.append(
&mut self
.validator_registry_delta_chain_tip
.hash_tree_root_internal(),
);
result.append(&mut self.latest_randao_mixes.hash_tree_root_internal());
result.append(&mut self.latest_vdf_outputs.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_epoch_calculation_slot
.hash_tree_root_internal(),
);
result.append(
&mut self
.current_epoch_calculation_slot
.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.custody_challenges.hash_tree_root_internal());
result.append(&mut self.previous_justified_slot.hash_tree_root_internal());
result.append(&mut self.justified_slot.hash_tree_root_internal());
result.append(&mut self.justification_bitfield.hash_tree_root_internal());
result.append(&mut self.finalized_slot.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_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 Expand Up @@ -1852,11 +1868,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 = 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
Loading