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

micro benchmarks #32

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- Introducing an example for proving knowledge of exponent
- Add api to get SRS size.
- Adding micro benchmarks for MSM, FFT and Poly Evaluation.

### Improvements

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ The additional flags allow using assembly implementation of `square_in_place` an
For benchmark, run:

```
RAYON_NUM_THREADS=N cargo bench
RAYON_NUM_THREADS=N cargo bench --features bench
```

where N is the number of threads you want to use (N = 1 for single-thread).
Expand Down
4 changes: 3 additions & 1 deletion plonk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ path = "benches/bench.rs"
harness = false

[features]
std = []
std = [ ]
# exposing apis for testing purpose
test_apis = []
# enabling mircobench
bench = []
151 changes: 147 additions & 4 deletions plonk/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
// along with the Jellyfish library. If not, see <https://mit-license.org/>.

// For benchmark, run:
// RAYON_NUM_THREADS=N cargo bench
// RAYON_NUM_THREADS=N cargo bench --features bench
// where N is the number of threads you want to use (N = 1 for single-thread).

use ark_bls12_377::{Bls12_377, Fr as Fr377};
use ark_bls12_381::{Bls12_381, Fr as Fr381};
use ark_bn254::{Bn254, Fr as Fr254};
use ark_bw6_761::{Fr as Fr761, BW6_761};
use ark_ff::PrimeField;
use ark_std::{fs::File, io::Write};
use jf_plonk::{
bencher::{init_timers, total_fft_time, total_msm_time, total_poly_eval_time},
circuit::{Circuit, PlonkCircuit},
errors::PlonkError,
proof_system::{PlonkKzgSnark, Snark},
Expand Down Expand Up @@ -54,6 +56,7 @@ macro_rules! plonk_prove_bench {

let (pk, _) = PlonkKzgSnark::<$bench_curve>::preprocess(&srs, &cs).unwrap();

init_timers();
let start = ark_std::time::Instant::now();

for _ in 0..NUM_REPETITIONS {
Expand All @@ -62,13 +65,98 @@ macro_rules! plonk_prove_bench {
)
.unwrap();
}
println!("=====================================");
println!(
"proving time for {}, {} with dim {}: {} ns/gate",
stringify!($bench_curve),
stringify!($bench_plonk_type),
$num_gates,
start.elapsed().as_nanos() / NUM_REPETITIONS as u128 / $num_gates as u128
);
println!(
"total batch verify time: {:.2} ms",
start.elapsed().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64
);
println!(
"time spend on FFT: {:.2} ms, or {:.2}%",
total_fft_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_fft_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);
println!(
"time spend on MSM: {:.2} ms, or {:.2}%",
total_msm_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_msm_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);
println!(
"time spend on poly evaluation: {:.2} ms, or {:.2}%",
total_poly_eval_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_poly_eval_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);
println!("=====================================");
};
}

macro_rules! plonk_prove_mt_bench {
($bench_curve:ty, $bench_field:ty, $bench_plonk_type:expr, $num_gates:expr, $file:expr) => {
let rng = &mut ark_std::test_rng();
let cs = gen_circuit_for_bench::<$bench_field>($num_gates, $bench_plonk_type).unwrap();

let max_degree = $num_gates + 2;
let srs = PlonkKzgSnark::<$bench_curve>::universal_setup(max_degree, rng).unwrap();

let (pk, _) = PlonkKzgSnark::<$bench_curve>::preprocess(&srs, &cs).unwrap();

init_timers();
let start = ark_std::time::Instant::now();

for _ in 0..NUM_REPETITIONS {
let _ = PlonkKzgSnark::<$bench_curve>::prove::<_, _, StandardTranscript>(
rng, &cs, &pk, None,
)
.unwrap();
}
println!("=====================================");
println!(
"proving time for {}, {}: {} ns/gate",
"proving time for {}, {} with dim {}: {} ns/gate",
stringify!($bench_curve),
stringify!($bench_plonk_type),
$num_gates,
start.elapsed().as_nanos() / NUM_REPETITIONS as u128 / $num_gates as u128
);
println!(
"total batch verify time: {:.2} ms",
start.elapsed().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64
);
println!(
"time spend on FFT: {:.2} ms, or {:.2}%",
total_fft_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_fft_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);
println!(
"time spend on MSM: {:.2} ms, or {:.2}%",
total_msm_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_msm_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);
println!(
"time spend on poly evaluation: {:.2} ms, or {:.2}%",
total_poly_eval_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_poly_eval_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);
println!("=====================================");
$file
.write_all(
format!(
"{} {:.2} {:.2} {:.2} {:.2} {:.2}\n",
$num_gates,
start.elapsed().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
total_fft_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_fft_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64,
total_msm_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_msm_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64,
)
.as_ref(),
)
.expect("Unable to write data");
};
}

