Skip to content

feat: use criterion for benchmarks #25

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

Merged
merged 16 commits into from
Aug 11, 2025
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
11 changes: 11 additions & 0 deletions .github/workflows/rust_benchmarks_parallel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ jobs:
- name: Install nightly-2024-08-01 with rust-src for Powdr
run: rustup toolchain install nightly-2024-08-01 --component rust-src

- name: Format
run: cargo fmt --all -- --check

- name: Build full workspace (release)
env:
GITHUB_TOKEN: ${{ secrets.GH_PAT }} # for ACIR (required by protoc-prebuilt)
run: cargo build --release --workspace

- name: Clippy
run: cargo clippy --workspace --all-targets --all-features

detect-crates:
needs: prepare-cache
runs-on: macos-latest
Expand Down Expand Up @@ -189,6 +195,11 @@ jobs:
if: ${{ contains(matrix.crate, 'powdr') }}
run: rustup toolchain install nightly-2024-08-01 --component rust-src

- name: Install Nargo
uses: noir-lang/noirup@v0.1.4
with:
toolchain: stable

- name: Run benches in ${{ matrix.crate }}
run: |
cd ${{ matrix.crate }}
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/rust_benchmarks_serial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ jobs:
- name: Install nightly-2024-08-01 with rust-src for Powdr
run: rustup toolchain install nightly-2024-08-01 --component rust-src

- name: Format
run: cargo fmt --all -- --check

- name: Build full workspace (release)
env:
GITHUB_TOKEN: ${{ secrets.GH_PAT }} # for ACIR (required by protoc-prebuilt)
run: cargo build --release --workspace

- name: Clippy
run: cargo clippy --workspace --all-targets --all-features

# ────────────────── 2. Rust workspace benchmarks ──────────────────
bench-workspace:
needs: prepare-cache
Expand Down Expand Up @@ -91,6 +97,11 @@ jobs:
- name: Install nightly-2024-08-01 with rust-src for Powdr
run: rustup toolchain install nightly-2024-08-01 --component rust-src

- name: Install Nargo
uses: noir-lang/noirup@v0.1.4
with:
toolchain: stable

