Skip to content

Commit

Permalink
Configured EIP-7594 vector tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtiomTr committed Sep 12, 2024
1 parent 5f940b2 commit 9b65b59
Show file tree
Hide file tree
Showing 16 changed files with 361 additions and 124 deletions.
6 changes: 3 additions & 3 deletions arkworks/benches/eip_4844.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kzg::eip_4844::{
blob_to_kzg_commitment_rust, bytes_to_blob, compute_blob_kzg_proof_rust,
compute_kzg_proof_rust, verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust,
verify_kzg_proof_rust, compute_cells_and_kzg_proofs_rust,
compute_cells_and_kzg_proofs_rust, compute_kzg_proof_rust, verify_blob_kzg_proof_batch_rust,
verify_blob_kzg_proof_rust, verify_kzg_proof_rust,
};
use kzg_bench::benches::eip_4844::bench_eip_4844;
use rust_kzg_arkworks::eip_4844::load_trusted_setup_filename_rust;
Expand All @@ -21,7 +21,7 @@ fn bench_eip_4844_(c: &mut Criterion) {
&compute_blob_kzg_proof_rust,
&verify_blob_kzg_proof_rust,
&verify_blob_kzg_proof_batch_rust,
&compute_cells_and_kzg_proofs_rust
&compute_cells_and_kzg_proofs_rust,
);
}

Expand Down
9 changes: 4 additions & 5 deletions arkworks/src/eip_4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use kzg::eip_4844::{
blob_to_kzg_commitment_rust, compute_blob_kzg_proof_rust, compute_kzg_proof_rust,
load_trusted_setup_rust, verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust,
verify_kzg_proof_rust, Blob, Bytes32, Bytes48, CKZGSettings, KZGCommitment, KZGProof,
PrecomputationTableManager, BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1, BYTES_PER_G2, C_KZG_RET,
C_KZG_RET_BADARGS, C_KZG_RET_OK, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS,
TRUSTED_SETUP_NUM_G2_POINTS, BYTES_PER_BLOB,
PrecomputationTableManager, BYTES_PER_BLOB, BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1,
BYTES_PER_G2, C_KZG_RET, C_KZG_RET_BADARGS, C_KZG_RET_OK, FIELD_ELEMENTS_PER_BLOB,
TRUSTED_SETUP_NUM_G1_POINTS, TRUSTED_SETUP_NUM_G2_POINTS,
};
use kzg::{cfg_into_iter, Fr, G1};
use std::ptr::null_mut;
Expand Down Expand Up @@ -424,7 +424,6 @@ pub unsafe extern "C" fn compute_kzg_proof(
C_KZG_RET_OK
}


/// # Safety
#[no_mangle]
pub unsafe extern "C" fn compute_cells_and_kzg_proofs(
Expand All @@ -437,4 +436,4 @@ pub unsafe extern "C" fn compute_cells_and_kzg_proofs(
if cells.is_null() || proofs.is_null() || blob.is_null() || s.is_null() {
return C_KZG_RET_BADARGS;
}
}
}
35 changes: 35 additions & 0 deletions blst/tests/eip_7694.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#[cfg(test)]
mod tests {
use kzg::eip_4844::{bytes_to_blob, compute_cells_and_kzg_proofs_rust};
use kzg_bench::tests::eip_7594::test_vectors_compute_cells_and_kzg_proofs;
use rust_kzg_blst::{
eip_4844::load_trusted_setup,
types::{
fft_settings::FsFFTSettings,
fp::FsFp,
fr::FsFr,
g1::{FsG1, FsG1Affine},
g2::FsG2,
kzg_settings::FsKZGSettings,
poly::FsPoly,
},
};

#[test]
pub fn bytes_to_bls_field_test_() {
test_vectors_compute_cells_and_kzg_proofs::<
FsFr,
FsG1,
FsG2,
FsPoly,
FsFFTSettings,
FsKZGSettings,
FsFp,
FsG1Affine,
>(
&load_trusted_setup,
&compute_cells_and_kzg_proofs_rust,
&bytes_to_blob,
);
}
}
4 changes: 3 additions & 1 deletion constantine/benches/eip_4844.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kzg::eip_4844::{
blob_to_kzg_commitment_rust, bytes_to_blob, compute_blob_kzg_proof_rust, compute_cells_and_kzg_proofs_rust, compute_kzg_proof_rust, verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust, verify_kzg_proof_rust
blob_to_kzg_commitment_rust, bytes_to_blob, compute_blob_kzg_proof_rust,
compute_cells_and_kzg_proofs_rust, compute_kzg_proof_rust, verify_blob_kzg_proof_batch_rust,
verify_blob_kzg_proof_rust, verify_kzg_proof_rust,
};
use kzg_bench::benches::eip_4844::bench_eip_4844;
use rust_kzg_constantine::{
Expand Down
5 changes: 4 additions & 1 deletion kzg-bench/src/benches/eip_4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ pub fn bench_eip_4844<
compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> Result<(TG1, TFr), String>,
verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result<bool, String>,
compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result<TG1, String>,
compute_cells_and_kzg_proofs: &dyn Fn(&[u8], &TKZGSettings) -> Result<(Vec<Vec<TFr>>, Vec<TG1>), String>,
compute_cells_and_kzg_proofs: &dyn Fn(
&[u8],
&TKZGSettings,
) -> Result<(Vec<Vec<TFr>>, Vec<TG1>), String>,
verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result<bool, String>,
verify_blob_kzg_proof_batch: &dyn Fn(
&[Vec<TFr>],
Expand Down
28 changes: 15 additions & 13 deletions kzg-bench/src/test_vectors/compute_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
#![allow(dead_code)]

use alloc::string::String;
use alloc::vec::Vec;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct Input<'a> {
blob: &'a str,
z: &'a str,
}

impl Input<'_> {
pub fn get_blob_bytes(&self) -> Vec<u8> {
hex::decode(&self.blob[2..]).unwrap()
}

pub fn get_z_bytes(&self) -> Vec<u8> {
hex::decode(&self.z[2..]).unwrap()
pub fn get_blob_bytes(&self) -> Result<Vec<u8>, String> {
hex::decode(self.blob[2..]).map_err(|_| "Invalid blob".to_string())
}
}

#[derive(Deserialize)]
pub struct Test<'a> {
#[serde(borrow)]
pub input: Input<'a>,
#[serde(borrow)]
output: Option<(&'a str, &'a str)>,
output: Option<(Vec<String>, Vec<String>)>,
}

