From 9274a661993d315a40f0e5ec3b3cac3a80e78825 Mon Sep 17 00:00:00 2001 From: Potuz Date: Mon, 8 Jul 2024 13:32:33 -0300 Subject: [PATCH] fix Merkle proof generator --- presets/mainnet/eip-7732.yaml | 1 - presets/minimal/eip-7732.yaml | 1 - pysetup/spec_builders/eip7732.py | 12 ++++++++++-- specs/_features/eip7732/builder.md | 8 ++++---- specs/_features/eip7732/p2p-interface.md | 14 +++++++++++--- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/presets/mainnet/eip-7732.yaml b/presets/mainnet/eip-7732.yaml index eca2dd705..eb43c981a 100644 --- a/presets/mainnet/eip-7732.yaml +++ b/presets/mainnet/eip-7732.yaml @@ -7,4 +7,3 @@ PTC_SIZE: 512 # 2**2 (= 4) MAX_PAYLOAD_ATTESTATIONS: 4 KZG_COMMITMENT_INCLUSION_PROOF_DEPTH_EIP7732: 13 -KZG_GENERALIZED_INDEX_PREFIX: 486 diff --git a/presets/minimal/eip-7732.yaml b/presets/minimal/eip-7732.yaml index efbfdfad9..751c1325a 100644 --- a/presets/minimal/eip-7732.yaml +++ b/presets/minimal/eip-7732.yaml @@ -7,4 +7,3 @@ PTC_SIZE: 2 # 2**2 (= 4) MAX_PAYLOAD_ATTESTATIONS: 4 KZG_COMMITMENT_INCLUSION_PROOF_DEPTH_EIP7732: 13 -KZG_GENERALIZED_INDEX_PREFIX: 486 diff --git a/pysetup/spec_builders/eip7732.py b/pysetup/spec_builders/eip7732.py index 2117f9633..0c335fe1d 100644 --- a/pysetup/spec_builders/eip7732.py +++ b/pysetup/spec_builders/eip7732.py @@ -13,6 +13,16 @@ class EIP7732SpecBuilder(BaseSpecBuilder): from eth2spec.electra import {preset_name} as electra ''' + @classmethod + def sundry_functions(cls) -> str: + return ''' +def concat_generalized_indices(*indices: GeneralizedIndex) -> GeneralizedIndex: + o = GeneralizedIndex(1) + for i in indices: + o = GeneralizedIndex(o * bit_floor(i) + (i - bit_floor(i))) + return o''' + + @classmethod def hardcoded_custom_type_dep_constants(cls, spec_object) -> Dict[str, str]: return { @@ -20,8 +30,6 @@ from eth2spec.electra import {preset_name} as electra 'MAX_PAYLOAD_ATTESTATIONS': spec_object.preset_vars['MAX_PAYLOAD_ATTESTATIONS'].value, 'KZG_COMMITMENT_INCLUSION_PROOF_DEPTH_EIP7732': spec_object.preset_vars['KZG_COMMITMENT_INCLUSION_PROOF_DEPTH_EIP7732'].value, - 'KZG_GENERALIZED_INDEX_PREFIX': - spec_object.preset_vars['KZG_GENERALIZED_INDEX_PREFIX'].value, } @classmethod diff --git a/specs/_features/eip7732/builder.md b/specs/_features/eip7732/builder.md index 9d28e2e6c..491f625d8 100644 --- a/specs/_features/eip7732/builder.md +++ b/specs/_features/eip7732/builder.md @@ -73,6 +73,10 @@ def get_blob_sidecars(signed_block: SignedBeaconBlock, sidecars: List[BlobSidecar] = [] for index, blob in enumerate(blobs): proof = compute_merkle_proof( + blob_kzg_commitments, + get_generalized_index(List[KZGCommitment, MAX_BLOB_COMMITMENTS_PER_BLOCK], index), + ) + proof += compute_merkle_proof( block.body, get_generalized_index( BeaconBlockBody, @@ -81,10 +85,6 @@ def get_blob_sidecars(signed_block: SignedBeaconBlock, "blob_kzg_commitments_root", ), ) - proof += compute_merkle_proof( - blob_kzg_commitments, - get_generalized_index(List[KZGCommitment, MAX_BLOB_COMMITMENTS_PER_BLOCK], index), - ) sidecars.append( BlobSidecar( index=index, diff --git a/specs/_features/eip7732/p2p-interface.md b/specs/_features/eip7732/p2p-interface.md index dd4e7a827..52546070f 100644 --- a/specs/_features/eip7732/p2p-interface.md +++ b/specs/_features/eip7732/p2p-interface.md @@ -36,7 +36,6 @@ This document contains the consensus-layer networking specification for EIP7732. | Name | Value | Description | |------------------------------------------|-----------------------------------|---------------------------------------------------------------------| | `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH_EIP7732` | `13` # TODO: Compute it when the spec stabilizes | Merkle proof depth for the `blob_kzg_commitments` list item | -| `KZG_GENERALIZED_INDEX_PREFIX` | `486` # TODO: Compute it when the spec stabilizes | Generalized index for the first item in the `blob_kzg_commitments` list | ### Containers @@ -63,8 +62,17 @@ class BlobSidecar(Container): ```python def verify_blob_sidecar_inclusion_proof(blob_sidecar: BlobSidecar) -> bool: - # hardcoded here because the block does not include the commitments but only their root. - gindex = GeneralizedIndex(KZG_GENERALIZED_INDEX_PREFIX + blob_sidecar.index) + inner_gindex = get_generalized_index( + List[KZGCommitment, MAX_BLOB_COMMITMENTS_PER_BLOCK], + blob_sidecar.index + ) + outer_gindex = get_generalized_index( + BeaconBlockBody, + "signed_execution_payload_header", + "message", + "blob_kzg_commitments_root", + ) + gindex = get_subtree_index(concat_generalized_indices(outer_gindex, inner_gindex)) return is_valid_merkle_branch( leaf=blob_sidecar.kzg_commitment.hash_tree_root(),