Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
prover_instance.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
24#include <chrono>
25
26namespace bb {
27
69template <IsUltraOrMegaHonk Flavor_> class ProverInstance_ {
70 public:
71 using Flavor = Flavor_;
72 using FF = typename Flavor::FF;
73
74 private:
81
82 // Flag indicating whether the polynomials will be constructed with fixed block sizes for each gate type
84
85 MetaData metadata; // circuit size and public inputs metadata
86 size_t overflow_size{ 0 }; // size of the structured execution trace overflow
87 size_t final_active_wire_idx{ 0 }; // idx of last non-trivial wire value in the trace
88
89 public:
91
92 std::vector<FF> public_inputs;
93 ProverPolynomials polynomials; // the multilinear polynomials used by the prover
95 SubrelationSeparators alphas; // a challenge for each subrelation
97 std::vector<FF> gate_challenges;
98 FF target_sum{ 0 }; // Sumcheck target sum; typically nonzero for a ProtogalaxyProver's accumulator
99
100 HonkProof ipa_proof; // utilized only for UltraRollupFlavor
101
102 bool is_relaxed_instance = false; // whether this instance is relaxed or not
103 bool is_complete = false; // whether this instance has been completely populated
104 std::vector<uint32_t> memory_read_records;
105 std::vector<uint32_t> memory_write_records;
106
108
109 ActiveRegionData active_region_data; // specifies active regions of execution trace
110
111 void set_dyadic_size(size_t size) { metadata.dyadic_size = size; }
112 void set_overflow_size(size_t size) { overflow_size = size; }
114 size_t dyadic_size() const { return metadata.dyadic_size; }
115 size_t log_dyadic_size() const { return numeric::get_msb(dyadic_size()); }
122 MetaData get_metadata() const { return metadata; }
123 size_t get_overflow_size() const { return overflow_size; }
125
127 {
128 return typename Flavor::PrecomputedData{ polynomials.get_precomputed(), metadata };
129 }
130
132 TraceSettings trace_settings = {},
134 : is_structured(trace_settings.structure.has_value())
136 {
137 BB_BENCH_NAME("ProverInstance(Circuit&)");
138 vinfo("Constructing ProverInstance");
139 auto start = std::chrono::steady_clock::now();
140 // Decider proving keys can be constructed multiple times, hence, we check whether the circuit has been
141 // finalized
142 if (!circuit.circuit_finalized) {
143 circuit.finalize_circuit(/* ensure_nonzero = */ true);
144 }
145
146 // If using a structured trace, set fixed block sizes, check their validity, and set the dyadic circuit size
148 metadata.dyadic_size = compute_dyadic_size(circuit); // set dyadic size directly from circuit block sizes
150 if (is_structured) {
151 circuit.blocks.set_fixed_block_sizes(trace_settings); // The structuring is set
152 if (verbose_logging) {
153 circuit.blocks.summarize();
154 }
156 overflow_size = circuit.blocks.overflow.size();
157 metadata.dyadic_size = compute_structured_dyadic_size(circuit); // set the dyadic size accordingly
158 } else {
159 metadata.dyadic_size = compute_dyadic_size(circuit); // set dyadic based on circuit block sizes
160 }
161 }
162
163 circuit.blocks.compute_offsets(is_structured); // compute offset of each block within the trace
164
165 // Find index of last non-trivial wire value in the trace
166 for (auto& block : circuit.blocks.get()) {
167 if (block.size() > 0) {
168 final_active_wire_idx = block.trace_offset() + block.size() - 1;
169 }
170 }
171
172 vinfo("allocating polynomials object in prover instance...");
173 {
174 BB_BENCH_NAME("allocating polynomials");
175
177
178 // If not using structured trace OR if using structured trace but overflow has occurred (overflow block in
179 // use), allocate full size polys
180 // is_structured = false;
181 if ((IsMegaFlavor<Flavor> && !is_structured) || (is_structured && circuit.blocks.has_overflow)) {
182 // Allocate full size polynomials
184 } else { // Allocate only a correct amount of memory for each polynomial
186
188
189 allocate_selectors(circuit);
190
192
194
195 if constexpr (IsMegaFlavor<Flavor>) {
197 }
198 if constexpr (HasDataBus<Flavor>) {
200 }
201 }
202 // We can finally set the shifted polynomials now that all of the to_be_shifted polynomials are
203 // defined.
204 polynomials.set_shifted(); // Ensure shifted wires are set correctly
205 }
206
207 // Construct and add to proving key the wire, selector and copy constraint polynomials
208 vinfo("populating trace...");
210
211 {
212 BB_BENCH_NAME("constructing prover instance after trace populate");
213
214 // If Goblin, construct the databus polynomials
215 if constexpr (IsMegaFlavor<Flavor>) {
216 BB_BENCH_NAME("constructing databus polynomials");
217
219 }
220 }
221 // Set the lagrange polynomials
222 polynomials.lagrange_first.at(0) = 1;
223 polynomials.lagrange_last.at(final_active_wire_idx) = 1;
224
225 {
226 BB_BENCH_NAME("constructing lookup table polynomials");
227
228 construct_lookup_table_polynomials<Flavor>(
229 polynomials.get_tables(), circuit, dyadic_size(), NUM_DISABLED_ROWS_IN_SUMCHECK);
230 }
231
232 {
233 BB_BENCH_NAME("constructing lookup read counts");
234
235 construct_lookup_read_counts<Flavor>(
236 polynomials.lookup_read_counts, polynomials.lookup_read_tags, circuit, dyadic_size());
237 }
238 { // Public inputs handling
239 metadata.num_public_inputs = circuit.blocks.pub_inputs.size();
240 metadata.pub_inputs_offset = circuit.blocks.pub_inputs.trace_offset();
241 for (size_t i = 0; i < metadata.num_public_inputs; ++i) {
242 size_t idx = i + metadata.pub_inputs_offset;
243 public_inputs.emplace_back(polynomials.w_r[idx]);
244 }
245
246 if constexpr (HasIPAAccumulator<Flavor>) { // Set the IPA claim indices
247 ipa_proof = circuit.ipa_proof;
248 }
249 }
250 auto end = std::chrono::steady_clock::now();
251 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
252 vinfo("time to construct proving key: ", diff.count(), " ms.");
253 }
254
255 ProverInstance_() = default;
260 ~ProverInstance_() = default;
261
263
264 private:
265 static constexpr size_t num_zero_rows = Flavor::has_zero_row ? 1 : 0;
266 static constexpr size_t NUM_WIRES = Circuit::NUM_WIRES;
267
269
270 void allocate_wires();
271
273
275
276 void allocate_selectors(const Circuit&);
277
279
281 requires IsMegaFlavor<Flavor>;
282
284 requires HasDataBus<Flavor>;
285
290 size_t compute_structured_dyadic_size(Circuit& circuit) { return circuit.blocks.get_structured_dyadic_size(); }
291
293 requires HasDataBus<Flavor>;
294
296 requires IsMegaFlavor<Flavor>;
297
298 void populate_memory_records(const Circuit& circuit);
299};
300
301} // namespace bb
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:88
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:218
CommitmentKey object over a pairing group 𝔾₁.
A container for the prover polynomials handles.
WitnessEntities< Commitment > WitnessCommitments
A container for the witness commitments.
Curve::ScalarField FF
bb::CommitmentKey< Curve > CommitmentKey
std::array< FF, NUM_SUBRELATIONS - 1 > SubrelationSeparators
MegaCircuitBuilder CircuitBuilder
bb::Polynomial< FF > Polynomial
static constexpr bool has_zero_row
A ProverInstance is normally constructed from a finalized circuit and it contains all the information...
size_t pub_inputs_offset() const
ProverInstance_(ProverInstance_ &&)=delete
std::vector< uint32_t > memory_write_records
static constexpr size_t num_zero_rows
void allocate_selectors(const Circuit &)
Flavor::PrecomputedData get_precomputed()
ProverInstance_()=default
bb::RelationParameters< FF > relation_parameters
CommitmentKey commitment_key
size_t compute_dyadic_size(Circuit &)
Helper method to compute quantities like total number of gates and dyadic circuit size.
static void move_structured_trace_overflow_to_overflow_block(Circuit &circuit)
Check that the number of gates in each block does not exceed its fixed capacity. Move any overflow to...
ProverInstance_ & operator=(ProverInstance_ &&)=delete
SubrelationSeparators alphas
size_t get_final_active_wire_idx() const
void set_final_active_wire_idx(size_t idx)
size_t log_dyadic_size() const
size_t compute_structured_dyadic_size(Circuit &circuit)
Compute dyadic size based on a structured trace with fixed block size.
void allocate_ecc_op_polynomials(const Circuit &)
ProverPolynomials polynomials
void set_overflow_size(size_t size)
void allocate_permutation_argument_polynomials()
void allocate_table_lookup_polynomials(const Circuit &)
typename Flavor::WitnessCommitments WitnessCommitments
size_t dyadic_size() const
std::vector< FF > public_inputs
WitnessCommitments commitments
std::vector< uint32_t > memory_read_records
ProverInstance_(const ProverInstance_ &)=delete
typename Flavor::CircuitBuilder Circuit
void populate_memory_records(const Circuit &circuit)
Copy RAM/ROM record of reads and writes from the circuit to the instance.
ProverInstance_ & operator=(const ProverInstance_ &)=delete
typename Flavor::FF FF
void construct_databus_polynomials(Circuit &)
size_t get_overflow_size() const
typename Flavor::ProverPolynomials ProverPolynomials
typename Flavor::SubrelationSeparators SubrelationSeparators
void allocate_databus_polynomials(const Circuit &)
typename Flavor::Polynomial Polynomial
size_t num_public_inputs() const
typename Flavor::CommitmentKey CommitmentKey
void set_dyadic_size(size_t size)
ActiveRegionData active_region_data
std::vector< FF > gate_challenges
ProverInstance_(Circuit &circuit, TraceSettings trace_settings={}, const CommitmentKey &commitment_key=CommitmentKey())
static constexpr size_t NUM_WIRES
~ProverInstance_()=default
MetaData get_metadata() const
static void populate(Builder &builder, ProverPolynomials &, ActiveRegionData &)
Given a circuit, populate a proving key with wire polys, selector polys, and sigma/id polys.
#define vinfo(...)
Definition log.hpp:79
Base class templates for structures that contain data parameterized by the fundamental polynomials of...
bool verbose_logging
Definition log.cpp:6
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
Entry point for Barretenberg command-line interface.
std::vector< fr > HonkProof
Definition proof.hpp:15
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Contains various functions that help construct Honk Sigma and Id polynomials.
Dyadic trace size and public inputs metadata; Common between prover and verifier keys.
Definition flavor.hpp:136
size_t pub_inputs_offset
Definition flavor.hpp:139
size_t num_public_inputs
Definition flavor.hpp:138
size_t dyadic_size
Definition flavor.hpp:137
The precomputed data needed to compute a Honk VK.
Definition flavor.hpp:145
Container for parameters used by the grand product (permutation, lookup) Honk relations.