-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathprover_builder.rs
331 lines (260 loc) · 10.4 KB
/
prover_builder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use crate::file_writer::BBFiles;
use crate::utils::{map_with_newline, snake_case};
pub trait ProverBuilder {
fn create_prover_hpp(&mut self, name: &str);
fn create_prover_cpp(
&mut self,
name: &str,
commitment_polys: &[String],
lookup_names: &[String],
);
}
impl ProverBuilder for BBFiles {
fn create_prover_hpp(&mut self, name: &str) {
let include_str = includes_hpp(&snake_case(name));
let prover_hpp = format!("
{include_str}
namespace bb {{
class {name}Prover {{
using Flavor = {name}Flavor;
using FF = Flavor::FF;
using PCS = Flavor::PCS;
using PCSCommitmentKey = Flavor::CommitmentKey;
using ProvingKey = Flavor::ProvingKey;
using Polynomial = Flavor::Polynomial;
using ProverPolynomials = Flavor::ProverPolynomials;
using CommitmentLabels = Flavor::CommitmentLabels;
using Transcript = Flavor::Transcript;
public:
explicit {name}Prover(std::shared_ptr<ProvingKey> input_key, std::shared_ptr<PCSCommitmentKey> commitment_key);
void execute_preamble_round();
void execute_wire_commitments_round();
void execute_log_derivative_inverse_round();
void execute_relation_check_rounds();
void execute_zeromorph_rounds();
HonkProof export_proof();
HonkProof construct_proof();
std::shared_ptr<Transcript> transcript = std::make_shared<Transcript>();
std::vector<FF> public_inputs;
bb::RelationParameters<FF> relation_parameters;
std::shared_ptr<ProvingKey> key;
// Container for spans of all polynomials required by the prover (i.e. all multivariates evaluated by Sumcheck).
ProverPolynomials prover_polynomials;
CommitmentLabels commitment_labels;
typename Flavor::WitnessCommitments witness_commitments;
Polynomial quotient_W;
SumcheckOutput<Flavor> sumcheck_output;
std::shared_ptr<PCSCommitmentKey> commitment_key;
using ZeroMorph = ZeroMorphProver_<PCS>;
private:
HonkProof proof;
}};
}} // namespace bb
");
self.write_file(
&self.prover,
&format!("{}_prover.hpp", snake_case(name)),
&prover_hpp,
);
}
/// Create the prover cpp file
///
/// Committed polys are included as we manually unroll all commitments, as we do not commit to everything
fn create_prover_cpp(
&mut self,
name: &str,
commitment_polys: &[String],
lookup_names: &[String],
) {
let include_str = includes_cpp(&snake_case(name));
let polynomial_commitment_phase = create_commitments_phase(commitment_polys);
let (call_log_derivative_phase, log_derivative_inverse_phase): (String, String) =
if lookup_names.is_empty() {
("".to_owned(), "".to_owned())
} else {
(
"execute_log_derivative_inverse_round();".to_owned(),
create_log_derivative_inverse_round(lookup_names),
)
};
let prover_cpp = format!("
{include_str}
namespace bb {{
using Flavor = {name}Flavor;
using FF = Flavor::FF;
/**
* Create {name}Prover from proving key, witness and manifest.
*
* @param input_key Proving key.
* @param input_manifest Input manifest
*
* @tparam settings Settings class.
* */
{name}Prover::{name}Prover(std::shared_ptr<Flavor::ProvingKey> input_key,
std::shared_ptr<PCSCommitmentKey> commitment_key)
: key(input_key)
, commitment_key(commitment_key)
{{
for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_unshifted(), key->get_all())) {{
ASSERT(bb::flavor_get_label(prover_polynomials, prover_poly) ==
bb::flavor_get_label(*key, key_poly));
prover_poly = key_poly.share();
}}
for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_shifted(), key->get_to_be_shifted())) {{
ASSERT(bb::flavor_get_label(prover_polynomials, prover_poly) ==
bb::flavor_get_label(*key, key_poly) + \"_shift\");
prover_poly = key_poly.shifted();
}}
}}
/**
* @brief Add circuit size, public input size, and public inputs to transcript
*
*/
void {name}Prover::execute_preamble_round()
{{
const auto circuit_size = static_cast<uint32_t>(key->circuit_size);
transcript->send_to_verifier(\"circuit_size\", circuit_size);
}}
/**
* @brief Compute commitments to all of the witness wires (apart from the logderivative inverse wires)
*
*/
void {name}Prover::execute_wire_commitments_round()
{{
{polynomial_commitment_phase}
}}
void {name}Prover::execute_log_derivative_inverse_round()
{{
{log_derivative_inverse_phase}
}}
/**
* @brief Run Sumcheck resulting in u = (u_1,...,u_d) challenges and all evaluations at u being calculated.
*
*/
void {name}Prover::execute_relation_check_rounds()
{{
using Sumcheck = SumcheckProver<Flavor>;
auto sumcheck = Sumcheck(key->circuit_size, transcript);
FF alpha = transcript->template get_challenge<FF>(\"Sumcheck:alpha\");
std::vector<FF> gate_challenges(numeric::get_msb(key->circuit_size));
for (size_t idx = 0; idx < gate_challenges.size(); idx++) {{
gate_challenges[idx] = transcript->template get_challenge<FF>(\"Sumcheck:gate_challenge_\" + std::to_string(idx));
}}
sumcheck_output = sumcheck.prove(prover_polynomials, relation_parameters, alpha, gate_challenges);
}}
/**
* @brief Execute the ZeroMorph protocol to prove the multilinear evaluations produced by Sumcheck
* @details See https://hackmd.io/dlf9xEwhTQyE3hiGbq4FsA?view for a complete description of the unrolled protocol.
*
* */
void {name}Prover::execute_zeromorph_rounds()
{{
ZeroMorph::prove(prover_polynomials.get_unshifted(),
prover_polynomials.get_to_be_shifted(),
sumcheck_output.claimed_evaluations.get_unshifted(),
sumcheck_output.claimed_evaluations.get_shifted(),
sumcheck_output.challenge,
commitment_key,
transcript);
}}
HonkProof {name}Prover::export_proof()
{{
proof = transcript->proof_data;
return proof;
}}
HonkProof {name}Prover::construct_proof()
{{
// Add circuit size public input size and public inputs to transcript.
execute_preamble_round();
// Compute wire commitments
execute_wire_commitments_round();
// Compute sorted list accumulator and commitment
{call_log_derivative_phase}
// Fiat-Shamir: alpha
// Run sumcheck subprotocol.
execute_relation_check_rounds();
// Fiat-Shamir: rho, y, x, z
// Execute Zeromorph multilinear PCS
execute_zeromorph_rounds();
return export_proof();
}}
}} // namespace bb
");
self.write_file(
&self.prover,
&format!("{}_prover.cpp", snake_case(name)),
&prover_cpp,
);
}
}
fn includes_hpp(name: &str) -> String {
format!(
"
#pragma once
#include \"barretenberg/commitment_schemes/zeromorph/zeromorph.hpp\"
#include \"barretenberg/plonk/proof_system/types/proof.hpp\"
#include \"barretenberg/relations/relation_parameters.hpp\"
#include \"barretenberg/sumcheck/sumcheck_output.hpp\"
#include \"barretenberg/transcript/transcript.hpp\"
#include \"barretenberg/vm/generated/{name}_flavor.hpp\"
"
)
}
fn includes_cpp(name: &str) -> String {
format!(
"
#include \"{name}_prover.hpp\"
#include \"barretenberg/commitment_schemes/claim.hpp\"
#include \"barretenberg/commitment_schemes/commitment_key.hpp\"
#include \"barretenberg/honk/proof_system/logderivative_library.hpp\"
#include \"barretenberg/honk/proof_system/permutation_library.hpp\"
#include \"barretenberg/plonk_honk_shared/library/grand_product_library.hpp\"
#include \"barretenberg/polynomials/polynomial.hpp\"
#include \"barretenberg/relations/lookup_relation.hpp\"
#include \"barretenberg/relations/permutation_relation.hpp\"
#include \"barretenberg/sumcheck/sumcheck.hpp\"
"
)
}
/// Commitment Transform
///
/// Produces code to perform kzg commitment, then stores in the witness_commitments struct
fn commitment_transform(name: &String) -> String {
format!("witness_commitments.{name} = commitment_key->commit(key->{name});")
}
/// Send to Verifier Transform
///
/// Sends commitment produces in commitment_transform to the verifier
fn send_to_verifier_transform(name: &String) -> String {
format!("transcript->send_to_verifier(commitment_labels.{name}, witness_commitments.{name});")
}
fn create_commitments_phase(polys_to_commit_to: &[String]) -> String {
let all_commit_operations = map_with_newline(polys_to_commit_to, commitment_transform);
let send_to_verifier_operations =
map_with_newline(polys_to_commit_to, send_to_verifier_transform);
format!(
"
// Commit to all polynomials (apart from logderivative inverse polynomials, which are committed to in the later logderivative phase)
{all_commit_operations}
// Send all commitments to the verifier
{send_to_verifier_operations}
"
)
}
fn create_log_derivative_inverse_round(lookup_operations: &[String]) -> String {
let all_commit_operations = map_with_newline(lookup_operations, commitment_transform);
let send_to_verifier_operations =
map_with_newline(lookup_operations, send_to_verifier_transform);
format!(
"
auto [beta, gamm] = transcript->template get_challenges<FF>(\"beta\", \"gamma\");
relation_parameters.beta = beta;
relation_parameters.gamma = gamm;
key->compute_logderivative_inverses(relation_parameters);
// Commit to all logderivative inverse polynomials
{all_commit_operations}
// Send all commitments to the verifier
{send_to_verifier_operations}
"
)
}