Remove blob clones in KZG verification (#4852)

## Issue Addressed

This PR removes two instances of blob clones during blob verification that may not be necessary.
This commit is contained in:
Jimmy Chen 2023-10-18 06:52:54 +00:00
parent a7c46bf7ed
commit 1b4545cd9d
5 changed files with 12 additions and 18 deletions

View File

@ -5123,7 +5123,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
kzg_utils::validate_blobs::<T::EthSpec>( kzg_utils::validate_blobs::<T::EthSpec>(
kzg, kzg,
expected_kzg_commitments, expected_kzg_commitments,
blobs, blobs.iter().collect(),
&kzg_proofs, &kzg_proofs,
) )
.map_err(BlockProductionError::KzgError)?; .map_err(BlockProductionError::KzgError)?;

View File

@ -502,8 +502,7 @@ pub fn verify_kzg_for_blob<T: EthSpec>(
kzg: &Kzg<T::Kzg>, kzg: &Kzg<T::Kzg>,
) -> Result<KzgVerifiedBlob<T>, AvailabilityCheckError> { ) -> Result<KzgVerifiedBlob<T>, AvailabilityCheckError> {
let _timer = crate::metrics::start_timer(&crate::metrics::KZG_VERIFICATION_SINGLE_TIMES); let _timer = crate::metrics::start_timer(&crate::metrics::KZG_VERIFICATION_SINGLE_TIMES);
//TODO(sean) remove clone if validate_blob::<T>(kzg, &blob.blob, blob.kzg_commitment, blob.kzg_proof)
if validate_blob::<T>(kzg, blob.blob.clone(), blob.kzg_commitment, blob.kzg_proof)
.map_err(AvailabilityCheckError::Kzg)? .map_err(AvailabilityCheckError::Kzg)?
{ {
Ok(KzgVerifiedBlob { blob }) Ok(KzgVerifiedBlob { blob })
@ -524,14 +523,9 @@ pub fn verify_kzg_for_blob_list<T: EthSpec>(
let _timer = crate::metrics::start_timer(&crate::metrics::KZG_VERIFICATION_BATCH_TIMES); let _timer = crate::metrics::start_timer(&crate::metrics::KZG_VERIFICATION_BATCH_TIMES);
let (blobs, (commitments, proofs)): (Vec<_>, (Vec<_>, Vec<_>)) = blob_list let (blobs, (commitments, proofs)): (Vec<_>, (Vec<_>, Vec<_>)) = blob_list
.iter() .iter()
.map(|blob| (blob.blob.clone(), (blob.kzg_commitment, blob.kzg_proof))) .map(|blob| (&blob.blob, (blob.kzg_commitment, blob.kzg_proof)))
.unzip(); .unzip();
if validate_blobs::<T>( if validate_blobs::<T>(kzg, commitments.as_slice(), blobs, proofs.as_slice())
kzg,
commitments.as_slice(),
blobs.as_slice(),
proofs.as_slice(),
)
.map_err(AvailabilityCheckError::Kzg)? .map_err(AvailabilityCheckError::Kzg)?
{ {
Ok(()) Ok(())

View File

@ -12,12 +12,12 @@ fn ssz_blob_to_crypto_blob<T: EthSpec>(
/// Validate a single blob-commitment-proof triplet from a `BlobSidecar`. /// Validate a single blob-commitment-proof triplet from a `BlobSidecar`.
pub fn validate_blob<T: EthSpec>( pub fn validate_blob<T: EthSpec>(
kzg: &Kzg<T::Kzg>, kzg: &Kzg<T::Kzg>,
blob: Blob<T>, blob: &Blob<T>,
kzg_commitment: KzgCommitment, kzg_commitment: KzgCommitment,
kzg_proof: KzgProof, kzg_proof: KzgProof,
) -> Result<bool, KzgError> { ) -> Result<bool, KzgError> {
kzg.verify_blob_kzg_proof( kzg.verify_blob_kzg_proof(
&ssz_blob_to_crypto_blob::<T>(&blob)?, &ssz_blob_to_crypto_blob::<T>(blob)?,
kzg_commitment, kzg_commitment,
kzg_proof, kzg_proof,
) )
@ -27,12 +27,12 @@ pub fn validate_blob<T: EthSpec>(
pub fn validate_blobs<T: EthSpec>( pub fn validate_blobs<T: EthSpec>(
kzg: &Kzg<T::Kzg>, kzg: &Kzg<T::Kzg>,
expected_kzg_commitments: &[KzgCommitment], expected_kzg_commitments: &[KzgCommitment],
blobs: &[Blob<T>], blobs: Vec<&Blob<T>>,
kzg_proofs: &[KzgProof], kzg_proofs: &[KzgProof],
) -> Result<bool, KzgError> { ) -> Result<bool, KzgError> {
let blobs = blobs let blobs = blobs
.iter() .into_iter()
.map(|blob| ssz_blob_to_crypto_blob::<T>(blob)) // Avoid this clone .map(|blob| ssz_blob_to_crypto_blob::<T>(blob))
.collect::<Result<Vec<_>, KzgError>>()?; .collect::<Result<Vec<_>, KzgError>>()?;
kzg.verify_blob_kzg_proof_batch(&blobs, expected_kzg_commitments, kzg_proofs) kzg.verify_blob_kzg_proof_batch(&blobs, expected_kzg_commitments, kzg_proofs)

View File

@ -91,7 +91,7 @@ impl<E: EthSpec> Case for KZGVerifyBlobKZGProof<E> {
let kzg = get_kzg::<E::Kzg>()?; let kzg = get_kzg::<E::Kzg>()?;
let result = parse_input(&self.input).and_then(|(blob, commitment, proof)| { let result = parse_input(&self.input).and_then(|(blob, commitment, proof)| {
validate_blob::<E>(&kzg, blob, commitment, proof) validate_blob::<E>(&kzg, &blob, commitment, proof)
.map_err(|e| Error::InternalError(format!("Failed to validate blob: {:?}", e))) .map_err(|e| Error::InternalError(format!("Failed to validate blob: {:?}", e)))
}); });

View File

@ -54,7 +54,7 @@ impl<E: EthSpec> Case for KZGVerifyBlobKZGProofBatch<E> {
let kzg = get_kzg::<E::Kzg>()?; let kzg = get_kzg::<E::Kzg>()?;
let result = parse_input(&self.input).and_then(|(commitments, blobs, proofs)| { let result = parse_input(&self.input).and_then(|(commitments, blobs, proofs)| {
validate_blobs::<E>(&kzg, &commitments, &blobs, &proofs) validate_blobs::<E>(&kzg, &commitments, blobs.iter().collect(), &proofs)
.map_err(|e| Error::InternalError(format!("Failed to validate blobs: {:?}", e))) .map_err(|e| Error::InternalError(format!("Failed to validate blobs: {:?}", e)))
}); });