Skip to content
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

Blob references in ckzg #4723

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions beacon_node/beacon_chain/src/kzg_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn validate_blob<T: EthSpec>(
kzg_proof: KzgProof,
) -> Result<bool, KzgError> {
kzg.verify_blob_kzg_proof(
ssz_blob_to_crypto_blob::<T>(&blob)?,
&ssz_blob_to_crypto_blob::<T>(&blob)?,
kzg_commitment,
kzg_proof,
)
Expand Down Expand Up @@ -45,15 +45,15 @@ pub fn compute_blob_kzg_proof<T: EthSpec>(
kzg_commitment: KzgCommitment,
) -> Result<KzgProof, KzgError> {
// Avoid this blob clone
kzg.compute_blob_kzg_proof(ssz_blob_to_crypto_blob::<T>(blob)?, kzg_commitment)
kzg.compute_blob_kzg_proof(&ssz_blob_to_crypto_blob::<T>(blob)?, kzg_commitment)
}

/// Compute the kzg commitment for a given blob.
pub fn blob_to_kzg_commitment<T: EthSpec>(
kzg: &Kzg<T::Kzg>,
blob: &Blob<T>,
) -> Result<KzgCommitment, KzgError> {
kzg.blob_to_kzg_commitment(ssz_blob_to_crypto_blob::<T>(blob)?)
kzg.blob_to_kzg_commitment(&ssz_blob_to_crypto_blob::<T>(blob)?)
}

/// Compute the kzg proof for a given blob and an evaluation point z.
Expand All @@ -63,7 +63,7 @@ pub fn compute_kzg_proof<T: EthSpec>(
z: Hash256,
) -> Result<(KzgProof, Hash256), KzgError> {
let z = z.0.into();
kzg.compute_kzg_proof(ssz_blob_to_crypto_blob::<T>(blob)?, z)
kzg.compute_kzg_proof(&ssz_blob_to_crypto_blob::<T>(blob)?, &z)
.map(|(proof, z)| (proof, Hash256::from_slice(&z.to_vec())))
}

Expand All @@ -75,5 +75,5 @@ pub fn verify_kzg_proof<T: EthSpec>(
z: Hash256,
y: Hash256,
) -> Result<bool, KzgError> {
kzg.verify_kzg_proof(kzg_commitment, z.0.into(), y.0.into(), kzg_proof)
kzg.verify_kzg_proof(kzg_commitment, &z.0.into(), &y.0.into(), kzg_proof)
}
8 changes: 4 additions & 4 deletions consensus/types/src/blob_sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::test_utils::TestRandom;
use crate::{Blob, ChainSpec, Domain, EthSpec, Fork, Hash256, SignedBlobSidecar, SignedRoot, Slot};
use bls::SecretKey;
use derivative::Derivative;
use kzg::{Kzg, KzgCommitment, KzgPreset, KzgProof};
use kzg::{Kzg, KzgCommitment, KzgPreset, KzgProof, BYTES_PER_FIELD_ELEMENT};
use rand::Rng;
use serde_derive::{Deserialize, Serialize};
use ssz::Encode;
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<T: EthSpec> BlobSidecar<T> {
// each field element contained in the blob is < BLS_MODULUS
for i in 0..T::Kzg::FIELD_ELEMENTS_PER_BLOB {
let Some(byte) = blob_bytes.get_mut(
i.checked_mul(T::Kzg::BYTES_PER_FIELD_ELEMENT)
i.checked_mul(BYTES_PER_FIELD_ELEMENT)
.ok_or("overflow".to_string())?,
) else {
return Err(format!("blob byte index out of bounds: {:?}", i));
Expand All @@ -151,11 +151,11 @@ impl<T: EthSpec> BlobSidecar<T> {
let kzg_blob = T::blob_from_bytes(&blob).unwrap();

let commitment = kzg
.blob_to_kzg_commitment(kzg_blob.clone())
.blob_to_kzg_commitment(&kzg_blob)
.map_err(|e| format!("error computing kzg commitment: {:?}", e))?;

let proof = kzg
.compute_blob_kzg_proof(kzg_blob, commitment)
.compute_blob_kzg_proof(&kzg_blob, commitment)
.map_err(|e| format!("error computing kzg proof: {:?}", e))?;

Ok(Self {
Expand Down
4 changes: 2 additions & 2 deletions crypto/kzg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ serde_derive = "1.0.116"
ethereum_serde_utils = "0.5.0"
hex = "0.4.2"
ethereum_hashing = "1.0.0-beta.2"
c-kzg = { git = "https://github.com/ethereum/c-kzg-4844", rev = "fa3c62989527073fdce8b2138bb27a52bb2407c5" , features = ["mainnet-spec"]}
c_kzg_min = { package = "c-kzg", git = "https://github.com/ethereum//c-kzg-4844", rev = "fa3c62989527073fdce8b2138bb27a52bb2407c5", features = ["minimal-spec"], optional = true }
c-kzg = { git = "https://github.com/ethereum/c-kzg-4844", rev = "f5f6f863d475847876a2bd5ee252058d37c3a15d" , features = ["mainnet-spec", "serde"]}
c_kzg_min = { package = "c-kzg", git = "https://github.com/ethereum//c-kzg-4844", rev = "f5f6f863d475847876a2bd5ee252058d37c3a15d", features = ["minimal-spec", "serde"], optional = true }
arbitrary = { version = "1.0", features = ["derive"] }

[features]
Expand Down
71 changes: 37 additions & 34 deletions crypto/kzg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ops::Deref;
use std::str::FromStr;

pub use crate::{kzg_commitment::KzgCommitment, kzg_proof::KzgProof, trusted_setup::TrustedSetup};
pub use c_kzg::{Bytes32, Bytes48, BYTES_PER_COMMITMENT, BYTES_PER_PROOF};
pub use c_kzg::{Bytes32, Bytes48, BYTES_PER_COMMITMENT, BYTES_PER_FIELD_ELEMENT, BYTES_PER_PROOF};

#[derive(Debug)]
pub enum Error {
Expand All @@ -24,6 +24,8 @@ pub enum Error {
pub enum CryptoError {
CKzg(c_kzg::Error),
CKzgMin(c_kzg_min::Error),
/// Trusted setup is for the incorrect kzg preset.
InconsistentTrustedSetup,
}

impl From<c_kzg::Error> for CryptoError {
Expand Down Expand Up @@ -69,7 +71,6 @@ pub trait KzgPreset:
type Error: Into<CryptoError>;

const BYTES_PER_BLOB: usize;
const BYTES_PER_FIELD_ELEMENT: usize;
const FIELD_ELEMENTS_PER_BLOB: usize;

fn spec_name() -> KzgPresetId;
Expand All @@ -87,13 +88,13 @@ pub trait KzgPreset:
fn load_trusted_setup(trusted_setup: TrustedSetup) -> Result<Self::KzgSettings, CryptoError>;

fn compute_blob_kzg_proof(
blob: Self::Blob,
blob: &Self::Blob,
kzg_commitment: KzgCommitment,
trusted_setup: &Self::KzgSettings,
) -> Result<KzgProof, CryptoError>;

fn verify_blob_kzg_proof(
blob: Self::Blob,
blob: &Self::Blob,
kzg_commitment: KzgCommitment,
kzg_proof: KzgProof,
trusted_setup: &Self::KzgSettings,
Expand All @@ -107,20 +108,20 @@ pub trait KzgPreset:
) -> Result<bool, CryptoError>;

fn blob_to_kzg_commitment(
blob: Self::Blob,
blob: &Self::Blob,
trusted_setup: &Self::KzgSettings,
) -> Result<KzgCommitment, CryptoError>;

fn compute_kzg_proof(
blob: Self::Blob,
z: Self::Bytes32,
blob: &Self::Blob,
z: &Self::Bytes32,
trusted_setup: &Self::KzgSettings,
) -> Result<(KzgProof, Self::Bytes32), CryptoError>;

fn verify_kzg_proof(
kzg_commitment: KzgCommitment,
z: Self::Bytes32,
y: Self::Bytes32,
z: &Self::Bytes32,
y: &Self::Bytes32,
kzg_proof: KzgProof,
trusted_setup: &Self::KzgSettings,
) -> Result<bool, CryptoError>;
Expand All @@ -136,7 +137,6 @@ macro_rules! implement_kzg_preset {
type Error = $module_name::Error;

const BYTES_PER_BLOB: usize = $module_name::BYTES_PER_BLOB;
const BYTES_PER_FIELD_ELEMENT: usize = $module_name::BYTES_PER_FIELD_ELEMENT;
const FIELD_ELEMENTS_PER_BLOB: usize = $module_name::FIELD_ELEMENTS_PER_BLOB;

fn spec_name() -> KzgPresetId {
Expand All @@ -146,37 +146,40 @@ macro_rules! implement_kzg_preset {
fn load_trusted_setup(
trusted_setup: TrustedSetup,
) -> Result<Self::KzgSettings, CryptoError> {
if trusted_setup.g1_len() != Self::FIELD_ELEMENTS_PER_BLOB {
return Err(CryptoError::InconsistentTrustedSetup);
}
$module_name::KzgSettings::load_trusted_setup(
trusted_setup.g1_points(),
trusted_setup.g2_points(),
&trusted_setup.g1_points(),
&trusted_setup.g2_points(),
)
.map_err(CryptoError::from)
}

fn compute_blob_kzg_proof(
blob: Self::Blob,
blob: &Self::Blob,
kzg_commitment: KzgCommitment,
trusted_setup: &Self::KzgSettings,
) -> Result<KzgProof, CryptoError> {
$module_name::KzgProof::compute_blob_kzg_proof(
blob,
kzg_commitment.into(),
&kzg_commitment.into(),
trusted_setup,
)
.map(|proof| KzgProof(proof.to_bytes().into_inner()))
.map_err(CryptoError::from)
}

fn verify_blob_kzg_proof(
blob: Self::Blob,
blob: &Self::Blob,
kzg_commitment: KzgCommitment,
kzg_proof: KzgProof,
trusted_setup: &Self::KzgSettings,
) -> Result<bool, CryptoError> {
$module_name::KzgProof::verify_blob_kzg_proof(
blob,
kzg_commitment.into(),
kzg_proof.into(),
&kzg_commitment.into(),
&kzg_proof.into(),
trusted_setup,
)
.map_err(CryptoError::from)
Expand All @@ -198,7 +201,7 @@ macro_rules! implement_kzg_preset {
}

fn blob_to_kzg_commitment(
blob: Self::Blob,
blob: &Self::Blob,
trusted_setup: &Self::KzgSettings,
) -> Result<KzgCommitment, CryptoError> {
$module_name::KzgCommitment::blob_to_kzg_commitment(blob, trusted_setup)
Expand All @@ -207,8 +210,8 @@ macro_rules! implement_kzg_preset {
}

fn compute_kzg_proof(
blob: Self::Blob,
z: Self::Bytes32,
blob: &Self::Blob,
z: &Self::Bytes32,
trusted_setup: &Self::KzgSettings,
) -> Result<(KzgProof, Self::Bytes32), CryptoError> {
$module_name::KzgProof::compute_kzg_proof(blob, z, trusted_setup)
Expand All @@ -218,16 +221,16 @@ macro_rules! implement_kzg_preset {

fn verify_kzg_proof(
kzg_commitment: KzgCommitment,
z: Self::Bytes32,
y: Self::Bytes32,
z: &Self::Bytes32,
y: &Self::Bytes32,
kzg_proof: KzgProof,
trusted_setup: &Self::KzgSettings,
) -> Result<bool, CryptoError> {
$module_name::KzgProof::verify_kzg_proof(
kzg_commitment.into(),
&kzg_commitment.into(),
z,
y,
kzg_proof.into(),
&kzg_proof.into(),
trusted_setup,
)
.map_err(CryptoError::from)
Expand Down Expand Up @@ -274,7 +277,7 @@ impl<P: KzgPreset> Kzg<P> {
/// Compute the kzg proof given a blob and its kzg commitment.
pub fn compute_blob_kzg_proof(
&self,
blob: P::Blob,
blob: &P::Blob,
kzg_commitment: KzgCommitment,
) -> Result<KzgProof, Error> {
P::compute_blob_kzg_proof(blob, kzg_commitment, &self.trusted_setup)
Expand All @@ -284,7 +287,7 @@ impl<P: KzgPreset> Kzg<P> {
/// Verify a kzg proof given the blob, kzg commitment and kzg proof.
pub fn verify_blob_kzg_proof(
&self,
blob: P::Blob,
blob: &P::Blob,
kzg_commitment: KzgCommitment,
kzg_proof: KzgProof,
) -> Result<bool, Error> {
Expand Down Expand Up @@ -322,17 +325,17 @@ impl<P: KzgPreset> Kzg<P> {
}

/// Converts a blob to a kzg commitment.
pub fn blob_to_kzg_commitment(&self, blob: P::Blob) -> Result<KzgCommitment, Error> {
pub fn blob_to_kzg_commitment(&self, blob: &P::Blob) -> Result<KzgCommitment, Error> {
P::blob_to_kzg_commitment(blob, &self.trusted_setup).map_err(Error::InvalidBlob)
}

/// Computes the kzg proof for a given `blob` and an evaluation point `z`
pub fn compute_kzg_proof(
&self,
blob: P::Blob,
z: Bytes32,
blob: &P::Blob,
z: &Bytes32,
) -> Result<(KzgProof, Bytes32), Error> {
P::compute_kzg_proof(blob, P::bytes32_in(z), &self.trusted_setup)
P::compute_kzg_proof(blob, &P::bytes32_in(*z), &self.trusted_setup)
.map_err(Error::KzgProofComputationFailed)
.map(|(proof, y)| (proof, P::bytes32_out(y)))
}
Expand All @@ -341,14 +344,14 @@ impl<P: KzgPreset> Kzg<P> {
pub fn verify_kzg_proof(
&self,
kzg_commitment: KzgCommitment,
z: Bytes32,
y: Bytes32,
z: &Bytes32,
y: &Bytes32,
kzg_proof: KzgProof,
) -> Result<bool, Error> {
P::verify_kzg_proof(
kzg_commitment,
P::bytes32_in(z),
P::bytes32_in(y),
&P::bytes32_in(*z),
&P::bytes32_in(*y),
kzg_proof,
&self.trusted_setup,
)
Expand Down
Loading