Skip to content

Commit

Permalink
Removing redundant arguments from a blob
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkingLee committed Jul 27, 2023
1 parent 3995962 commit cb7da60
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
15 changes: 6 additions & 9 deletions crates/melo-erasure-coding/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fn random_bytes(len: usize) -> Vec<u8> {

fn blob_proof_case(field_elements_per_blob: usize, minimize: usize) {
// Build a random blob
let bytes_per_blob: usize = 32 * field_elements_per_blob;
let blob_data_len: usize = 31 * field_elements_per_blob;

let actual_byte_len = blob_data_len - minimize;
Expand All @@ -65,10 +64,10 @@ fn blob_proof_case(field_elements_per_blob: usize, minimize: usize) {
assert!(commitment_poly == commitment);
// Calculate the proof for the blob
let (commitment, proof) =
blob.commit_and_proof(&kzg, bytes_per_blob, field_elements_per_blob).unwrap();
blob.commit_and_proof(&kzg, field_elements_per_blob).unwrap();
// Verify the proof
let result = blob
.verify(&kzg, &commitment, &proof, bytes_per_blob, field_elements_per_blob)
.verify(&kzg, &commitment, &proof, field_elements_per_blob)
.unwrap();

assert!(commitment_poly == commitment);
Expand All @@ -78,13 +77,13 @@ fn blob_proof_case(field_elements_per_blob: usize, minimize: usize) {
let proof_mut = KZGProof(proof.0.add(&FsG1::rand()));
// Verification fails
let verify = blob
.verify(&kzg, &commitment, &proof_mut, bytes_per_blob, field_elements_per_blob)
.verify(&kzg, &commitment, &proof_mut, field_elements_per_blob)
.unwrap();
assert!(!verify);
// Modify a value in the commit, verification fails
let commitment_mut = KZGCommitment(commitment.0.add(&FsG1::rand()));
let verify = blob
.verify(&kzg, &commitment_mut, &proof, bytes_per_blob, field_elements_per_blob)
.verify(&kzg, &commitment_mut, &proof, field_elements_per_blob)
.unwrap();
assert!(!verify);
// Modify the blob
Expand All @@ -93,7 +92,7 @@ fn blob_proof_case(field_elements_per_blob: usize, minimize: usize) {

// Verification of the blob's proof fails
let verify = blob
.verify(&kzg, &commitment, &proof, bytes_per_blob, field_elements_per_blob)
.verify(&kzg, &commitment, &proof, field_elements_per_blob)
.unwrap();
assert!(!verify);
}
Expand Down Expand Up @@ -167,7 +166,6 @@ fn test_blob_verify_batch() {
// Build a random blob vector
let blob_count: usize = 4;
let field_elements_per_blob: usize = 4096;
let bytes_per_blob: usize = 32 * field_elements_per_blob;
let blob_data_len: usize = 31 * field_elements_per_blob;
let mut blobs: Vec<Blob> = Vec::new();
for _ in 0..blob_count {
Expand All @@ -182,7 +180,7 @@ fn test_blob_verify_batch() {
let kzg = KZG::new(embedded_kzg_settings());
for blob in blobs.iter() {
let (commitment, proof) =
blob.commit_and_proof(&kzg, bytes_per_blob, field_elements_per_blob).unwrap();
blob.commit_and_proof(&kzg, field_elements_per_blob).unwrap();
commitments.push(commitment);
proofs.push(proof);
}
Expand All @@ -193,7 +191,6 @@ fn test_blob_verify_batch() {
&commitments,
&proofs,
&kzg,
bytes_per_blob,
field_elements_per_blob,
)
.unwrap();
Expand Down
52 changes: 46 additions & 6 deletions primitives/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use alloc::{
};
use derive_more::{AsMut, AsRef, Deref, DerefMut, From};
use kzg::eip_4844::{
bytes_of_uint64, hash, BYTES_PER_FIELD_ELEMENT, CHALLENGE_INPUT_SIZE,
bytes_of_uint64, hash, CHALLENGE_INPUT_SIZE,
FIAT_SHAMIR_PROTOCOL_DOMAIN,
};
use kzg::{Fr, G1};
Expand All @@ -33,6 +33,8 @@ use rust_kzg_blst::{
types::{fr::FsFr, g1::FsG1, poly::FsPoly},
};

use crate::config::BYTES_PER_FIELD_ELEMENT;

#[derive(Debug, Default, Clone, PartialEq, Eq, From, AsRef, AsMut, Deref, DerefMut)]
#[repr(transparent)]
pub struct Blob(pub Vec<BlsScalar>);
Expand Down Expand Up @@ -105,9 +107,11 @@ impl Blob {
&self,
commitment: &KZGCommitment,
kzg: &KZG,
bytes_per_blob: usize,
field_elements_per_blob: usize,
) -> Result<KZGProof, String> {
check_field_elements_per_blob(field_elements_per_blob)?;
let bytes_per_blob: usize = BYTES_PER_FIELD_ELEMENT * field_elements_per_blob;

let x = compute_challenge(
&self.to_fs_fr_vec(),
commitment,
Expand All @@ -122,9 +126,11 @@ impl Blob {
pub fn commit_and_proof(
&self,
kzg: &KZG,
bytes_per_blob: usize,
field_elements_per_blob: usize,
) -> Result<(KZGCommitment, KZGProof), String> {
check_field_elements_per_blob(field_elements_per_blob)?;
let bytes_per_blob: usize = BYTES_PER_FIELD_ELEMENT * field_elements_per_blob;

let poly = self.to_poly();
let commitment = kzg.commit(&poly)?;
let x = compute_challenge(
Expand All @@ -143,9 +149,11 @@ impl Blob {
kzg: &KZG,
commitment: &KZGCommitment,
proof: &KZGProof,
bytes_per_blob: usize,
field_elements_per_blob: usize,
) -> Result<bool, String> {
check_field_elements_per_blob(field_elements_per_blob)?;
let bytes_per_blob: usize = BYTES_PER_FIELD_ELEMENT * field_elements_per_blob;

let x = compute_challenge(
&self.to_fs_fr_vec(),
commitment,
Expand All @@ -162,10 +170,30 @@ impl Blob {
commitments: &[KZGCommitment],
proofs: &[KZGProof],
kzg: &KZG,
bytes_per_blob: usize,
field_elements_per_blob: usize,
) -> Result<bool, String> {
// validate_batched_input(commitments_g1, proofs_g1)?;

if commitments.iter().any(|commitment| !commitment.0.is_valid()) {
return Err("Invalid commitment".to_string());
}

if proofs.iter().any(|proof| !proof.0.is_valid()) {
return Err("Invalid proof".to_string());
}

// 检查 commitment 、blobs 和 proof 的长度是否一致
if blobs.len() != commitments.len() || blobs.len() != proofs.len() {
return Err(format!(
"Invalid input length. Expected {} got commitments: {} and proofs: {}",
blobs.len(),
commitments.len(),
proofs.len()
));
}

check_field_elements_per_blob(field_elements_per_blob)?;
let bytes_per_blob: usize = BYTES_PER_FIELD_ELEMENT * field_elements_per_blob;

let (zs, ys) = compute_challenges_and_evaluate_polynomial(
blobs,
commitments,
Expand Down Expand Up @@ -198,6 +226,15 @@ impl Blob {
}
}

fn check_field_elements_per_blob(
field_elements_per_blob: usize,
) -> Result<(), String> {
if !field_elements_per_blob.is_power_of_two() {
return Err("field_elements_per_blob must be powers of two".to_string());
}
Ok(())
}

fn compute_challenges_and_evaluate_polynomial(
blobs: &[Blob],
commitments: &[KZGCommitment],
Expand All @@ -221,6 +258,9 @@ fn compute_challenges_and_evaluate_polynomial(
)
}

// This is a copy from kzg-rust https://github.com/sifraitech/rust-kzg/blob/main/blst/src/eip_4844.rs#L337
// Used to calculate the challenge value for the Blob, where we pass in the constant field_elements_per_blob
// as a parameter for ease of use by the application layer
fn compute_challenge(
blob: &[FsFr],
commitment: &FsG1,
Expand Down
2 changes: 0 additions & 2 deletions primitives/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub const BYTES_PER_BLOB: usize = BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB;
pub const BYTES_PER_FIELD_ELEMENT: usize = 32;
pub const FIELD_ELEMENTS_PER_BLOB: usize = 4;
pub const EMBEDDED_KZG_SETTINGS_BYTES: &[u8] = include_bytes!("../../scripts/eth-public-parameters.bin");
6 changes: 5 additions & 1 deletion primitives/src/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,12 @@ impl Polynomial {
FsPoly::new(size).map(Self)
}

fn is_valid(&self) -> bool {
self.0.coeffs.len().is_power_of_two()
}

pub fn checked(&self) -> Result<Self, String> {
if !self.0.coeffs.len().is_power_of_two() {
if !self.is_valid() {
return Err("Polynomial size must be a power of two".to_string());
}
Ok(self.clone())
Expand Down

0 comments on commit cb7da60

Please sign in to comment.