-
Notifications
You must be signed in to change notification settings - Fork 788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Use reference to an array representing a blob instead of an owned KzgBlob (only for peerdas functions) #6179
Changes from 3 commits
ed05f07
3ceb017
8cbd70d
d7eb4b6
c3b4667
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<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) | ||
Comment on lines
+131
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the main change that fixes the stack overflow on The other changes change mainnet code path - it's probably not something we can merge to the I think we can merge this branch to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep this is correct |
||
}) | ||
.collect::<Result<Vec<_>, KzgError>>()?; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -75,25 +78,29 @@ impl Kzg { | |
} | ||
|
||
/// Compute the kzg proof given a blob and its kzg commitment. | ||
pub fn compute_blob_kzg_proof( | ||
#[allow(clippy::needless_lifetimes)] | ||
pub fn compute_blob_kzg_proof<'a>( | ||
&self, | ||
blob: &Blob, | ||
blob: KzgBlobRef<'a>, | ||
kzg_commitment: KzgCommitment, | ||
) -> Result<KzgProof, Error> { | ||
c_kzg::KzgProof::compute_blob_kzg_proof(blob, &kzg_commitment.into(), &self.trusted_setup) | ||
let blob = Blob::from_bytes(blob)?; | ||
c_kzg::KzgProof::compute_blob_kzg_proof(&blob, &kzg_commitment.into(), &self.trusted_setup) | ||
.map(|proof| KzgProof(proof.to_bytes().into_inner())) | ||
.map_err(Into::into) | ||
} | ||
|
||
/// Verify a kzg proof given the blob, kzg commitment and kzg proof. | ||
pub fn verify_blob_kzg_proof( | ||
#[allow(clippy::needless_lifetimes)] | ||
pub fn verify_blob_kzg_proof<'a>( | ||
&self, | ||
blob: &Blob, | ||
blob: KzgBlobRef<'a>, | ||
kzg_commitment: KzgCommitment, | ||
kzg_proof: KzgProof, | ||
) -> Result<(), Error> { | ||
let blob = Blob::from_bytes(blob)?; | ||
if !c_kzg::KzgProof::verify_blob_kzg_proof( | ||
blob, | ||
&blob, | ||
&kzg_commitment.into(), | ||
&kzg_proof.into(), | ||
&self.trusted_setup, | ||
|
@@ -108,9 +115,10 @@ impl Kzg { | |
/// | ||
/// Note: This method is slightly faster than calling `Self::verify_blob_kzg_proof` in a loop sequentially. | ||
/// TODO(pawan): test performance against a parallelized rayon impl. | ||
pub fn verify_blob_kzg_proof_batch( | ||
#[allow(clippy::needless_lifetimes)] | ||
pub fn verify_blob_kzg_proof_batch<'a>( | ||
&self, | ||
blobs: &[Blob], | ||
blobs: &[KzgBlobRef<'a>], | ||
kzg_commitments: &[KzgCommitment], | ||
kzg_proofs: &[KzgProof], | ||
) -> Result<(), Error> { | ||
|
@@ -124,8 +132,12 @@ impl Kzg { | |
.map(|proof| Bytes48::from(*proof)) | ||
.collect::<Vec<_>>(); | ||
|
||
let blobs = blobs | ||
.iter() | ||
.map(|blob| Blob::from_bytes(blob.as_slice())) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
if !c_kzg::KzgProof::verify_blob_kzg_proof_batch( | ||
blobs, | ||
&blobs, | ||
&commitments_bytes, | ||
&proofs_bytes, | ||
&self.trusted_setup, | ||
|
@@ -137,19 +149,23 @@ impl Kzg { | |
} | ||
|
||
/// Converts a blob to a kzg commitment. | ||
pub fn blob_to_kzg_commitment(&self, blob: &Blob) -> Result<KzgCommitment, Error> { | ||
c_kzg::KzgCommitment::blob_to_kzg_commitment(blob, &self.trusted_setup) | ||
#[allow(clippy::needless_lifetimes)] | ||
pub fn blob_to_kzg_commitment<'a>(&self, blob: KzgBlobRef<'a>) -> Result<KzgCommitment, Error> { | ||
let blob = Blob::from_bytes(blob)?; | ||
c_kzg::KzgCommitment::blob_to_kzg_commitment(&blob, &self.trusted_setup) | ||
.map(|commitment| KzgCommitment(commitment.to_bytes().into_inner())) | ||
.map_err(Into::into) | ||
} | ||
|
||
/// Computes the kzg proof for a given `blob` and an evaluation point `z` | ||
pub fn compute_kzg_proof( | ||
#[allow(clippy::needless_lifetimes)] | ||
pub fn compute_kzg_proof<'a>( | ||
&self, | ||
blob: &Blob, | ||
blob: KzgBlobRef<'a>, | ||
z: &Bytes32, | ||
) -> Result<(KzgProof, Bytes32), Error> { | ||
c_kzg::KzgProof::compute_kzg_proof(blob, z, &self.trusted_setup) | ||
let blob = Blob::from_bytes(blob)?; | ||
c_kzg::KzgProof::compute_kzg_proof(&blob, z, &self.trusted_setup) | ||
.map(|(proof, y)| (KzgProof(proof.to_bytes().into_inner()), y)) | ||
.map_err(Into::into) | ||
} | ||
|
@@ -173,7 +189,11 @@ 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> { | ||
#[allow(clippy::needless_lifetimes)] | ||
pub fn compute_cells_and_proofs<'a>( | ||
&self, | ||
blob: KzgBlobRef<'a>, | ||
) -> Result<CellsAndKzgProofs, Error> { | ||
let blob_bytes: &[u8; BYTES_PER_BLOB] = blob | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this conversion can now be removed as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yep that's true, this conversion is infallible |
||
.as_ref() | ||
.try_into() | ||
|
@@ -245,11 +265,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"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main change is here, we push the allocation of the blob into the kzg library
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, could we adopt the same change for
c-kzg-4844
? (to prepare it for increased blob count)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think c-kzg would need to allocate under the hood -- I'll open up a PR when I get back on my laptop to track arguments for and against