- name: Run workspace benchmarks
run: cargo bench

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion binius/src/bin/measure_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn benchmark_sha2(num_bytes: usize) -> Result<SubMetrics, Error> {
metrics.preprocessing_peak_memory = peak_memory;

let mut buffer: Vec<u8> = Vec::new();
let _ = constraint_system
constraint_system
.serialize(&mut buffer, binius_utils::SerializationMode::CanonicalTower)
.expect("Failed to serialize constraint system");
metrics.preprocessing_size = buffer.len();
Expand Down
2 changes: 1 addition & 1 deletion binius/src/bin/measure_no_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn benchmark_sha2(num_bytes: usize) -> Result<SubMetrics, Error> {
metrics.preprocessing_peak_memory = peak_memory;

let mut buffer: Vec<u8> = Vec::new();
let _ = constraint_system
constraint_system
.serialize(&mut buffer, binius_utils::SerializationMode::CanonicalTower)
.expect("Failed to serialize constraint system");
metrics.preprocessing_size = buffer.len();
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/bin/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {
fn benchmark_sha2(num_bytes: usize) -> SubMetrics {
let mut metrics = SubMetrics::new(num_bytes);

let ((data, pw), peak_memory) = measure_peak_memory(|| sha256_no_lookup_prepare());
let ((data, pw), peak_memory) = measure_peak_memory(sha256_no_lookup_prepare);
metrics.preprocessing_peak_memory = peak_memory;

let gate_serializer = U32GateSerializer;
Expand Down
32 changes: 16 additions & 16 deletions plonky2/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ mod tests {
#[test]
fn test_sha256() -> Result<()> {
let mut msg = vec![0; 128_usize];
for i in 0..127 {
msg[i] = i as u8;
for (i, byte) in msg.iter_mut().enumerate().take(127) {
*byte = i as u8;
}

let msg_bits = array_to_bits(&msg);
Expand All @@ -434,15 +434,15 @@ mod tests {
let targets = make_circuits(&mut builder, len as u64);
let mut pw = PartialWitness::new();

for i in 0..len {
pw.set_bool_target(targets.message[i], msg_bits[i])?;
for (target, &bit) in targets.message.iter().zip(msg_bits.iter()).take(len) {
pw.set_bool_target(*target, bit)?;
}

for i in 0..EXPECTED_RES.len() {
if EXPECTED_RES[i] == 1 {
builder.assert_one(targets.digest[i].target);
for (&expected, digest) in EXPECTED_RES.iter().zip(targets.digest.iter()) {
if expected == 1 {
builder.assert_one(digest.target);
} else {
builder.assert_zero(targets.digest[i].target);
builder.assert_zero(digest.target);
}
}

Expand All @@ -464,8 +464,8 @@ mod tests {
#[should_panic]
fn test_sha256_failure() {
let mut msg = vec![0; 128_usize];
for i in 0..127 {
msg[i] = i as u8;
for (i, byte) in msg.iter_mut().enumerate().take(127) {
*byte = i as u8;
}

let msg_bits = array_to_bits(&msg);
Expand All @@ -477,18 +477,18 @@ mod tests {
let targets = make_circuits(&mut builder, len as u64);
let mut pw = PartialWitness::new();

for i in 0..len {
pw.set_bool_target(targets.message[i], msg_bits[i]).unwrap();
for (target, &bit) in targets.message.iter().zip(msg_bits.iter()).take(len) {
pw.set_bool_target(*target, bit).unwrap();
}

let mut rng = rand::thread_rng();
let rnd = rng.gen_range(0..256);
for i in 0..EXPECTED_RES.len() {
let b = (i == rnd && EXPECTED_RES[i] != 1) || (i != rnd && EXPECTED_RES[i] == 1);
for (i, (&expected, digest)) in EXPECTED_RES.iter().zip(targets.digest.iter()).enumerate() {
let b = (i == rnd && expected != 1) || (i != rnd && expected == 1);
if b {
builder.assert_one(targets.digest[i].target);
builder.assert_one(digest.target);
} else {
builder.assert_zero(targets.digest[i].target);
builder.assert_zero(digest.target);
}
}

Expand Down
2 changes: 1 addition & 1 deletion plonky3-powdr/src/bin/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
fn benchmark_sha2(num_bytes: usize) -> SubMetrics {
let mut metrics = SubMetrics::new(num_bytes);

let (mut pipeline, peak_memory) = measure_peak_memory(|| prepare_pipeline());
let (mut pipeline, peak_memory) = measure_peak_memory(prepare_pipeline);
metrics.preprocessing_peak_memory = peak_memory;
println!(
"Preprocessing peak memory: {} GB",
Expand Down
3 changes: 2 additions & 1 deletion provekit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ edition = "2024"
[dependencies]
rand = { workspace = true }
utils = { workspace = true }
criterion = { workspace = true }
noir-r1cs = { git = "https://github.com/worldfnd/ProveKit", branch = "main" }

[[bench]]
name = "provekit"
name = "prove_verify"
harness = false
10 changes: 10 additions & 0 deletions provekit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ProveKit SHA256 Benchmark

This benchmark code is using the ProveKit of World Foundation(https://github.com/worldfnd/ProveKit).


## Benchmarking

```bash
cargo bench
```
71 changes: 71 additions & 0 deletions provekit/benches/prove_verify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use criterion::{BatchSize, Criterion, black_box, criterion_group, criterion_main};
use provekit::{ProvekitSha256Benchmark, WORKSPACE_ROOT};
use std::path::PathBuf;
use utils::bench::{SubMetrics, display_submetrics, measure_peak_memory, write_json_submetrics};

const INPUT_EXPONENTS: [u32; 1] = [11];

fn sha256_benchmarks(c: &mut Criterion) {
let (bench_harness, preprocessing_peak_memory) =
measure_peak_memory(|| ProvekitSha256Benchmark::new(&INPUT_EXPONENTS));

let mut all_metrics = Vec::new();

for &exp in INPUT_EXPONENTS.iter() {
let mut metrics = SubMetrics::new(1 << exp);
metrics.preprocessing_peak_memory = preprocessing_peak_memory;

let package_name = format!("sha256_bench_2e{exp}");
let circuit_path = PathBuf::from(WORKSPACE_ROOT)
.join("target")
.join(format!("{package_name}.json"));
let toml_path = PathBuf::from(WORKSPACE_ROOT)
.join("circuits/hash/sha256-provekit")
.join(format!("sha256-bench-2e{exp}"))
.join("Prover.toml");

metrics.preprocessing_size = std::fs::metadata(circuit_path)
.map(|m| m.len())
.unwrap_or(0) as usize
+ std::fs::metadata(toml_path).map(|m| m.len()).unwrap_or(0) as usize;

let (proof, proving_peak_memory) = measure_peak_memory(|| bench_harness.run_prove(exp));
metrics.proving_peak_memory = proving_peak_memory;
metrics.proof_size = proof.whir_r1cs_proof.transcript.len();

all_metrics.push(metrics);
}

println!("{}", display_submetrics(&all_metrics));

let json_path = "sha2_provekit_submetrics.json";
write_json_submetrics(json_path, &all_metrics[0]);

let mut group = c.benchmark_group("SHA256 Prove & Verify");
group.sample_size(10);

for &exp in INPUT_EXPONENTS.iter() {
let input_size = 1 << exp;
let prove_id = format!("Prove ({} bytes)", input_size);
group.bench_function(prove_id, |bench| {
bench.iter(|| {
let proof = bench_harness.run_prove(exp);
black_box(proof);
});
});

let verify_id = format!("Verify ({} bytes)", input_size);
group.bench_function(verify_id, |bench| {
bench.iter_batched(
|| bench_harness.prepare_verify(exp),
|(proof, proof_scheme)| bench_harness.run_verify(&proof, proof_scheme).unwrap(),
BatchSize::SmallInput,
);
});
}

group.finish();
}

criterion_group!(benches, sha256_benchmarks);
criterion_main!(benches);
6 changes: 0 additions & 6 deletions provekit/benches/provekit.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ type = "lib"
authors = ["Ryan Cao"]

[dependencies]
sha256 = { tag = "v0.1.1", git = "https://github.com/noir-lang/sha256" }
sha256 = { tag = "v0.1.4", git = "https://github.com/noir-lang/sha256" }
Loading
Loading