8#include "barretenberg/bbapi/generated/bb_types.hpp"
19void write_vk_outputs(
const bbapi::wire::CircuitComputeVkResponse& vk_response,
20 const std::filesystem::path& output_dir,
21 const API::Flags& flags)
23 if (flags.output_format ==
"json") {
26 write_file(output_dir /
"vk.json", std::vector<uint8_t>(json_content.begin(), json_content.end()));
27 info(
"VK (JSON) saved to ", output_dir /
"vk.json");
29 write_file(output_dir /
"vk", vk_response.bytes);
30 info(
"VK saved to ", output_dir /
"vk");
31 write_file(output_dir /
"vk_hash", vk_response.hash);
32 info(
"VK Hash saved to ", output_dir /
"vk_hash");
36void write_proof_outputs(
const bbapi::wire::CircuitProveResponse& prove_response,
37 const std::filesystem::path& output_dir,
38 const API::Flags& flags)
40 if (flags.output_format ==
"json") {
44 std::string proof_json =
ProofJson::build(proof_domain, vk_hash, flags.scheme);
45 write_file(output_dir /
"proof.json", std::vector<uint8_t>(proof_json.begin(), proof_json.end()));
46 info(
"Proof (JSON) saved to ", output_dir /
"proof.json");
49 write_file(output_dir /
"public_inputs.json", std::vector<uint8_t>(pi_json.begin(), pi_json.end()));
50 info(
"Public inputs (JSON) saved to ", output_dir /
"public_inputs.json");
54 auto public_inputs_buf =
to_buffer(pi_domain);
57 write_file(output_dir /
"public_inputs", public_inputs_buf);
59 info(
"Public inputs saved to ", output_dir /
"public_inputs");
60 info(
"Proof saved to ", output_dir /
"proof");
67 [[maybe_unused]]
const std::filesystem::path& bytecode_path,
68 [[maybe_unused]]
const std::filesystem::path& witness_path)
75 const std::filesystem::path& bytecode_path,
76 const std::filesystem::path& witness_path,
77 const std::filesystem::path& vk_path,
78 const std::filesystem::path& output_dir)
82 if (output_dir ==
"-") {
83 throw_or_abort(
"Stdout output is not supported. Please specify an output directory.");
86 bbapi::wire::ProofSystemSettings settings{ .ipa_accumulation = flags.
ipa_accumulation,
93 std::vector<uint8_t> vk_bytes;
94 if (!vk_path.empty() && !flags.
write_vk) {
101 bbapi::wire::CircuitProve{
102 .circuit = bbapi::wire::CircuitInput{ .name =
"circuit",
104 .verification_key =
std::move(vk_bytes) },
108 write_proof_outputs(response, output_dir, flags);
110 write_vk_outputs(response.vk, output_dir, flags);
115 const std::filesystem::path& public_inputs_path,
116 const std::filesystem::path& proof_path,
117 const std::filesystem::path& vk_path)
124 std::vector<uint8_t> vk_bytes;
126 auto public_inputs_content =
read_file(public_inputs_path);
130 public_inputs = many_from_buffer_exact<uint256_t>(public_inputs_content,
"UltraHonk public inputs file");
133 auto proof_content =
read_file(proof_path);
137 proof = many_from_buffer_exact<uint256_t>(proof_content,
"UltraHonk proof file");
147 bbapi::wire::ProofSystemSettings settings{ .ipa_accumulation = flags.
ipa_accumulation,
153 bbapi::wire::CircuitVerify{
157 .settings = settings,
160 return response.verified;
164 [[maybe_unused]]
const std::filesystem::path& bytecode_path,
165 [[maybe_unused]]
const std::filesystem::path& witness_path)
172 const std::filesystem::path& bytecode_path,
173 const std::filesystem::path& output_dir)
177 if (output_dir ==
"-") {
178 throw_or_abort(
"Stdout output is not supported. Please specify an output directory.");
183 bbapi::wire::ProofSystemSettings settings{ .ipa_accumulation = flags.
ipa_accumulation,
190 bbapi::wire::CircuitComputeVk{
191 .circuit = bbapi::wire::CircuitInputNoVK{ .name =
"circuit", .bytecode =
std::move(
bytecode) },
192 .settings = settings,
195 write_vk_outputs(response, output_dir, flags);
199 [[maybe_unused]]
const std::filesystem::path& bytecode_path)
206 std::string functions_string =
"{\"functions\": [\n ";
210 bbapi::wire::ProofSystemSettings settings{ .ipa_accumulation = flags.ipa_accumulation,
211 .oracle_hash_type = flags.oracle_hash_type,
212 .disable_zk = flags.disable_zk };
217 bbapi::wire::CircuitStats{
218 .circuit = bbapi::wire::CircuitInput{ .name =
"circuit", .bytecode =
bytecode, .verification_key = {} },
219 .include_gates_per_opcode = flags.include_gates_per_opcode,
220 .settings = settings,
223 vinfo(
"Calculated circuit size in gate_count: ", response.num_gates);
226 std::string gates_per_opcode_str;
227 if (flags.include_gates_per_opcode) {
229 for (
size_t count : response.gates_per_opcode) {
231 gates_per_opcode_str +=
",";
240 auto result_string =
format(
241 "{\n \"acir_opcodes\": ",
242 response.num_acir_opcodes,
243 ",\n \"circuit_size\": ",
245 (flags.include_gates_per_opcode ?
format(
",\n \"gates_per_opcode\": [", gates_per_opcode_str,
"]") :
""),
248 functions_string =
format(functions_string, result_string);
253 const std::filesystem::path& output_path,
254 const std::filesystem::path& vk_path)
260 bbapi::wire::ProofSystemSettings settings{ .ipa_accumulation = flags.
ipa_accumulation,
267 request, bbapi::wire::CircuitWriteSolidityVerifier{ .verification_key = vk_bytes, .settings = settings });
270 if (output_path ==
"-") {
275 response.solidity_code.size()));
277 info(
"Honk solidity verifier saved to ", output_path);
279 info(
"ZK Honk solidity verifier saved to ", output_path);
std::shared_ptr< Napi::ThreadSafeFunction > bytecode
#define BB_BENCH_NAME(name)
Non-template handler declarations for the bb service.
Shared type definitions for the Barretenberg RPC API.
Wire <-> domain conversion helpers for the bbapi handlers.
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)
void write_vk(const Flags &flags, const std::filesystem::path &bytecode_path, const std::filesystem::path &output_path) override
bool verify(const Flags &flags, const std::filesystem::path &public_inputs_path, const std::filesystem::path &proof_path, const std::filesystem::path &vk_path) override
bool check(const Flags &flags, const std::filesystem::path &bytecode_path, const std::filesystem::path &witness_path) override
bool prove_and_verify(const Flags &flags, const std::filesystem::path &bytecode_path, const std::filesystem::path &witness_path)
void gates(const Flags &flags, const std::filesystem::path &bytecode_path) override
void write_solidity_verifier(const Flags &flags, const std::filesystem::path &output_path, const std::filesystem::path &vk_path) override
std::string format(Args... args)
std::vector< uint8_t > get_bytecode(const std::string &bytecodePath)
wire::CircuitProveResponse handle_circuit_prove(BBApiRequest &ctx, wire::CircuitProve &&cmd)
std::vector< Uint256 > uint256_vec_to_wire(const std::vector< bb::numeric::uint256_t > &d)
std::vector< bb::numeric::uint256_t > uint256_vec_from_wire(const std::vector< Uint256 > &w)
wire::CircuitInfoResponse handle_circuit_stats(BBApiRequest &ctx, wire::CircuitStats &&cmd)
wire::CircuitVerifyResponse handle_circuit_verify(BBApiRequest &ctx, wire::CircuitVerify &&cmd)
wire::CircuitComputeVkResponse handle_circuit_compute_vk(BBApiRequest &ctx, wire::CircuitComputeVk &&cmd)
wire::CircuitWriteSolidityVerifierResponse handle_circuit_write_solidity_verifier(BBApiRequest &ctx, wire::CircuitWriteSolidityVerifier &&cmd)
Entry point for Barretenberg command-line interface.
std::vector< uint8_t > read_vk_file(const std::filesystem::path &vk_path)
Read a verification key file with an actionable error message if not found.
std::optional< nlohmann::json > try_parse_json(const std::vector< uint8_t > &content)
Try to parse file content as JSON.
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
void write_file(const std::string &filename, std::span< const uint8_t > data)
std::string bytes_to_hex_string(const std::vector< uint8_t > &bytes)
Convert bytes to a hex string with 0x prefix.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
std::string to_string(bb::avm2::ValueTag tag)
std::vector< uint8_t > to_buffer(T const &value)
bool optimized_solidity_verifier
std::string oracle_hash_type
static std::vector< uint256_t > parse(const nlohmann::json &json)
static std::string build(const std::vector< T > &fields, const std::string &vk_hash, const std::string &scheme)
static std::vector< uint8_t > parse_to_bytes(const nlohmann::json &json)
static std::string build(const std::vector< T > &fields, const std::string &hash, const std::string &scheme)
void throw_or_abort(std::string const &err)