Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
cli.cpp
Go to the documentation of this file.
1
38#include <atomic>
39#include <fstream>
40#include <iostream>
41#include <mutex>
42
43namespace bb {
44
45// TODO(https://github.com/AztecProtocol/barretenberg/issues/1257): Remove unused/seemingly unnecessary flags.
46// TODO(https://github.com/AztecProtocol/barretenberg/issues/1258): Improve defaults.
47
48// Helper function to recursively print active subcommands for CLI11 app debugging
49void print_active_subcommands(const CLI::App& app, const std::string& prefix = "bb command: ")
50{
51 // get_subcommands() returns a vector of pointers to subcommands
52 for (auto* subcmd : app.get_subcommands()) {
53 // Check if this subcommand was activated (nonzero count)
54 if (subcmd->count() > 0) {
55 vinfo(prefix, subcmd->get_name());
56 // Recursively print any subcommands of this subcommand
57 print_active_subcommands(*subcmd, prefix + " ");
58 }
59 }
60}
61
62// Recursive helper to find the deepest parsed subcommand.
63CLI::App* find_deepest_subcommand(CLI::App* app)
64{
65 for (auto& sub : app->get_subcommands()) {
66 if (sub->parsed()) {
67 // Check recursively if this subcommand has a deeper parsed subcommand.
68 if (CLI::App* deeper = find_deepest_subcommand(sub); deeper != nullptr) {
69 return deeper;
70 }
71 return sub;
72 }
73 }
74 return nullptr;
75}
76
77// Helper function to print options for a given subcommand.
78void print_subcommand_options(const CLI::App* sub)
79{
80 for (const auto& opt : sub->get_options()) {
81 if (opt->count() > 0) { // Only print options that were set.
82 if (opt->results().size() > 1) {
83 vinfo(" Warning: the following option is called more than once");
84 }
85 vinfo(" ", opt->get_name(), ": ", opt->results()[0]);
86 }
87 }
88}
89
109int parse_and_run_cli_command(int argc, char* argv[])
110{
111 std::string name = "Barretenberg\nYour favo(u)rite zkSNARK library written in C++, a perfectly good computer "
112 "programming language.";
113
114 // Check AVM support at runtime via global boolean
115 if (avm_enabled) {
116 name += "\nAztec Virtual Machine (AVM): enabled";
117 } else {
118 name += "\nAztec Virtual Machine (AVM): disabled";
119 }
120#ifdef ENABLE_AVM_TRANSPILER
121 name += "\nAVM Transpiler: enabled";
122#else
123 name += "\nAVM Transpiler: disabled";
124#endif
125#ifdef STARKNET_GARAGA_FLAVORS
126 name += "\nStarknet Garaga Extensions: enabled";
127#else
128 name += "\nStarknet Garaga Extensions: disabled";
129#endif
130 CLI::App app{ name };
131 argv = app.ensure_utf8(argv);
132 app.formatter(std::make_shared<Formatter>());
133
134 // If no arguments are provided, print help and exit.
135 if (argc == 1) {
136 std::cout << app.help() << std::endl;
137 return 0;
138 }
139
140 // prevent two or more subcommands being executed
141 app.require_subcommand(0, 1);
142
143 API::Flags flags{};
144 // Some paths, with defaults, that may or may not be set by commands
145 std::filesystem::path bytecode_path{ "./target/program.json" };
146 std::filesystem::path witness_path{ "./target/witness.gz" };
147 std::filesystem::path ivc_inputs_path{ "./ivc-inputs.msgpack" };
148 std::filesystem::path output_path{
149 "./out"
150 }; // sometimes a directory where things will be written, sometimes the path of a file to be written
151 std::filesystem::path public_inputs_path{ "./target/public_inputs" };
152 std::filesystem::path proof_path{ "./target/proof" };
153 std::filesystem::path vk_path{ "./target/vk" };
154 flags.scheme = "";
155 flags.oracle_hash_type = "poseidon2";
156 flags.crs_path = srs::bb_crs_path();
157 flags.include_gates_per_opcode = false;
158
159 /***************************************************************************************************************
160 * Flag: --help-extended (detected early to set group visibility)
161 ***************************************************************************************************************/
162 // Check if --help-extended was passed before parsing (since we need to modify group visibility before CLI setup)
163 bool show_extended_help = false;
164 for (int i = 1; i < argc; ++i) {
165 if (std::string(argv[i]) == "--help-extended") {
166 show_extended_help = true;
167 break;
168 }
169 }
170 // Group names - empty string hides the group from help, non-empty shows it
171 const std::string advanced_group = show_extended_help ? "Advanced Options (Aztec/Power Users)" : "";
172 const std::string aztec_internal_group = show_extended_help ? "Aztec Internal Commands" : "";
173
174 const auto add_output_path_option = [&](CLI::App* subcommand, auto& _output_path) {
175 return subcommand->add_option("--output_path, -o",
176 _output_path,
177 "Directory to write files or path of file to write, depending on subcommand.");
178 };
179
180 // Helper to add --help-extended to subcommands (for help consistency)
181 const auto add_help_extended_flag = [&](CLI::App* subcommand) {
182 subcommand->add_flag("--help-extended", "Show all options including advanced ones.");
183 };
184
185 /***************************************************************************************************************
186 * Subcommand: Adders for options that we will create for more than one subcommand
187 ***************************************************************************************************************/
188
189 const auto add_ipa_accumulation_flag = [&](CLI::App* subcommand) {
190 return subcommand
191 ->add_flag("--ipa_accumulation",
192 flags.ipa_accumulation,
193 "Accumulate/Aggregate IPA (Inner Product Argument) claims")
194 ->group(advanced_group);
195 };
196
197 const auto add_scheme_option = [&](CLI::App* subcommand) {
198 return subcommand
199 ->add_option(
200 "--scheme, -s",
201 flags.scheme,
202 "The type of proof to be constructed. This can specify a proving system, an accumulation scheme, or a "
203 "particular type of circuit to be constructed and proven for some implicit scheme.")
204 ->envname("BB_SCHEME")
205 ->default_val("ultra_honk")
206 ->check(CLI::IsMember({ "chonk", "avm", "ultra_honk" }).name("is_member"))
207 ->group(advanced_group);
208 };
209
210 const auto add_crs_path_option = [&](CLI::App* subcommand) {
211 return subcommand
212 ->add_option("--crs_path, -c",
213 flags.crs_path,
214 "Path CRS directory. Missing CRS files will be retrieved from the internet.")
215 ->check(CLI::ExistingDirectory)
216 ->group(advanced_group);
217 };
218
219 const auto add_oracle_hash_option = [&](CLI::App* subcommand) {
220 return subcommand
221 ->add_option(
222 "--oracle_hash",
223 flags.oracle_hash_type,
224 "The hash function used by the prover as random oracle standing in for a verifier's challenge "
225 "generation. Poseidon2 is to be used for proofs that are intended to be verified inside of a "
226 "circuit. Keccak is optimized for verification in an Ethereum smart contract, where Keccak "
227 "has a privileged position due to the existence of an EVM precompile. Starknet is optimized "
228 "for verification in a Starknet smart contract, which can be generated using the Garaga library. "
229 "Prefer using --verifier_target instead.")
230 ->check(CLI::IsMember({ "poseidon2", "keccak", "starknet" }).name("is_member"))
231 ->group(advanced_group);
232 };
233
234 const auto add_verifier_target_option = [&](CLI::App* subcommand) {
235 return subcommand
236 ->add_option("--verifier_target, -t",
237 flags.verifier_target,
238 "Target verification environment. Determines hash function and ZK settings.\n"
239 "\n"
240 "Options:\n"
241 " evm Ethereum/Solidity (keccak, ZK)\n"
242 " evm-no-zk Ethereum/Solidity without ZK\n"
243 " noir-recursive Noir circuits (poseidon2, ZK)\n"
244 " noir-recursive-no-zk Noir circuits without ZK\n"
245 " noir-rollup Rollup with IPA (poseidon2, ZK)\n"
246 " noir-rollup-no-zk Rollup without ZK\n"
247 " starknet Starknet via Garaga (ZK)\n"
248 " starknet-no-zk Starknet without ZK")
249 ->envname("BB_VERIFIER_TARGET")
250 ->check(CLI::IsMember({ "evm",
251 "evm-no-zk",
252 "noir-recursive",
253 "noir-recursive-no-zk",
254 "noir-rollup",
255 "noir-rollup-no-zk",
256 "starknet",
257 "starknet-no-zk" }));
258 };
259
260 const auto add_write_vk_flag = [&](CLI::App* subcommand) {
261 return subcommand->add_flag("--write_vk", flags.write_vk, "Write the provided circuit's verification key");
262 };
263
264 const auto remove_zk_option = [&](CLI::App* subcommand) {
265 return subcommand
266 ->add_flag("--disable_zk",
267 flags.disable_zk,
268 "Use a non-zk version of --scheme. Prefer using --verifier_target *-no-zk variants instead.")
269 ->group(advanced_group);
270 };
271
272 const auto add_bytecode_path_option = [&](CLI::App* subcommand) {
273 subcommand->add_option("--bytecode_path, -b", bytecode_path, "Path to ACIR bytecode generated by Noir.")
274 /* ->check(CLI::ExistingFile) OR stdin indicator - */;
275 };
276
277 const auto add_witness_path_option = [&](CLI::App* subcommand) {
278 subcommand->add_option("--witness_path, -w", witness_path, "Path to partial witness generated by Noir.")
279 /* ->check(CLI::ExistingFile) OR stdin indicator - */;
280 };
281
282 const auto add_ivc_inputs_path_options = [&](CLI::App* subcommand) {
283 subcommand
284 ->add_option(
285 "--ivc_inputs_path", ivc_inputs_path, "For IVC, path to input stack with bytecode and witnesses.")
286 ->group(advanced_group);
287 };
288
289 const auto add_public_inputs_path_option = [&](CLI::App* subcommand) {
290 return subcommand->add_option(
291 "--public_inputs_path, -i", public_inputs_path, "Path to public inputs.") /* ->check(CLI::ExistingFile) */;
292 };
293
294 const auto add_proof_path_option = [&](CLI::App* subcommand) {
295 return subcommand->add_option(
296 "--proof_path, -p", proof_path, "Path to a proof.") /* ->check(CLI::ExistingFile) */;
297 };
298
299 const auto add_vk_path_option = [&](CLI::App* subcommand) {
300 return subcommand->add_option("--vk_path, -k", vk_path, "Path to a verification key.")
301 /* ->check(CLI::ExistingFile) */;
302 };
303
304 const auto add_verbose_flag = [&](CLI::App* subcommand) {
305 return subcommand->add_flag("--verbose, --verbose_logging, -v", flags.verbose, "Output all logs to stderr.")
306 ->group(advanced_group);
307 };
308
309 const auto add_debug_flag = [&](CLI::App* subcommand) {
310 return subcommand->add_flag("--debug_logging, -d", flags.debug, "Output debug logs to stderr.")
311 ->group(advanced_group);
312 };
313
314 const auto add_include_gates_per_opcode_flag = [&](CLI::App* subcommand) {
315 return subcommand->add_flag("--include_gates_per_opcode",
316 flags.include_gates_per_opcode,
317 "Include gates_per_opcode in the output of the gates command.");
318 };
319
320 const auto add_slow_low_memory_flag = [&](CLI::App* subcommand) {
321 return subcommand
322 ->add_flag("--slow_low_memory", flags.slow_low_memory, "Enable low memory mode (can be 2x slower or more).")
323 ->group(advanced_group);
324 };
325
326 const auto add_storage_budget_option = [&](CLI::App* subcommand) {
327 return subcommand
328 ->add_option("--storage_budget",
329 flags.storage_budget,
330 "Storage budget for FileBackedMemory (e.g. '500m', '2g'). When exceeded, falls "
331 "back to RAM (requires --slow_low_memory).")
332 ->group(advanced_group);
333 };
334
335 const auto add_vk_policy_option = [&](CLI::App* subcommand) {
336 return subcommand
337 ->add_option("--vk_policy",
338 flags.vk_policy,
339 "Policy for handling verification keys. 'default' uses the provided VK as-is, 'check' "
340 "verifies the provided VK matches the computed VK (throws error on mismatch), 'recompute' "
341 "always ignores the provided VK and treats it as nullptr, 'rewrite' checks the VK and "
342 "rewrites the input file with the correct VK if there's a mismatch (for check command).")
343 ->check(CLI::IsMember({ "default", "check", "recompute", "rewrite" }).name("is_member"))
344 ->group(advanced_group);
345 };
346
347 const auto add_circuit_kind_option = [&](CLI::App* subcommand) {
348 return subcommand
349 ->add_option("--circuit_kind",
350 flags.circuit_kind,
351 "Chonk-only: which Mega flavor to derive the VK against. One of: "
352 "'app' (MegaAppFlavor), 'kernel' (MegaKernelFlavor), 'hiding' (MegaZKFlavor "
353 "for the IVC hiding kernel). Required for `bb write_vk --scheme chonk` — the "
354 "caller must know the kind because it determines the VK shape.")
355 ->check(CLI::IsMember({ "app", "kernel", "hiding" }).name("is_member"))
356 ->group(advanced_group);
357 };
358
359 const auto add_optimized_solidity_verifier_flag = [&](CLI::App* subcommand) {
360 return subcommand->add_flag(
361 "--optimized", flags.optimized_solidity_verifier, "Use the optimized Solidity verifier.");
362 };
363
364 const auto add_output_format_option = [&](CLI::App* subcommand) {
365 return subcommand
366 ->add_option("--output_format",
367 flags.output_format,
368 "Output format for proofs and verification keys: 'binary' (default) or 'json'.\n"
369 "JSON format includes metadata like bb_version, scheme, and verifier_target.")
370 ->check(CLI::IsMember({ "binary", "json" }).name("is_member"));
371 };
372
373 bool print_bench = false;
374 const auto add_print_bench_flag = [&](CLI::App* subcommand) {
375 return subcommand
376 ->add_flag(
377 "--print_bench", print_bench, "Pretty print op counts to standard error in a human-readable format.")
378 ->group(advanced_group);
379 };
380
381 std::string bench_out;
382 const auto add_bench_out_option = [&](CLI::App* subcommand) {
383 return subcommand->add_option("--bench_out", bench_out, "Path to write the op counts in a json.")
384 ->group(advanced_group);
385 };
386 std::string bench_out_hierarchical;
387 const auto add_bench_out_hierarchical_option = [&](CLI::App* subcommand) {
388 return subcommand
389 ->add_option("--bench_out_hierarchical",
390 bench_out_hierarchical,
391 "Path to write the hierarchical benchmark data (op counts and timings with "
392 "parent-child relationships) as json.")
393 ->group(advanced_group);
394 };
395 std::string memory_profile_out;
396 const auto add_memory_profile_out_option = [&](CLI::App* subcommand) {
397 return subcommand
398 ->add_option("--memory_profile_out",
399 memory_profile_out,
400 "Path to write memory profile data (polynomial breakdown by category, RSS "
401 "checkpoints, CRS size) as json.")
402 ->group(advanced_group);
403 };
404
405 std::string trace_out_perfetto;
406 const auto add_trace_out_perfetto_option = [&](CLI::App* subcommand) {
407 return subcommand
408 ->add_option("--trace_out_perfetto",
409 trace_out_perfetto,
410 "Path to write a Chrome Trace Event Format JSON of every instrumented "
411 "BB_BENCH scope (per-call timeline). Drop the file into ui.perfetto.dev "
412 "or chrome://tracing.")
413 ->group(advanced_group);
414 };
415 std::string trace_out_perfetto_aggregate;
416 const auto add_trace_out_perfetto_aggregate_option = [&](CLI::App* subcommand) {
417 return subcommand
418 ->add_option("--trace_out_perfetto_aggregate",
419 trace_out_perfetto_aggregate,
420 "Path to write a synthesized Chrome Trace Event Format JSON derived from the "
421 "aggregate stats. Smaller than --trace_out_perfetto but lossy about individual "
422 "call timing.")
423 ->group(advanced_group);
424 };
425
426 /***************************************************************************************************************
427 * Top-level flags
428 ***************************************************************************************************************/
429 add_verbose_flag(&app);
430 add_debug_flag(&app);
431 add_crs_path_option(&app);
432
433 /***************************************************************************************************************
434 * Builtin flag: --version
435 ***************************************************************************************************************/
436 app.set_version_flag("--version", BB_VERSION, "Print the version string.");
437
438 /***************************************************************************************************************
439 * Flag: --help-extended (register with CLI11)
440 ***************************************************************************************************************/
441 app.add_flag("--help-extended", "Show all options including advanced and Aztec-specific commands.");
442
443 /***************************************************************************************************************
444 * Subcommand: acir_roundtrip
445 ***************************************************************************************************************/
446 std::filesystem::path acir_roundtrip_output_path;
447 CLI::App* acir_roundtrip_cmd =
448 app.add_subcommand("acir_roundtrip",
449 "[Internal testing] Deserialize an ACIR program from bytecode (msgpack), "
450 "re-serialize it back to msgpack, and write it to an output JSON file "
451 "in nargo-compatible format. Functional equivalence should then be verified "
452 "externally (e.g. by proving with the roundtripped bytecode).");
453
454 acir_roundtrip_cmd->group(aztec_internal_group);
455 add_bytecode_path_option(acir_roundtrip_cmd);
456 acir_roundtrip_cmd
457 ->add_option("--output_path,-o", acir_roundtrip_output_path, "Output path for the roundtripped bytecode JSON.")
458 ->required();
459
460 /***************************************************************************************************************
461 * Subcommand: check
462 ***************************************************************************************************************/
463 CLI::App* check = app.add_subcommand(
464 "check",
465 "A debugging tool to quickly check whether a witness satisfies a circuit The "
466 "function constructs the execution trace and iterates through it row by row, applying the "
467 "polynomial relations defining the gate types. For Chonk, we check the VKs in the folding stack.");
468
469 add_help_extended_flag(check);
470 add_scheme_option(check);
471 add_bytecode_path_option(check);
472 add_witness_path_option(check);
473 add_ivc_inputs_path_options(check);
474 add_vk_policy_option(check);
475
476 /***************************************************************************************************************
477 * Subcommand: gates
478 ***************************************************************************************************************/
479 CLI::App* gates = app.add_subcommand("gates",
480 "Construct a circuit from the given bytecode (in particular, expand black box "
481 "functions) and return the gate count information.");
482
483 add_help_extended_flag(gates);
484 add_scheme_option(gates);
485 add_verbose_flag(gates);
486 add_bytecode_path_option(gates);
487 add_include_gates_per_opcode_flag(gates);
488 add_verifier_target_option(gates);
489 add_oracle_hash_option(gates);
490 add_ipa_accumulation_flag(gates);
491
492 /***************************************************************************************************************
493 * Subcommand: prove
494 ***************************************************************************************************************/
495 CLI::App* prove = app.add_subcommand("prove", "Generate a proof.");
496
497 add_help_extended_flag(prove);
498 add_scheme_option(prove);
499 add_bytecode_path_option(prove);
500 add_witness_path_option(prove);
501 add_output_path_option(prove, output_path);
502 add_ivc_inputs_path_options(prove);
503 add_vk_path_option(prove);
504 add_vk_policy_option(prove);
505 add_verbose_flag(prove);
506 add_debug_flag(prove);
507 add_crs_path_option(prove);
508 add_verifier_target_option(prove);
509 add_oracle_hash_option(prove);
510 add_write_vk_flag(prove);
511 add_ipa_accumulation_flag(prove);
512 remove_zk_option(prove);
513 add_slow_low_memory_flag(prove);
514 add_print_bench_flag(prove);
515 add_bench_out_option(prove);
516 add_bench_out_hierarchical_option(prove);
517 add_memory_profile_out_option(prove);
518 add_trace_out_perfetto_option(prove);
519 add_trace_out_perfetto_aggregate_option(prove);
520 add_storage_budget_option(prove);
521 add_output_format_option(prove);
522
523 prove->add_flag("--verify", "Verify the proof natively, resulting in a boolean output. Useful for testing.");
524
525 /***************************************************************************************************************
526 * Subcommand: write_vk
527 ***************************************************************************************************************/
528 CLI::App* write_vk =
529 app.add_subcommand("write_vk",
530 "Write the verification key of a circuit. The circuit is constructed using "
531 "quickly generated but invalid witnesses (which must be supplied in Barretenberg in order "
532 "to expand ACIR black box opcodes), and no proof is constructed.");
533
534 add_help_extended_flag(write_vk);
535 add_scheme_option(write_vk);
536 add_bytecode_path_option(write_vk);
537 add_output_path_option(write_vk, output_path);
538 add_ivc_inputs_path_options(write_vk);
539
540 add_verbose_flag(write_vk);
541 add_debug_flag(write_vk);
542 add_crs_path_option(write_vk);
543 add_verifier_target_option(write_vk);
544 add_oracle_hash_option(write_vk);
545 add_ipa_accumulation_flag(write_vk);
546 remove_zk_option(write_vk);
547 add_output_format_option(write_vk);
548 add_circuit_kind_option(write_vk);
549
550 /***************************************************************************************************************
551 * Subcommand: verify
552 ***************************************************************************************************************/
553 CLI::App* verify = app.add_subcommand("verify", "Verify a proof.");
554
555 add_help_extended_flag(verify);
556 add_public_inputs_path_option(verify);
557 add_proof_path_option(verify);
558 add_vk_path_option(verify);
559
560 add_verbose_flag(verify);
561 add_debug_flag(verify);
562 add_scheme_option(verify);
563 add_crs_path_option(verify);
564 add_verifier_target_option(verify);
565 add_oracle_hash_option(verify);
566 remove_zk_option(verify);
567 add_ipa_accumulation_flag(verify);
568
569 /***************************************************************************************************************
570 * Subcommand: batch_verify
571 ***************************************************************************************************************/
572 std::filesystem::path batch_verify_proofs_dir{ "./proofs" };
573 CLI::App* batch_verify =
574 app.add_subcommand("batch_verify", "Batch-verify multiple Chonk proofs with batched IPA SRS MSMs.");
575
576 add_help_extended_flag(batch_verify);
577 add_scheme_option(batch_verify);
578 batch_verify->add_option("--proofs_dir", batch_verify_proofs_dir, "Directory containing proof_N/vk_N pairs.");
579 add_verbose_flag(batch_verify);
580 add_debug_flag(batch_verify);
581 add_crs_path_option(batch_verify);
582
583 /***************************************************************************************************************
584 * Subcommand: proof_stats
585 ***************************************************************************************************************/
586 CLI::App* proof_stats =
587 app.add_subcommand("proof_stats", "Output proof statistics (compressed size, number of public inputs).");
588
589 add_help_extended_flag(proof_stats);
590 add_scheme_option(proof_stats);
591 add_proof_path_option(proof_stats);
592 add_output_path_option(proof_stats, output_path);
593 add_verbose_flag(proof_stats);
594
595 /***************************************************************************************************************
596 * Subcommand: write_solidity_verifier
597 ***************************************************************************************************************/
598 CLI::App* write_solidity_verifier =
599 app.add_subcommand("write_solidity_verifier",
600 "Write a Solidity smart contract suitable for verifying proofs of circuit "
601 "satisfiability for the circuit with verification key at vk_path. Not all "
602 "hash types are implemented due to efficiency concerns.");
603
604 add_help_extended_flag(write_solidity_verifier);
605 add_scheme_option(write_solidity_verifier);
606 add_vk_path_option(write_solidity_verifier);
607 add_output_path_option(write_solidity_verifier, output_path);
608
609 add_verbose_flag(write_solidity_verifier);
610 add_verifier_target_option(write_solidity_verifier);
611 remove_zk_option(write_solidity_verifier);
612 add_crs_path_option(write_solidity_verifier);
613 add_optimized_solidity_verifier_flag(write_solidity_verifier);
614
615 std::filesystem::path avm_inputs_path{ "./target/avm_inputs.bin" };
616 const auto add_avm_inputs_option = [&](CLI::App* subcommand) {
617 return subcommand->add_option("--avm-inputs", avm_inputs_path, "");
618 };
619 std::filesystem::path avm_public_inputs_path{ "./target/avm_public_inputs.bin" };
620 const auto add_avm_public_inputs_option = [&](CLI::App* subcommand) {
621 return subcommand->add_option("--avm-public-inputs", avm_public_inputs_path, "");
622 };
623
624 /***************************************************************************************************************
625 * Subcommand: avm_simulate
626 ***************************************************************************************************************/
627 CLI::App* avm_simulate_command = app.add_subcommand("avm_simulate", "Simulate AVM execution.");
628 avm_simulate_command->group(aztec_internal_group);
629 add_verbose_flag(avm_simulate_command);
630 add_debug_flag(avm_simulate_command);
631 add_avm_inputs_option(avm_simulate_command);
632
633 /***************************************************************************************************************
634 * Subcommand: avm_prove
635 ***************************************************************************************************************/
636 CLI::App* avm_prove_command = app.add_subcommand("avm_prove", "Generate an AVM proof.");
637 avm_prove_command->group(aztec_internal_group);
638 add_verbose_flag(avm_prove_command);
639 add_debug_flag(avm_prove_command);
640 add_crs_path_option(avm_prove_command);
641 std::filesystem::path avm_prove_output_path{ "./proofs" };
642 add_output_path_option(avm_prove_command, avm_prove_output_path);
643 add_avm_inputs_option(avm_prove_command);
644
645 /***************************************************************************************************************
646 * Subcommand: avm_write_vk
647 ***************************************************************************************************************/
648 CLI::App* avm_write_vk_command = app.add_subcommand("avm_write_vk", "Write AVM verification key.");
649 avm_write_vk_command->group(aztec_internal_group);
650 add_verbose_flag(avm_write_vk_command);
651 add_debug_flag(avm_write_vk_command);
652 add_crs_path_option(avm_write_vk_command);
653 std::filesystem::path avm_write_vk_output_path{ "./keys" };
654 add_output_path_option(avm_write_vk_command, avm_write_vk_output_path);
655
656 /***************************************************************************************************************
657 * Subcommand: avm_check_circuit
658 ***************************************************************************************************************/
659 CLI::App* avm_check_circuit_command = app.add_subcommand("avm_check_circuit", "Check AVM circuit satisfiability.");
660 avm_check_circuit_command->group(aztec_internal_group);
661 add_verbose_flag(avm_check_circuit_command);
662 add_debug_flag(avm_check_circuit_command);
663 add_crs_path_option(avm_check_circuit_command);
664 add_avm_inputs_option(avm_check_circuit_command);
665
666 /***************************************************************************************************************
667 * Subcommand: avm_verify
668 ***************************************************************************************************************/
669 CLI::App* avm_verify_command = app.add_subcommand("avm_verify", "Verify an AVM proof.");
670 avm_verify_command->group(aztec_internal_group);
671 add_verbose_flag(avm_verify_command);
672 add_debug_flag(avm_verify_command);
673 add_crs_path_option(avm_verify_command);
674 add_avm_public_inputs_option(avm_verify_command);
675 add_proof_path_option(avm_verify_command);
676
677 /***************************************************************************************************************
678 * Subcommand: aztec_process_artifact
679 ***************************************************************************************************************/
680 CLI::App* aztec_process = app.add_subcommand(
681 "aztec_process",
682 "Process Aztec contract artifacts: transpile and generate verification keys for all private functions.\n"
683 "If input is a directory (and no output specified), recursively processes all artifacts found in the "
684 "directory.\n"
685 "Multiple -i flags can be specified when no -o flag is present for parallel processing.");
686 aztec_process->group(aztec_internal_group);
687
688 std::vector<std::string> artifact_input_paths;
689 std::string artifact_output_path;
690 bool force_regenerate = false;
691
692 aztec_process->add_option("-i,--input",
693 artifact_input_paths,
694 "Input artifact JSON path or directory to search (optional, defaults to current "
695 "directory). Can be specified multiple times when no -o flag is present.");
696 aztec_process->add_option(
697 "-o,--output",
698 artifact_output_path,
699 "Output artifact JSON path (optional, same as input if not specified). Cannot be used with multiple -i flags.");
700 aztec_process->add_flag("-f,--force", force_regenerate, "Force regeneration of verification keys");
701 add_verbose_flag(aztec_process);
702 add_debug_flag(aztec_process);
703
704 /***************************************************************************************************************
705 * Subcommand: aztec_process cache_paths
706 ***************************************************************************************************************/
707 CLI::App* cache_paths_command =
708 aztec_process->add_subcommand("cache_paths",
709 "Output cache paths for verification keys in an artifact.\n"
710 "Format: <hash>:<cache_path>:<function_name> (one per line).");
711
712 std::string cache_paths_input;
713 cache_paths_command->add_option("input", cache_paths_input, "Input artifact JSON path (required).")->required();
714 add_verbose_flag(cache_paths_command);
715 add_debug_flag(cache_paths_command);
716
717 /***************************************************************************************************************
718 * Subcommand: msgpack
719 ***************************************************************************************************************/
720 CLI::App* msgpack_command = app.add_subcommand("msgpack", "Msgpack API interface.");
721
722 // Subcommand: msgpack schema
723 CLI::App* msgpack_schema_command =
724 msgpack_command->add_subcommand("schema", "Output a msgpack schema encoded as JSON to stdout.");
725 add_verbose_flag(msgpack_schema_command);
726
727 // Subcommand: msgpack curve_constants
728 CLI::App* msgpack_curve_constants_command =
729 msgpack_command->add_subcommand("curve_constants", "Output curve constants as msgpack to stdout.");
730 add_verbose_flag(msgpack_curve_constants_command);
731
732 // Subcommand: msgpack run
733 CLI::App* msgpack_run_command =
734 msgpack_command->add_subcommand("run", "Execute msgpack API commands from stdin or file.");
735 add_verbose_flag(msgpack_run_command);
736 std::string msgpack_input_file;
737 msgpack_run_command->add_option(
738 "-i,--input", msgpack_input_file, "Input file containing msgpack buffers (defaults to stdin)");
739 size_t request_ring_size = 1024 * 1024; // 1MB default
740 msgpack_run_command
741 ->add_option(
742 "--request-ring-size", request_ring_size, "Request ring buffer size for shared memory IPC (default: 1MB)")
743 ->check(CLI::PositiveNumber);
744 size_t response_ring_size = 1024 * 1024; // 1MB default
745 msgpack_run_command
746 ->add_option("--response-ring-size",
747 response_ring_size,
748 "Response ring buffer size for shared memory IPC (default: 1MB)")
749 ->check(CLI::PositiveNumber);
750 int max_clients = 1;
751 msgpack_run_command
752 ->add_option("--max-clients",
753 max_clients,
754 "Maximum concurrent clients for socket IPC servers (default: 1, only used for .sock files)")
755 ->check(CLI::PositiveNumber);
756
757 /***************************************************************************************************************
758 * Build the CLI11 App
759 ***************************************************************************************************************/
760
761 CLI11_PARSE(app, argc, argv);
762
763 // Handle --help-extended: print help and exit
764 if (show_extended_help) {
765 std::cout << app.help() << '\n';
766 return 0;
767 }
768
769 // Apply verifier_target to derive oracle_hash_type, disable_zk, and ipa_accumulation
770 // This only applies when verifier_target is explicitly set
771 if (!flags.verifier_target.empty()) {
772 // Check for conflicting flags - verifier_target should not be combined with low-level flags
773 // We need to check the active subcommand for these options
774 CLI::App* active_sub = find_deepest_subcommand(&app);
775 if (active_sub != nullptr) {
776 // Helper to safely get option count (returns 0 if option doesn't exist)
777 auto get_option_count = [](CLI::App* sub, const std::string& name) -> size_t {
778 try {
779 return sub->get_option(name)->count();
780 } catch (const CLI::OptionNotFound&) {
781 return 0;
782 }
783 };
784
785 if (get_option_count(active_sub, "--oracle_hash") > 0) {
786 throw_or_abort("Cannot use --verifier_target with --oracle_hash. "
787 "The --verifier_target flag sets oracle_hash automatically.");
788 }
789 if (get_option_count(active_sub, "--disable_zk") > 0) {
790 throw_or_abort("Cannot use --verifier_target with --disable_zk. "
791 "Use a '-no-zk' variant of --verifier_target instead (e.g., 'evm-no-zk').");
792 }
793 if (get_option_count(active_sub, "--ipa_accumulation") > 0) {
794 throw_or_abort("Cannot use --verifier_target with --ipa_accumulation. "
795 "Use '--verifier_target noir-rollup' for IPA accumulation.");
796 }
797 }
798
799 // Map verifier_target to underlying flags
800 if (flags.verifier_target == "evm") {
801 flags.oracle_hash_type = "keccak";
802 } else if (flags.verifier_target == "evm-no-zk") {
803 flags.oracle_hash_type = "keccak";
804 flags.disable_zk = true;
805 } else if (flags.verifier_target == "noir-recursive") {
806 flags.oracle_hash_type = "poseidon2";
807 } else if (flags.verifier_target == "noir-recursive-no-zk") {
808 flags.oracle_hash_type = "poseidon2";
809 flags.disable_zk = true;
810 } else if (flags.verifier_target == "noir-rollup") {
811 flags.oracle_hash_type = "poseidon2";
812 flags.ipa_accumulation = true;
813 } else if (flags.verifier_target == "noir-rollup-no-zk") {
814 flags.oracle_hash_type = "poseidon2";
815 flags.ipa_accumulation = true;
816 flags.disable_zk = true;
817 } else if (flags.verifier_target == "starknet") {
818 flags.oracle_hash_type = "starknet";
819 } else if (flags.verifier_target == "starknet-no-zk") {
820 flags.oracle_hash_type = "starknet";
821 flags.disable_zk = true;
822 }
823 vinfo("verifier_target '",
824 flags.verifier_target,
825 "' -> oracle_hash_type='",
826 flags.oracle_hash_type,
827 "', disable_zk=",
828 flags.disable_zk,
829 ", ipa_accumulation=",
830 flags.ipa_accumulation);
831 }
832
833 // Immediately after parsing, we can init the global CRS factory. Note this does not yet read or download any
834 // points; that is done on-demand.
835 srs::init_net_crs_factory(flags.crs_path);
836 if ((prove->parsed() || write_vk->parsed()) && output_path != "-") {
837 // If writing to an output folder, make sure it exists.
838 std::filesystem::create_directories(output_path);
839 }
840 if (flags.debug) {
842 } else if (flags.verbose) {
844 }
845 slow_low_memory = flags.slow_low_memory;
846#if !defined(__wasm__) || defined(ENABLE_WASM_BENCH)
847 if (!flags.storage_budget.empty()) {
848 storage_budget = parse_size_string(flags.storage_budget);
849 }
850 if (!memory_profile_out.empty()) {
852 vinfo("Memory profiling enabled via --memory_profile_out");
853 }
854 if (print_bench || !bench_out.empty() || !bench_out_hierarchical.empty() || !trace_out_perfetto.empty() ||
855 !trace_out_perfetto_aggregate.empty()) {
857 vinfo("BB_BENCH enabled via --print_bench / --bench_out / --trace_out_perfetto");
858 }
859 if (!trace_out_perfetto.empty()) {
861 vinfo("Per-call BB_BENCH event capture enabled via --trace_out_perfetto");
862 }
863#endif
864
866 info("Scheme is: ", flags.scheme, ", num threads: ", get_num_cpus());
867 if (CLI::App* deepest = find_deepest_subcommand(&app)) {
869 }
870
871 // TODO(AD): it is inflexible that Chonk shares an API command (prove) with UH this way. The base API class is a
872 // poor fit. It would be better to have a separate handling for each scheme with subcommands to prove.
873 const auto execute_non_prove_command = [&](API& api) {
874 if (check->parsed()) {
875 api.check(flags, bytecode_path, witness_path);
876 return 0;
877 }
878 if (gates->parsed()) {
879 api.gates(flags, bytecode_path);
880 return 0;
881 }
882 if (write_vk->parsed()) {
883 api.write_vk(flags, bytecode_path, output_path);
884 return 0;
885 }
886 if (verify->parsed()) {
887 const bool verified = api.verify(flags, public_inputs_path, proof_path, vk_path);
888 vinfo("verified: ", verified);
889 return verified ? 0 : 1;
890 }
891 if (write_solidity_verifier->parsed()) {
892 // Validate that verifier_target is compatible with Solidity verifier
893 if (!flags.verifier_target.empty() && flags.verifier_target != "evm" &&
894 flags.verifier_target != "evm-no-zk") {
895 throw_or_abort("write_solidity_verifier requires --verifier_target to be 'evm' or 'evm-no-zk', got '" +
896 flags.verifier_target + "'");
897 }
898 api.write_solidity_verifier(flags, output_path, vk_path);
899 return 0;
900 }
901 auto subcommands = app.get_subcommands();
902 const std::string message = std::string("No handler for subcommand ") + subcommands[0]->get_name();
903 throw_or_abort(message);
904 return 1;
905 };
906
907 try {
908 // ACIR roundtrip (internal testing)
909 if (acir_roundtrip_cmd->parsed()) {
910 acir_roundtrip(bytecode_path, acir_roundtrip_output_path);
911 return 0;
912 }
913
914 // MSGPACK
915 if (msgpack_schema_command->parsed()) {
917 return 0;
918 }
919 if (msgpack_curve_constants_command->parsed()) {
921 return 0;
922 }
923 if (msgpack_run_command->parsed()) {
924 return execute_msgpack_run(msgpack_input_file, max_clients, request_ring_size, response_ring_size);
925 }
926 if (aztec_process->parsed()) {
927#ifdef __wasm__
928 throw_or_abort("Aztec artifact processing is not supported in WASM builds.");
929#else
930 // Handle cache_paths subcommand
931 if (cache_paths_command->parsed()) {
932 return get_cache_paths(cache_paths_input) ? 0 : 1;
933 }
934
935 // Check for invalid combination of multiple inputs with output path
936 if (!artifact_output_path.empty() && artifact_input_paths.size() > 1) {
937 throw_or_abort("Cannot specify --output when multiple --input flags are provided.");
938 }
939
940 // Default to current directory if no inputs specified
941 if (artifact_input_paths.empty()) {
942 artifact_input_paths.push_back(".");
943 }
944
945 // Handle multiple inputs (process in parallel)
946 if (artifact_input_paths.size() > 1) {
947 // Validate all inputs are files, not directories
948 for (const auto& input : artifact_input_paths) {
949 if (std::filesystem::is_directory(input)) {
950 throw_or_abort("When using multiple --input flags, all inputs must be files, not directories.");
951 }
952 }
953
954 // Process all artifacts in parallel
955 std::atomic<bool> all_success = true;
956 std::vector<std::string> failures;
957 std::mutex failures_mutex;
958
959 parallel_for(artifact_input_paths.size(), [&](size_t i) {
960 const auto& input = artifact_input_paths[i];
961 if (!process_aztec_artifact(input, input, force_regenerate)) {
962 all_success = false;
963 std::lock_guard<std::mutex> lock(failures_mutex);
964 failures.push_back(input);
965 }
966 });
967
968 if (!all_success) {
969 info("Failed to process ", failures.size(), " artifact(s)");
970 return 1;
971 }
972 info("Successfully processed ", artifact_input_paths.size(), " artifact(s)");
973 return 0;
974 }
975
976 // Single input case
977 std::string input = artifact_input_paths[0];
978
979 // Check if input is a directory
980 if (std::filesystem::is_directory(input)) {
981 // If output specified for directory input, that's an error
982 if (!artifact_output_path.empty()) {
984 "Cannot specify --output when input is a directory. Artifacts are updated in-place.");
985 }
986 // Recursively process all artifacts in directory
987 return process_all_artifacts(input, force_regenerate) ? 0 : 1;
988 }
989
990 // Input is a file, process single artifact
991 std::string output = artifact_output_path.empty() ? input : artifact_output_path;
992 return process_aztec_artifact(input, output, force_regenerate) ? 0 : 1;
993#endif
994 }
995 // AVM - functions will throw at runtime if not supported (via stub module)
996 else if (avm_prove_command->parsed()) {
997 // This outputs both files: proof and vk, under the given directory.
998 avm_prove(avm_inputs_path, avm_prove_output_path);
999 } else if (avm_check_circuit_command->parsed()) {
1000 avm_check_circuit(avm_inputs_path);
1001 } else if (avm_verify_command->parsed()) {
1002 return avm_verify(proof_path, avm_public_inputs_path) ? 0 : 1;
1003 } else if (avm_simulate_command->parsed()) {
1004 avm_simulate(avm_inputs_path);
1005 } else if (avm_write_vk_command->parsed()) {
1006 avm_write_verification_key(avm_write_vk_output_path);
1007 } else if (flags.scheme == "chonk") {
1008 ChonkAPI api;
1009 if (prove->parsed()) {
1010 if (!std::filesystem::exists(ivc_inputs_path)) {
1011 throw_or_abort("The prove command for Chonk expect a valid file passed with --ivc_inputs_path "
1012 "<ivc-inputs.msgpack> (default ./ivc-inputs.msgpack)");
1013 }
1014 api.prove(flags, ivc_inputs_path, output_path);
1015#if !defined(__wasm__) || defined(ENABLE_WASM_BENCH)
1016 if (print_bench) {
1017 vinfo("Printing BB_BENCH results...");
1020 }
1021 if (!bench_out.empty()) {
1022 std::ofstream file(bench_out);
1024 }
1025 if (!bench_out_hierarchical.empty()) {
1026 std::ofstream file(bench_out_hierarchical);
1028 }
1029 if (!trace_out_perfetto.empty()) {
1030 std::ofstream file(trace_out_perfetto);
1032 vinfo("Perfetto per-call trace written to ", trace_out_perfetto);
1033 }
1034 if (!trace_out_perfetto_aggregate.empty()) {
1035 std::ofstream file(trace_out_perfetto_aggregate);
1037 vinfo("Perfetto aggregate trace written to ", trace_out_perfetto_aggregate);
1038 }
1039#endif
1040 if (!memory_profile_out.empty()) {
1041 std::ofstream file(memory_profile_out);
1043 vinfo("Memory profile written to ", memory_profile_out);
1044 }
1045 return 0;
1046 }
1047 if (check->parsed()) {
1048 if (!std::filesystem::exists(ivc_inputs_path)) {
1049 throw_or_abort("The check command for Chonk expect a valid file passed with --ivc_inputs_path "
1050 "<ivc-inputs.msgpack> (default ./ivc-inputs.msgpack)");
1051 }
1052 return api.check_precomputed_vks(flags, ivc_inputs_path) ? 0 : 1;
1053 }
1054 if (batch_verify->parsed()) {
1055 const bool verified = api.batch_verify(flags, batch_verify_proofs_dir);
1056 vinfo("batch verified: ", verified);
1057 return verified ? 0 : 1;
1058 }
1059 if (proof_stats->parsed()) {
1060 api.proof_stats(proof_path, output_path);
1061 return 0;
1062 }
1063 return execute_non_prove_command(api);
1064 } else if (flags.scheme == "ultra_honk") {
1065 UltraHonkAPI api;
1066 if (prove->parsed()) {
1067 api.prove(flags, bytecode_path, witness_path, vk_path, output_path);
1068#if !defined(__wasm__) || defined(ENABLE_WASM_BENCH)
1069 if (print_bench) {
1071 }
1072 if (!bench_out.empty()) {
1073 std::ofstream file(bench_out);
1075 }
1076 if (!bench_out_hierarchical.empty()) {
1077 std::ofstream file(bench_out_hierarchical);
1079 }
1080 if (!trace_out_perfetto.empty()) {
1081 std::ofstream file(trace_out_perfetto);
1083 vinfo("Perfetto per-call trace written to ", trace_out_perfetto);
1084 }
1085 if (!trace_out_perfetto_aggregate.empty()) {
1086 std::ofstream file(trace_out_perfetto_aggregate);
1088 vinfo("Perfetto aggregate trace written to ", trace_out_perfetto_aggregate);
1089 }
1090#endif
1091 return 0;
1092 }
1093 return execute_non_prove_command(api);
1094 } else {
1095 throw_or_abort("No match for API command");
1096 return 1;
1097 }
1098 } catch (std::runtime_error const& err) {
1099#ifndef BB_NO_EXCEPTIONS
1100 std::cerr << err.what() << std::endl;
1101 return 1;
1102#endif
1103 }
1104 return 0;
1105}
1106} // namespace bb
size_t parse_size_string(const std::string &size_str)
bool slow_low_memory
size_t storage_budget
UltraHonk-specific command definitions for the Barretenberg RPC API.
Definition api.hpp:7
CLI API for Chonk (Aztec's client-side proving).
Definition api_chonk.hpp:33
void proof_stats(const std::filesystem::path &proof_path, const std::filesystem::path &output_path)
Output proof statistics: compressed proof and its size.
bool batch_verify(const Flags &flags, const std::filesystem::path &proofs_dir)
Batch-verify multiple Chonk proofs from a directory of proof_N/vk_N pairs.
void prove(const Flags &flags, const std::filesystem::path &input_path, const std::filesystem::path &output_dir)
Main production entry point: generate a Chonk proof from private execution steps.
Definition api_chonk.cpp:64
bool check_precomputed_vks(const Flags &flags, const std::filesystem::path &input_path)
Validate that precomputed VKs in ivc-inputs.msgpack match computed VKs.
void prove(const Flags &flags, const std::filesystem::path &bytecode_path, const std::filesystem::path &witness_path, const std::filesystem::path &vk_path, const std::filesystem::path &output_dir)
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Definition group.hpp:38
#define CLI11_PARSE(app,...)
#define info(...)
Definition log.hpp:93
#define vinfo(...)
Definition log.hpp:94
Programmatic interface for generating msgpack-encoded curve constants.
LogLevel bb_log_level
Definition log.cpp:9
std::string get_msgpack_schema_as_json()
bool use_memory_profile
MemoryProfile GLOBAL_MEMORY_PROFILE
GlobalBenchStatsContainer GLOBAL_BENCH_STATS
Definition bb_bench.cpp:822
std::atomic< bool > capture_per_call_events
Definition bb_bench.cpp:177
bool use_bb_bench
Definition bb_bench.cpp:175
void init_net_crs_factory(const std::filesystem::path &path)
std::filesystem::path bb_crs_path()
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void print_subcommand_options(const CLI::App *sub)
Definition cli.cpp:78
void avm_simulate(const std::filesystem::path &inputs_path)
Simulates an public transaction.
Definition api_avm.cpp:78
int execute_msgpack_run(const std::string &msgpack_input_file, int max_clients, size_t request_ring_size, size_t response_ring_size)
Execute msgpack run command.
int parse_and_run_cli_command(int argc, char *argv[])
Parse command line arguments and run the corresponding command.
Definition cli.cpp:109
bool process_all_artifacts(const std::string &search_path, bool force)
Process all discovered contract artifacts in a directory tree.
bool get_cache_paths(const std::string &input_path)
Get cache paths for all verification keys in an artifact.
void write_curve_constants_msgpack_to_stdout()
Write msgpack-encoded curve constants to stdout.
bool process_aztec_artifact(const std::string &input_path, const std::string &output_path, bool force)
Process Aztec contract artifacts: transpile and generate verification keys.
size_t get_num_cpus()
Definition thread.cpp:34
void acir_roundtrip(const std::filesystem::path &bytecode_path, const std::filesystem::path &output_path)
Deserialize an ACIR program from bytecode (msgpack), re-serialize it, and write the result to an outp...
Definition api_acir.cpp:14
bool avm_verify(const std::filesystem::path &proof_path, const std::filesystem::path &public_inputs_path)
Verifies an avm proof and writes the result to stdout.
Definition api_avm.cpp:65
void print_active_subcommands(const CLI::App &app, const std::string &prefix="bb command: ")
Definition cli.cpp:49
void avm_write_verification_key(const std::filesystem::path &output_path)
Writes an avm (incomplete) verification key to a file.
Definition api_avm.cpp:90
void avm_prove(const std::filesystem::path &inputs_path, const std::filesystem::path &output_path)
Writes an avm proof to a file.
Definition api_avm.cpp:31
const char * BB_VERSION
Definition version.hpp:14
void avm_check_circuit(const std::filesystem::path &inputs_path)
Stub - throws runtime error if called.
Definition api_avm.cpp:53
const bool avm_enabled
Definition api_avm.cpp:15
CLI::App * find_deepest_subcommand(CLI::App *app)
Definition cli.cpp:63
void parallel_for(size_t num_iterations, const std::function< void(size_t)> &func)
Definition thread.cpp:112
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string name
std::string scheme
Definition api.hpp:18
void print_aggregate_counts_hierarchical(std::ostream &) const
Definition bb_bench.cpp:548
void serialize_trace_events_json(std::ostream &) const
Definition bb_bench.cpp:406
void print_aggregate_counts(std::ostream &, size_t) const
Definition bb_bench.cpp:303
void serialize_aggregate_data_json(std::ostream &) const
Definition bb_bench.cpp:342
void serialize_aggregate_trace_json(std::ostream &) const
Definition bb_bench.cpp:474
void serialize_json(std::ostream &os) const
void throw_or_abort(std::string const &err)