impl Test<'_> {
pub fn get_output_bytes(&self) -> Option<(Vec<u8>, Vec<u8>)> {
self.output.map(|(proof, y)| {
pub fn get_output(&self) -> Option<(Vec<Vec<u8>>, Vec<Vec<u8>>)> {
self.output.clone().map(|(cells, proofs)| {
(
hex::decode(&proof[2..]).unwrap(),
hex::decode(&y[2..]).unwrap(),
cells
.iter()
.map(|s| hex::decode(s[2..]).unwrap())
.collect::<Vec<_>>(),
proofs
.iter()
.map(|s| hex::decode(s[2..]).unwrap())
.collect::<Vec<Bytes48>>(),
)
})
}
Expand Down
4 changes: 3 additions & 1 deletion kzg-bench/src/test_vectors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod blob_to_kzg_commitment;
pub mod compute_blob_kzg_proof;
pub mod compute_cells_and_kzg_proofs;
pub mod compute_kzg_proof;
pub mod recover_cells_and_kzg_proofs;
pub mod verify_blob_kzg_proof;
pub mod verify_blob_kzg_proof_batch;
pub mod verify_cell_kzg_proof_batch;
pub mod verify_kzg_proof;
pub mod compute_cells_and_kzg_proofs;
49 changes: 49 additions & 0 deletions kzg-bench/src/test_vectors/recover_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![allow(dead_code)]

use crate::Bytes48;
use crate::{Cell, Error};
use alloc::string::String;
use alloc::vec::Vec;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct Input {
cell_indices: Vec<u64>,
cells: Vec<String>,
}

impl Input {
pub fn get_cell_indices(&self) -> Result<Vec<u64>, Error> {
Ok(self.cell_indices.clone())
}

pub fn get_cells(&self) -> Result<Vec<Cell>, Error> {
self.cells
.iter()
.map(|s| Cell::from_hex(s))
.collect::<Result<Vec<Cell>, Error>>()
}
}

#[derive(Deserialize)]
pub struct Test {
pub input: Input,
output: Option<(Vec<String>, Vec<String>)>,
}

impl Test {
pub fn get_output(&self) -> Option<(Vec<Cell>, Vec<Bytes48>)> {
self.output.clone().map(|(cells, proofs)| {
(
cells
.iter()
.map(|s| Cell::from_hex(s).unwrap())
.collect::<Vec<Cell>>(),
proofs
.iter()
.map(|s| Bytes48::from_hex(s).unwrap())
.collect::<Vec<Bytes48>>(),
)
})
}
}
53 changes: 53 additions & 0 deletions kzg-bench/src/test_vectors/verify_cell_kzg_proof_batch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![allow(dead_code)]

use crate::{Bytes48, Cell, Error};
use alloc::string::String;
use alloc::vec::Vec;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct Input {
commitments: Vec<String>,
cell_indices: Vec<u64>,
cells: Vec<String>,
proofs: Vec<String>,
}

impl Input {
pub fn get_commitments(&self) -> Result<Vec<Bytes48>, Error> {
self.commitments
.iter()
.map(|s| Bytes48::from_hex(s))
.collect::<Result<Vec<Bytes48>, Error>>()
}

pub fn get_cell_indices(&self) -> Result<Vec<u64>, Error> {
Ok(self.cell_indices.clone())
}

pub fn get_cells(&self) -> Result<Vec<Cell>, Error> {
self.cells
.iter()
.map(|s| Cell::from_hex(s))
.collect::<Result<Vec<Cell>, Error>>()
}

pub fn get_proofs(&self) -> Result<Vec<Bytes48>, Error> {
self.proofs
.iter()
.map(|s| Bytes48::from_hex(s))
.collect::<Result<Vec<Bytes48>, Error>>()
}
}

#[derive(Deserialize)]
pub struct Test {
pub input: Input,
output: Option<bool>,
}

impl Test {
pub fn get_output(&self) -> Option<bool> {
self.output
}
}
72 changes: 72 additions & 0 deletions kzg-bench/src/tests/eip_7594.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use super::utils::{get_manifest_dir, get_trusted_setup_path};
use crate::test_vectors::compute_cells_and_kzg_proofs;
use kzg::{FFTSettings, Fr, G1Affine, G1Fp, G1GetFp, G1Mul, KZGSettings, Poly, G1, G2};
use std::path::PathBuf;

const COMPUTE_CELLS_AND_KZG_PROOFS_TEST_VECTORS: &str =
"src/test_vectors/compute_cells_and_kzg_proofs/*/*/*";
const RECOVER_CELLS_AND_KZG_PROOFS_TEST_VECTORS: &str =
"src/test_vectors/recover_cells_and_kzg_proofs/*/*/*";
const VERIFY_CELL_KZG_PROOF_BATCH_TEST_VECTORS: &str =
"src/test_vectors/verify_cell_kzg_proof_batch/*/*/*";

pub fn test_vectors_compute_cells_and_kzg_proofs<
TFr: Fr,
TG1: G1 + G1Mul<TFr> + G1GetFp<TG1Fp>,
TG2: G2,
TPoly: Poly<TFr>,
TFFTSettings: FFTSettings<TFr>,
TKZGSettings: KZGSettings<TFr, TG1, TG2, TFFTSettings, TPoly, TG1Fp, TG1Affine>,
TG1Fp: G1Fp,
TG1Affine: G1Affine<TG1, TG1Fp>,
>(
load_trusted_setup: &dyn Fn(&str) -> Result<TKZGSettings, String>,
compute_cells_and_kzg_proofs: &dyn Fn(
&[TFr],
&TKZGSettings,
) -> Result<(Vec<TFr>, Vec<TG1>), String>,
bytes_to_blob: &dyn Fn(&[u8]) -> Result<Vec<TFr>, String>,
) {
let settings = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap();
let test_files: Vec<PathBuf> = glob::glob(&format!(
"{}/{}",
get_manifest_dir(),
COMPUTE_CELLS_AND_KZG_PROOFS_TEST_VECTORS
))
.unwrap()
.collect::<Vec<Result<_>>>()
.unwrap();
assert!(!test_files.is_empty());

for test_file in test_files {
let yaml_data = fs::read_to_string(test_file).unwrap();
let test: compute_cells_and_kzg_proofs::Test = serde_yaml::from_str(&yaml_data).unwrap();

let blob = match bytes_to_blob(&test.input.get_blob_bytes()) {
Ok(blob) => blob,
Err(_) => {
assert!(test.get_output().is_none());
continue;
}
};

match compute_cells_and_kzg_proofs(&blob, &settings) {
Err(_) => assert!(test.get_output().is_none()),
Ok((recv_cells, recv_proofs)) => {
let (exp_cells, exp_proofs) = test.get_output().unwrap();

let recv_cells = recv_cells
.into_iter()
.map(|it| it.to_bytes().to_vec())
.collect::<Vec<Vec<u8>>>();
let recv_proofs = recv_proofs
.into_iter()
.map(|it| it.to_bytes().to_vec())
.collect::<Vec<Vec<u8>>>();

assert_eq!(recv_cells, exp_cells, "Cells do not match");
assert_eq!(recv_proofs, exp_proofs, "Proofs do not match");
}
}
}
}
1 change: 1 addition & 0 deletions kzg-bench/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod c_bindings;
pub mod consts;
pub mod das;
pub mod eip_4844;
pub mod eip_7594;
pub mod fft_fr;
pub mod fft_g1;
pub mod finite;
Expand Down
Loading

0 comments on commit 9b65b59

Please sign in to comment.