Skip to content

Commit

Permalink
add benchmarks & initial profiling (#177)
Browse files Browse the repository at this point in the history
* add Nova benchmarks logic

* abstract benchmarks, add HyperNova & ProtoGalaxy's IVC benchmarks

* add profiler & flamegraph at benches

* add bench compile to github CI
  • Loading branch information
arnaucube authored Nov 28, 2024
1 parent 40dccb1 commit 7ec7c02
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 9 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ jobs:
- name: Run examples
run: cargo run --release --example 2>&1 | grep -E '^ ' | xargs -n1 cargo run --release --example

# run the benchmarks with the flag `--no-run` to ensure that they compile,
# but without executing them.
bench:
if: github.event.pull_request.draft == false
name: Bench compile
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
- uses: Swatinem/rust-cache@v2
- uses: actions-rs/cargo@v1
with:
command: bench
args: --no-run

fmt:
if: github.event.pull_request.draft == false
name: Rustfmt
Expand Down
10 changes: 10 additions & 0 deletions benches/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# benchmarks
*Note: we're starting to benchmark & profile Sonobe, current results are pre-optimizations.*

- Benchmark
- Run: `cargo bench`
- To run a specific benchmark, for example Nova's benchmark, run: `cargo bench --bench=nova`
- Profiling
- eg. `cargo bench --bench=nova -- --profile-time 3`


57 changes: 57 additions & 0 deletions benches/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use ark_ec::CurveGroup;
use ark_ff::PrimeField;
use criterion::*;

use folding_schemes::{
frontend::{utils::CustomFCircuit, FCircuit},
Error, FoldingScheme,
};

pub(crate) fn bench_ivc_opt<
C1: CurveGroup,
C2: CurveGroup,
FS: FoldingScheme<C1, C2, CustomFCircuit<C1::ScalarField>>,
>(
c: &mut Criterion,
name: String,
n: usize,
prep_param: FS::PreprocessorParam,
) -> Result<(), Error>
where
C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
C2::BaseField: PrimeField,
{
let fcircuit_size = 1 << n; // 2^n

let f_circuit = CustomFCircuit::<C1::ScalarField>::new(fcircuit_size)?;

let mut rng = rand::rngs::OsRng;

// prepare the FS prover & verifier params
let fs_params = FS::preprocess(&mut rng, &prep_param)?;

let z_0 = vec![C1::ScalarField::from(3_u32)];
let mut fs = FS::init(&fs_params, f_circuit, z_0)?;

// warmup steps
for _ in 0..5 {
fs.prove_step(rng, vec![], None)?;
}

let mut group = c.benchmark_group(format!(
"{} - FCircuit: {} (2^{}) constraints",
name, fcircuit_size, n
));
group.significance_level(0.1).sample_size(10);
group.bench_function("prove_step", |b| {
b.iter(|| black_box(fs.clone()).prove_step(rng, vec![], None).unwrap())
});

// verify the IVCProof
let ivc_proof = fs.ivc_proof();
group.bench_function("verify", |b| {
b.iter(|| FS::verify(black_box(fs_params.1.clone()), black_box(ivc_proof.clone())).unwrap())
});
group.finish();
Ok(())
}
83 changes: 83 additions & 0 deletions benches/hypernova.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use criterion::*;

use ark_bn254::{constraints::GVar as bn_GVar, Fr as bn_Fr, G1Projective as bn_G};
use ark_grumpkin::{constraints::GVar as grumpkin_GVar, Projective as grumpkin_G};
use ark_pallas::{constraints::GVar as pallas_GVar, Fr as pallas_Fr, Projective as pallas_G};
use ark_vesta::{constraints::GVar as vesta_GVar, Projective as vesta_G};

use folding_schemes::{
commitment::pedersen::Pedersen,
folding::{hypernova::HyperNova, nova::PreprocessorParam},
frontend::{utils::CustomFCircuit, FCircuit},
transcript::poseidon::poseidon_canonical_config,
};

mod common;
use common::bench_ivc_opt;

fn bench_hypernova_ivc(c: &mut Criterion) {
let poseidon_config = poseidon_canonical_config::<pallas_Fr>();

// iterate over the powers of n
for n in [0_usize, 14, 16, 18, 19, 20, 21, 22].iter() {
let fcircuit_size = 1 << n; // 2^n
let fcircuit = CustomFCircuit::<pallas_Fr>::new(fcircuit_size).unwrap();
let prep_param = PreprocessorParam::new(poseidon_config.clone(), fcircuit);

bench_ivc_opt::<
pallas_G,
vesta_G,
HyperNova<
pallas_G,
pallas_GVar,
vesta_G,
vesta_GVar,
CustomFCircuit<pallas_Fr>,
Pedersen<pallas_G>,
Pedersen<vesta_G>,
1,
1,
false,
>,
>(
c,
"HyperNova - Pallas-Vesta curves".to_string(),
*n,
prep_param,
)
.unwrap();
}

let poseidon_config = poseidon_canonical_config::<bn_Fr>();
for n in [0_usize, 14, 16, 18, 19, 20, 21, 22].iter() {
let fcircuit_size = 1 << n; // 2^n
let fcircuit = CustomFCircuit::<bn_Fr>::new(fcircuit_size).unwrap();
let prep_param = PreprocessorParam::new(poseidon_config.clone(), fcircuit);

bench_ivc_opt::<
bn_G,
grumpkin_G,
HyperNova<
bn_G,
bn_GVar,
grumpkin_G,
grumpkin_GVar,
CustomFCircuit<bn_Fr>,
Pedersen<bn_G>,
Pedersen<grumpkin_G>,
1,
1,
false,
>,
>(
c,
"HyperNova - BN254-Grumpkin curves".to_string(),
*n,
prep_param,
)
.unwrap();
}
}

criterion_group!(benches, bench_hypernova_ivc);
criterion_main!(benches);
79 changes: 79 additions & 0 deletions benches/nova.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use criterion::*;
use pprof::criterion::{Output, PProfProfiler};

use ark_bn254::{constraints::GVar as bn_GVar, Fr as bn_Fr, G1Projective as bn_G};
use ark_grumpkin::{constraints::GVar as grumpkin_GVar, Projective as grumpkin_G};
use ark_pallas::{constraints::GVar as pallas_GVar, Fr as pallas_Fr, Projective as pallas_G};
use ark_vesta::{constraints::GVar as vesta_GVar, Projective as vesta_G};

use folding_schemes::{
commitment::pedersen::Pedersen,
folding::nova::{Nova, PreprocessorParam},
frontend::{utils::CustomFCircuit, FCircuit},
transcript::poseidon::poseidon_canonical_config,
};

mod common;
use common::bench_ivc_opt;

fn bench_nova_ivc(c: &mut Criterion) {
let poseidon_config = poseidon_canonical_config::<pallas_Fr>();

// iterate over the powers of n
for n in [0_usize, 14, 16, 18, 19, 20, 21, 22].iter() {
let fcircuit_size = 1 << n; // 2^n
let fcircuit = CustomFCircuit::<pallas_Fr>::new(fcircuit_size).unwrap();
let prep_param = PreprocessorParam::new(poseidon_config.clone(), fcircuit);

bench_ivc_opt::<
pallas_G,
vesta_G,
Nova<
pallas_G,
pallas_GVar,
vesta_G,
vesta_GVar,
CustomFCircuit<pallas_Fr>,
Pedersen<pallas_G>,
Pedersen<vesta_G>,
false,
>,
>(c, "Nova - Pallas-Vesta curves".to_string(), *n, prep_param)
.unwrap();
}

let poseidon_config = poseidon_canonical_config::<bn_Fr>();
for n in [0_usize, 14, 16, 18, 19, 20, 21, 22].iter() {
let fcircuit_size = 1 << n; // 2^n
let fcircuit = CustomFCircuit::<bn_Fr>::new(fcircuit_size).unwrap();
let prep_param = PreprocessorParam::new(poseidon_config.clone(), fcircuit);

bench_ivc_opt::<
bn_G,
grumpkin_G,
Nova<
bn_G,
bn_GVar,
grumpkin_G,
grumpkin_GVar,
CustomFCircuit<bn_Fr>,
Pedersen<bn_G>,
Pedersen<grumpkin_G>,
false,
>,
>(
c,
"Nova - BN254-Grumpkin curves".to_string(),
*n,
prep_param,
)
.unwrap();
}
}

criterion_group! {
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = bench_nova_ivc
}
criterion_main!(benches);
77 changes: 77 additions & 0 deletions benches/protogalaxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use criterion::*;

use ark_bn254::{constraints::GVar as bn_GVar, Fr as bn_Fr, G1Projective as bn_G};
use ark_grumpkin::{constraints::GVar as grumpkin_GVar, Projective as grumpkin_G};
use ark_pallas::{constraints::GVar as pallas_GVar, Fr as pallas_Fr, Projective as pallas_G};
use ark_vesta::{constraints::GVar as vesta_GVar, Projective as vesta_G};

use folding_schemes::{
commitment::pedersen::Pedersen,
folding::protogalaxy::ProtoGalaxy,
frontend::{utils::CustomFCircuit, FCircuit},
transcript::poseidon::poseidon_canonical_config,
};

mod common;
use common::bench_ivc_opt;

fn bench_protogalaxy_ivc(c: &mut Criterion) {
let poseidon_config = poseidon_canonical_config::<pallas_Fr>();

// iterate over the powers of n
for n in [0_usize, 14, 16, 18, 19, 20, 21, 22].iter() {
let fcircuit_size = 1 << n; // 2^n
let fcircuit = CustomFCircuit::<pallas_Fr>::new(fcircuit_size).unwrap();
let prep_param = (poseidon_config.clone(), fcircuit);

bench_ivc_opt::<
pallas_G,
vesta_G,
ProtoGalaxy<
pallas_G,
pallas_GVar,
vesta_G,
vesta_GVar,
CustomFCircuit<pallas_Fr>,
Pedersen<pallas_G>,
Pedersen<vesta_G>,
>,
>(
c,
"ProtoGalaxy - Pallas-Vesta curves".to_string(),
*n,
prep_param,
)
.unwrap();
}

let poseidon_config = poseidon_canonical_config::<bn_Fr>();
for n in [0_usize, 14, 16, 18, 19, 20, 21, 22].iter() {
let fcircuit_size = 1 << n; // 2^n
let fcircuit = CustomFCircuit::<bn_Fr>::new(fcircuit_size).unwrap();
let prep_param = (poseidon_config.clone(), fcircuit);

bench_ivc_opt::<
bn_G,
grumpkin_G,
ProtoGalaxy<
bn_G,
bn_GVar,
grumpkin_G,
grumpkin_GVar,
CustomFCircuit<bn_Fr>,
Pedersen<bn_G>,
Pedersen<grumpkin_G>,
>,
>(
c,
"ProtoGalaxy - BN254-Grumpkin curves".to_string(),
*n,
prep_param,
)
.unwrap();
}
}

criterion_group!(benches, bench_protogalaxy_ivc);
criterion_main!(benches);
19 changes: 19 additions & 0 deletions folding-schemes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ num-bigint = {version = "0.4", features = ["rand"]}
tracing = { version = "0.1", default-features = false, features = [ "attributes" ] }
tracing-subscriber = { version = "0.2" }

# for benchmarks
criterion = "0.5"
pprof = { version = "0.13", features = ["criterion", "flamegraph"] }

# This allows the crate to be built when targeting WASM.
# See more at: https://docs.rs/getrandom/#webassembly-support
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
Expand All @@ -53,6 +57,21 @@ parallel = []
light-test = []


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

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

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

[[example]]
name = "sha256"
path = "../examples/sha256.rs"
Expand Down
Loading

0 comments on commit 7ec7c02

Please sign in to comment.