Update kzg library to use bytes only interface
This commit is contained in:
parent
680a5c7fc8
commit
f04486dc71
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -788,7 +788,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "c-kzg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/pawanjay176/c-kzg-4844?rev=c9e4fa0dabdd000738b7fcdf85a72880a5da8748#c9e4fa0dabdd000738b7fcdf85a72880a5da8748"
|
||||
source = "git+https://github.com/ethereum/c-kzg-4844?rev=69f6155d7524247be9d3f54ab3bfbe33a0345622#69f6155d7524247be9d3f54ab3bfbe33a0345622"
|
||||
dependencies = [
|
||||
"hex",
|
||||
"libc",
|
||||
|
@ -136,13 +136,6 @@ pub fn validate_blob_for_gossip<T: BeaconChainTypes>(
|
||||
});
|
||||
}
|
||||
|
||||
// Verify that kzg commitments in the block are valid BLS g1 points
|
||||
for commitment in kzg_commitments {
|
||||
if kzg::bytes_to_g1(&commitment.0).is_err() {
|
||||
return Err(BlobError::InvalidKZGCommitment);
|
||||
}
|
||||
}
|
||||
|
||||
// Validate commitments agains transactions in the block.
|
||||
if verify_kzg_commitments_against_transactions::<T::EthSpec>(transactions, kzg_commitments)
|
||||
.is_err()
|
||||
@ -150,16 +143,6 @@ pub fn validate_blob_for_gossip<T: BeaconChainTypes>(
|
||||
return Err(BlobError::TransactionCommitmentMismatch);
|
||||
}
|
||||
|
||||
// Check that blobs are < BLS_MODULUS
|
||||
// TODO(pawan): Add this check after there's some resolution of this
|
||||
// issue https://github.com/ethereum/c-kzg-4844/issues/11
|
||||
// As of now, `bytes_to_bls_field` does not fail in the c-kzg library if blob >= BLS_MODULUS
|
||||
|
||||
// Validate that kzg proof is a valid g1 point
|
||||
if kzg::bytes_to_g1(&blob_sidecar.kzg_aggregated_proof.0).is_err() {
|
||||
return Err(BlobError::InvalidKzgProof);
|
||||
}
|
||||
|
||||
// Validatate that the kzg proof is valid against the commitments and blobs
|
||||
let kzg = chain
|
||||
.kzg
|
||||
|
@ -1,14 +1,11 @@
|
||||
use kzg::{Error as KzgError, Kzg, BYTES_PER_BLOB};
|
||||
use types::{Blob, BlobsSidecar, EthSpec, Hash256, KzgCommitment, KzgProof, Slot};
|
||||
|
||||
fn ssz_blob_to_crypto_blob<T: EthSpec>(blob: Blob<T>) -> Option<[u8; BYTES_PER_BLOB]> {
|
||||
if blob.len() != BYTES_PER_BLOB {
|
||||
return None;
|
||||
}
|
||||
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);
|
||||
Some(arr)
|
||||
arr.into()
|
||||
}
|
||||
|
||||
pub fn validate_blobs_sidecar<T: EthSpec>(
|
||||
@ -29,8 +26,7 @@ pub fn validate_blobs_sidecar<T: EthSpec>(
|
||||
.blobs
|
||||
.into_iter()
|
||||
.map(|blob| ssz_blob_to_crypto_blob::<T>(blob.clone())) // TODO(pawan): avoid this clone
|
||||
.collect::<Option<Vec<_>>>()
|
||||
.ok_or_else(|| KzgError::InvalidBlob("Invalid blobs in sidecar".to_string()))?;
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
kzg.verify_aggregate_kzg_proof(
|
||||
&blobs,
|
||||
@ -46,13 +42,12 @@ pub fn compute_aggregate_kzg_proof<T: EthSpec>(
|
||||
let blobs = blobs
|
||||
.into_iter()
|
||||
.map(|blob| ssz_blob_to_crypto_blob::<T>(blob.clone())) // TODO(pawan): avoid this clone
|
||||
.collect::<Option<Vec<_>>>()
|
||||
.ok_or_else(|| KzgError::InvalidBlob("Invalid blobs".to_string()))?;
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
kzg.compute_aggregate_kzg_proof(&blobs)
|
||||
}
|
||||
|
||||
pub fn blob_to_kzg_commitment<T: EthSpec>(kzg: &Kzg, blob: Blob<T>) -> Option<KzgCommitment> {
|
||||
let blob = ssz_blob_to_crypto_blob::<T>(blob)?;
|
||||
Some(kzg.blob_to_kzg_commitment(blob))
|
||||
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)
|
||||
}
|
||||
|
@ -216,10 +216,10 @@ impl BlockId {
|
||||
pub async fn blobs_sidecar<T: BeaconChainTypes>(
|
||||
&self,
|
||||
chain: &BeaconChain<T>,
|
||||
) -> Result<(Arc<BlobsSidecar<T::EthSpec>>), warp::Rejection> {
|
||||
) -> Result<Arc<BlobsSidecar<T::EthSpec>>, warp::Rejection> {
|
||||
let root = self.root(chain)?.0;
|
||||
match chain.store.get_blobs(&root) {
|
||||
Ok(Some(blob)) => Ok((Arc::new(blob))),
|
||||
Ok(Some(blob)) => Ok(Arc::new(blob)),
|
||||
Ok(None) => Err(warp_utils::reject::custom_not_found(format!(
|
||||
"Blob with block root {} is not in the store",
|
||||
root
|
||||
|
@ -18,7 +18,7 @@ eth2_serde_utils = "0.1.1"
|
||||
hex = "0.4.2"
|
||||
eth2_hashing = "0.3.0"
|
||||
ethereum-types = "0.12.1"
|
||||
c-kzg = {git = "https://github.com/pawanjay176/c-kzg-4844", rev = "c9e4fa0dabdd000738b7fcdf85a72880a5da8748" }
|
||||
c-kzg = {git = "https://github.com/ethereum/c-kzg-4844", rev = "69f6155d7524247be9d3f54ab3bfbe33a0345622" }
|
||||
|
||||
[features]
|
||||
default = ["mainnet-spec"]
|
||||
|
@ -3,20 +3,16 @@ mod kzg_proof;
|
||||
mod trusted_setup;
|
||||
|
||||
pub use crate::{kzg_commitment::KzgCommitment, kzg_proof::KzgProof, trusted_setup::TrustedSetup};
|
||||
pub use c_kzg::bytes_to_g1;
|
||||
pub use c_kzg::{
|
||||
Blob, Error as CKzgError, KzgSettings, BYTES_PER_BLOB, BYTES_PER_FIELD_ELEMENT,
|
||||
Blob, Error as CKzgError, KZGSettings, BYTES_PER_BLOB, BYTES_PER_FIELD_ELEMENT,
|
||||
FIELD_ELEMENTS_PER_BLOB,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug)]
|
||||
/// TODO(pawan): add docs after the c_kzg interface changes to bytes only.
|
||||
pub enum Error {
|
||||
InvalidTrustedSetup(CKzgError),
|
||||
InvalidKzgCommitment(CKzgError),
|
||||
InvalidKzgProof(CKzgError),
|
||||
KzgVerificationFailed(CKzgError),
|
||||
InvalidLength(String),
|
||||
KzgProofComputationFailed(CKzgError),
|
||||
InvalidBlob(String),
|
||||
@ -24,7 +20,7 @@ pub enum Error {
|
||||
|
||||
/// A wrapper over a kzg library that holds the trusted setup parameters.
|
||||
pub struct Kzg {
|
||||
trusted_setup: KzgSettings,
|
||||
trusted_setup: KZGSettings,
|
||||
}
|
||||
|
||||
impl Kzg {
|
||||
@ -35,7 +31,7 @@ impl Kzg {
|
||||
/// The number of G2 points should be equal to 65.
|
||||
pub fn new_from_trusted_setup(trusted_setup: TrustedSetup) -> Result<Self, Error> {
|
||||
Ok(Self {
|
||||
trusted_setup: KzgSettings::load_trusted_setup(
|
||||
trusted_setup: KZGSettings::load_trusted_setup(
|
||||
trusted_setup.g1_points(),
|
||||
trusted_setup.g2_points(),
|
||||
)
|
||||
@ -50,14 +46,14 @@ impl Kzg {
|
||||
#[deprecated]
|
||||
pub fn new_from_file(file_path: PathBuf) -> Result<Self, Error> {
|
||||
Ok(Self {
|
||||
trusted_setup: KzgSettings::load_trusted_setup_file(file_path)
|
||||
trusted_setup: KZGSettings::load_trusted_setup_file(file_path)
|
||||
.map_err(Error::InvalidTrustedSetup)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Compute the aggregated kzg proof given an array of blobs.
|
||||
pub fn compute_aggregate_kzg_proof(&self, blobs: &[Blob]) -> Result<KzgProof, Error> {
|
||||
c_kzg::KzgProof::compute_aggregate_kzg_proof(blobs, &self.trusted_setup)
|
||||
c_kzg::KZGProof::compute_aggregate_kzg_proof(blobs, &self.trusted_setup)
|
||||
.map_err(Error::KzgProofComputationFailed)
|
||||
.map(|proof| KzgProof(proof.to_bytes()))
|
||||
}
|
||||
@ -77,12 +73,9 @@ impl Kzg {
|
||||
}
|
||||
let commitments = expected_kzg_commitments
|
||||
.into_iter()
|
||||
.map(|comm| {
|
||||
c_kzg::KzgCommitment::from_bytes(&comm.0).map_err(Error::InvalidKzgCommitment)
|
||||
})
|
||||
.collect::<Result<Vec<c_kzg::KzgCommitment>, Error>>()?;
|
||||
let proof =
|
||||
c_kzg::KzgProof::from_bytes(&kzg_aggregated_proof.0).map_err(Error::InvalidKzgProof)?;
|
||||
.map(|comm| comm.0.into())
|
||||
.collect::<Vec<c_kzg::KZGCommitment>>();
|
||||
let proof: c_kzg::KZGProof = kzg_aggregated_proof.0.into();
|
||||
proof
|
||||
.verify_aggregate_kzg_proof(blobs, &commitments, &self.trusted_setup)
|
||||
.map_err(Error::InvalidKzgProof)
|
||||
@ -91,7 +84,7 @@ impl Kzg {
|
||||
/// Converts a blob to a kzg commitment.
|
||||
pub fn blob_to_kzg_commitment(&self, blob: Blob) -> KzgCommitment {
|
||||
KzgCommitment(
|
||||
c_kzg::KzgCommitment::blob_to_kzg_commitment(blob, &self.trusted_setup).to_bytes(),
|
||||
c_kzg::KZGCommitment::blob_to_kzg_commitment(blob, &self.trusted_setup).to_bytes(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user