|
1 | 1 | //! Run this bench using `cargo criterion -p poly-commitment --bench ipa`
|
2 | 2 |
|
3 |
| -use ark_ff::UniformRand; |
4 |
| -use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial, Radix2EvaluationDomain}; |
| 3 | +use ark_ff::{UniformRand, Zero}; |
| 4 | +use ark_poly::{ |
| 5 | + univariate::DensePolynomial, DenseUVPolynomial, EvaluationDomain, Evaluations, |
| 6 | + Radix2EvaluationDomain, |
| 7 | +}; |
5 | 8 | use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
|
6 | 9 | use groupmap::GroupMap;
|
7 | 10 | use mina_curves::pasta::{Fp, Vesta, VestaParameters};
|
8 | 11 | use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, sponge::DefaultFqSponge, FqSponge};
|
| 12 | +use num_bigint::BigUint; |
| 13 | +use o1_utils::{BigUintFieldHelpers, FieldHelpers}; |
9 | 14 | use poly_commitment::{
|
10 | 15 | commitment::CommitmentCurve, ipa::SRS, utils::DensePolynomialOrEvaluations, PolyComm, SRS as _,
|
11 | 16 | };
|
| 17 | +use rand::Rng; |
12 | 18 |
|
13 | 19 | fn benchmark_ipa_commit_vesta(c: &mut Criterion) {
|
14 | 20 | let mut group = c.benchmark_group("IPA Commit");
|
@@ -41,6 +47,89 @@ fn benchmark_ipa_commit_vesta(c: &mut Criterion) {
|
41 | 47 | }
|
42 | 48 | }
|
43 | 49 |
|
| 50 | +// This benchmark demonstrates that |
| 51 | +// `commit_evaluations_non_hiding` is generally faster than |
| 52 | +// `commit_non_hiding` when committing evaluations; and especially so |
| 53 | +// when the evaluations vector is sparse or contains small elements. |
| 54 | +fn benchmark_ipa_commit_evals_vesta(c: &mut Criterion) { |
| 55 | + let mut group = c.benchmark_group("IPA Commit Evaluations"); |
| 56 | + let mut rng = o1_utils::tests::make_test_rng(None); |
| 57 | + |
| 58 | + group.measurement_time(core::time::Duration::from_secs(10)); |
| 59 | + |
| 60 | + let srs_size_log = 15; |
| 61 | + |
| 62 | + let n = 1 << srs_size_log; |
| 63 | + let srs = SRS::<Vesta>::create(n); |
| 64 | + let domain = Radix2EvaluationDomain::new(n).unwrap(); |
| 65 | + srs.get_lagrange_basis_from_domain_size(n); |
| 66 | + |
| 67 | + for sparsity in [0.05, 0.2, 0.5, 0.99].into_iter() { |
| 68 | + for bitlen in [16, 32, 64, 128, 256].into_iter() { |
| 69 | + // When bitlen > |Fp|, Fp::rand() % bitlenmod will return |
| 70 | + // just field value without performing modulo reduction. |
| 71 | + let bitlenmod: BigUint = Fp::from(1).to_biguint() << bitlen; |
| 72 | + group.bench_function( |
| 73 | + format!( |
| 74 | + "com w/o Lagrange (|SRS| 2^{{{}}}, sparsity {}%, bitlen {})", |
| 75 | + srs_size_log, |
| 76 | + (sparsity * 100.0) as usize, |
| 77 | + bitlen |
| 78 | + ), |
| 79 | + |b| { |
| 80 | + b.iter_batched( |
| 81 | + || { |
| 82 | + let evaluations: Vec<Fp> = (0..n) |
| 83 | + .map(|_| { |
| 84 | + if rng.gen::<f64>() < sparsity { |
| 85 | + (Fp::rand(&mut rng).to_biguint() % bitlenmod.clone()) |
| 86 | + .to_field() |
| 87 | + .unwrap() |
| 88 | + } else { |
| 89 | + Fp::zero() |
| 90 | + } |
| 91 | + }) |
| 92 | + .collect(); |
| 93 | + Evaluations::from_vec_and_domain(evaluations, domain) |
| 94 | + }, |
| 95 | + |evals| black_box(srs.commit_non_hiding(&evals.interpolate(), 1)), |
| 96 | + BatchSize::LargeInput, |
| 97 | + ) |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + group.bench_function( |
| 102 | + format!( |
| 103 | + "com Lagrange (|SRS| 2^{{{}}}, sparsity {}%, bitlen {})", |
| 104 | + srs_size_log, |
| 105 | + (sparsity * 100.0) as usize, |
| 106 | + bitlen |
| 107 | + ), |
| 108 | + |b| { |
| 109 | + b.iter_batched( |
| 110 | + || { |
| 111 | + let evaluations: Vec<Fp> = (0..n) |
| 112 | + .map(|_| { |
| 113 | + if rng.gen::<f64>() < sparsity { |
| 114 | + (Fp::rand(&mut rng).to_biguint() % bitlenmod.clone()) |
| 115 | + .to_field() |
| 116 | + .unwrap() |
| 117 | + } else { |
| 118 | + Fp::zero() |
| 119 | + } |
| 120 | + }) |
| 121 | + .collect(); |
| 122 | + Evaluations::from_vec_and_domain(evaluations, domain) |
| 123 | + }, |
| 124 | + |evals| black_box(srs.commit_evaluations_non_hiding(domain, &evals)), |
| 125 | + BatchSize::LargeInput, |
| 126 | + ) |
| 127 | + }, |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
44 | 133 | fn benchmark_ipa_open_vesta(c: &mut Criterion) {
|
45 | 134 | let mut group = c.benchmark_group("IPA");
|
46 | 135 | let group_map = <Vesta as CommitmentCurve>::Map::setup();
|
@@ -88,6 +177,7 @@ fn benchmark_ipa_open_vesta(c: &mut Criterion) {
|
88 | 177 | criterion_group!(
|
89 | 178 | benches,
|
90 | 179 | benchmark_ipa_commit_vesta,
|
| 180 | + benchmark_ipa_commit_evals_vesta, |
91 | 181 | benchmark_ipa_open_vesta
|
92 | 182 | );
|
93 | 183 | criterion_main!(benches);
|
0 commit comments