From 31b5c3e30ef9e260fc5395c377027d3183e7ea54 Mon Sep 17 00:00:00 2001 From: Agnish Ghosh <80243668+agnxsh@users.noreply.github.com> Date: Tue, 24 Sep 2024 18:16:05 +0530 Subject: [PATCH] add: inclusion proof and dc sidecar verification (#6577) * add: inclusion proof and dc sidecar verification * review 1 --- beacon_chain/spec/eip7594_helpers.nim | 55 ++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/beacon_chain/spec/eip7594_helpers.nim b/beacon_chain/spec/eip7594_helpers.nim index e1824d610..71cf41d6e 100644 --- a/beacon_chain/spec/eip7594_helpers.nim +++ b/beacon_chain/spec/eip7594_helpers.nim @@ -306,4 +306,57 @@ func get_extended_sample_count*(samples_per_slot: int, worstCaseConditionCount, i) <= falsePositiveThreshold: return i - NUMBER_OF_COLUMNS \ No newline at end of file + NUMBER_OF_COLUMNS + +# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.6/specs/_features/eip7594/p2p-interface.md#verify_data_column_sidecar_inclusion_proof +proc verify_data_column_sidecar_inclusion_proof*(sidecar: DataColumnSidecar): + Result[void, cstring] = + ## Verify if the given KZG Commitments are in included + ## in the beacon block or not + let gindex = + KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH_GINDEX.GeneralizedIndex + if not is_valid_merkle_branch( + hash_tree_root(sidecar.kzg_commitments), + sidecar.kzg_commitments_inclusion_proof, + KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH.int, + get_subtree_index(gindex), + sidecar.signed_block_header.message.body_root): + + return err("DataColumnSidecar: Inclusion proof is invalid") + + ok() + +# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.6/specs/_features/eip7594/p2p-interface.md#verify_data_column_sidecar_kzg_proofs +proc verify_data_column_sidecar_kzg_proofs*(sidecar: DataColumnSidecar): + Result[void, cstring] = + ## Verify if the KZG Proofs consisting in the `DataColumnSidecar` + ## is valid or not. + + # Check if the data column sidecar index < NUMBER_OF_COLUMNS + if not (sidecar.index < NUMBER_OF_COLUMNS): + return err("Data column sidecar index exceeds the NUMBER_OF_COLUMNS") + + # Check is the sidecar column length = sidecar.kzg_commitments length + # and sidecar.kzg_commitments length = sidecar.kzg_proofs length + if not (sidecar.column.len == sidecar.kzg_commitments.len): + return err("Data column sidecar length is not equal to the kzg_commitments length") + + if not (sidecar.kzg_commitments.len == sidecar.kzg_proofs.len): + return err("Sidecar kzg_commitments length is not equal to the kzg_proofs length") + + # Iterate through the cell indices + var cellIndices = + newSeq[CellIndex](MAX_BLOB_COMMITMENTS_PER_BLOCK) + for _ in 0..