Expand Down Expand Up @@ -97,20 +185,38 @@ macro_rules! plonk_verify_bench {
PlonkKzgSnark::<$bench_curve>::prove::<_, _, StandardTranscript>(rng, &cs, &pk, None)
.unwrap();

init_timers();
let start = ark_std::time::Instant::now();

for _ in 0..NUM_REPETITIONS {
let _ =
PlonkKzgSnark::<$bench_curve>::verify::<StandardTranscript>(&vk, &[], &proof, None)
.unwrap();
}

println!("=====================================");
println!(
"verifying time for {}, {}: {} ns",
"verifying time for {}, {} with dim {}: {} ns",
stringify!($bench_curve),
stringify!($bench_plonk_type),
$num_gates,
start.elapsed().as_nanos() / NUM_REPETITIONS as u128
);
println!(
"total batch verify time: {:.2} ms",
start.elapsed().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64
);
println!(
"time spend on FFT: {:.2} ms, or {:.2}%",
total_fft_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_fft_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);
println!(
"time spend on MSM: {:.2} ms, or {:.2}%",
total_msm_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_msm_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);

println!("=====================================");
};
}

Expand Down Expand Up @@ -144,6 +250,7 @@ macro_rules! plonk_batch_verify_bench {
let public_inputs_ref = vec![&pub_input[..]; $num_proofs];
let proofs_ref = vec![&proof; $num_proofs];

init_timers();
let start = ark_std::time::Instant::now();

for _ in 0..NUM_REPETITIONS {
Expand All @@ -163,6 +270,21 @@ macro_rules! plonk_batch_verify_bench {
stringify!($num_proofs),
start.elapsed().as_nanos() / NUM_REPETITIONS as u128 / $num_proofs as u128
);

println!(
"total batch verify time: {:.2} ms",
start.elapsed().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64
);
println!(
"time spend on FFT: {:.2} ms, or {:.2}%",
total_fft_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_fft_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);
println!(
"time spend on MSM: {:.2} ms, or {:.2}%",
total_msm_time().as_nanos() as f64 / NUM_REPETITIONS as f64 / 1_000_000f64,
100f64 * total_msm_time().as_nanos() as f64 / start.elapsed().as_nanos() as f64
);
};
}

Expand All @@ -177,7 +299,28 @@ fn bench_batch_verify() {
plonk_batch_verify_bench!(BW6_761, Fr761, PlonkType::UltraPlonk, 1000);
}

fn bench_intense() {
let mut f = File::create(format!(
"../target/{}-threads.txt",
rayon::current_num_threads()
))
.expect("Unable to create file");
Comment on lines +303 to +307
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut f = File::create(format!(
"../target/{}-threads.txt",
rayon::current_num_threads()
))
.expect("Unable to create file");
let mut path = PathBuf::new();
path.push(env!("CARGO_MANIFEST_DIR"));
path.push(format!("target/plonkbench/{}-threads", rayon::current_num_threads()));
path.set_extension("txt");
let mut f = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(path.clone())
.unwrap();

You can try using something like this to overwrite the file instead of appending to it in case the file already exists (which File::create does)


for i in 10..=30 {
let dim = 1 << i;
println!("bench with log(dim) = {}", i);
plonk_prove_mt_bench!(Bls12_377, Fr377, PlonkType::TurboPlonk, dim, f);
}

for i in 10..=30 {
let dim = 1 << i;
println!("bench with log(dim) = {}", i);
plonk_verify_bench!(Bls12_377, Fr377, PlonkType::TurboPlonk, dim);
}
}

fn main() {
bench_intense();
bench_prove();
bench_verify();
bench_batch_verify();
Expand Down
Loading