From 902055f29565234cd5f799af75eba3b91e8690ed Mon Sep 17 00:00:00 2001 From: Pawan Dhananjay Date: Tue, 22 Nov 2022 01:58:21 +0530 Subject: [PATCH] ugly utils --- beacon_node/beacon_chain/Cargo.toml | 1 + beacon_node/beacon_chain/src/kzg_utils.rs | 65 +++++++++++++++++------ beacon_node/beacon_chain/src/lib.rs | 1 + consensus/types/src/eth_spec.rs | 5 ++ crypto/kzg/src/lib.rs | 1 + 5 files changed, 57 insertions(+), 16 deletions(-) diff --git a/beacon_node/beacon_chain/Cargo.toml b/beacon_node/beacon_chain/Cargo.toml index 39ff16c6b..51c1fbb57 100644 --- a/beacon_node/beacon_chain/Cargo.toml +++ b/beacon_node/beacon_chain/Cargo.toml @@ -50,6 +50,7 @@ lru = "0.7.1" tempfile = "3.1.0" bitvec = "0.20.4" bls = { path = "../../crypto/bls" } +kzg = { path = "../../crypto/kzg" } safe_arith = { path = "../../consensus/safe_arith" } fork_choice = { path = "../../consensus/fork_choice" } task_executor = { path = "../../common/task_executor" } diff --git a/beacon_node/beacon_chain/src/kzg_utils.rs b/beacon_node/beacon_chain/src/kzg_utils.rs index 11060a131..ef6045043 100644 --- a/beacon_node/beacon_chain/src/kzg_utils.rs +++ b/beacon_node/beacon_chain/src/kzg_utils.rs @@ -1,27 +1,60 @@ -use types::{Blob, BlobsSidecar, EthSpec, KzgCommitment, KzgProof}; +use kzg::Kzg; +use types::{Blob, BlobsSidecar, EthSpec, Hash256, KzgCommitment, KzgProof, Slot}; -pub fn validate_blobs_sidecar( +// TODO(pawan): make this generic over blob size +fn ssz_blob_to_crypto_blob(blob: Blob) -> Option<[u8; 131072]> { + if blob.len() != 131072 { + return None; + } + let blob_vec: Vec = blob.into(); + let mut arr = [0; 131072]; + arr.copy_from_slice(&blob_vec); + Some(arr) +} + +pub fn validate_blobs_sidecar( + kzg: &Kzg, slot: Slot, beacon_block_root: Hash256, expected_kzg_commitments: &[KzgCommitment], - blobs_sidecar: BlobsSidecar, -) -> bool { - //TODO(pawan): change to a Result later - if slot != blobs_sidecar.blobs + blobs_sidecar: BlobsSidecar, +) -> Result { + if slot != blobs_sidecar.beacon_block_slot || beacon_block_root != blobs_sidecar.beacon_block_root || blobs_sidecar.blobs.len() != expected_kzg_commitments.len() - || !verify_aggregate_kzg_proof( - blobs_sidecar.blobs, - expected_kzg_commitments, - blobs_sidecar.kzg_aggregate_proof, - ) { - return false; - } else { - return true; + return Ok(false); } + + let blobs = blobs_sidecar + .blobs + .into_iter() + .map(|blob| ssz_blob_to_crypto_blob::(blob.clone())) // TODO(pawan): avoid this clone + .collect::>>() + .ok_or_else(|| "Invalid blobs in sidecar".to_string())?; + + kzg.verify_aggregate_kzg_proof( + &blobs, + expected_kzg_commitments, + blobs_sidecar.kzg_aggregate_proof, + ) + .map_err(|e| format!("Failed to verify kzg proof: {:?}", e)) } -pub fn compute_aggregate_kzg_proof(blobs: &[Blob]) -> KzgProof { - unimplemented!() +pub fn compute_aggregate_kzg_proof( + kzg: &Kzg, + blobs: &[Blob], +) -> Result { + let blobs = blobs + .into_iter() + .map(|blob| ssz_blob_to_crypto_blob::(blob.clone())) // TODO(pawan): avoid this clone + .collect::>>() + .ok_or_else(|| "Invalid blobs in sidecar".to_string())?; + kzg.compute_aggregate_kzg_proof(&blobs) + .map_err(|e| format!("Failed to compute kzg proof: {:?}", e)) +} + +pub fn blob_to_kzg_commitment(kzg: &Kzg, blob: Blob) -> Option { + let blob = ssz_blob_to_crypto_blob::(blob)?; + Some(kzg.blob_to_kzg_commitment(blob)) } diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 19b90de1a..e1831078d 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -23,6 +23,7 @@ pub mod fork_choice_signal; pub mod fork_revert; mod head_tracker; pub mod historical_blocks; +pub mod kzg_utils; pub mod merge_readiness; mod metrics; pub mod migrate; diff --git a/consensus/types/src/eth_spec.rs b/consensus/types/src/eth_spec.rs index 745aa304d..fd1064a34 100644 --- a/consensus/types/src/eth_spec.rs +++ b/consensus/types/src/eth_spec.rs @@ -258,6 +258,11 @@ pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq + fn chunks_per_blob() -> usize { Self::FieldElementsPerBlob::to_usize() } + + /// Returns the `BYTES_PER_BLOB` constant for the specification. + fn bytes_per_blob() -> usize { + Self::BytesPerBlob::to_usize() + } } /// Macro to inherit some type values from another EthSpec. diff --git a/crypto/kzg/src/lib.rs b/crypto/kzg/src/lib.rs index 0f9638546..8f068848c 100644 --- a/crypto/kzg/src/lib.rs +++ b/crypto/kzg/src/lib.rs @@ -24,6 +24,7 @@ pub enum Error { KzgProofComputationFailed(CKzgError), } +/// A wrapper over a kzg library that holds the trusted setup parameters. pub struct Kzg { trusted_setup: KZGSettings, }