108 std::string name =
"Barretenberg\nYour favo(u)rite zkSNARK library written in C++, a perfectly good computer "
109 "programming language.";
113 name +=
"\nAztec Virtual Machine (AVM): enabled";
115 name +=
"\nAztec Virtual Machine (AVM): disabled";
117#ifdef ENABLE_AVM_TRANSPILER
118 name +=
"\nAVM Transpiler: enabled";
120 name +=
"\nAVM Transpiler: disabled";
122#ifdef STARKNET_GARAGA_FLAVORS
123 name +=
"\nStarknet Garaga Extensions: enabled";
125 name +=
"\nStarknet Garaga Extensions: disabled";
127 CLI::App app{ name };
128 argv = app.ensure_utf8(argv);
138 app.require_subcommand(0, 1);
142 std::filesystem::path bytecode_path{
"./target/program.json" };
143 std::filesystem::path witness_path{
"./target/witness.gz" };
144 std::filesystem::path ivc_inputs_path{
"./ivc-inputs.msgpack" };
145 std::filesystem::path output_path{
148 std::filesystem::path public_inputs_path{
"./target/public_inputs" };
149 std::filesystem::path proof_path{
"./target/proof" };
150 std::filesystem::path vk_path{
"./target/vk" };
152 flags.oracle_hash_type =
"poseidon2";
154 flags.include_gates_per_opcode =
false;
155 const auto add_output_path_option = [&](CLI::App* subcommand,
auto& _output_path) {
156 return subcommand->add_option(
"--output_path, -o",
158 "Directory to write files or path of file to write, depending on subcommand.");
165 const auto add_ipa_accumulation_flag = [&](CLI::App* subcommand) {
166 return subcommand->add_flag(
167 "--ipa_accumulation", flags.ipa_accumulation,
"Accumulate/Aggregate IPA (Inner Product Argument) claims");
170 const auto add_scheme_option = [&](CLI::App* subcommand) {
175 "The type of proof to be constructed. This can specify a proving system, an accumulation scheme, or a "
176 "particular type of circuit to be constructed and proven for some implicit scheme.")
177 ->envname(
"BB_SCHEME")
178 ->default_val(
"ultra_honk")
179 ->check(CLI::IsMember({
"chonk",
"avm",
"ultra_honk" }).name(
"is_member"));
182 const auto add_crs_path_option = [&](CLI::App* subcommand) {
184 ->add_option(
"--crs_path, -c",
186 "Path CRS directory. Missing CRS files will be retrieved from the internet.")
187 ->check(CLI::ExistingDirectory);
190 const auto add_oracle_hash_option = [&](CLI::App* subcommand) {
194 flags.oracle_hash_type,
195 "The hash function used by the prover as random oracle standing in for a verifier's challenge "
196 "generation. Poseidon2 is to be used for proofs that are intended to be verified inside of a "
197 "circuit. Keccak is optimized for verification in an Ethereum smart contract, where Keccak "
198 "has a privileged position due to the existence of an EVM precompile. Starknet is optimized "
199 "for verification in a Starknet smart contract, which can be generated using the Garaga library.")
200 ->check(CLI::IsMember({
"poseidon2",
"keccak",
"starknet" }).name(
"is_member"));
203 const auto add_write_vk_flag = [&](CLI::App* subcommand) {
204 return subcommand->add_flag(
"--write_vk", flags.write_vk,
"Write the provided circuit's verification key");
207 const auto remove_zk_option = [&](CLI::App* subcommand) {
208 return subcommand->add_flag(
"--disable_zk",
210 "Use a non-zk version of --scheme. This flag is set to false by default.");
213 const auto add_bytecode_path_option = [&](CLI::App* subcommand) {
214 subcommand->add_option(
"--bytecode_path, -b", bytecode_path,
"Path to ACIR bytecode generated by Noir.")
218 const auto add_witness_path_option = [&](CLI::App* subcommand) {
219 subcommand->add_option(
"--witness_path, -w", witness_path,
"Path to partial witness generated by Noir.")
223 const auto add_ivc_inputs_path_options = [&](CLI::App* subcommand) {
224 subcommand->add_option(
225 "--ivc_inputs_path", ivc_inputs_path,
"For IVC, path to input stack with bytecode and witnesses.")
229 const auto add_public_inputs_path_option = [&](CLI::App* subcommand) {
230 return subcommand->add_option(
231 "--public_inputs_path, -i", public_inputs_path,
"Path to public inputs.") ;
234 const auto add_proof_path_option = [&](CLI::App* subcommand) {
235 return subcommand->add_option(
236 "--proof_path, -p", proof_path,
"Path to a proof.") ;
239 const auto add_vk_path_option = [&](CLI::App* subcommand) {
240 return subcommand->add_option(
"--vk_path, -k", vk_path,
"Path to a verification key.")
244 const auto add_verifier_type_option = [&](CLI::App* subcommand) {
246 ->add_option(
"--verifier_type",
248 "Is a verification key for use a standalone single circuit verifier (e.g. a SNARK or folding "
249 "recursive verifier) or is it for an ivc verifier? `standalone` produces a verification key "
250 "is sufficient for verifying proofs about a single circuit (including the non-encsapsulated "
251 "use case where an IVC scheme is manually constructed via recursive UltraHonk proof "
252 "verification). `standalone_hiding` is similar to `standalone` but is used for the last step "
253 "where the structured trace is not utilized. `ivc` produces a verification key for verifying "
254 "the stack of run though a dedicated ivc verifier class (currently the only option is the "
256 ->check(CLI::IsMember({
"standalone",
"standalone_hiding",
"ivc" }).name(
"is_member"));
259 const auto add_verbose_flag = [&](CLI::App* subcommand) {
260 return subcommand->add_flag(
"--verbose, --verbose_logging, -v", flags.verbose,
"Output all logs to stderr.");
263 const auto add_debug_flag = [&](CLI::App* subcommand) {
264 return subcommand->add_flag(
"--debug_logging, -d", flags.debug,
"Output debug logs to stderr.");
267 const auto add_include_gates_per_opcode_flag = [&](CLI::App* subcommand) {
268 return subcommand->add_flag(
"--include_gates_per_opcode",
269 flags.include_gates_per_opcode,
270 "Include gates_per_opcode in the output of the gates command.");
273 const auto add_slow_low_memory_flag = [&](CLI::App* subcommand) {
274 return subcommand->add_flag(
275 "--slow_low_memory", flags.slow_low_memory,
"Enable low memory mode (can be 2x slower or more).");
278 const auto add_storage_budget_option = [&](CLI::App* subcommand) {
279 return subcommand->add_option(
"--storage_budget",
280 flags.storage_budget,
281 "Storage budget for FileBackedMemory (e.g. '500m', '2g'). When exceeded, falls "
282 "back to RAM (requires --slow_low_memory).");
285 const auto add_vk_policy_option = [&](CLI::App* subcommand) {
287 ->add_option(
"--vk_policy",
289 "Policy for handling verification keys. 'default' uses the provided VK as-is, 'check' "
290 "verifies the provided VK matches the computed VK (throws error on mismatch), 'recompute' "
291 "always ignores the provided VK and treats it as nullptr, 'rewrite' checks the VK and "
292 "rewrites the input file with the correct VK if there's a mismatch (for check command).")
293 ->check(CLI::IsMember({
"default",
"check",
"recompute",
"rewrite" }).name(
"is_member"));
296 const auto add_optimized_solidity_verifier_flag = [&](CLI::App* subcommand) {
297 return subcommand->add_flag(
298 "--optimized", flags.optimized_solidity_verifier,
"Use the optimized Solidity verifier.");
301 bool print_bench =
false;
302 const auto add_print_bench_flag = [&](CLI::App* subcommand) {
303 return subcommand->add_flag(
304 "--print_bench", print_bench,
"Pretty print op counts to standard error in a human-readable format.");
307 std::string bench_out;
308 const auto add_bench_out_option = [&](CLI::App* subcommand) {
309 return subcommand->add_option(
"--bench_out", bench_out,
"Path to write the op counts in a json.");
311 std::string bench_out_hierarchical;
312 const auto add_bench_out_hierarchical_option = [&](CLI::App* subcommand) {
313 return subcommand->add_option(
"--bench_out_hierarchical",
314 bench_out_hierarchical,
315 "Path to write the hierarchical benchmark data (op counts and timings with "
316 "parent-child relationships) as json.");
322 add_verbose_flag(&app);
323 add_debug_flag(&app);
324 add_crs_path_option(&app);
329 app.set_version_flag(
"--version",
BB_VERSION,
"Print the version string.");
334 CLI::App* check = app.add_subcommand(
336 "A debugging tool to quickly check whether a witness satisfies a circuit The "
337 "function constructs the execution trace and iterates through it row by row, applying the "
338 "polynomial relations defining the gate types. For Chonk, we check the VKs in the folding stack.");
340 add_scheme_option(check);
341 add_bytecode_path_option(check);
342 add_witness_path_option(check);
343 add_ivc_inputs_path_options(check);
344 add_vk_policy_option(check);
349 CLI::App* gates = app.add_subcommand(
"gates",
350 "Construct a circuit from the given bytecode (in particular, expand black box "
351 "functions) and return the gate count information.");
353 add_scheme_option(gates);
354 add_verbose_flag(gates);
355 add_bytecode_path_option(gates);
356 add_include_gates_per_opcode_flag(gates);
357 add_oracle_hash_option(gates);
358 add_ipa_accumulation_flag(gates);
363 CLI::App* prove = app.add_subcommand(
"prove",
"Generate a proof.");
365 add_scheme_option(prove);
366 add_bytecode_path_option(prove);
367 add_witness_path_option(prove);
368 add_output_path_option(prove, output_path);
369 add_ivc_inputs_path_options(prove);
370 add_vk_path_option(prove);
371 add_vk_policy_option(prove);
372 add_verbose_flag(prove);
373 add_debug_flag(prove);
374 add_crs_path_option(prove);
375 add_oracle_hash_option(prove);
376 add_write_vk_flag(prove);
377 add_ipa_accumulation_flag(prove);
378 remove_zk_option(prove);
379 add_slow_low_memory_flag(prove);
380 add_print_bench_flag(prove);
381 add_bench_out_option(prove);
382 add_bench_out_hierarchical_option(prove);
383 add_storage_budget_option(prove);
385 prove->add_flag(
"--verify",
"Verify the proof natively, resulting in a boolean output. Useful for testing.");
391 app.add_subcommand(
"write_vk",
392 "Write the verification key of a circuit. The circuit is constructed using "
393 "quickly generated but invalid witnesses (which must be supplied in Barretenberg in order "
394 "to expand ACIR black box opcodes), and no proof is constructed.");
396 add_scheme_option(write_vk);
397 add_bytecode_path_option(write_vk);
398 add_output_path_option(write_vk, output_path);
399 add_ivc_inputs_path_options(write_vk);
401 add_verbose_flag(write_vk);
402 add_debug_flag(write_vk);
403 add_crs_path_option(write_vk);
404 add_oracle_hash_option(write_vk);
405 add_ipa_accumulation_flag(write_vk);
406 add_verifier_type_option(write_vk)->default_val(
"standalone");
407 remove_zk_option(write_vk);
412 CLI::App* verify = app.add_subcommand(
"verify",
"Verify a proof.");
414 add_public_inputs_path_option(verify);
415 add_proof_path_option(verify);
416 add_vk_path_option(verify);
418 add_verbose_flag(verify);
419 add_debug_flag(verify);
420 add_scheme_option(verify);
421 add_crs_path_option(verify);
422 add_oracle_hash_option(verify);
423 remove_zk_option(verify);
424 add_ipa_accumulation_flag(verify);
429 CLI::App* write_solidity_verifier =
430 app.add_subcommand(
"write_solidity_verifier",
431 "Write a Solidity smart contract suitable for verifying proofs of circuit "
432 "satisfiability for the circuit with verification key at vk_path. Not all "
433 "hash types are implemented due to efficiency concerns.");
435 add_scheme_option(write_solidity_verifier);
436 add_vk_path_option(write_solidity_verifier);
437 add_output_path_option(write_solidity_verifier, output_path);
439 add_verbose_flag(write_solidity_verifier);
440 remove_zk_option(write_solidity_verifier);
441 add_crs_path_option(write_solidity_verifier);
442 add_optimized_solidity_verifier_flag(write_solidity_verifier);
444 std::filesystem::path avm_inputs_path{
"./target/avm_inputs.bin" };
445 const auto add_avm_inputs_option = [&](CLI::App* subcommand) {
446 return subcommand->add_option(
"--avm-inputs", avm_inputs_path,
"");
448 std::filesystem::path avm_public_inputs_path{
"./target/avm_public_inputs.bin" };
449 const auto add_avm_public_inputs_option = [&](CLI::App* subcommand) {
450 return subcommand->add_option(
"--avm-public-inputs", avm_public_inputs_path,
"");
456 CLI::App* avm_simulate_command = app.add_subcommand(
"avm_simulate",
"");
457 avm_simulate_command->group(
"");
458 add_verbose_flag(avm_simulate_command);
459 add_debug_flag(avm_simulate_command);
460 add_avm_inputs_option(avm_simulate_command);
465 CLI::App* avm_prove_command = app.add_subcommand(
"avm_prove",
"");
466 avm_prove_command->group(
"");
467 add_verbose_flag(avm_prove_command);
468 add_debug_flag(avm_prove_command);
469 add_crs_path_option(avm_prove_command);
470 std::filesystem::path avm_prove_output_path{
"./proofs" };
471 add_output_path_option(avm_prove_command, avm_prove_output_path);
472 add_avm_inputs_option(avm_prove_command);
477 CLI::App* avm_write_vk_command = app.add_subcommand(
"avm_write_vk",
"");
478 avm_write_vk_command->group(
"");
479 add_verbose_flag(avm_write_vk_command);
480 add_debug_flag(avm_write_vk_command);
481 add_crs_path_option(avm_write_vk_command);
482 std::filesystem::path avm_write_vk_output_path{
"./keys" };
483 add_output_path_option(avm_write_vk_command, avm_write_vk_output_path);
488 CLI::App* avm_check_circuit_command = app.add_subcommand(
"avm_check_circuit",
"");
489 avm_check_circuit_command->group(
"");
490 add_verbose_flag(avm_check_circuit_command);
491 add_debug_flag(avm_check_circuit_command);
492 add_crs_path_option(avm_check_circuit_command);
493 add_avm_inputs_option(avm_check_circuit_command);
498 CLI::App* avm_verify_command = app.add_subcommand(
"avm_verify",
"");
499 avm_verify_command->group(
"");
500 add_verbose_flag(avm_verify_command);
501 add_debug_flag(avm_verify_command);
502 add_crs_path_option(avm_verify_command);
503 add_avm_public_inputs_option(avm_verify_command);
504 add_proof_path_option(avm_verify_command);
505 add_vk_path_option(avm_verify_command);
510 CLI::App* aztec_process = app.add_subcommand(
512 "Process Aztec contract artifacts: transpile and generate verification keys for all private functions.\n"
513 "If input is a directory (and no output specified), recursively processes all artifacts found in the "
515 "Multiple -i flags can be specified when no -o flag is present for parallel processing.");
517 std::vector<std::string> artifact_input_paths;
518 std::string artifact_output_path;
519 bool force_regenerate =
false;
521 aztec_process->add_option(
"-i,--input",
522 artifact_input_paths,
523 "Input artifact JSON path or directory to search (optional, defaults to current "
524 "directory). Can be specified multiple times when no -o flag is present.");
525 aztec_process->add_option(
527 artifact_output_path,
528 "Output artifact JSON path (optional, same as input if not specified). Cannot be used with multiple -i flags.");
529 aztec_process->add_flag(
"-f,--force", force_regenerate,
"Force regeneration of verification keys");
530 add_verbose_flag(aztec_process);
531 add_debug_flag(aztec_process);
536 CLI::App* cache_paths_command =
537 aztec_process->add_subcommand(
"cache_paths",
538 "Output cache paths for verification keys in an artifact.\n"
539 "Format: <hash>:<cache_path>:<function_name> (one per line).");
541 std::string cache_paths_input;
542 cache_paths_command->add_option(
"input", cache_paths_input,
"Input artifact JSON path (required).")->required();
543 add_verbose_flag(cache_paths_command);
544 add_debug_flag(cache_paths_command);
549 CLI::App* msgpack_command = app.add_subcommand(
"msgpack",
"Msgpack API interface.");
552 CLI::App* msgpack_schema_command =
553 msgpack_command->add_subcommand(
"schema",
"Output a msgpack schema encoded as JSON to stdout.");
554 add_verbose_flag(msgpack_schema_command);
557 CLI::App* msgpack_curve_constants_command =
558 msgpack_command->add_subcommand(
"curve_constants",
"Output curve constants as msgpack to stdout.");
559 add_verbose_flag(msgpack_curve_constants_command);
562 CLI::App* msgpack_run_command =
563 msgpack_command->add_subcommand(
"run",
"Execute msgpack API commands from stdin or file.");
564 add_verbose_flag(msgpack_run_command);
565 std::string msgpack_input_file;
566 msgpack_run_command->add_option(
567 "-i,--input", msgpack_input_file,
"Input file containing msgpack buffers (defaults to stdin)");
568 size_t request_ring_size = 1024 * 1024;
571 "--request-ring-size", request_ring_size,
"Request ring buffer size for shared memory IPC (default: 1MB)")
572 ->check(CLI::PositiveNumber);
573 size_t response_ring_size = 1024 * 1024;
575 ->add_option(
"--response-ring-size",
577 "Response ring buffer size for shared memory IPC (default: 1MB)")
578 ->check(CLI::PositiveNumber);
581 ->add_option(
"--max-clients",
583 "Maximum concurrent clients for socket IPC servers (default: 1, only used for .sock files)")
584 ->check(CLI::PositiveNumber);
594 if ((prove->parsed() || write_vk->parsed()) && output_path !=
"-") {
596 std::filesystem::create_directories(output_path);
601#if !defined(__wasm__) || defined(ENABLE_WASM_BENCH)
602 if (!flags.storage_budget.empty()) {
605 if (print_bench || !bench_out.empty() || !bench_out_hierarchical.empty()) {
607 vinfo(
"BB_BENCH enabled via --print_bench or --bench_out");
619 const auto execute_non_prove_command = [&](
API& api) {
620 if (check->parsed()) {
621 api.check(flags, bytecode_path, witness_path);
624 if (gates->parsed()) {
625 api.gates(flags, bytecode_path);
628 if (write_vk->parsed()) {
629 api.write_vk(flags, bytecode_path, output_path);
632 if (verify->parsed()) {
633 const bool verified = api.verify(flags, public_inputs_path, proof_path, vk_path);
634 vinfo(
"verified: ", verified);
635 return verified ? 0 : 1;
637 if (write_solidity_verifier->parsed()) {
638 api.write_solidity_verifier(flags, output_path, vk_path);
641 auto subcommands = app.get_subcommands();
642 const std::string message = std::string(
"No handler for subcommand ") + subcommands[0]->get_name();
649 if (msgpack_schema_command->parsed()) {
653 if (msgpack_curve_constants_command->parsed()) {
657 if (msgpack_run_command->parsed()) {
658 return execute_msgpack_run(msgpack_input_file, max_clients, request_ring_size, response_ring_size);
660 if (aztec_process->parsed()) {
662 throw_or_abort(
"Aztec artifact processing is not supported in WASM builds.");
665 if (cache_paths_command->parsed()) {
670 if (!artifact_output_path.empty() && artifact_input_paths.size() > 1) {
671 throw_or_abort(
"Cannot specify --output when multiple --input flags are provided.");
675 if (artifact_input_paths.empty()) {
676 artifact_input_paths.push_back(
".");
680 if (artifact_input_paths.size() > 1) {
682 for (
const auto& input : artifact_input_paths) {
683 if (std::filesystem::is_directory(input)) {
684 throw_or_abort(
"When using multiple --input flags, all inputs must be files, not directories.");
689 std::atomic<bool> all_success =
true;
690 std::vector<std::string> failures;
691 std::mutex failures_mutex;
693 parallel_for(artifact_input_paths.size(), [&](
size_t i) {
694 const auto& input = artifact_input_paths[i];
695 if (!process_aztec_artifact(input, input, force_regenerate)) {
697 std::lock_guard<std::mutex> lock(failures_mutex);
698 failures.push_back(input);
703 info(
"Failed to process ", failures.size(),
" artifact(s)");
706 info(
"Successfully processed ", artifact_input_paths.size(),
" artifact(s)");
711 std::string input = artifact_input_paths[0];
714 if (std::filesystem::is_directory(input)) {
716 if (!artifact_output_path.empty()) {
718 "Cannot specify --output when input is a directory. Artifacts are updated in-place.");
725 std::string output = artifact_output_path.empty() ? input : artifact_output_path;
730 else if (avm_prove_command->parsed()) {
732 avm_prove(avm_inputs_path, avm_prove_output_path);
733 }
else if (avm_check_circuit_command->parsed()) {
735 }
else if (avm_verify_command->parsed()) {
736 return avm_verify(proof_path, avm_public_inputs_path, vk_path) ? 0 : 1;
737 }
else if (avm_simulate_command->parsed()) {
739 }
else if (avm_write_vk_command->parsed()) {
741 }
else if (flags.scheme ==
"chonk") {
743 if (prove->parsed()) {
744 if (!std::filesystem::exists(ivc_inputs_path)) {
745 throw_or_abort(
"The prove command for Chonk expect a valid file passed with --ivc_inputs_path "
746 "<ivc-inputs.msgpack> (default ./ivc-inputs.msgpack)");
748 api.
prove(flags, ivc_inputs_path, output_path);
749#if !defined(__wasm__) || defined(ENABLE_WASM_BENCH)
751 vinfo(
"Printing BB_BENCH results...");
755 if (!bench_out.empty()) {
756 std::ofstream file(bench_out);
759 if (!bench_out_hierarchical.empty()) {
760 std::ofstream file(bench_out_hierarchical);
766 if (check->parsed()) {
767 if (!std::filesystem::exists(ivc_inputs_path)) {
768 throw_or_abort(
"The check command for Chonk expect a valid file passed with --ivc_inputs_path "
769 "<ivc-inputs.msgpack> (default ./ivc-inputs.msgpack)");
773 return execute_non_prove_command(api);
774 }
else if (flags.scheme ==
"ultra_honk") {
776 if (prove->parsed()) {
777 api.
prove(flags, bytecode_path, witness_path, vk_path, output_path);
778#if !defined(__wasm__) || defined(ENABLE_WASM_BENCH)
782 if (!bench_out.empty()) {
783 std::ofstream file(bench_out);
786 if (!bench_out_hierarchical.empty()) {
787 std::ofstream file(bench_out_hierarchical);
793 return execute_non_prove_command(api);
798 }
catch (std::runtime_error
const& err) {
799#ifndef BB_NO_EXCEPTIONS