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
33template <IsUltraOrMegaHonk Flavor_> class ProverInstance_ {
34 public:
35 using Flavor = Flavor_;
36 using FF = typename Flavor::FF;
37
38 private:
42 using WitnessCommitments = typename Flavor::WitnessCommitments;
44 using SubrelationSeparator = typename Flavor::SubrelationSeparator;
45
46 MetaData metadata; // circuit size and public inputs metadata
47 // index of the last constrained wire in the execution trace; initialize to size_t::max to indicate uninitialized
48 size_t final_active_wire_idx{ std::numeric_limits<size_t>::max() };
49
50 public:
52
53 std::vector<FF> public_inputs;
54 ProverPolynomials polynomials; // the multilinear polynomials used by the prover
56 SubrelationSeparator alpha; // single challenge from which powers are computed for batching subrelations
58 std::vector<FF> gate_challenges;
59
60 HonkProof ipa_proof; // utilized only for UltraRollupFlavor
61
62 bool is_complete = false; // whether this instance has been completely populated
63 std::vector<uint32_t> memory_read_records;
64 std::vector<uint32_t> memory_write_records;
65
67
68 void set_dyadic_size(size_t size) { metadata.dyadic_size = size; }
70 size_t dyadic_size() const { return metadata.dyadic_size; }
71 size_t log_dyadic_size() const { return numeric::get_msb(dyadic_size()); }
72 size_t pub_inputs_offset() const { return metadata.pub_inputs_offset; }
78 MetaData get_metadata() const { return metadata; }
80 {
81 BB_ASSERT(final_active_wire_idx != std::numeric_limits<size_t>::max(),
82 "final_active_wire_idx has not been initialized");
84 }
87 {
88 return get_final_active_wire_idx() + 1; // +1 because index is inclusive
89 }
90
91 Flavor::PrecomputedData get_precomputed()
92 {
93 return typename Flavor::PrecomputedData{ polynomials.get_precomputed(), metadata };
94 }
95
98 {
99 BB_BENCH_NAME("ProverInstance(Circuit&)");
100 vinfo("Constructing ProverInstance");
101 auto start = std::chrono::steady_clock::now();
102
103 // Check pairing point tagging: either no pairing points were created,
104 // or all pairing points have been aggregated into a single equivalence class
105 BB_ASSERT(circuit.pairing_points_tagging.has_single_pairing_point_tag(),
106 "Pairing points must all be aggregated together. Either no pairing points should be created, or "
107 "all created pairing points must be aggregated into a single pairing point. Found ",
108 circuit.pairing_points_tagging.num_unique_pairing_points(),
109 " different pairing points.");
110 // Check pairing point tagging: check that the pairing points have been set to public
111 BB_ASSERT(circuit.pairing_points_tagging.has_public_pairing_points() ||
112 !circuit.pairing_points_tagging.has_pairing_points(),
113 "Pairing points must be set to public in the circuit before constructing the ProverInstance.");
114
115 // Decider proving keys can be constructed multiple times, hence, we check whether the circuit has been
116 // finalized
117 if (!circuit.circuit_finalized) {
118 circuit.finalize_circuit(/* ensure_nonzero = */ true);
119 }
121
122 // Find index of last non-trivial wire value in the trace
123 circuit.blocks.compute_offsets(); // compute offset of each block within the trace
124 for (auto& block : circuit.blocks.get()) {
125 if (block.size() > 0) {
126 final_active_wire_idx = block.trace_offset() + block.size() - 1;
127 }
128 }
129
130 vinfo("allocating polynomials object in prover instance...");
131 {
132 BB_BENCH_NAME("allocating polynomials");
133
135
137
139
140 allocate_selectors(circuit);
141
143
145
146 if constexpr (IsMegaFlavor<Flavor>) {
148 }
149 if constexpr (HasDataBus<Flavor>) {
151 }
152
153 // Set the shifted polynomials now that all of the to_be_shifted polynomials are defined.
154 polynomials.set_shifted();
155 }
156
157 // Construct and add to proving key the wire, selector and copy constraint polynomials
158 vinfo("populating trace...");
160
161 {
162 BB_BENCH_NAME("constructing prover instance after trace populate");
163
164 // If Goblin, construct the databus polynomials
165 if constexpr (IsMegaFlavor<Flavor>) {
166 BB_BENCH_NAME("constructing databus polynomials");
167
169 }
170 }
171 // Set the lagrange polynomials
172 polynomials.lagrange_first.at(0) = 1;
173 polynomials.lagrange_last.at(final_active_wire_idx) = 1;
174
175 {
176 BB_BENCH_NAME("constructing lookup table polynomials");
177
178 construct_lookup_table_polynomials<Flavor>(polynomials.get_tables(), circuit);
179 }
180
181 {
182 BB_BENCH_NAME("constructing lookup read counts");
183
184 construct_lookup_read_counts<Flavor>(polynomials.lookup_read_counts, polynomials.lookup_read_tags, circuit);
185 }
186 { // Public inputs handling
187 metadata.num_public_inputs = circuit.blocks.pub_inputs.size();
188 metadata.pub_inputs_offset = circuit.blocks.pub_inputs.trace_offset();
189 for (size_t i = 0; i < metadata.num_public_inputs; ++i) {
190 size_t idx = i + metadata.pub_inputs_offset;
191 public_inputs.emplace_back(polynomials.w_r[idx]);
192 }
193
194 if constexpr (HasIPAAccumulator<Flavor>) { // Set the IPA claim indices
195 ipa_proof = circuit.ipa_proof;
196 }
197 }
198 auto end = std::chrono::steady_clock::now();
199 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
200 vinfo("time to construct proving key: ", diff.count(), " ms.");
201 }
202
203 ProverInstance_() = default;
208 ~ProverInstance_() = default;
209
210 private:
211 static constexpr size_t num_zero_rows = Flavor::has_zero_row ? 1 : 0;
212 static constexpr size_t NUM_WIRES = Circuit::NUM_WIRES;
213
215
216 void allocate_wires();
217
219
221
222 void allocate_selectors(const Circuit&);
223
225
227 requires IsMegaFlavor<Flavor>;
228
230 requires HasDataBus<Flavor>;
231
233 requires HasDataBus<Flavor>;
234
235 void populate_memory_records(const Circuit& circuit);
236};
237
238} // namespace bb
#define BB_ASSERT(expression,...)
Definition assert.hpp:67
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:77
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:219
A container for the prover polynomials.
typename Curve::ScalarField FF
ECCVMCircuitBuilder CircuitBuilder
bb::Polynomial< FF > Polynomial
bb::CommitmentKey< Curve > CommitmentKey
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
ProverInstance_(Circuit &circuit, const CommitmentKey &commitment_key=CommitmentKey())
bb::RelationParameters< FF > relation_parameters
CommitmentKey commitment_key
size_t compute_dyadic_size(Circuit &)
Compute the minimum dyadic (power-of-2) circuit size.
ProverInstance_ & operator=(ProverInstance_ &&)=delete
size_t get_final_active_wire_idx() const
void set_final_active_wire_idx(size_t idx)
size_t log_dyadic_size() const
void allocate_ecc_op_polynomials(const Circuit &)
ProverPolynomials polynomials
SubrelationSeparator alpha
size_t trace_active_range_size() const
Get the size of the active trace range (0 to the final active wire index)
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 &)
typename Flavor::ProverPolynomials ProverPolynomials
void allocate_databus_polynomials(const Circuit &)
typename Flavor::SubrelationSeparator SubrelationSeparator
typename Flavor::Polynomial Polynomial
size_t num_public_inputs() const
typename Flavor::CommitmentKey CommitmentKey
void set_dyadic_size(size_t size)
std::vector< FF > gate_challenges
static constexpr size_t NUM_WIRES
~ProverInstance_()=default
MetaData get_metadata() const
static void populate(Builder &builder, ProverPolynomials &)
Given a circuit, populate a proving key with wire polys, selector polys, and sigma/id polys.
#define vinfo(...)
Definition log.hpp:80
Base class templates for structures that contain data parameterized by the fundamental polynomials of...
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
std::vector< fr > HonkProof
Definition proof.hpp:15
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:112
size_t pub_inputs_offset
Definition flavor.hpp:115
size_t num_public_inputs
Definition flavor.hpp:114
size_t dyadic_size
Definition flavor.hpp:113
Container for parameters used by the grand product (permutation, lookup) Honk relations.