Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ipa.bench.cpp
Go to the documentation of this file.
4#include <benchmark/benchmark.h>
5
6using namespace benchmark;
7using namespace bb;
8
9namespace {
13
14constexpr size_t MIN_POLYNOMIAL_DEGREE_LOG2 = 10;
15constexpr size_t MAX_POLYNOMIAL_DEGREE_LOG2 = 16;
16
19std::vector<std::shared_ptr<NativeTranscript>> prover_transcripts(MAX_POLYNOMIAL_DEGREE_LOG2 -
20 MIN_POLYNOMIAL_DEGREE_LOG2 + 1);
21std::vector<OpeningClaim<Curve>> opening_claims(MAX_POLYNOMIAL_DEGREE_LOG2 - MIN_POLYNOMIAL_DEGREE_LOG2 + 1);
22static void DoSetup(const benchmark::State&)
23{
25 ck = CommitmentKey<Curve>(1 << MAX_POLYNOMIAL_DEGREE_LOG2);
26 vk = VerifierCommitmentKey<Curve>(1 << MAX_POLYNOMIAL_DEGREE_LOG2, srs::get_grumpkin_crs_factory());
27}
28
29void ipa_open(State& state) noexcept
30{
32 for (auto _ : state) {
33 state.PauseTiming();
34 size_t n = 1 << static_cast<size_t>(state.range(0));
35 // Construct the polynomial
36 Polynomial poly(n);
37 for (size_t i = 0; i < n; ++i) {
38 poly.at(i) = Fr::random_element(&engine);
39 }
40 auto x = Fr::random_element(&engine);
41 auto eval = poly.evaluate(x);
42 const OpeningPair<Curve> opening_pair = { x, eval };
43 const OpeningClaim<Curve> opening_claim{ opening_pair, ck.commit(poly) };
44 // initialize empty prover transcript
45 auto prover_transcript = std::make_shared<NativeTranscript>();
46 state.ResumeTiming();
47 // Compute proof
48 IPA<Curve>::compute_opening_proof(ck, { poly, opening_pair }, prover_transcript);
49 // Store info for verifier
50 prover_transcripts[static_cast<size_t>(state.range(0)) - MIN_POLYNOMIAL_DEGREE_LOG2] = prover_transcript;
51 opening_claims[static_cast<size_t>(state.range(0)) - MIN_POLYNOMIAL_DEGREE_LOG2] = opening_claim;
52 }
53}
54void ipa_verify(State& state) noexcept
55{
56 for (auto _ : state) {
57 state.PauseTiming();
58 // Retrieve proofs
59 auto prover_transcript = prover_transcripts[static_cast<size_t>(state.range(0)) - MIN_POLYNOMIAL_DEGREE_LOG2];
60 auto opening_claim = opening_claims[static_cast<size_t>(state.range(0)) - MIN_POLYNOMIAL_DEGREE_LOG2];
61 // initialize verifier transcript from proof data
62 auto verifier_transcript = std::make_shared<NativeTranscript>(prover_transcript->export_proof());
63
64 state.ResumeTiming();
65 auto result = IPA<Curve>::reduce_verify(vk, opening_claim, verifier_transcript);
66 BB_ASSERT(result);
67 }
68}
69
70// Batch verification benchmarks: compare N individual verifications vs one batch verification.
71// Uses default IPA poly_length (ECCVM size = 2^17) to match real Chonk usage.
72
73// Pre-generated single proof for batch benchmarks (reused N times to avoid expensive setup)
74OpeningClaim<Curve> batch_claim;
75HonkProof batch_proof_data_single;
76
77static void DoBatchSetup(const benchmark::State&)
78{
80 static constexpr size_t BENCH_POLY_LENGTH = IPA<Curve>::poly_length;
81 static bool initialized = false;
82 if (initialized) {
83 return;
84 }
85 initialized = true;
86
87 ck = CommitmentKey<Curve>(BENCH_POLY_LENGTH);
89
91 Polynomial poly(BENCH_POLY_LENGTH);
92 for (size_t j = 0; j < BENCH_POLY_LENGTH; j++) {
93 poly.at(j) = Fr::random_element(&engine);
94 }
95 auto x = Fr::random_element(&engine);
96 auto eval = poly.evaluate(x);
97 batch_claim = { { x, eval }, ck.commit(poly) };
98
100 IPA<Curve>::compute_opening_proof(ck, { poly, { x, eval } }, pt);
101 batch_proof_data_single = pt->export_proof();
102}
103
107void ipa_verify_individual(State& state) noexcept
108{
109 const size_t num_proofs = static_cast<size_t>(state.range(0));
110 for (auto _ : state) {
111 state.PauseTiming();
112 // Create fresh verifier transcripts from the same proof data
113 std::vector<std::shared_ptr<NativeTranscript>> transcripts(num_proofs);
114 for (size_t i = 0; i < num_proofs; i++) {
115 transcripts[i] = std::make_shared<NativeTranscript>(batch_proof_data_single);
116 }
117 state.ResumeTiming();
118
119 for (size_t i = 0; i < num_proofs; i++) {
120 auto result = IPA<Curve>::reduce_verify(vk, batch_claim, transcripts[i]);
121 BB_ASSERT(result);
122 }
123 }
124}
125
129void ipa_batch_verify(State& state) noexcept
130{
131 const size_t num_proofs = static_cast<size_t>(state.range(0));
132 for (auto _ : state) {
133 state.PauseTiming();
134 std::vector<OpeningClaim<Curve>> claims(num_proofs, batch_claim);
135 std::vector<std::shared_ptr<NativeTranscript>> transcripts(num_proofs);
136 for (size_t i = 0; i < num_proofs; i++) {
137 transcripts[i] = std::make_shared<NativeTranscript>(batch_proof_data_single);
138 }
139 state.ResumeTiming();
140
141 auto result = IPA<Curve>::batch_reduce_verify(vk, claims, transcripts);
142 BB_ASSERT(result);
143 }
144}
145
146} // namespace
147BENCHMARK(ipa_open)
148 ->Unit(kMillisecond)
149 ->DenseRange(MIN_POLYNOMIAL_DEGREE_LOG2, MAX_POLYNOMIAL_DEGREE_LOG2)
150 ->Setup(DoSetup);
151BENCHMARK(ipa_verify)
152 ->Unit(kMillisecond)
153 ->DenseRange(MIN_POLYNOMIAL_DEGREE_LOG2, MAX_POLYNOMIAL_DEGREE_LOG2)
154 ->Setup(DoSetup);
155BENCHMARK(ipa_verify_individual)->Unit(kMillisecond)->Arg(1)->Arg(2)->Arg(4)->Arg(8)->Setup(DoBatchSetup);
156BENCHMARK(ipa_batch_verify)->Unit(kMillisecond)->Arg(1)->Arg(2)->Arg(4)->Arg(8)->Setup(DoBatchSetup);
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
CommitmentKey object over a pairing group 𝔾₁.
IPA (inner product argument) commitment scheme class.
Definition ipa.hpp:86
Unverified claim (C,r,v) for some witness polynomial p(X) such that.
Definition claim.hpp:55
Opening pair (r,v) for some witness polynomial p(X) such that p(r) = v.
Definition claim.hpp:21
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
numeric::RNG & engine
BENCHMARK(ipa_verify_individual) -> Unit(kMillisecond) ->Arg(1) ->Arg(2) ->Arg(4) ->Arg(8) ->Setup(DoBatchSetup)
BENCHMARK_MAIN()
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:217
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
std::shared_ptr< factories::CrsFactory< curve::Grumpkin > > get_grumpkin_crs_factory()
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
std::vector< fr > HonkProof
Definition proof.hpp:15
CommitmentKey< Curve > ck
VerifierCommitmentKey< Curve > vk
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static field random_element(numeric::RNG *engine=nullptr) noexcept