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: Planned, auditors: [], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
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 std::vector<uint32_t> memory_read_records;
63 std::vector<uint32_t> memory_write_records;
64
66
67 void set_dyadic_size(size_t size) { metadata.dyadic_size = size; }
69 size_t dyadic_size() const { return metadata.dyadic_size; }
70 size_t log_dyadic_size() const { return numeric::get_msb(dyadic_size()); }
71 size_t pub_inputs_offset() const { return metadata.pub_inputs_offset; }
77 MetaData get_metadata() const { return metadata; }
79 {
80 BB_ASSERT(final_active_wire_idx != std::numeric_limits<size_t>::max(),
81 "final_active_wire_idx has not been initialized");
83 }
86 {
87 return get_final_active_wire_idx() + 1; // +1 because index is inclusive
88 }
89
90 Flavor::PrecomputedData get_precomputed()
91 {
92 return typename Flavor::PrecomputedData{ polynomials.get_precomputed(), metadata };
93 }
94
97 {
98 BB_BENCH_NAME("ProverInstance(Circuit&)");
99 vinfo("Constructing ProverInstance");
100 auto start = std::chrono::steady_clock::now();
101
102 // Check pairing point tagging: either no pairing points were created,
103 // or all pairing points have been aggregated into a single equivalence class
104 BB_ASSERT(circuit.pairing_points_tagging.has_single_pairing_point_tag(),
105 "Pairing points must all be aggregated together. Either no pairing points should be created, or "
106 "all created pairing points must be aggregated into a single pairing point. Found "
107 << circuit.pairing_points_tagging.num_unique_pairing_points() << " different pairing points.");
108 // Check pairing point tagging: check that the pairing points have been set to public
109 BB_ASSERT(circuit.pairing_points_tagging.has_public_pairing_points() ||
110 !circuit.pairing_points_tagging.has_pairing_points(),
111 "Pairing points must be set to public in the circuit before constructing the ProverInstance.");
112
113 // Decider proving keys can be constructed multiple times, hence, we check whether the circuit has been
114 // finalized
115 if (!circuit.circuit_finalized) {
116 circuit.finalize_circuit(/* ensure_nonzero = */ true);
117 }
119
120 // Find index of last non-trivial wire value in the trace
121 circuit.blocks.compute_offsets(); // compute offset of each block within the trace
122 for (auto& block : circuit.blocks.get()) {
123 if (block.size() > 0) {
124 final_active_wire_idx = block.trace_offset() + block.size() - 1;
125 }
126 }
127
128 vinfo("allocating polynomials object in prover instance...");
129 {
130 BB_BENCH_NAME("allocating polynomials");
131
133
135
137
138 allocate_selectors(circuit);
139
141
143
144 if constexpr (IsMegaFlavor<Flavor>) {
146 }
147 if constexpr (HasDataBus<Flavor>) {
149 }
150
151 // Set the shifted polynomials now that all of the to_be_shifted polynomials are defined.
152 polynomials.set_shifted();
153 }
154
155 // Construct and add to proving key the wire, selector and copy constraint polynomials
156 vinfo("populating trace...");
158
159 {
160 BB_BENCH_NAME("constructing prover instance after trace populate");
161
162 // If Goblin, construct the databus polynomials
163 if constexpr (IsMegaFlavor<Flavor>) {
164 BB_BENCH_NAME("constructing databus polynomials");
165
167 }
168 }
169 // Set the lagrange polynomials
170 polynomials.lagrange_first.at(0) = 1;
171 polynomials.lagrange_last.at(final_active_wire_idx) = 1;
172
173 {
174 BB_BENCH_NAME("constructing lookup table polynomials");
175
176 construct_lookup_table_polynomials<Flavor>(polynomials.get_tables(), circuit);
177 }
178
179 {
180 BB_BENCH_NAME("constructing lookup read counts");
181
182 construct_lookup_read_counts<Flavor>(polynomials.lookup_read_counts, polynomials.lookup_read_tags, circuit);
183 }
184 { // Public inputs handling
185 metadata.num_public_inputs = circuit.blocks.pub_inputs.size();
186 metadata.pub_inputs_offset = circuit.blocks.pub_inputs.trace_offset();
187 for (size_t i = 0; i < metadata.num_public_inputs; ++i) {
188 size_t idx = i + metadata.pub_inputs_offset;
189 public_inputs.emplace_back(polynomials.w_r[idx]);
190 }
191
192 if constexpr (HasIPAAccumulator<Flavor>) { // Set the IPA claim indices
193 ipa_proof = circuit.ipa_proof;
194 }
195 }
196 auto end = std::chrono::steady_clock::now();
197 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
198 vinfo("time to construct proving key: ", diff.count(), " ms.");
199 }
200
201 ProverInstance_() = default;
206 ~ProverInstance_() = default;
207
208 private:
209 static constexpr size_t num_zero_rows = Flavor::has_zero_row ? 1 : 0;
210 static constexpr size_t NUM_WIRES = Circuit::NUM_WIRES;
211
213
214 void allocate_wires();
215
217
219
220 void allocate_selectors(const Circuit&);
221
223
225 requires IsMegaFlavor<Flavor>;
226
228 requires HasDataBus<Flavor>;
229
231 requires HasDataBus<Flavor>;
232
233 void populate_memory_records(const Circuit& circuit);
234};
235
236} // namespace bb
#define BB_ASSERT(expression,...)
Definition assert.hpp:80
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:93
#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:94
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.