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
23#include <chrono>
24
25namespace bb {
26
32template <IsUltraOrMegaHonk Flavor_> class ProverInstance_ {
33 public:
34 using Flavor = Flavor_;
35 using FF = typename Flavor::FF;
36
37 private:
41 using WitnessCommitments = typename Flavor::WitnessCommitments;
43 using SubrelationSeparator = typename Flavor::SubrelationSeparator;
44
45 MetaData metadata; // circuit size and public inputs metadata
46 // index of the last constrained wire in the execution trace; initialize to size_t::max to indicate uninitialized
47 size_t final_active_wire_idx{ std::numeric_limits<size_t>::max() };
48
49 public:
51
52 std::vector<FF> public_inputs;
53 ProverPolynomials polynomials; // the multilinear polynomials used by the prover
55 SubrelationSeparator alpha; // single challenge from which powers are computed for batching subrelations
57 std::vector<FF> gate_challenges;
58
59 HonkProof ipa_proof; // utilized for rollup proofs (IO::HasIPA)
60
61 std::vector<uint32_t> memory_read_records;
62 std::vector<uint32_t> memory_write_records;
63
65
66 void set_dyadic_size(size_t size) { metadata.dyadic_size = size; }
68 size_t dyadic_size() const { return metadata.dyadic_size; }
69 size_t log_dyadic_size() const { return numeric::get_msb(dyadic_size()); }
70 size_t pub_inputs_offset() const { return metadata.pub_inputs_offset; }
76 MetaData get_metadata() const { return metadata; }
78 {
79 BB_ASSERT(final_active_wire_idx != std::numeric_limits<size_t>::max(),
80 "final_active_wire_idx has not been initialized");
82 }
85 {
86 return get_final_active_wire_idx() + 1; // +1 because index is inclusive
87 }
88
89 Flavor::PrecomputedData get_precomputed()
90 {
91 return typename Flavor::PrecomputedData{ polynomials.get_precomputed(), metadata };
92 }
93
96 {
97 BB_BENCH_NAME("ProverInstance(Circuit&)");
98 vinfo("Constructing ProverInstance");
99 auto start = std::chrono::steady_clock::now();
100
101 // Check pairing point tagging: either no pairing points were created,
102 // or all pairing points have been aggregated into a single equivalence class
103 BB_ASSERT(circuit.pairing_points_tagging.has_single_pairing_point_tag(),
104 "Pairing points must all be aggregated together. Either no pairing points should be created, or "
105 "all created pairing points must be aggregated into a single pairing point. Found "
106 << circuit.pairing_points_tagging.num_unique_pairing_points() << " different pairing points.");
107 // Check pairing point tagging: check that the pairing points have been set to public
108 BB_ASSERT(circuit.pairing_points_tagging.has_public_pairing_points() ||
109 !circuit.pairing_points_tagging.has_pairing_points(),
110 "Pairing points must be set to public in the circuit before constructing the ProverInstance.");
111
112 // Decider proving keys can be constructed multiple times, hence, we check whether the circuit has been
113 // finalized
114 if (!circuit.circuit_finalized) {
115 circuit.finalize_circuit(/* ensure_nonzero = */ true);
116 }
118
119 // Find index of last non-trivial wire value in the trace
120 circuit.blocks.compute_offsets(); // compute offset of each block within the trace
121 for (auto& block : circuit.blocks.get()) {
122 if (block.size() > 0) {
123 final_active_wire_idx = block.trace_offset() + block.size() - 1;
124 }
125 }
126
127 vinfo("allocating polynomials object in prover instance...");
128 {
129 BB_BENCH_NAME("allocating polynomials");
130
132
134
136
137 allocate_selectors(circuit);
138
140
142
143 if constexpr (IsMegaFlavor<Flavor>) {
145 }
146 if constexpr (HasDataBus<Flavor>) {
148 }
149
150 // Set the shifted polynomials now that all of the to_be_shifted polynomials are defined.
151 polynomials.set_shifted();
152 }
153
154 // Construct and add to proving key the wire, selector and copy constraint polynomials
155 vinfo("populating trace...");
157
158 {
159 BB_BENCH_NAME("constructing prover instance after trace populate");
160
161 // If Goblin, construct the databus polynomials
162 if constexpr (IsMegaFlavor<Flavor>) {
163 BB_BENCH_NAME("constructing databus polynomials");
164
166 }
167 }
168 // Set the lagrange polynomials
169 polynomials.lagrange_first.at(0) = 1;
170 polynomials.lagrange_last.at(final_active_wire_idx) = 1;
171
172 {
173 BB_BENCH_NAME("constructing lookup table polynomials");
174
175 construct_lookup_table_polynomials<Flavor>(polynomials.get_tables(), circuit);
176 }
177
178 {
179 BB_BENCH_NAME("constructing lookup read counts");
180
181 construct_lookup_read_counts<Flavor>(polynomials.lookup_read_counts, polynomials.lookup_read_tags, circuit);
182 }
183 { // Public inputs handling
184 metadata.num_public_inputs = circuit.blocks.pub_inputs.size();
185 metadata.pub_inputs_offset = circuit.blocks.pub_inputs.trace_offset();
186 for (size_t i = 0; i < metadata.num_public_inputs; ++i) {
187 size_t idx = i + metadata.pub_inputs_offset;
188 public_inputs.emplace_back(polynomials.w_r[idx]);
189 }
190
191 // Copy IPA proof if present (data-driven, not flavor-dependent)
192 ipa_proof = circuit.ipa_proof;
193 }
194 auto end = std::chrono::steady_clock::now();
195 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
196 vinfo("time to construct proving key: ", diff.count(), " ms.");
197 }
198
199 ProverInstance_() = default;
204 ~ProverInstance_() = default;
205
206 private:
207 static constexpr size_t NUM_WIRES = Circuit::NUM_WIRES;
208
210
211 void allocate_wires();
212
214
216
217 void allocate_selectors(const Circuit&);
218
220
222 requires IsMegaFlavor<Flavor>;
223
225 requires HasDataBus<Flavor>;
226
228 requires HasDataBus<Flavor>;
229
230 void populate_memory_records(const Circuit& circuit);
231};
232
233} // namespace bb
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:225
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
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.