From 8c780109966bd9364286c01e3785e485d70ea0c5 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 1 Aug 2024 17:33:39 +1000 Subject: [PATCH] chore: Use reference to an array representing a blob instead of an owned KzgBlob (#6179) * add KzgBlobRef type * modify code to use KzgBlobRef * clippy * Remove Deneb blob related changes to maintain compatibility with `c-kzg-4844`. --------- Co-authored-by: Jimmy Chen --- consensus/types/src/data_column_sidecar.rs | 9 ++++++--- crypto/kzg/src/lib.rs | 20 ++++++++++--------- .../cases/kzg_compute_cells_and_kzg_proofs.rs | 6 +++--- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/consensus/types/src/data_column_sidecar.rs b/consensus/types/src/data_column_sidecar.rs index 9b140ca0652..aa00e7d0213 100644 --- a/consensus/types/src/data_column_sidecar.rs +++ b/consensus/types/src/data_column_sidecar.rs @@ -9,7 +9,7 @@ use bls::Signature; use derivative::Derivative; #[cfg_attr(test, double)] use kzg::Kzg; -use kzg::{Blob as KzgBlob, CellRef as KzgCellRef, Error as KzgError}; +use kzg::{CellRef as KzgCellRef, Error as KzgError}; use kzg::{KzgCommitment, KzgProof}; use merkle_proof::verify_merkle_proof; #[cfg(test)] @@ -128,8 +128,11 @@ impl DataColumnSidecar { let blob_cells_and_proofs_vec = blobs .into_par_iter() .map(|blob| { - let blob = KzgBlob::from_bytes(blob).map_err(KzgError::from)?; - kzg.compute_cells_and_proofs(&blob) + let blob = blob + .as_ref() + .try_into() + .expect("blob should have a guaranteed size due to FixedVector"); + kzg.compute_cells_and_proofs(blob) }) .collect::, KzgError>>()?; diff --git a/crypto/kzg/src/lib.rs b/crypto/kzg/src/lib.rs index b9f556d5a47..c45fb8dfdd0 100644 --- a/crypto/kzg/src/lib.rs +++ b/crypto/kzg/src/lib.rs @@ -20,8 +20,11 @@ pub use peerdas_kzg::{ Cell, CellIndex as CellID, CellRef, TrustedSetup as PeerDASTrustedSetup, }; use peerdas_kzg::{prover::ProverError, verifier::VerifierError, PeerDASContext}; + pub type CellsAndKzgProofs = ([Cell; CELLS_PER_EXT_BLOB], [KzgProof; CELLS_PER_EXT_BLOB]); +pub type KzgBlobRef<'a> = &'a [u8; BYTES_PER_BLOB]; + #[derive(Debug)] pub enum Error { /// An error from the underlying kzg library. @@ -173,15 +176,14 @@ impl Kzg { } /// Computes the cells and associated proofs for a given `blob` at index `index`. - pub fn compute_cells_and_proofs(&self, blob: &Blob) -> Result { - let blob_bytes: &[u8; BYTES_PER_BLOB] = blob - .as_ref() - .try_into() - .expect("Expected blob to have size {BYTES_PER_BLOB}"); - + #[allow(clippy::needless_lifetimes)] + pub fn compute_cells_and_proofs<'a>( + &self, + blob: KzgBlobRef<'a>, + ) -> Result { let (cells, proofs) = self .context - .compute_cells_and_kzg_proofs(blob_bytes) + .compute_cells_and_kzg_proofs(blob) .map_err(Error::ProverKZG)?; // Convert the proof type to a c-kzg proof type @@ -245,11 +247,11 @@ impl Kzg { } pub mod mock { - use crate::{Blob, Cell, CellsAndKzgProofs, BYTES_PER_CELL, CELLS_PER_EXT_BLOB}; + use crate::{Cell, CellsAndKzgProofs, KzgBlobRef, BYTES_PER_CELL, CELLS_PER_EXT_BLOB}; use crate::{Error, KzgProof}; #[allow(clippy::type_complexity)] - pub fn compute_cells_and_proofs(_blob: &Blob) -> Result { + pub fn compute_cells_and_proofs(_blob: KzgBlobRef) -> Result { let empty_cells = vec![Cell::new([0; BYTES_PER_CELL]); CELLS_PER_EXT_BLOB] .try_into() .expect("expected {CELLS_PER_EXT_BLOB} number of items"); diff --git a/testing/ef_tests/src/cases/kzg_compute_cells_and_kzg_proofs.rs b/testing/ef_tests/src/cases/kzg_compute_cells_and_kzg_proofs.rs index de3eca46cb6..74a44fdddfc 100644 --- a/testing/ef_tests/src/cases/kzg_compute_cells_and_kzg_proofs.rs +++ b/testing/ef_tests/src/cases/kzg_compute_cells_and_kzg_proofs.rs @@ -1,6 +1,6 @@ use super::*; use crate::case_result::compare_result; -use kzg::{Blob as KzgBlob, CellsAndKzgProofs}; +use kzg::CellsAndKzgProofs; use serde::Deserialize; use std::marker::PhantomData; @@ -32,11 +32,11 @@ impl Case for KZGComputeCellsAndKZGProofs { fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> { let cells_and_proofs = parse_blob::(&self.input.blob).and_then(|blob| { - let blob = KzgBlob::from_bytes(&blob).map_err(|e| { + let blob = blob.as_ref().try_into().map_err(|e| { Error::InternalError(format!("Failed to convert blob to kzg blob: {e:?}")) })?; let kzg = get_kzg()?; - kzg.compute_cells_and_proofs(&blob).map_err(|e| { + kzg.compute_cells_and_proofs(blob).map_err(|e| { Error::InternalError(format!("Failed to compute cells and kzg proofs: {e:?}")) }) });