Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing parallel features #11

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ exclude = [

[dependencies]
ark-bn254 = "0.4.0"
ark-ec = "0.4.2"
ark-ff = "0.4.1"
ark-ec = {version = "0.4.2", features = ["parallel"]}
ark-ff = {version = "0.4.2", features = ["parallel"]}
ark-serialize = "0.4.2"
ark-std = "0.4.0"
ark-std = {version = "0.4.0", features = ["parallel"]}
directories = "5.0.1"
hex-literal = "0.4.1"
rand = "0.8.5"
Expand All @@ -28,7 +28,7 @@ num-bigint = "0.4"
rayon = "^1.5"
num-traits = "0.2"
byteorder = "1.4"
ark-poly = "0.4.2"
ark-poly = {version = "0.4.2", features = ["parallel"]}
crossbeam-channel = "0.5"
num_cpus = "1.13.0"

Expand Down Expand Up @@ -69,6 +69,11 @@ name = "bench_kzg_commit"
harness = false
path = "benches/bench_kzg_commit.rs"

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

[[bench]]
name = "bench_kzg_proof"
harness = false
Expand All @@ -81,7 +86,7 @@ path = "benches/bench_kzg_verify.rs"

[profile.bench]
opt-level = 3
debug = true
debug = false
strip = "none"
debug-assertions = false
overflow-checks = false
Expand Down
56 changes: 56 additions & 0 deletions benches/bench_kzg_commit_large_blobs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rand::Rng;
use rust_kzg_bn254::{blob::Blob, kzg::Kzg, polynomial::PolynomialFormat};
use std::time::Duration;

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

c.bench_function("bench_kzg_commit_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();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
b.iter(|| kzg.commit(&input_poly).unwrap());
});

c.bench_function("bench_kzg_commit_16mb", |b| {
let random_blob: Vec<u8> = (0..16_252_000)
.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();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
b.iter(|| kzg.commit(&input_poly).unwrap());
});
}

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

criterion_group!(
name = benches;
config = criterion_config();
targets = bench_kzg_commit
);
criterion_main!(benches);
5 changes: 3 additions & 2 deletions src/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ark_serialize::Read;
use ark_std::{ops::Div, str::FromStr, One, Zero};
use crossbeam_channel::{bounded, Sender};
use num_traits::ToPrimitive;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::{fs::File, io, io::BufReader};

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -559,15 +560,15 @@ impl Kzg {
}

let points_projective: Vec<G1Projective> = self.g1[..length]
.iter()
.par_iter()
.map(|&p| G1Projective::from(p))
.collect();

match GeneralEvaluationDomain::<Fr>::new(length) {
Some(domain) => {
let ifft_result = domain.ifft(&points_projective);
let ifft_result_affine: Vec<_> =
ifft_result.iter().map(|p| p.into_affine()).collect();
ifft_result.par_iter().map(|p| p.into_affine()).collect();
Ok(ifft_result_affine)
},
None => Err(KzgError::FftError(
Expand Down
Binary file added tests/test-files/mainnet-data/g1.32mb.point
Binary file not shown.