Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
prover.cpp
Go to the documentation of this file.
2
3#include <cstdlib>
4
17
18namespace bb::avm2 {
19
20// Maximum number of polynomials to batch commit at once.
22 getenv("AVM_MAX_MSM_BATCH_SIZE") != nullptr ? std::stoul(getenv("AVM_MAX_MSM_BATCH_SIZE")) : 32;
23
25using FF = Flavor::FF;
26
37 const PCSCommitmentKey& commitment_key)
38 : key(std::move(input_key))
39 , vk(std::move(vk))
40 , prover_polynomials(*key)
41 , commitment_key(commitment_key)
42{}
43
49{
50 FF vk_hash = vk->hash();
51 transcript->add_to_hash_buffer("avm_vk_hash", vk_hash);
52 vinfo("AVM vk hash in prover: ", vk_hash);
53}
54
60{
61 BB_BENCH_NAME("AvmProver::execute_public_inputs_round");
62
63 using C = ColumnAndShifts;
64 // We take the starting values of the public inputs polynomials to add to the transcript
65 const auto public_inputs_cols = std::vector({ &prover_polynomials.get(C::public_inputs_cols_0_),
66 &prover_polynomials.get(C::public_inputs_cols_1_),
67 &prover_polynomials.get(C::public_inputs_cols_2_),
68 &prover_polynomials.get(C::public_inputs_cols_3_) });
69 for (size_t i = 0; i < public_inputs_cols.size(); ++i) {
70 for (size_t j = 0; j < AVM_PUBLIC_INPUTS_COLUMNS_MAX_LENGTH; ++j) {
71 // The public inputs are added to the hash buffer, but do not increase the size of the proof
72 transcript->add_to_hash_buffer("public_input_" + std::to_string(i) + "_" + std::to_string(j),
73 j < public_inputs_cols[i]->size() ? public_inputs_cols[i]->at(j) : FF(0));
74 }
75 }
76}
82{
83 BB_BENCH_NAME("AvmProver::execute_wire_commitments_round");
84 // Commit to all polynomials (apart from logderivative inverse polynomials, which are committed to in the later
85 // logderivative phase)
86 auto wire_polys = prover_polynomials.get_wires();
87 const auto& labels = prover_polynomials.get_wires_labels();
88 auto batch = commitment_key.start_batch();
89 for (size_t idx = 0; idx < wire_polys.size(); ++idx) {
90 batch.add_to_batch(wire_polys[idx], labels[idx], /*mask for zk?*/ false);
91 }
92 batch.commit_and_send_to_verifier(transcript, AVM_MAX_MSM_BATCH_SIZE);
93}
94
96{
97 BB_BENCH_NAME("AvmProver::execute_log_derivative_inverse_round");
98
99 auto [beta, gamma] = transcript->template get_challenges<FF>(std::array<std::string, 2>{ "beta", "gamma" });
102 std::vector<std::function<void()>> tasks;
103
104 bb::constexpr_for<0, std::tuple_size_v<Flavor::LookupRelations>, 1>([&]<size_t relation_idx>() {
106 tasks.push_back([&]() {
107 // We need to resize the inverse polynomials for the relation, now that the selectors have been computed.
109 Relation::Settings::INVERSES,
110 Relation::Settings::SRC_SELECTOR,
111 Relation::Settings::DST_SELECTOR);
112
113 AVM_TRACK_TIME(std::string("prove/log_derivative_inverse_round/") + std::string(Relation::NAME),
114 (compute_logderivative_inverse<FF, Relation>(
115 prover_polynomials, relation_parameters, key->circuit_size)));
116 });
117 });
118
119 bb::parallel_for(tasks.size(), [&](size_t i) { tasks[i](); });
120}
121
123{
124 BB_BENCH_NAME("AvmProver::execute_log_derivative_inverse_commitments_round");
125 auto batch = commitment_key.start_batch();
126 // Commit to all logderivative inverse polynomials and send to verifier
127 for (auto [derived_poly, commitment, label] : zip_view(prover_polynomials.get_derived(),
128 witness_commitments.get_derived(),
129 prover_polynomials.get_derived_labels())) {
130
131 batch.add_to_batch(derived_poly, label, /*mask for zk?*/ false);
132 }
133 batch.commit_and_send_to_verifier(transcript, AVM_MAX_MSM_BATCH_SIZE);
134}
135
141{
142 BB_BENCH_NAME("AvmProver::execute_relation_check_rounds");
143 using Sumcheck = SumcheckProver<Flavor>;
144
145 // Multiply each linearly independent subrelation contribution by `alpha^i` for i = 0, ..., NUM_SUBRELATIONS - 1.
146 const FF alpha = transcript->template get_challenge<FF>("Sumcheck:alpha");
147
148 // Generate gate challenges
149 std::vector<FF> gate_challenges =
150 transcript->template get_dyadic_powers_of_challenge<FF>("Sumcheck:gate_challenge", key->log_circuit_size);
151
152 Sumcheck sumcheck(key->circuit_size,
155 alpha,
156 gate_challenges,
158 key->log_circuit_size);
159
160 sumcheck_output = sumcheck.prove();
161}
162
164{
165 BB_BENCH_NAME("AvmProver::execute_pcs_rounds");
166
168 using PolynomialBatcher = GeminiProver_<Curve>::PolynomialBatcher;
169
170 // Batch polynomials using short scalars to reduce ECCVM circuit size
171 auto unshifted_polys = prover_polynomials.get_unshifted();
172 auto shifted_polys = prover_polynomials.get_to_be_shifted();
173
174 // Generate the same batching challenge labels as on the verifier side
175 std::vector<std::string> unshifted_batching_challenge_labels;
176 unshifted_batching_challenge_labels.reserve(unshifted_polys.size() - 1);
177 for (size_t idx = 0; idx < unshifted_polys.size() - 1; idx++) {
178 unshifted_batching_challenge_labels.push_back("rho_" + std::to_string(idx));
179 }
180 std::vector<std::string> shifted_batching_challenge_labels;
181 shifted_batching_challenge_labels.reserve(shifted_polys.size());
182 for (size_t idx = 0; idx < shifted_polys.size(); idx++) {
183 shifted_batching_challenge_labels.push_back("rho_" + std::to_string(unshifted_polys.size() - 1 + idx));
184 }
185
186 // Get short batching challenges from transcript
187 auto unshifted_challenges = transcript->template get_challenges<FF>(unshifted_batching_challenge_labels);
188 auto shifted_challenges = transcript->template get_challenges<FF>(shifted_batching_challenge_labels);
189
190 // Batch the polynomials
191 Polynomial squashed_unshifted(key->circuit_size);
192 squashed_unshifted += unshifted_polys[0]; // First polynomial has coefficient 1
193 for (size_t i = 0; i < unshifted_challenges.size(); ++i) {
194 squashed_unshifted.add_scaled(unshifted_polys[i + 1], unshifted_challenges[i]);
195 }
196
197 Polynomial squashed_shifted(Polynomial::shiftable(key->circuit_size));
198 for (size_t i = 0; i < shifted_challenges.size(); ++i) {
199 squashed_shifted.add_scaled(shifted_polys[i], shifted_challenges[i]);
200 }
201
202 PolynomialBatcher polynomial_batcher(key->circuit_size);
203 polynomial_batcher.set_unshifted(RefVector{ squashed_unshifted });
204 polynomial_batcher.set_to_be_shifted_by_one(RefVector{ squashed_shifted });
205
206 const OpeningClaim prover_opening_claim = ShpleminiProver_<Curve>::prove(
207 key->circuit_size, polynomial_batcher, sumcheck_output.challenge, commitment_key, transcript);
208
210}
211
213{
214 return transcript->export_proof();
215}
216
218{
219 // Add circuit size public input size and public inputs to transcript.
221
222 // Add public inputs to transcript.
223 AVM_TRACK_TIME("prove/public_inputs_round", execute_public_inputs_round());
224
225 // Compute wire commitments.
226 AVM_TRACK_TIME("prove/wire_commitments_round", execute_wire_commitments_round());
227
228 // Compute log derivative inverses.
229 AVM_TRACK_TIME("prove/log_derivative_inverse_round", execute_log_derivative_inverse_round());
230
231 // Compute commitments to logderivative inverse polynomials.
232 AVM_TRACK_TIME("prove/log_derivative_inverse_commitments_round",
234
235 // Run sumcheck subprotocol.
237
238 // Execute PCS.
239 AVM_TRACK_TIME("prove/pcs_rounds", execute_pcs_rounds());
240
241 return export_proof();
242}
243
244} // namespace bb::avm2
#define AVM_PUBLIC_INPUTS_COLUMNS_MAX_LENGTH
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:219
CommitmentKey object over a pairing group 𝔾₁.
CommitBatch start_batch()
Class responsible for computation of the batched multilinear polynomials required by the Gemini proto...
Definition gemini.hpp:126
static void compute_opening_proof(const CK &ck, const ProverOpeningClaim< Curve > &opening_claim, const std::shared_ptr< Transcript > &prover_trancript)
Computes the KZG commitment to an opening proof polynomial at a single evaluation point.
Definition kzg.hpp:43
Unverified claim (C,r,v) for some witness polynomial p(X) such that.
Definition claim.hpp:53
void add_scaled(PolynomialSpan< const Fr > other, const Fr &scaling_factor)
adds the polynomial q(X) 'other', multiplied by a scaling factor.
static Polynomial shiftable(size_t virtual_size)
Utility to create a shiftable polynomial of given virtual size.
Polynomial p and an opening pair (r,v) such that p(r) = v.
Definition claim.hpp:34
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
static OpeningClaim prove(size_t circuit_size, PolynomialBatcher &polynomial_batcher, std::span< FF > multilinear_challenge, const CommitmentKey< Curve > &commitment_key, const std::shared_ptr< Transcript > &transcript, const std::array< Polynomial, NUM_SMALL_IPA_EVALUATIONS > &libra_polynomials={}, const std::vector< Polynomial > &sumcheck_round_univariates={}, const std::vector< std::array< FF, 3 > > &sumcheck_round_evaluations={})
Definition shplemini.hpp:36
The implementation of the sumcheck Prover for statements of the form for multilinear polynomials .
Definition sumcheck.hpp:289
DataType & get(ColumnAndShifts c)
Definition flavor.hpp:153
AvmFlavorSettings::FF FF
Definition flavor.hpp:36
virtual void execute_pcs_rounds()
Definition prover.cpp:163
std::shared_ptr< Transcript > transcript
Definition prover.hpp:44
SumcheckOutput< Flavor > sumcheck_output
Definition prover.hpp:60
PCSCommitmentKey commitment_key
Definition prover.hpp:62
std::shared_ptr< VerificationKey > vk
Definition prover.hpp:51
virtual void execute_relation_check_rounds()
Run Sumcheck resulting in u = (u_1,...,u_d) challenges and all evaluations at u being calculated.
Definition prover.cpp:140
AvmProver(std::shared_ptr< ProvingKey > input_key, std::shared_ptr< VerificationKey > vk, const PCSCommitmentKey &commitment_key)
Definition prover.cpp:35
virtual void execute_preamble_round()
Add circuit size, public input size, and public inputs to transcript.
Definition prover.cpp:48
ProverPolynomials prover_polynomials
Definition prover.hpp:54
virtual void execute_log_derivative_inverse_commitments_round()
Definition prover.cpp:122
Flavor::WitnessCommitments witness_commitments
Definition prover.hpp:56
virtual void execute_log_derivative_inverse_round()
Definition prover.cpp:95
bb::RelationParameters< FF > relation_parameters
Definition prover.hpp:48
Flavor::FF FF
Definition prover.hpp:14
virtual HonkProof construct_proof()
Definition prover.cpp:217
std::shared_ptr< ProvingKey > key
Definition prover.hpp:50
virtual void execute_public_inputs_round()
Add public inputs to transcript.
Definition prover.cpp:59
virtual void execute_wire_commitments_round()
Compute commitments to all of the witness wires (apart from the logderivative inverse wires)
Definition prover.cpp:81
virtual HonkProof export_proof()
Definition prover.cpp:212
#define vinfo(...)
Definition log.hpp:94
void resize_inverses(AvmFlavor::ProverPolynomials &prover_polynomials, Column inverses_col, Column src_selector_col, Column dst_selector_col)
const size_t AVM_MAX_MSM_BATCH_SIZE
Definition prover.cpp:21
ColumnAndShifts
Definition columns.hpp:34
AvmFlavorSettings::FF FF
Definition field.hpp:10
std::vector< fr > HonkProof
Definition proof.hpp:15
void parallel_for(size_t num_iterations, const std::function< void(size_t)> &func)
Definition thread.cpp:111
VerifierCommitmentKey< Curve > vk
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
#define AVM_TRACK_TIME(key, body)
Definition stats.hpp:16
void add_to_batch(Polynomial< Fr > &poly, const std::string &label, bool mask)