Blob references in ckzg (#4723)

* Move to using references in ckzg functions

* cleanup TrustedSetup a bit

* Remove BYTES_PER_FIELD_ELEMENT from KzgPreset
This commit is contained in:
Pawan Dhananjay
2023-09-13 19:14:30 -07:00
committed by GitHub
parent 4a31e369bf
commit 2cfcb51207
6 changed files with 72 additions and 73 deletions

View File

@@ -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]

View File

@@ -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 {
@@ -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 {
@@ -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;
@@ -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,
@@ -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>;
@@ -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 {
@@ -146,21 +146,24 @@ 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()))
@@ -168,15 +171,15 @@ macro_rules! implement_kzg_preset {
}
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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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> {
@@ -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)))
}
@@ -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,
)

View File

@@ -1,4 +1,4 @@
use c_kzg::{BYTES_PER_G1_POINT, BYTES_PER_G2_POINT, FIELD_ELEMENTS_PER_BLOB};
use c_kzg::{BYTES_PER_G1_POINT, BYTES_PER_G2_POINT};
use serde::{
de::{self, Deserializer, Visitor},
Deserialize, Serialize,
@@ -22,7 +22,6 @@ struct G2Point([u8; BYTES_PER_G2_POINT]);
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TrustedSetup {
#[serde(rename = "setup_G1_lagrange")]
#[serde(deserialize_with = "deserialize_g1_points")]
g1_points: Vec<G1Point>,
#[serde(rename = "setup_G2")]
g2_points: Vec<G2Point>,
@@ -134,23 +133,6 @@ impl<'de> Deserialize<'de> for G2Point {
}
}
fn deserialize_g1_points<'de, D>(deserializer: D) -> Result<Vec<G1Point>, D::Error>
where
D: Deserializer<'de>,
{
let mut decoded: Vec<G1Point> = serde::de::Deserialize::deserialize(deserializer)?;
// FIELD_ELEMENTS_PER_BLOB is a compile time parameter that
// depends on whether lighthouse is compiled with minimal or mainnet features.
// Minimal and mainnet trusted setup parameters differ only by the
// number of G1 points they contain.
//
// Hence, we truncate the number of G1 points after deserialisation
// to ensure that we have the right number of g1 points in the
// trusted setup.
decoded.truncate(FIELD_ELEMENTS_PER_BLOB);
Ok(decoded)
}
fn strip_prefix(s: &str) -> &str {
if let Some(stripped) = s.strip_prefix("0x") {
stripped