Skip to content

Commit

Permalink
Toggle range check in benchmarks by a feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
alxkzmn committed Feb 23, 2024
1 parent fd40e24 commit 910b281
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 36 deletions.
2 changes: 2 additions & 0 deletions kzg_prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ edition = "2021"
[features]
dev-graph = ["halo2_proofs/dev-graph", "plotters"]
profiling = []
no_range_check = []


[dependencies]
halo2_proofs = { git = "https://github.com/summa-dev/halo2"}
Expand Down
25 changes: 25 additions & 0 deletions kzg_prover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,28 @@ cargo doc --no-deps --open

For testing purposes, it's not necessary to download the `ptau` file. The `generate_setup_artifacts` function can manage this by generating a new setup from a randomly generated value. This automated generation process is intended for testing and development convenience, and it should not be used in production.
For real-world situations, you must provide the path of a specific `ptau` file to the `generate_setup_artifacts`. The circuit will use the randomness from the given file. You can find an example that initializes a `Snapshot` instance [here](https://github.com/summa-dev/summa-solvency/blob/11d4fce5d18f6175804aa792fc9fc5ac27bf5c00/backend/src/apis/snapshot.rs#L115-L116) in the backend.

## Benchmarks

The following benchmarks are available in the `kzg` module:

- `range_check_proof`: the zk-SNARK proof generation time of the polynomial interpolation of user balances with range check;
- `opening_grand_sum`: the time to generate the KZG opening proof of the grand sum of user balances;
- `opening_user`: the time to generate the KZG opening proof of a single user inclusion;
- `calculate_h`: the time to calculate the h(X) for the amortized KZG approach;
- `amortized_opening_all`: the time to generate open proofs for all 2^K user inclusions using the amortized approach;
- `amortized_opening_user`: the time to generate the KZG opening proof of a single user inclusion using the precomputed h(x) from the amortized approach;
- `verifying_grand_sum`: the time to verify the KZG opening proof of the grand sum of user balances;
- `verifying_user`: the time to verify the KZG opening proof of a single user inclusion.

To run the benchmarks with the default full configuration of the circuit (range check enabled), use the following command:

```shell
cargo bench
```

To run the quick benchmarks with the range check disabled (K=9..12), use the following command:

```shell
cargo bench --features "no_range_check"
```
82 changes: 46 additions & 36 deletions kzg_prover/benches/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ use halo2_proofs::{arithmetic::Field, halo2curves::bn256::Fr as Fp};
use num_bigint::BigUint;
use rand::{rngs::OsRng, Rng};

#[cfg(feature = "no_range_check")]
use summa_solvency::circuits::univariate_grand_sum::NoRangeCheckConfig;
#[cfg(not(feature = "no_range_check"))]
use summa_solvency::circuits::univariate_grand_sum::UnivariateGrandSumConfig;
use summa_solvency::{
circuits::{
univariate_grand_sum::{CircuitConfig, UnivariateGrandSum, UnivariateGrandSumConfig},
univariate_grand_sum::{CircuitConfig, UnivariateGrandSum},
utils::{
compute_h_parallel, full_prover, generate_setup_artifacts,
open_all_user_points_amortized, open_grand_sums, open_grand_sums_gwc,
Expand Down Expand Up @@ -37,7 +41,7 @@ fn bench_kzg<
let circuit = UnivariateGrandSum::<N_USERS, N_CURRENCIES, CONFIG>::init_empty();
let (params, pk, vk) = generate_setup_artifacts(K, None, &circuit).unwrap();

let range_check_bench_name = format!("<{}> range check", name);
let range_check_proof_bench_name = format!("<{}> range check", name);
let opening_grand_sum_bench_name = format!("<{}> opening grand sum", name);
let opening_user_bench_name = format!("<{}> opening single user inclusion", name);
let calculate_h_bench_name =
Expand Down Expand Up @@ -68,7 +72,7 @@ fn bench_kzg<

let circuit = UnivariateGrandSum::<N_USERS, N_CURRENCIES, CONFIG>::init(entries.to_vec());

c.bench_function(&range_check_bench_name, |b| {
c.bench_function(&range_check_proof_bench_name, |b| {
b.iter_batched(
|| circuit.clone(), // Setup function: clone the circuit for each iteration
|circuit| {
Expand Down Expand Up @@ -261,6 +265,7 @@ fn criterion_benchmark(_c: &mut Criterion) {
const N_POINTS: usize = 3;

// Demonstrating that a higher value of K has a more significant impact on benchmark performance than the number of users
#[cfg(not(feature = "no_range_check"))]
{
const K: u32 = 18;
const N_USERS: usize = 16;
Expand All @@ -275,6 +280,7 @@ fn criterion_benchmark(_c: &mut Criterion) {
format!("../csv/entry_{N_USERS}.csv").as_str(),
);
}
#[cfg(not(feature = "no_range_check"))]
{
const K: u32 = 17;
const N_USERS: usize = 64;
Expand All @@ -289,39 +295,43 @@ fn criterion_benchmark(_c: &mut Criterion) {
format!("../csv/entry_{N_USERS}.csv").as_str(),
);
}
// Use the following benchmarks for quick evaluation/prototyping (no range check)
// {
// const K: u32 = 9;
// const N_USERS: usize = 64;
// bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
// format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
// format!("../csv/entry_{N_USERS}.csv").as_str(),
// );
// }
// {
// const K: u32 = 10;
// const N_USERS: usize = 64;
// bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
// format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
// format!("../csv/entry_{N_USERS}.csv").as_str(),
// );
// }
// {
// const K: u32 = 11;
// const N_USERS: usize = 64;
// bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
// format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
// format!("../csv/entry_{N_USERS}.csv").as_str(),
// );
// }
// {
// const K: u32 = 12;
// const N_USERS: usize = 64;
// bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
// format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
// format!("../csv/entry_{N_USERS}.csv").as_str(),
// );
// }
//Use the following benchmarks for quick evaluation/prototyping (no range check)
#[cfg(feature = "no_range_check")]
{
const K: u32 = 9;
const N_USERS: usize = 64;
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
format!("../csv/entry_{N_USERS}.csv").as_str(),
);
}
#[cfg(feature = "no_range_check")]
{
const K: u32 = 10;
const N_USERS: usize = 64;
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
format!("../csv/entry_{N_USERS}.csv").as_str(),
);
}
#[cfg(feature = "no_range_check")]
{
const K: u32 = 11;
const N_USERS: usize = 64;
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
format!("../csv/entry_{N_USERS}.csv").as_str(),
);
}
#[cfg(feature = "no_range_check")]
{
const K: u32 = 12;
const N_USERS: usize = 64;
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
format!("../csv/entry_{N_USERS}.csv").as_str(),
);
}
}

criterion_group!(benches, criterion_benchmark);
Expand Down

0 comments on commit 910b281

Please sign in to comment.