Skip to content

Commit

Permalink
Fix inclusion proof construction in EIP-7594
Browse files Browse the repository at this point in the history
WARNING: EIP-7594 contains a new preset variable with a name nearly identical to an older one.
Compare:
KZG_COMMITMENT_INCLUSION_PROOF_DEPTH  added in Deneb.
KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH added in EIP-7594.

Also note that neither variable is independent.
KZG_COMMITMENT_INCLUSION_PROOF_DEPTH is derived from MAX_BLOB_COMMITMENTS_PER_BLOCK.
KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH is effectively a constant.
  • Loading branch information
weekday-grandine-io committed May 10, 2024
1 parent 58979f0 commit 3ba1179
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 72 deletions.
102 changes: 38 additions & 64 deletions eip_7594/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use types::{
containers::SignedBeaconBlock,
primitives::{BlobIndex, KzgCommitment, KzgProof},
},
eip7594::{ColumnIndex, DataColumnSidecar, KzgCommitmentInclusionProofDepth},
eip7594::{BlobCommitmentsInclusionProof, ColumnIndex, DataColumnSidecar},
phase0::{
containers::{BeaconBlockHeader, SignedBeaconBlockHeader},
primitives::{DomainType, Epoch, NodeId, SubnetId},
Expand Down Expand Up @@ -354,7 +354,7 @@ fn get_data_column_sidecars<P: Preset>(
kzg_commitments_inclusion_proof: kzg_commitment_inclusion_proof(
&signed_block.message.body,
column_index,
)?,
),
});
}

