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