Skip to content

Commit

Permalink
Create ProofAndEvaluation struct. Simplify verify...batch() logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoAlice committed Sep 28, 2023
1 parent 8d558b4 commit b75d8bd
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions ethereum-consensus/src/deneb/polynomial_commitments.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![allow(unused)]
use crate::{crypto::hash, primitives, ssz::prelude::ByteVector};
use crate::{primitives, ssz::prelude::*};
use alloy_primitives::{uint, U256};
use blst::min_pk::PublicKey;
use c_kzg::{Bytes32, Bytes48, Error, KzgSettings};
use ssz_rs::prelude::*;
use std::ops::Deref;

pub const BLS_MODULUS: U256 =
Expand All @@ -20,14 +17,13 @@ pub type VersionedHash = primitives::Bytes32;
pub type BLSFieldElement = U256;
pub type Polynomial = Vec<BLSFieldElement>; // Should this polynomial type be an array?

const fn create_g1_point_at_infinity() -> [u8; 48] {
let mut arr: [u8; 48] = [0; 48];
arr[0] = 0xc0;
arr
}

pub struct Blob(ByteVector<BYTES_PER_BLOB>);

pub struct ProofAndEvaluation {
proof: KzgProof,
evaluation: Bytes32,
}

#[derive(SimpleSerialize, Default, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct KzgCommitment(ByteVector<BYTES_PER_COMMITMENT>);
Expand Down Expand Up @@ -58,14 +54,16 @@ fn compute_kzg_proof(
blob: Blob,
z_bytes: Bytes32,
kzg_settings: &KzgSettings,
) -> Result<(KzgProof, Bytes32), Error> {
) -> Result<ProofAndEvaluation, Error> {
let inner = blob.0.as_ref();
let blob = c_kzg::Blob::from_bytes(inner).unwrap();

let (proof, evaluation) = c_kzg::KzgProof::compute_kzg_proof(&blob, &z_bytes, kzg_settings)?;
let proof = ByteVector::try_from(proof.to_bytes().as_ref()).unwrap();

Ok((KzgProof(proof), evaluation))
let result = ProofAndEvaluation { proof: KzgProof(proof), evaluation };

Ok(result)
}

fn compute_blob_kzg_proof(
Expand Down Expand Up @@ -130,17 +128,17 @@ fn verify_blob_kzg_proof_batch(
) -> Result<bool, Error> {
let mut c_kzg_blobs = Vec::with_capacity(blobs.len());

for bytes in blobs.iter().map(|blob| blob.0.as_ref()) {
let blob = c_kzg::Blob::from_bytes(bytes)?;
for blob in blobs {
let inner = blob.0.as_ref();
let blob = c_kzg::Blob::from_bytes(inner)?;
c_kzg_blobs.push(blob);
}

let out = c_kzg::KzgProof::verify_blob_kzg_proof_batch(
c_kzg::KzgProof::verify_blob_kzg_proof_batch(
&c_kzg_blobs,
commitments_bytes,
proofs_bytes,
kzg_settings,
)?;

Ok(out)
)
.map_err(Into::into)
}

0 comments on commit b75d8bd

Please sign in to comment.