ugly utils

This commit is contained in:
Pawan Dhananjay 2022-11-22 01:58:21 +05:30 committed by realbigsean
parent e8b5f311aa
commit 902055f295
No known key found for this signature in database
GPG Key ID: B372B64D866BF8CC
5 changed files with 57 additions and 16 deletions

View File

@ -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" }

View File

@ -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<T: EthSpec>(blob: Blob<T>) -> Option<[u8; 131072]> {
if blob.len() != 131072 {
return None;
}
let blob_vec: Vec<u8> = blob.into();
let mut arr = [0; 131072];
arr.copy_from_slice(&blob_vec);
Some(arr)
}
pub fn validate_blobs_sidecar<T: EthSpec>(
kzg: &Kzg,
slot: Slot,
beacon_block_root: Hash256,
expected_kzg_commitments: &[KzgCommitment],
blobs_sidecar: BlobsSidecar<T: EthSpec>,
) -> bool {
//TODO(pawan): change to a Result later
if slot != blobs_sidecar.blobs
blobs_sidecar: BlobsSidecar<T>,
) -> Result<bool, String> {
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::<T>(blob.clone())) // TODO(pawan): avoid this clone
.collect::<Option<Vec<_>>>()
.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<T: EthSpec>]) -> KzgProof {
unimplemented!()
pub fn compute_aggregate_kzg_proof<T: EthSpec>(
kzg: &Kzg,
blobs: &[Blob<T>],
) -> Result<KzgProof, String> {
let blobs = blobs
.into_iter()
.map(|blob| ssz_blob_to_crypto_blob::<T>(blob.clone())) // TODO(pawan): avoid this clone
.collect::<Option<Vec<_>>>()
.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<T: EthSpec>(kzg: &Kzg, blob: Blob<T>) -> Option<KzgCommitment> {
let blob = ssz_blob_to_crypto_blob::<T>(blob)?;
Some(kzg.blob_to_kzg_commitment(blob))
}

View File

@ -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;

View File

@ -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.

View File

@ -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,
}