Skip to content

Commit

Permalink
more mods
Browse files Browse the repository at this point in the history
  • Loading branch information
anupsv committed Sep 11, 2024
1 parent 70415af commit 96a2ed4
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ name = "bench_g1_ifft"
harness = false
path = "benches/bench_g1_ifft.rs"


[[bench]]
name = "bench_kzg_commit_with_cache"
harness = false
path = "benches/bench_kzg_commit_with_cache.rs"

[[bench]]
name = "bench_kzg_setup"
harness = false
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ The `data_setup_custom` (for testing) or `data_setup_mins` should be used to spe

### `commit()`

The `commit` function takes in a `polynomial`. It is computed over `lagrange` basis by performing the (i)FFT.
The `commit` function takes in a `polynomial`. It is computed over `lagrange` basis by performing the (i)FFT depending on the `polynomial` form specified.

### `initialize_cache()`

The `initialize_cache` function takes in a bool `force`. If `force` is set to `true` which delete's files which ends in `.cache` in the `cache_dir`. It computes the needed cache and writes it to files on disk into `cache_dir`.

### `commit_with_cache()`

The `commit_with_cache` function takes in a `polynomial` and `cache_dir`. It computes the commitment over the cached IFFT'd SRS points. The cache has to be already populated.



### `compute_kzg_proof_with_roots_of_unity()`

Expand Down
57 changes: 57 additions & 0 deletions benches/bench_kzg_commit_with_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rand::Rng;
use rust_kzg_bn254::{blob::Blob, consts::BYTES_PER_FIELD_ELEMENT, errors::KzgError, kzg::Kzg, polynomial::PolynomialFormat};
use std::time::Duration;

fn bench_kzg_commit_with_cache(c: &mut Criterion) {
let mut rng = rand::thread_rng();
let cache_dir = "/tmp";
let mut kzg = Kzg::setup(
"tests/test-files/mainnet-data/g1.32mb.point",
"",
"tests/test-files/mainnet-data/g2.point.powerOf2",
268435456,
524288,
cache_dir.to_owned(),
)
.unwrap();
kzg.initialize_cache(false).unwrap();

c.bench_function("bench_kzg_commit_with_cache_8mb", |b| {
let random_blob: Vec<u8> = (0..8000000)
.map(|_| rng.gen_range(32..=126) as u8)
.collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input
.to_polynomial(PolynomialFormat::InCoefficientForm)
.unwrap();

b.iter(|| Kzg::commit_with_cache(&input_poly, cache_dir).unwrap());
});

c.bench_function("bench_kzg_commit_with_cache_16mb", |b| {
let random_blob: Vec<u8> = (0..16000000)
.map(|_| rng.gen_range(32..=126) as u8)
.collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input
.to_polynomial(PolynomialFormat::InCoefficientForm)
.unwrap();

b.iter(|| Kzg::commit_with_cache(&input_poly, cache_dir).unwrap());
});
}

fn criterion_config() -> Criterion {
Criterion::default()
.warm_up_time(Duration::from_secs(5)) // Warm-up time
.measurement_time(Duration::from_secs(25)) // Measurement time
.sample_size(10) // Number of samples to take
}

criterion_group!(
name = benches;
config = criterion_config();
targets = bench_kzg_commit_with_cache
);
criterion_main!(benches);
31 changes: 24 additions & 7 deletions src/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,20 @@ impl Kzg {
self.g2.to_vec()
}

pub fn commit_with_cache(polynomial: &Polynomial, cache_dir: &str) -> Result<G1Affine, KzgError> {
let poly_len = polynomial.len();

let bases = Self::read_from_cache_if_exists(poly_len, &cache_dir);
if bases.is_empty() {
return Err(KzgError::CommitError("unable to commit using cache.".to_string()));
}

match G1Projective::msm(&bases, &polynomial.to_vec()) {
Ok(res) => Ok(res.into_affine()),
Err(err) => Err(KzgError::CommitError(err.to_string())),
}
}

/// commit the actual polynomial with the values setup
pub fn commit(&self, polynomial: &Polynomial) -> Result<G1Affine, KzgError> {
if polynomial.len() > self.g1.len() {
Expand Down Expand Up @@ -607,13 +621,16 @@ impl Kzg {
quotient
}

pub fn read_from_cache_if_exists(&self, length: usize) -> Vec<G1Affine> {
fn read_from_cache_if_exists(length: usize, cache_dir: &str) -> Vec<G1Affine> {
// check if the cache_dir has the file with the length in it
let cache_file = format!("{}/2_pow_{}.cache", self.cache_dir, length);

match Self::parallel_read_g1_points_native(cache_file, length as u32, true) {
Ok(points) => points,
Err(_) => Vec::new(),
let cache_file = format!("{}/2_pow_{}.cache", cache_dir, length);
if !cache_dir.is_empty() && check_directory(&cache_dir).is_ok() && fs::metadata(&cache_file).is_ok() {
match Self::parallel_read_g1_points_native(cache_file, length as u32, true) {
Ok(points) => return points,
Err(_) => return Vec::new(),
};
} else {
return Vec::new();
}
}

Expand Down Expand Up @@ -656,7 +673,7 @@ impl Kzg {
));
}

let cached_points = self.read_from_cache_if_exists(length);
let cached_points = Self::read_from_cache_if_exists(length, &self.cache_dir);
if cached_points.is_empty() {
let points_projective: Vec<G1Projective> = self.g1[..length]
.par_iter()
Expand Down
10 changes: 7 additions & 3 deletions tests/kzg_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod tests {
)
.unwrap();

let tmp_dir = Path::new("/tmp");
let tmp_dir = Path::new(cache_dir);

// Iterate over the files in the /tmp directory
for entry in fs::read_dir(tmp_dir).unwrap() {
Expand All @@ -200,13 +200,17 @@ mod tests {
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();

let commitment_raw_computed = kzg.commit(&input_poly);
let commitment_raw_computed = kzg.commit(&input_poly).unwrap();

kzg.initialize_cache(false).unwrap();

let commitment_cache = kzg.commit(&input_poly);
let commitment_cache = kzg.commit(&input_poly).unwrap();
let commitment_cache_pure_method = Kzg::commit_with_cache(&input_poly, cache_dir).unwrap();


assert_eq!(commitment_raw_computed, commitment_cache);
assert_eq!(commitment_raw_computed, commitment_cache_pure_method);
assert_eq!(commitment_cache, commitment_cache_pure_method);
}

#[test]
Expand Down

0 comments on commit 96a2ed4

Please sign in to comment.