Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
oink_prover.cpp
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
13
14namespace bb {
15
21template <IsUltraOrMegaHonk Flavor> void OinkProver<Flavor>::prove()
22{
23 BB_BENCH_NAME("OinkProver::prove");
24 if (!prover_instance->commitment_key.initialized()) {
25 prover_instance->commitment_key = CommitmentKey(prover_instance->dyadic_size());
26 }
27 // Add circuit size public input size and public inputs to transcript->
28 execute_preamble_round();
29 // For ZK flavors: create and commit to Gemini masking polynomial
30 commit_to_masking_poly();
31 // Compute first three wire commitments
32 execute_wire_commitments_round();
33 // Compute sorted list accumulator and commitment
34 execute_sorted_list_accumulator_round();
35 // Fiat-Shamir: beta & gamma
36 execute_log_derivative_inverse_round();
37 // Compute grand product(s) and commitments.
38 execute_grand_product_computation_round();
39
40 // Generate relation separator alpha for sumcheck computation
41 prover_instance->alpha = generate_alpha_round();
42
43 // #ifndef __wasm__
44 // Free the commitment key
45 prover_instance->commitment_key = CommitmentKey();
46 // #endif
47}
48
53template <IsUltraOrMegaHonk Flavor> typename OinkProver<Flavor>::Proof OinkProver<Flavor>::export_proof()
54{
55 return transcript->export_proof();
56}
57
62template <IsUltraOrMegaHonk Flavor> void OinkProver<Flavor>::execute_preamble_round()
63{
64 BB_BENCH_NAME("OinkProver::execute_preamble_round");
65 fr vk_hash = honk_vk->hash_with_origin_tagging(*transcript);
66 transcript->add_to_hash_buffer(domain_separator + "vk_hash", vk_hash);
67 vinfo("vk hash in Oink prover: ", vk_hash);
68
69 for (size_t i = 0; i < prover_instance->num_public_inputs(); ++i) {
70 auto public_input_i = prover_instance->public_inputs[i];
71 transcript->send_to_verifier(domain_separator + "public_input_" + std::to_string(i), public_input_i);
72 }
73}
74
80template <IsUltraOrMegaHonk Flavor> void OinkProver<Flavor>::execute_wire_commitments_round()
81{
82 BB_BENCH_NAME("OinkProver::execute_wire_commitments_round");
83 // Commit to the first three wire polynomials
84 // We only commit to the fourth wire polynomial after adding memory recordss
85 auto batch = prover_instance->commitment_key.start_batch();
86 // Commit to the first three wire polynomials
87 // We only commit to the fourth wire polynomial after adding memory records
88
89 batch.add_to_batch(prover_instance->polynomials.w_l, commitment_labels.w_l, /*mask?*/ Flavor::HasZK);
90 batch.add_to_batch(prover_instance->polynomials.w_r, commitment_labels.w_r, /*mask?*/ Flavor::HasZK);
91 batch.add_to_batch(prover_instance->polynomials.w_o, commitment_labels.w_o, /*mask?*/ Flavor::HasZK);
92
93 if constexpr (IsMegaFlavor<Flavor>) {
94
95 // Commit to Goblin ECC op wires.
96 // Note even with zk, we do not mask here. The masking for these is done differently.
97 // It is necessary that "random" ops are added to the op_queue, which is then used to populate these ecc op
98 // wires. This is more holistic and obviates the need to extend with random values.
99 bool mask_ecc_op_polys = false; // Flavor::HasZK
100
101 for (auto [polynomial, label] :
102 zip_view(prover_instance->polynomials.get_ecc_op_wires(), commitment_labels.get_ecc_op_wires())) {
103 {
104 BB_BENCH_NAME("COMMIT::ecc_op_wires");
105 batch.add_to_batch(polynomial, domain_separator + label, mask_ecc_op_polys);
106 };
107 }
108
109 // Commit to DataBus related polynomials
110 for (auto [polynomial, label] :
111 zip_view(prover_instance->polynomials.get_databus_entities(), commitment_labels.get_databus_entities())) {
112 {
113 BB_BENCH_NAME("COMMIT::databus");
114 bool is_unmasked_databus_commitment = label == "CALLDATA";
115 batch.add_to_batch(polynomial, label, /*mask?*/ Flavor::HasZK && !is_unmasked_databus_commitment);
116 }
117 }
118 }
119
120 auto computed_commitments = batch.commit_and_send_to_verifier(transcript);
121 prover_instance->commitments.w_l = computed_commitments[0];
122 prover_instance->commitments.w_r = computed_commitments[1];
123 prover_instance->commitments.w_o = computed_commitments[2];
124
125 if constexpr (IsMegaFlavor<Flavor>) {
126 size_t commitment_idx = 3;
127 for (auto& commitment : prover_instance->commitments.get_ecc_op_wires()) {
128 commitment = computed_commitments[commitment_idx];
129 commitment_idx++;
130 }
131
132 for (auto& commitment : prover_instance->commitments.get_databus_entities()) {
133 commitment = computed_commitments[commitment_idx];
134 commitment_idx++;
135 }
136 }
137}
138
143template <IsUltraOrMegaHonk Flavor> void OinkProver<Flavor>::execute_sorted_list_accumulator_round()
144{
145 BB_BENCH_NAME("OinkProver::execute_sorted_list_accumulator_round");
146 // Get eta challenges
147 auto [eta, eta_two, eta_three] = transcript->template get_challenges<FF>(std::array<std::string, 3>{
148 domain_separator + "eta", domain_separator + "eta_two", domain_separator + "eta_three" });
149 prover_instance->relation_parameters.eta = eta;
150 prover_instance->relation_parameters.eta_two = eta_two;
151 prover_instance->relation_parameters.eta_three = eta_three;
152
154 prover_instance->memory_read_records,
155 prover_instance->memory_write_records,
156 eta,
157 eta_two,
158 eta_three);
159
160 // Commit to lookup argument polynomials and the finalized (i.e. with memory records) fourth wire polynomial
161 auto batch = prover_instance->commitment_key.start_batch();
162 batch.add_to_batch(prover_instance->polynomials.lookup_read_counts,
163 commitment_labels.lookup_read_counts,
164 /*mask?*/ Flavor::HasZK);
165 batch.add_to_batch(
166 prover_instance->polynomials.lookup_read_tags, commitment_labels.lookup_read_tags, /*mask?*/ Flavor::HasZK);
167 batch.add_to_batch(
168 prover_instance->polynomials.w_4, domain_separator + commitment_labels.w_4, /*mask?*/ Flavor::HasZK);
169 auto computed_commitments = batch.commit_and_send_to_verifier(transcript);
170
171 prover_instance->commitments.lookup_read_counts = computed_commitments[0];
172 prover_instance->commitments.lookup_read_tags = computed_commitments[1];
173 prover_instance->commitments.w_4 = computed_commitments[2];
174}
175
180template <IsUltraOrMegaHonk Flavor> void OinkProver<Flavor>::execute_log_derivative_inverse_round()
181{
182 BB_BENCH_NAME("OinkProver::execute_log_derivative_inverse_round");
183 auto [beta, gamma] = transcript->template get_challenges<FF>(
184 std::array<std::string, 2>{ domain_separator + "beta", domain_separator + "gamma" });
185 prover_instance->relation_parameters.beta = beta;
186 prover_instance->relation_parameters.gamma = gamma;
187
188 // Compute the inverses used in log-derivative lookup relations
190 prover_instance->polynomials, prover_instance->dyadic_size(), prover_instance->relation_parameters);
191
192 auto batch = prover_instance->commitment_key.start_batch();
193 batch.add_to_batch(prover_instance->polynomials.lookup_inverses,
194 commitment_labels.lookup_inverses,
195 /*mask?*/ Flavor::HasZK);
196
197 // If Mega, commit to the databus inverse polynomials and send
198 if constexpr (IsMegaFlavor<Flavor>) {
199 for (auto [polynomial, label] :
200 zip_view(prover_instance->polynomials.get_databus_inverses(), commitment_labels.get_databus_inverses())) {
201 batch.add_to_batch(polynomial, label, /*mask?*/ Flavor::HasZK);
202 };
203 }
204 auto computed_commitments = batch.commit_and_send_to_verifier(transcript);
205
206 prover_instance->commitments.lookup_inverses = computed_commitments[0];
207 if constexpr (IsMegaFlavor<Flavor>) {
208 size_t commitment_idx = 1;
209 for (auto& commitment : prover_instance->commitments.get_databus_inverses()) {
210 commitment = computed_commitments[commitment_idx];
211 commitment_idx++;
212 };
213 }
214}
215
220template <IsUltraOrMegaHonk Flavor> void OinkProver<Flavor>::execute_grand_product_computation_round()
221{
222 BB_BENCH_NAME("OinkProver::execute_grand_product_computation_round");
223 // Compute the permutation grand product polynomial
224
226 prover_instance->public_inputs,
227 prover_instance->pub_inputs_offset(),
228 prover_instance->relation_parameters,
229 prover_instance->get_final_active_wire_idx() + 1);
230
231 {
232 BB_BENCH_NAME("COMMIT::z_perm");
233 prover_instance->commitments.z_perm =
234 commit_to_witness_polynomial(prover_instance->polynomials.z_perm, commitment_labels.z_perm);
235 }
236}
237
238template <IsUltraOrMegaHonk Flavor> typename Flavor::SubrelationSeparator OinkProver<Flavor>::generate_alpha_round()
239{
240 BB_BENCH_NAME("OinkProver::generate_alpha_round");
241
242 // Get the single alpha challenge for sumcheck computation
243 // Powers of this challenge will be used to batch subrelations
244 return transcript->template get_challenge<FF>(domain_separator + "alpha");
245}
246
254template <IsUltraOrMegaHonk Flavor>
256 const std::string& label)
257{
258 BB_BENCH_NAME("OinkProver::commit_to_witness_polynomial");
259 // Mask the polynomial when proving in zero-knowledge
260 if constexpr (Flavor::HasZK) {
261 polynomial.mask();
262 };
263
264 typename Flavor::Commitment commitment;
265
266 commitment = prover_instance->commitment_key.commit(polynomial);
267 // Send the commitment to the verifier
268 transcript->send_to_verifier(domain_separator + label, commitment);
269
270 return commitment;
271}
272
273template <IsUltraOrMegaHonk Flavor> void OinkProver<Flavor>::commit_to_masking_poly()
274{
275 if constexpr (Flavor::HasZK) {
276 // Create a random masking polynomial for Gemini
277 const size_t polynomial_size = prover_instance->dyadic_size();
278 prover_instance->polynomials.gemini_masking_poly = Polynomial<FF>::random(polynomial_size);
279
280 // Commit to the masking polynomial and send to transcript
281 auto masking_commitment =
282 prover_instance->commitment_key.commit(prover_instance->polynomials.gemini_masking_poly);
283 transcript->send_to_verifier("Gemini:masking_poly_comm", masking_commitment);
284 }
285};
286
287template class OinkProver<UltraFlavor>;
288template class OinkProver<UltraZKFlavor>;
289template class OinkProver<UltraKeccakFlavor>;
290#ifdef STARKNET_GARAGA_FLAVORS
293#endif
295template class OinkProver<UltraRollupFlavor>;
296template class OinkProver<MegaFlavor>;
297template class OinkProver<MegaZKFlavor>;
298template class OinkProver<MegaAvmFlavor>;
299
300} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:219
static constexpr bool HasZK
typename G1::affine_element Commitment
Class for all the oink rounds, which are shared between the folding prover and ultra prover.
Proof export_proof()
Export the Oink proof.
Flavor::Commitment commit_to_witness_polynomial(Polynomial< FF > &polynomial, const std::string &label)
A uniform method to mask, commit, and send the corresponding commitment to the verifier.
void execute_log_derivative_inverse_round()
Compute log derivative inverse polynomial and its commitment, if required.
void execute_grand_product_computation_round()
Compute permutation and lookup grand product polynomials and their commitments.
void prove()
Oink Prover function that runs all the rounds of the verifier.
SubrelationSeparator generate_alpha_round()
void execute_preamble_round()
Add circuit size, public input size, and public inputs to transcript.
typename Flavor::CommitmentKey CommitmentKey
void execute_sorted_list_accumulator_round()
Compute sorted witness-table accumulator and commit to the resulting polynomials.
void commit_to_masking_poly()
void execute_wire_commitments_round()
Commit to the wire polynomials (part of the witness), with the exception of the fourth wire,...
typename Transcript::Proof Proof
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
static Polynomial random(size_t size, size_t start_index=0)
void mask()
Add random values to the coefficients of a polynomial. In practice, this is used for ensuring the com...
static void compute_logderivative_inverses(Flavor::ProverPolynomials &polynomials, const size_t circuit_size, RelationParameters< FF > &relation_parameters)
Compute the inverse polynomials used in the log derivative lookup relations.
static void compute_grand_product_polynomial(Flavor::ProverPolynomials &polynomials, std::vector< FF > &public_inputs, const size_t pub_inputs_offset, RelationParameters< FF > &relation_parameters, size_t size_override=0)
Computes public_input_delta and the permutation grand product polynomial.
static void add_ram_rom_memory_records_to_wire_4(typename Flavor::ProverPolynomials &polynomials, const std::vector< uint32_t > &memory_read_records, const std::vector< uint32_t > &memory_write_records, const FF &eta, const FF &eta_two, const FF &eta_three)
Add RAM/ROM memory records to the fourth wire polynomial.
#define vinfo(...)
Definition log.hpp:94
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)