mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-09 03:17:55 +00:00
Update kzg interface (#4077)
* Update kzg interface * Update utils * Update dependency * Address review comments
This commit is contained in:
@@ -16,7 +16,7 @@ serde_derive = "1.0.116"
|
||||
eth2_serde_utils = "0.1.1"
|
||||
hex = "0.4.2"
|
||||
eth2_hashing = "0.3.0"
|
||||
c-kzg = {git = "https://github.com/ethereum/c-kzg-4844", rev = "69f6155d7524247be9d3f54ab3bfbe33a0345622" }
|
||||
c-kzg = {git = "https://github.com/ethereum/c-kzg-4844", rev = "549739fcb3aaec6fe5651e1912f05c604b45621b" }
|
||||
arbitrary = { version = "1.0", features = ["derive"], optional = true }
|
||||
|
||||
[features]
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use c_kzg::{Bytes48, BYTES_PER_COMMITMENT};
|
||||
use derivative::Derivative;
|
||||
use serde::de::{Deserialize, Deserializer};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
@@ -7,12 +8,16 @@ use std::fmt::{Debug, Display, Formatter};
|
||||
use std::str::FromStr;
|
||||
use tree_hash::{PackedEncoding, TreeHash};
|
||||
|
||||
const KZG_COMMITMENT_BYTES_LEN: usize = 48;
|
||||
|
||||
#[derive(Derivative, Clone, Encode, Decode)]
|
||||
#[derivative(PartialEq, Eq, Hash)]
|
||||
#[ssz(struct_behaviour = "transparent")]
|
||||
pub struct KzgCommitment(pub [u8; KZG_COMMITMENT_BYTES_LEN]);
|
||||
pub struct KzgCommitment(pub [u8; BYTES_PER_COMMITMENT]);
|
||||
|
||||
impl From<KzgCommitment> for Bytes48 {
|
||||
fn from(value: KzgCommitment) -> Self {
|
||||
value.0.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for KzgCommitment {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
@@ -22,13 +27,13 @@ impl Display for KzgCommitment {
|
||||
|
||||
impl Default for KzgCommitment {
|
||||
fn default() -> Self {
|
||||
KzgCommitment([0; KZG_COMMITMENT_BYTES_LEN])
|
||||
KzgCommitment([0; BYTES_PER_COMMITMENT])
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for KzgCommitment {
|
||||
fn tree_hash_type() -> tree_hash::TreeHashType {
|
||||
<[u8; KZG_COMMITMENT_BYTES_LEN] as TreeHash>::tree_hash_type()
|
||||
<[u8; BYTES_PER_COMMITMENT] as TreeHash>::tree_hash_type()
|
||||
}
|
||||
|
||||
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
|
||||
@@ -36,7 +41,7 @@ impl TreeHash for KzgCommitment {
|
||||
}
|
||||
|
||||
fn tree_hash_packing_factor() -> usize {
|
||||
<[u8; KZG_COMMITMENT_BYTES_LEN] as TreeHash>::tree_hash_packing_factor()
|
||||
<[u8; BYTES_PER_COMMITMENT] as TreeHash>::tree_hash_packing_factor()
|
||||
}
|
||||
|
||||
fn tree_hash_root(&self) -> tree_hash::Hash256 {
|
||||
@@ -86,15 +91,15 @@ impl FromStr for KzgCommitment {
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if let Some(stripped) = s.strip_prefix("0x") {
|
||||
let bytes = hex::decode(stripped).map_err(|e| e.to_string())?;
|
||||
if bytes.len() == KZG_COMMITMENT_BYTES_LEN {
|
||||
let mut kzg_commitment_bytes = [0; KZG_COMMITMENT_BYTES_LEN];
|
||||
if bytes.len() == BYTES_PER_COMMITMENT {
|
||||
let mut kzg_commitment_bytes = [0; BYTES_PER_COMMITMENT];
|
||||
kzg_commitment_bytes[..].copy_from_slice(&bytes);
|
||||
Ok(Self(kzg_commitment_bytes))
|
||||
} else {
|
||||
Err(format!(
|
||||
"InvalidByteLength: got {}, expected {}",
|
||||
bytes.len(),
|
||||
KZG_COMMITMENT_BYTES_LEN
|
||||
BYTES_PER_COMMITMENT
|
||||
))
|
||||
}
|
||||
} else {
|
||||
@@ -112,7 +117,7 @@ impl Debug for KzgCommitment {
|
||||
#[cfg(feature = "arbitrary")]
|
||||
impl arbitrary::Arbitrary<'_> for KzgCommitment {
|
||||
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
|
||||
let mut bytes = [0u8; KZG_COMMITMENT_BYTES_LEN];
|
||||
let mut bytes = [0u8; BYTES_PER_COMMITMENT];
|
||||
u.fill_buffer(&mut bytes)?;
|
||||
Ok(KzgCommitment(bytes))
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use c_kzg::{Bytes48, BYTES_PER_PROOF};
|
||||
use serde::de::{Deserialize, Deserializer};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
@@ -6,15 +7,19 @@ use std::fmt::Debug;
|
||||
use std::str::FromStr;
|
||||
use tree_hash::{PackedEncoding, TreeHash};
|
||||
|
||||
const KZG_PROOF_BYTES_LEN: usize = 48;
|
||||
|
||||
#[derive(PartialEq, Hash, Clone, Copy, Encode, Decode)]
|
||||
#[ssz(struct_behaviour = "transparent")]
|
||||
pub struct KzgProof(pub [u8; KZG_PROOF_BYTES_LEN]);
|
||||
pub struct KzgProof(pub [u8; BYTES_PER_PROOF]);
|
||||
|
||||
impl From<KzgProof> for Bytes48 {
|
||||
fn from(value: KzgProof) -> Self {
|
||||
value.0.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl KzgProof {
|
||||
pub fn empty() -> Self {
|
||||
let mut bytes = [0; KZG_PROOF_BYTES_LEN];
|
||||
let mut bytes = [0; BYTES_PER_PROOF];
|
||||
bytes[0] = 192;
|
||||
Self(bytes)
|
||||
}
|
||||
@@ -28,25 +33,25 @@ impl fmt::Display for KzgProof {
|
||||
|
||||
impl Default for KzgProof {
|
||||
fn default() -> Self {
|
||||
KzgProof([0; KZG_PROOF_BYTES_LEN])
|
||||
KzgProof([0; BYTES_PER_PROOF])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[u8; KZG_PROOF_BYTES_LEN]> for KzgProof {
|
||||
fn from(bytes: [u8; KZG_PROOF_BYTES_LEN]) -> Self {
|
||||
impl From<[u8; BYTES_PER_PROOF]> for KzgProof {
|
||||
fn from(bytes: [u8; BYTES_PER_PROOF]) -> Self {
|
||||
Self(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<[u8; KZG_PROOF_BYTES_LEN]> for KzgProof {
|
||||
fn into(self) -> [u8; KZG_PROOF_BYTES_LEN] {
|
||||
impl Into<[u8; BYTES_PER_PROOF]> for KzgProof {
|
||||
fn into(self) -> [u8; BYTES_PER_PROOF] {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for KzgProof {
|
||||
fn tree_hash_type() -> tree_hash::TreeHashType {
|
||||
<[u8; KZG_PROOF_BYTES_LEN]>::tree_hash_type()
|
||||
<[u8; BYTES_PER_PROOF]>::tree_hash_type()
|
||||
}
|
||||
|
||||
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
|
||||
@@ -54,7 +59,7 @@ impl TreeHash for KzgProof {
|
||||
}
|
||||
|
||||
fn tree_hash_packing_factor() -> usize {
|
||||
<[u8; KZG_PROOF_BYTES_LEN]>::tree_hash_packing_factor()
|
||||
<[u8; BYTES_PER_PROOF]>::tree_hash_packing_factor()
|
||||
}
|
||||
|
||||
fn tree_hash_root(&self) -> tree_hash::Hash256 {
|
||||
@@ -104,15 +109,15 @@ impl FromStr for KzgProof {
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if let Some(stripped) = s.strip_prefix("0x") {
|
||||
let bytes = hex::decode(stripped).map_err(|e| e.to_string())?;
|
||||
if bytes.len() == KZG_PROOF_BYTES_LEN {
|
||||
let mut kzg_proof_bytes = [0; KZG_PROOF_BYTES_LEN];
|
||||
if bytes.len() == BYTES_PER_PROOF {
|
||||
let mut kzg_proof_bytes = [0; BYTES_PER_PROOF];
|
||||
kzg_proof_bytes[..].copy_from_slice(&bytes);
|
||||
Ok(Self(kzg_proof_bytes))
|
||||
} else {
|
||||
Err(format!(
|
||||
"InvalidByteLength: got {}, expected {}",
|
||||
bytes.len(),
|
||||
KZG_PROOF_BYTES_LEN
|
||||
BYTES_PER_PROOF
|
||||
))
|
||||
}
|
||||
} else {
|
||||
@@ -130,7 +135,7 @@ impl Debug for KzgProof {
|
||||
#[cfg(feature = "arbitrary")]
|
||||
impl arbitrary::Arbitrary<'_> for KzgProof {
|
||||
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
|
||||
let mut bytes = [0u8; KZG_PROOF_BYTES_LEN];
|
||||
let mut bytes = [0u8; BYTES_PER_PROOF];
|
||||
u.fill_buffer(&mut bytes)?;
|
||||
Ok(KzgProof(bytes))
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ mod kzg_proof;
|
||||
mod trusted_setup;
|
||||
|
||||
pub use crate::{kzg_commitment::KzgCommitment, kzg_proof::KzgProof, trusted_setup::TrustedSetup};
|
||||
use c_kzg::Bytes48;
|
||||
pub use c_kzg::{
|
||||
Blob, Error as CKzgError, KZGSettings, BYTES_PER_BLOB, BYTES_PER_FIELD_ELEMENT,
|
||||
FIELD_ELEMENTS_PER_BLOB,
|
||||
@@ -13,9 +14,9 @@ use std::path::PathBuf;
|
||||
pub enum Error {
|
||||
InvalidTrustedSetup(CKzgError),
|
||||
InvalidKzgProof(CKzgError),
|
||||
InvalidLength(String),
|
||||
InvalidBytes(CKzgError),
|
||||
KzgProofComputationFailed(CKzgError),
|
||||
InvalidBlob(String),
|
||||
InvalidBlob(CKzgError),
|
||||
}
|
||||
|
||||
/// A wrapper over a kzg library that holds the trusted setup parameters.
|
||||
@@ -51,40 +52,67 @@ impl Kzg {
|
||||
})
|
||||
}
|
||||
|
||||
/// 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)
|
||||
/// Compute the kzg proof given a blob and its kzg commitment.
|
||||
pub fn compute_blob_kzg_proof(
|
||||
&self,
|
||||
blob: Blob,
|
||||
kzg_commitment: KzgCommitment,
|
||||
) -> Result<KzgProof, Error> {
|
||||
c_kzg::KZGProof::compute_blob_kzg_proof(blob, kzg_commitment.into(), &self.trusted_setup)
|
||||
.map_err(Error::KzgProofComputationFailed)
|
||||
.map(|proof| KzgProof(proof.to_bytes()))
|
||||
.map(|proof| KzgProof(proof.to_bytes().into_inner()))
|
||||
}
|
||||
|
||||
/// Verify an aggregate kzg proof given the blobs that generated the proof, the kzg commitments
|
||||
/// and the kzg proof.
|
||||
pub fn verify_aggregate_kzg_proof(
|
||||
/// Verify a kzg proof given the blob, kzg commitment and kzg proof.
|
||||
pub fn verify_blob_kzg_proof(
|
||||
&self,
|
||||
blob: Blob,
|
||||
kzg_commitment: KzgCommitment,
|
||||
kzg_proof: KzgProof,
|
||||
) -> Result<bool, Error> {
|
||||
c_kzg::KZGProof::verify_blob_kzg_proof(
|
||||
blob,
|
||||
kzg_commitment.into(),
|
||||
kzg_proof.into(),
|
||||
&self.trusted_setup,
|
||||
)
|
||||
.map_err(Error::InvalidKzgProof)
|
||||
}
|
||||
|
||||
/// Verify a batch of blob commitment proof triplets.
|
||||
///
|
||||
/// Note: This method is slightly faster than calling `Self::verify_blob_kzg_proof` in a loop sequentially.
|
||||
/// TODO(pawan): test performance against a parallelized rayon impl.
|
||||
pub fn verify_blob_kzg_proof_batch(
|
||||
&self,
|
||||
blobs: &[Blob],
|
||||
expected_kzg_commitments: &[KzgCommitment],
|
||||
kzg_aggregated_proof: KzgProof,
|
||||
kzg_commitments: &[KzgCommitment],
|
||||
kzg_proofs: &[KzgProof],
|
||||
) -> Result<bool, Error> {
|
||||
if blobs.len() != expected_kzg_commitments.len() {
|
||||
return Err(Error::InvalidLength(
|
||||
"blobs and expected_kzg_commitments should be of same size".to_string(),
|
||||
));
|
||||
}
|
||||
let commitments = expected_kzg_commitments
|
||||
let commitments_bytes = kzg_commitments
|
||||
.iter()
|
||||
.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)
|
||||
.map(|comm| Bytes48::from_bytes(&comm.0))
|
||||
.collect::<Result<Vec<Bytes48>, _>>()
|
||||
.map_err(Error::InvalidBytes)?;
|
||||
|
||||
let proofs_bytes = kzg_proofs
|
||||
.iter()
|
||||
.map(|proof| Bytes48::from_bytes(&proof.0))
|
||||
.collect::<Result<Vec<Bytes48>, _>>()
|
||||
.map_err(Error::InvalidBytes)?;
|
||||
c_kzg::KZGProof::verify_blob_kzg_proof_batch(
|
||||
blobs,
|
||||
&commitments_bytes,
|
||||
&proofs_bytes,
|
||||
&self.trusted_setup,
|
||||
)
|
||||
.map_err(Error::InvalidKzgProof)
|
||||
}
|
||||
|
||||
/// 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(),
|
||||
)
|
||||
pub fn blob_to_kzg_commitment(&self, blob: Blob) -> Result<KzgCommitment, Error> {
|
||||
c_kzg::KZGCommitment::blob_to_kzg_commitment(blob, &self.trusted_setup)
|
||||
.map_err(Error::InvalidBlob)
|
||||
.map(|com| KzgCommitment(com.to_bytes().into_inner()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user