-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update kzg interface * Update utils * Update dependency * Address review comments
- Loading branch information
1 parent
ae3e5f7
commit 76f49bd
Showing
6 changed files
with
127 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,57 @@ | ||
use kzg::{Error as KzgError, Kzg, BYTES_PER_BLOB}; | ||
use types::{Blob, BlobsSidecar, EthSpec, Hash256, KzgCommitment, KzgProof, Slot}; | ||
use types::{Blob, EthSpec, KzgCommitment, KzgProof}; | ||
|
||
/// Converts a blob ssz List object to an array to be used with the kzg | ||
/// crypto library. | ||
fn ssz_blob_to_crypto_blob<T: EthSpec>(blob: Blob<T>) -> kzg::Blob { | ||
let blob_vec: Vec<u8> = blob.into(); | ||
let mut arr = [0; BYTES_PER_BLOB]; | ||
arr.copy_from_slice(&blob_vec); | ||
arr.into() | ||
} | ||
|
||
pub fn validate_blobs_sidecar<T: EthSpec>( | ||
/// Validate a single blob-commitment-proof triplet from a `BlobSidecar`. | ||
pub fn validate_blob<T: EthSpec>( | ||
kzg: &Kzg, | ||
slot: Slot, | ||
beacon_block_root: Hash256, | ||
expected_kzg_commitments: &[KzgCommitment], | ||
blobs_sidecar: &BlobsSidecar<T>, | ||
blob: Blob<T>, | ||
kzg_commitment: KzgCommitment, | ||
kzg_proof: KzgProof, | ||
) -> Result<bool, KzgError> { | ||
if slot != blobs_sidecar.beacon_block_slot | ||
|| beacon_block_root != blobs_sidecar.beacon_block_root | ||
|| blobs_sidecar.blobs.len() != expected_kzg_commitments.len() | ||
{ | ||
return Ok(false); | ||
} | ||
|
||
let blobs = blobs_sidecar | ||
.blobs | ||
.clone() // TODO(pawan): avoid this clone | ||
.into_iter() | ||
.map(|blob| ssz_blob_to_crypto_blob::<T>(blob)) | ||
.collect::<Vec<_>>(); | ||
|
||
kzg.verify_aggregate_kzg_proof( | ||
&blobs, | ||
expected_kzg_commitments, | ||
blobs_sidecar.kzg_aggregated_proof, | ||
kzg.verify_blob_kzg_proof( | ||
ssz_blob_to_crypto_blob::<T>(blob), | ||
kzg_commitment, | ||
kzg_proof, | ||
) | ||
} | ||
|
||
pub fn compute_aggregate_kzg_proof<T: EthSpec>( | ||
/// Validate a batch of blob-commitment-proof triplets from multiple `BlobSidecars`. | ||
pub fn validate_blobs<T: EthSpec>( | ||
kzg: &Kzg, | ||
expected_kzg_commitments: &[KzgCommitment], | ||
blobs: &[Blob<T>], | ||
) -> Result<KzgProof, KzgError> { | ||
kzg_proofs: &[KzgProof], | ||
) -> Result<bool, KzgError> { | ||
let blobs = blobs | ||
.iter() | ||
.map(|blob| ssz_blob_to_crypto_blob::<T>(blob.clone())) // TODO(pawan): avoid this clone | ||
.map(|blob| ssz_blob_to_crypto_blob::<T>(blob.clone())) // Avoid this clone | ||
.collect::<Vec<_>>(); | ||
|
||
kzg.compute_aggregate_kzg_proof(&blobs) | ||
kzg.verify_blob_kzg_proof_batch(&blobs, expected_kzg_commitments, kzg_proofs) | ||
} | ||
|
||
/// Compute the kzg proof given an ssz blob and its kzg commitment. | ||
pub fn compute_blob_kzg_proof<T: EthSpec>( | ||
kzg: &Kzg, | ||
blob: Blob<T>, | ||
kzg_commitment: KzgCommitment, | ||
) -> Result<KzgProof, KzgError> { | ||
kzg.compute_blob_kzg_proof(ssz_blob_to_crypto_blob::<T>(blob), kzg_commitment) | ||
} | ||
|
||
pub fn blob_to_kzg_commitment<T: EthSpec>(kzg: &Kzg, blob: Blob<T>) -> KzgCommitment { | ||
let blob = ssz_blob_to_crypto_blob::<T>(blob); | ||
kzg.blob_to_kzg_commitment(blob) | ||
/// Compute the kzg commitment for a given blob. | ||
pub fn blob_to_kzg_commitment<T: EthSpec>( | ||
kzg: &Kzg, | ||
blob: Blob<T>, | ||
) -> Result<KzgCommitment, KzgError> { | ||
kzg.blob_to_kzg_commitment(ssz_blob_to_crypto_blob::<T>(blob)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters