Skip to content

Commit

Permalink
adding cache dir, auto switch to manual calc, test & bench
Browse files Browse the repository at this point in the history
  • Loading branch information
anupsv committed Sep 11, 2024
1 parent 962e8d5 commit a0b965b
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 61 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ byteorder = "1.4"
ark-poly = {version = "0.4.2", features = ["parallel"]}
crossbeam-channel = "0.5"
num_cpus = "1.13.0"
sys-info = "0.9"

[dev-dependencies]
criterion = "0.5"
Expand Down Expand Up @@ -74,6 +75,11 @@ name = "bench_kzg_commit_large_blobs"
harness = false
path = "benches/bench_kzg_commit_large_blobs.rs"

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

[[bench]]
name = "bench_kzg_proof"
harness = false
Expand Down
1 change: 1 addition & 0 deletions benches/bench_g1_ifft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn bench_g1_ifft(c: &mut Criterion) {
"tests/test-files/mainnet-data/g2.point.powerOf2",
3000,
3000,
"".to_owned(),
)
.unwrap();
b.iter(|| {
Expand Down
1 change: 1 addition & 0 deletions benches/bench_kzg_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn bench_kzg_commit(c: &mut Criterion) {
"tests/test-files/mainnet-data/g2.point.powerOf2",
268435456,
131072,
"".to_owned(),
)
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions benches/bench_kzg_commit_large_blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn bench_kzg_commit(c: &mut Criterion) {
"tests/test-files/mainnet-data/g2.point.powerOf2",
268435456,
524288,
"".to_owned(),
)
.unwrap();

Expand Down
33 changes: 33 additions & 0 deletions benches/bench_kzg_ifft_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rust_kzg_bn254::kzg::Kzg;
use std::time::Duration;

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

c.bench_function("bench_kzg_ifft_cache", |b| {
b.iter(|| kzg.initialize_cache(true).unwrap());
});
}

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

criterion_group!(
name = benches;
config = criterion_config();
targets = bench_kzg_ifft_cache
);
criterion_main!(benches);
1 change: 1 addition & 0 deletions benches/bench_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn bench_kzg_proof(c: &mut Criterion) {
"tests/test-files/mainnet-data/g2.point.powerOf2",
268435456,
131072,
"".to_owned(),
)
.unwrap();

Expand Down
2 changes: 2 additions & 0 deletions benches/bench_kzg_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn bench_kzg_setup(c: &mut Criterion) {
"tests/test-files/g2.point.powerOf2",
3000,
3000,
"".to_owned(),
)
.unwrap()
});
Expand All @@ -22,6 +23,7 @@ fn bench_kzg_setup(c: &mut Criterion) {
"tests/test-files/mainnet-data/g2.point.powerOf2",
268435456,
131072,
"".to_owned(),
)
.unwrap()
});
Expand Down
1 change: 1 addition & 0 deletions benches/bench_kzg_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn bench_kzg_verify(c: &mut Criterion) {
"tests/test-files/mainnet-data/g2.point.powerOf2",
268435456,
131072,
"".to_owned(),
)
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub const BYTES_PER_FIELD_ELEMENT: usize = 32;
pub const SIZE_OF_G1_AFFINE_COMPRESSED: usize = 32; // in bytes
pub const SIZE_OF_G2_AFFINE_COMPRESSED: usize = 64; // in bytes
pub const REQUIRED_FREE_SPACE: u64 = 100 * 1024 * 1024; // 100 MB in bytes
66 changes: 60 additions & 6 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
use ark_bn254::{Fq, Fq2, Fr, G1Affine, G1Projective, G2Affine, G2Projective};
use ark_ec::AffineRepr;
use ark_ff::{sbb, BigInt, BigInteger, Field, LegendreSymbol, PrimeField};
use ark_serialize::Write;
use ark_std::{str::FromStr, vec::Vec, One, Zero};
use crossbeam_channel::Receiver;
use std::cmp;
use std::{
cmp,
fs::{self, OpenOptions},
path::Path,
};
use sys_info::disk_info;

use crate::{
arith,
consts::{BYTES_PER_FIELD_ELEMENT, SIZE_OF_G1_AFFINE_COMPRESSED, SIZE_OF_G2_AFFINE_COMPRESSED},
consts::{
BYTES_PER_FIELD_ELEMENT, REQUIRED_FREE_SPACE, SIZE_OF_G1_AFFINE_COMPRESSED,
SIZE_OF_G2_AFFINE_COMPRESSED,
},
traits::ReadPointFromBytes,
};

Expand Down Expand Up @@ -297,16 +306,21 @@ pub fn read_g1_point_from_bytes_be(g1_bytes_be: &[u8]) -> Result<G1Affine, &str>
Ok(point)
}

pub fn process_chunks<T>(receiver: Receiver<(Vec<u8>, usize)>) -> Vec<(T, usize)>
pub fn process_chunks<T>(receiver: Receiver<(Vec<u8>, usize, bool)>) -> Vec<(T, usize)>
where
T: ReadPointFromBytes,
{
#[allow(clippy::unnecessary_filter_map)]
receiver
.iter()
.map(|(chunk, position)| {
let point =
T::read_point_from_bytes_be(&chunk).expect("Failed to read point from bytes");
.map(|(chunk, position, is_native)| {
let point: T = if is_native {
T::read_point_from_bytes_native_compressed(&chunk)
.expect("Failed to read point from bytes")
} else {
T::read_point_from_bytes_be(&chunk).expect("Failed to read point from bytes")
};

(point, position)
})
.collect()
Expand Down Expand Up @@ -362,3 +376,43 @@ pub fn is_on_curve_g2(g2: &G2Projective) -> bool {
right += &tmp;
left == right
}

pub fn check_directory<P: AsRef<Path> + std::fmt::Display>(path: P) -> Result<bool, String> {
let test_file_path = path.as_ref().join("cache_dir_write_test.tmp");

// Try to create and write to a temporary file
let result = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&test_file_path)
.and_then(|mut file| file.write_all(b"test"));

// Clean up the test file
let _ = fs::remove_file(test_file_path);

// Return true if writing was successful, otherwise false
if result.is_err() {
return Err(format!(
"directory {} unable to be modified by kzg program",
path
));
}

// Get disk information
let disk = match disk_info() {
Ok(info) => info,
Err(_) => return Err(format!("unable to get disk information for path {}", path)),
};

// Calculate available space in the directory's disk
let free_space = disk.free * 1024; // Convert from KB to bytes

if free_space < REQUIRED_FREE_SPACE {
return Err(format!(
"the cache path {} does not have {} space on disk",
path, REQUIRED_FREE_SPACE
));
}
Ok(true)
}
Loading

0 comments on commit a0b965b

Please sign in to comment.