diff --git a/.gitmodules b/.gitmodules index d4dc20fe3..9145748b5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -203,3 +203,6 @@ [submodule "vendor/withdrawals-testnets"] path = vendor/withdrawals-testnets url = https://github.com/ethpandaops/withdrawals-testnet.git +[submodule "vendor/nim-kzg4844"] + path = vendor/nim-kzg4844 + url = https://github.com/status-im/nim-kzg4844/ diff --git a/beacon_chain/gossip_processing/block_processor.nim b/beacon_chain/gossip_processing/block_processor.nim index 7b7cc270f..4a820586b 100644 --- a/beacon_chain/gossip_processing/block_processor.nim +++ b/beacon_chain/gossip_processing/block_processor.nim @@ -26,7 +26,6 @@ from ../consensus_object_pools/block_quarantine import from ../validators/validator_monitor import MsgSource, ValidatorMonitor, registerAttestationInBlock, registerBeaconBlock, registerSyncAggregateInBlock -from ../spec/state_transition_block import validate_blobs_sidecar export sszdump, signatures_batch diff --git a/beacon_chain/spec/datatypes/deneb.nim b/beacon_chain/spec/datatypes/deneb.nim index 9df3c2068..16167b24b 100644 --- a/beacon_chain/spec/datatypes/deneb.nim +++ b/beacon_chain/spec/datatypes/deneb.nim @@ -24,7 +24,9 @@ import ../digest, "."/[base, phase0, altair, bellatrix, capella] -export json_serialization, base +from ../../vendor/nim-kzg4844/kzg4844 import KZGCommitment, KZGProof + +export json_serialization, base, kzg4844 const # https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.3/specs/deneb/polynomial-commitments.md#constants @@ -39,11 +41,6 @@ const MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS* = 4096'u64 type - # this block belongs elsewhere - will figure out after implementing c-kzg bindings - KZGCommitment* = array[48, byte] - KZGProof* = array[48, byte] - BLSFieldElement* = array[32, byte] - KZGCommitments* = List[KZGCommitment, Limit MAX_BLOBS_PER_BLOCK] Blobs* = List[Blob, Limit MAX_BLOBS_PER_BLOCK] diff --git a/beacon_chain/spec/state_transition_block.nim b/beacon_chain/spec/state_transition_block.nim index fc583d4d2..137560584 100644 --- a/beacon_chain/spec/state_transition_block.nim +++ b/beacon_chain/spec/state_transition_block.nim @@ -22,8 +22,9 @@ import chronicles, metrics, ../extras, - ./datatypes/[phase0, altair, bellatrix], - "."/[beaconstate, eth2_merkleization, helpers, validator, signatures] + ./datatypes/[phase0, altair, bellatrix, deneb], + "."/[beaconstate, eth2_merkleization, helpers, validator, signatures], + kzg4844/kzg_abi, kzg4844/kzg_ex from std/algorithm import fill, sorted from std/sequtils import count, filterIt, mapIt @@ -751,23 +752,19 @@ func process_blob_kzg_commitments( else: return err("process_blob_kzg_commitments: verify_kzg_commitments_against_transactions failed") -# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.2/specs/eip4844/beacon-chain.md#validate_blobs_sidecar -proc validate_blobs_sidecar*(slot: Slot, root: Eth2Digest, - expected_kzg_commitments: seq[deneb.KZGCommitment], - blobs_sidecar: deneb.BlobsSidecar): - Result[void, cstring] = - if slot != blobs_sidecar.beacon_block_slot: - return err("validate_blobs_sidecar: different slot in block and sidecar") +# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.3/specs/deneb/fork-choice.md#validate_blobs +proc validate_blobs*(expected_kzg_commitments: seq[KZGCommitment], + blobs: seq[KzgBlob], + proofs: seq[KZGProof]): + Result[void, cstring] = + if expected_kzg_commitments.len != blobs.len: + return err("validate_blobs: different commitment and blob lengths") - if root != blobs_sidecar.beacon_block_root: - return err("validate_blobs_sidecar: different root in block and sidecar") + if proofs.len != blobs.len: + return err("validate_blobs: different proof and blob lengths") - if expected_kzg_commitments.len != blobs_sidecar.blobs.len: - return err("validate_blobs_sidecar: different commitment lengths") - - # TODO - # if not kzg_4844.verify_aggregate_kzg_proof(asSeq(blobs_sidecar.blobs), expected_kzg_commitments, blobs_sidecar.kzg_aggregated_proof): - # return err("validate_blobs_sidecar: aggregated kzg proof verification failed") + if verifyProofs(blobs, expected_kzg_commitments, proofs).isErr(): + return err("validate_blobs: proof verification failed") ok() diff --git a/config.nims b/config.nims index 57d605907..cbe919b53 100644 --- a/config.nims +++ b/config.nims @@ -193,6 +193,9 @@ switch("hint", "XCannotRaiseY:off") # Useful for Chronos metrics. #--define:chronosFutureTracking +--define:kzgExternalBlst +--define:kzgExternalBlstNoSha256 + # ############################################################ # # No LTO for crypto diff --git a/vendor/nim-kzg4844 b/vendor/nim-kzg4844 new file mode 160000 index 000000000..95978add3 --- /dev/null +++ b/vendor/nim-kzg4844 @@ -0,0 +1 @@ +Subproject commit 95978add33f743efcd46725c38fc074d96f7b5fb