Skip to content

Commit

Permalink
chore: Use reference to an array representing a blob instead of an ow…
Browse files Browse the repository at this point in the history
…ned 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 <[email protected]>
  • Loading branch information
kevaundray and jimmygchen authored Aug 1, 2024
1 parent 37dd0ea commit 8c78010
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
9 changes: 6 additions & 3 deletions consensus/types/src/data_column_sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -128,8 +128,11 @@ impl<E: EthSpec> DataColumnSidecar<E> {
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::<Result<Vec<_>, KzgError>>()?;

Expand Down
20 changes: 11 additions & 9 deletions crypto/kzg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<CellsAndKzgProofs, Error> {
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<CellsAndKzgProofs, Error> {
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
Expand Down Expand Up @@ -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<CellsAndKzgProofs, Error> {
pub fn compute_cells_and_proofs(_blob: KzgBlobRef) -> Result<CellsAndKzgProofs, Error> {
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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -32,11 +32,11 @@ impl<E: EthSpec> Case for KZGComputeCellsAndKZGProofs<E> {

fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
let cells_and_proofs = parse_blob::<E>(&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:?}"))
})
});
Expand Down

0 comments on commit 8c78010

Please sign in to comment.