2022-11-15 18:46:52 +00:00
|
|
|
mod kzg_commitment;
|
|
|
|
mod kzg_proof;
|
2023-01-09 07:04:16 +00:00
|
|
|
mod trusted_setup;
|
2022-11-15 18:46:52 +00:00
|
|
|
|
2023-01-09 07:04:16 +00:00
|
|
|
pub use crate::{kzg_commitment::KzgCommitment, kzg_proof::KzgProof, trusted_setup::TrustedSetup};
|
2022-12-14 15:55:42 +00:00
|
|
|
pub use c_kzg::{
|
2023-01-17 06:42:15 +00:00
|
|
|
Blob, Error as CKzgError, KZGSettings, BYTES_PER_BLOB, BYTES_PER_FIELD_ELEMENT,
|
2022-12-14 15:55:42 +00:00
|
|
|
FIELD_ELEMENTS_PER_BLOB,
|
|
|
|
};
|
2022-11-15 18:46:52 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
InvalidTrustedSetup(CKzgError),
|
2022-11-21 19:51:10 +00:00
|
|
|
InvalidKzgProof(CKzgError),
|
|
|
|
InvalidLength(String),
|
|
|
|
KzgProofComputationFailed(CKzgError),
|
2022-11-29 10:34:16 +00:00
|
|
|
InvalidBlob(String),
|
2022-11-15 18:46:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 20:28:21 +00:00
|
|
|
/// A wrapper over a kzg library that holds the trusted setup parameters.
|
2022-11-15 18:46:52 +00:00
|
|
|
pub struct Kzg {
|
2023-01-17 06:42:15 +00:00
|
|
|
trusted_setup: KZGSettings,
|
2022-11-15 18:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Kzg {
|
2023-01-09 07:04:16 +00:00
|
|
|
/// Load the kzg trusted setup parameters from a vec of G1 and G2 points.
|
|
|
|
///
|
|
|
|
/// The number of G1 points should be equal to FIELD_ELEMENTS_PER_BLOB
|
|
|
|
/// Note: this number changes based on the preset values.
|
|
|
|
/// The number of G2 points should be equal to 65.
|
|
|
|
pub fn new_from_trusted_setup(trusted_setup: TrustedSetup) -> Result<Self, Error> {
|
|
|
|
Ok(Self {
|
2023-01-17 06:42:15 +00:00
|
|
|
trusted_setup: KZGSettings::load_trusted_setup(
|
2023-01-09 07:04:16 +00:00
|
|
|
trusted_setup.g1_points(),
|
|
|
|
trusted_setup.g2_points(),
|
|
|
|
)
|
|
|
|
.map_err(Error::InvalidTrustedSetup)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a trusted setup given the path to the file containing the trusted setup values.
|
|
|
|
/// The format is specified in `c_kzg::KzgSettings::load_trusted_setup_file`.
|
|
|
|
///
|
|
|
|
/// Note: This function will likely be deprecated. Use `Kzg::new_from_trusted_setup` instead.
|
|
|
|
#[deprecated]
|
2022-11-15 18:46:52 +00:00
|
|
|
pub fn new_from_file(file_path: PathBuf) -> Result<Self, Error> {
|
|
|
|
Ok(Self {
|
2023-01-17 06:42:15 +00:00
|
|
|
trusted_setup: KZGSettings::load_trusted_setup_file(file_path)
|
2022-11-21 19:51:10 +00:00
|
|
|
.map_err(Error::InvalidTrustedSetup)?,
|
2022-11-15 18:46:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-09 07:04:16 +00:00
|
|
|
/// Compute the aggregated kzg proof given an array of blobs.
|
2022-11-21 19:51:10 +00:00
|
|
|
pub fn compute_aggregate_kzg_proof(&self, blobs: &[Blob]) -> Result<KzgProof, Error> {
|
2023-01-17 06:42:15 +00:00
|
|
|
c_kzg::KZGProof::compute_aggregate_kzg_proof(blobs, &self.trusted_setup)
|
2022-11-21 19:51:10 +00:00
|
|
|
.map_err(Error::KzgProofComputationFailed)
|
|
|
|
.map(|proof| KzgProof(proof.to_bytes()))
|
|
|
|
}
|
|
|
|
|
2023-01-09 07:04:16 +00:00
|
|
|
/// Verify an aggregate kzg proof given the blobs that generated the proof, the kzg commitments
|
|
|
|
/// and the kzg proof.
|
2022-11-21 19:51:10 +00:00
|
|
|
pub fn verify_aggregate_kzg_proof(
|
|
|
|
&self,
|
|
|
|
blobs: &[Blob],
|
|
|
|
expected_kzg_commitments: &[KzgCommitment],
|
|
|
|
kzg_aggregated_proof: 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
|
|
|
|
.into_iter()
|
2023-01-17 06:42:15 +00:00
|
|
|
.map(|comm| comm.0.into())
|
|
|
|
.collect::<Vec<c_kzg::KZGCommitment>>();
|
|
|
|
let proof: c_kzg::KZGProof = kzg_aggregated_proof.0.into();
|
2022-11-21 19:51:10 +00:00
|
|
|
proof
|
|
|
|
.verify_aggregate_kzg_proof(blobs, &commitments, &self.trusted_setup)
|
|
|
|
.map_err(Error::InvalidKzgProof)
|
|
|
|
}
|
2022-11-15 18:46:52 +00:00
|
|
|
|
2023-01-09 07:04:16 +00:00
|
|
|
/// Converts a blob to a kzg commitment.
|
2022-11-21 19:51:10 +00:00
|
|
|
pub fn blob_to_kzg_commitment(&self, blob: Blob) -> KzgCommitment {
|
|
|
|
KzgCommitment(
|
2023-01-17 06:42:15 +00:00
|
|
|
c_kzg::KZGCommitment::blob_to_kzg_commitment(blob, &self.trusted_setup).to_bytes(),
|
2022-11-21 19:51:10 +00:00
|
|
|
)
|
|
|
|
}
|
2022-11-15 18:46:52 +00:00
|
|
|
}
|