Expand All @@ -364,64 +364,39 @@ fn get_data_column_sidecars<P: Preset>(
fn kzg_commitment_inclusion_proof<P: Preset>(
body: &(impl PostDenebBeaconBlockBody<P> + ?Sized),
commitment_index: BlobIndex,
) -> Result<ContiguousVector<H256, KzgCommitmentInclusionProofDepth>> {
let depth = KzgCommitmentInclusionProofDepth::USIZE;

let mut proof = ContiguousVector::default();

// // TODO(feature/deneb): Try to break this up into something more readable.
// let mut merkle_tree = MerkleTree::<
// <P::MaxBlobCommitmentsPerBlock as MerkleElements<KzgCommitment>>::UnpackedMerkleTreeDepth,
// >::default();

// let chunks = body
// .blob_kzg_commitments()
// .iter()
// .map(SszHash::hash_tree_root);

// let commitment_indices = 0..body.blob_kzg_commitments().len();
// let proof_indices = commitment_index.try_into()?..(commitment_index + 1).try_into()?;

// let subproof = merkle_tree
// .extend_and_construct_proofs(chunks, commitment_indices, proof_indices)
// .exactly_one()
// .ok()
// .expect("exactly one proof is requested");

// // The first 13 or 5 nodes are computed from other elements of `body.blob_kzg_commitments`.
// proof[..depth - 4].copy_from_slice(subproof.as_slice());

// // The last 4 nodes are computed from other fields of `body`.
// proof[depth - 4] = body.bls_to_execution_changes().hash_tree_root();

// proof[depth - 3] = hashing::hash_256_256(
// body.sync_aggregate().hash_tree_root(),
// body.execution_payload().hash_tree_root(),
// );

// proof[depth - 2] = ZERO_HASHES[2];

// proof[depth - 1] = hashing::hash_256_256(
// hashing::hash_256_256(
// hashing::hash_256_256(
// body.randao_reveal().hash_tree_root(),
// body.eth1_data().hash_tree_root(),
// ),
// hashing::hash_256_256(body.graffiti(), body.proposer_slashings().hash_tree_root()),
// ),
// hashing::hash_256_256(
// hashing::hash_256_256(
// body.attester_slashings().hash_tree_root(),
// body.attestations().hash_tree_root(),
// ),
// hashing::hash_256_256(
// body.deposits().hash_tree_root(),
// body.voluntary_exits().hash_tree_root(),
// ),
// ),
// );

Ok(proof)
) -> BlobCommitmentsInclusionProof {
let mut proof = BlobCommitmentsInclusionProof::default();

proof[0] = body.bls_to_execution_changes().hash_tree_root();

proof[1] = hashing::hash_256_256(
body.sync_aggregate().hash_tree_root(),
body.execution_payload().hash_tree_root(),
);

proof[2] = ZERO_HASHES[2];

proof[3] = hashing::hash_256_256(
hashing::hash_256_256(
hashing::hash_256_256(
body.randao_reveal().hash_tree_root(),
body.eth1_data().hash_tree_root(),
),
hashing::hash_256_256(body.graffiti(), body.proposer_slashings().hash_tree_root()),
),
hashing::hash_256_256(
hashing::hash_256_256(
body.attester_slashings().hash_tree_root(),
body.attestations().hash_tree_root(),
),
hashing::hash_256_256(
body.deposits().hash_tree_root(),
body.voluntary_exits().hash_tree_root(),
),
),
);

proof
}

#[cfg(test)]
Expand Down Expand Up @@ -481,8 +456,8 @@ mod tests {

#[duplicate_item(
glob function_name preset;
["consensus-spec-tests/tests/mainnet/eip7594/merkle_proof/single_merkle_proof/BeaconBlockBody/*"] [kzg_commitment_inclusion_proof_mainnet] [Mainnet];
["consensus-spec-tests/tests/minimal/eip7594/merkle_proof/single_merkle_proof/BeaconBlockBody/*"] [kzg_commitment_inclusion_proof_minimal] [Minimal];
["consensus-spec-tests/tests/mainnet/eip7594/merkle_proof/single_merkle_proof/BeaconBlockBody/blob_kzg_commitments_*"] [kzg_commitment_inclusion_proof_mainnet] [Mainnet];
["consensus-spec-tests/tests/minimal/eip7594/merkle_proof/single_merkle_proof/BeaconBlockBody/blob_kzg_commitments_*"] [kzg_commitment_inclusion_proof_minimal] [Minimal];
)]
#[test_resources(glob)]
fn function_name(case: Case) {
Expand Down Expand Up @@ -520,8 +495,7 @@ mod tests {
// > If the implementation supports generating merkle proofs, check that the
// > self-generated proof matches the `proof` provided with the test.
//
let proof = kzg_commitment_inclusion_proof(&block_body, commitment_index)
.expect("inclusion proof should be constructed successfully");
let proof = kzg_commitment_inclusion_proof(&block_body, commitment_index);

assert_eq!(proof.as_slice(), branch);
}
Expand Down
2 changes: 1 addition & 1 deletion eth2_libp2p
Submodule eth2_libp2p updated 64 files
+2 −1 Cargo.toml
+378 −0 gossipsub/CHANGELOG.md
+50 −0 gossipsub/Cargo.toml
+1 −1 gossipsub/src/backoff.rs
+44 −10 gossipsub/src/behaviour.rs
+17 −18 gossipsub/src/behaviour/tests.rs
+6 −7 gossipsub/src/config.rs
+3 −0 gossipsub/src/error.rs
+0 −0 gossipsub/src/generated/compat.proto
+0 −0 gossipsub/src/generated/compat/mod.rs
+0 −0 gossipsub/src/generated/compat/pb.rs
+0 −0 gossipsub/src/generated/gossipsub/mod.rs
+0 −0 gossipsub/src/generated/gossipsub/pb.rs
+0 −0 gossipsub/src/generated/mod.rs
+0 −0 gossipsub/src/generated/rpc.proto
+0 −0 gossipsub/src/gossip_promises.rs
+0 −0 gossipsub/src/handler.rs
+134 −0 gossipsub/src/lib.rs
+1 −1 gossipsub/src/mcache.rs
+10 −2 gossipsub/src/metrics.rs
+0 −0 gossipsub/src/mod.rs
+3 −3 gossipsub/src/peer_score.rs
+404 −0 gossipsub/src/peer_score/params.rs
+978 −0 gossipsub/src/peer_score/tests.rs
+624 −0 gossipsub/src/protocol.rs
+92 −0 gossipsub/src/rpc_proto.rs
+436 −0 gossipsub/src/subscription_filter.rs
+219 −0 gossipsub/src/time_cache.rs
+123 −0 gossipsub/src/topic.rs
+72 −0 gossipsub/src/transform.rs
+818 −0 gossipsub/src/types.rs
+12 −18 src/config.rs
+4 −4 src/discovery/mod.rs
+0 −2 src/discovery/subnet_predicate.rs
+3 −6 src/gossipsub/protocol.rs
+2 −2 src/gossipsub/rpc_proto.rs
+4 −5 src/gossipsub/subscription_filter.rs
+1 −1 src/gossipsub/topic.rs
+0 −1 src/lib.rs
+12 −42 src/metrics.rs
+70 −70 src/peer_manager/mod.rs
+10 −47 src/peer_manager/network_behaviour.rs
+12 −11 src/peer_manager/peerdb.rs
+14 −16 src/peer_manager/peerdb/client.rs
+25 −16 src/peer_manager/peerdb/peer_info.rs
+4 −4 src/peer_manager/peerdb/score.rs
+50 −5 src/rpc/codec/ssz_snappy.rs
+1 −1 src/rpc/handler.rs
+10 −12 src/rpc/methods.rs
+9 −4 src/rpc/mod.rs
+3 −4 src/rpc/outbound.rs
+7 −0 src/rpc/protocol.rs
+0 −1 src/rpc/rate_limiter.rs
+6 −3 src/service/api_types.rs
+5 −1 src/service/behaviour.rs
+0 −3 src/service/gossip_cache.rs
+3 −3 src/service/gossipsub_scoring_parameters.rs
+114 −20 src/service/mod.rs
+2 −8 src/service/utils.rs
+3 −3 src/types/mod.rs
+113 −50 src/types/pubsub.rs
+0 −2 src/types/subnet.rs
+18 −18 src/types/topics.rs
+1 −1 tests/common.rs
8 changes: 5 additions & 3 deletions helper_functions/src/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,11 @@ mod spec_tests {
}

#[duplicate_item(
glob function_name preset;
["consensus-spec-tests/tests/mainnet/deneb/merkle_proof/single_merkle_proof/BeaconBlockBody/*"] [deneb_mainnet_beacon_block_body_proofs] [Mainnet];
["consensus-spec-tests/tests/minimal/deneb/merkle_proof/single_merkle_proof/BeaconBlockBody/*"] [deneb_minimal_beacon_block_body_proofs] [Minimal];
glob function_name preset;
["consensus-spec-tests/tests/mainnet/deneb/merkle_proof/single_merkle_proof/BeaconBlockBody/*"] [deneb_mainnet_beacon_block_body_proofs] [Mainnet];
["consensus-spec-tests/tests/minimal/deneb/merkle_proof/single_merkle_proof/BeaconBlockBody/*"] [deneb_minimal_beacon_block_body_proofs] [Minimal];
["consensus-spec-tests/tests/mainnet/eip7594/merkle_proof/single_merkle_proof/BeaconBlockBody/blob_kzg_commitment_*"] [eip_7594_mainnet_beacon_block_body_proofs] [Mainnet];
["consensus-spec-tests/tests/minimal/eip7594/merkle_proof/single_merkle_proof/BeaconBlockBody/blob_kzg_commitment_*"] [eip_7594_minimal_beacon_block_body_proofs] [Minimal];
)]
#[test_resources(glob)]
fn function_name(case: Case) {
Expand Down
8 changes: 4 additions & 4 deletions types/src/eip7594.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub type Cell = Box<ByteVector<BytesPerCell>>;

type DataColumn<P> = ContiguousList<Cell, <P as Preset>::MaxBlobCommitmentsPerBlock>;

// This exists in Preset but value for EIP7594 is different:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/_features/eip7594/p2p-interface.md#preset
pub type KzgCommitmentInclusionProofDepth = U4;
pub type KzgCommitmentsInclusionProofDepth = U4;

pub type BlobCommitmentsInclusionProof = ContiguousVector<H256, KzgCommitmentsInclusionProofDepth>;

#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Ssz)]
#[serde(deny_unknown_fields)]
Expand All @@ -42,7 +42,7 @@ pub struct DataColumnSidecar<P: Preset> {
pub kzg_commitments: ContiguousList<KzgCommitment, P::MaxBlobCommitmentsPerBlock>,
pub kzg_proofs: ContiguousList<KzgProof, P::MaxBlobCommitmentsPerBlock>,
pub signed_block_header: SignedBeaconBlockHeader,
pub kzg_commitments_inclusion_proof: ContiguousVector<H256, KzgCommitmentInclusionProofDepth>,
pub kzg_commitments_inclusion_proof: BlobCommitmentsInclusionProof,
}

impl<P: Preset> DataColumnSidecar<P> {
Expand Down

0 comments on commit 3ba1179

Please sign in to comment.