8#include "barretenberg/bbapi/generated/bb_types.hpp"
37void write_chonk_vk(std::vector<uint8_t>
bytecode,
const std::filesystem::path& output_path,
const API::Flags& flags)
39 bbapi::BBApiRequest request;
42 bbapi::wire::ChonkComputeVk{
47 const bool is_stdout = output_path ==
"-";
54 write_file(output_path /
"vk.json", std::vector<uint8_t>(json_content.begin(), json_content.end()));
55 info(
"VK (JSON) saved to ", output_path /
"vk.json");
57 write_file(output_path /
"vk", response.bytes);
63 const std::filesystem::path& input_path,
64 const std::filesystem::path& output_dir)
72 bbapi::wire::ChonkStart{ .num_circuits =
static_cast<uint32_t
>(raw_steps.size()) });
73 info(
"Chonk: starting with ", raw_steps.size(),
" circuits");
74 for (
size_t i = 0; i < raw_steps.size(); ++i) {
75 const auto& step = raw_steps[i];
77 bbapi::wire::ChonkLoad{ .circuit = bbapi::wire::CircuitInput{
78 .name = step.function_name,
79 .bytecode = step.bytecode,
80 .verification_key = step.vk,
84 info(
"Chonk: accumulating " + step.function_name);
91 const bool output_to_stdout = output_dir ==
"-";
93 const auto write_proof = [&]() {
94 const auto proof_fields = proof.to_field_elements();
95 if (output_to_stdout) {
96 vinfo(
"writing Chonk proof to stdout");
99 vinfo(
"writing Chonk proof (JSON) in directory ", output_dir);
102 write_file(output_dir /
"proof.json", std::vector<uint8_t>(json_content.begin(), json_content.end()));
103 info(
"Proof (JSON) saved to ", output_dir /
"proof.json");
105 vinfo(
"writing Chonk proof in directory ", output_dir);
113 vinfo(
"writing Chonk vk in directory ", output_dir);
115 Flags hiding_flags = flags;
117 write_chonk_vk(raw_steps[raw_steps.size() - 1].bytecode, output_dir, hiding_flags);
122 [[maybe_unused]]
const std::filesystem::path& public_inputs_path,
123 const std::filesystem::path& proof_path,
124 const std::filesystem::path& vk_path)
127 auto proof_fields = many_from_buffer<fr>(
read_file(proof_path));
135 return response.valid;
145 for (
size_t i = 0;; ++i) {
149 if (!std::filesystem::exists(proof_file) || !std::filesystem::exists(vk_file)) {
153 auto proof_fields = many_from_buffer<fr>(
read_file(proof_file));
158 if (proofs.empty()) {
159 throw_or_abort(
"batch_verify: no proof_0/vk_0 pairs found in " + proofs_dir.string());
162 info(
"ChonkAPI::batch_verify - found ", proofs.size(),
" proof/vk pairs in ", proofs_dir.string());
165 wire_proofs.reserve(proofs.size());
166 for (
const auto& p : proofs) {
171 request, bbapi::wire::ChonkBatchVerify{ .proofs =
std::move(wire_proofs), .vks =
std::move(vks) });
172 return response.valid;
177 auto proof_fields = many_from_buffer<fr>(
read_file(proof_path));
182 std::string json =
"{\n"
183 " \"compressed_proof_size_bytes\": " +
188 if (output_path ==
"-") {
191 write_file(output_path, std::vector<uint8_t>(json.begin(), json.end()));
193 auto compressed_proof_path = output_path;
194 compressed_proof_path.replace_extension(
".bin");
195 write_file(compressed_proof_path, compressed);
196 info(
"Proof stats written to ", output_path);
209 auto proof = ivc->prove();
210 auto vk_and_hash = ivc->get_hiding_kernel_vk_and_hash();
212 const bool verified = verifier.
verify(proof);
223 [[maybe_unused]]
const std::filesystem::path& output_path,
224 [[maybe_unused]]
const std::filesystem::path& vk_path)
237 bool check_failed =
false;
238 for (
size_t i = 0; i < raw_steps.size(); ++i) {
239 auto& step = raw_steps[i];
240 if (step.vk.empty()) {
241 info(
"FAIL: Expected precomputed vk for function ", step.function_name);
244 const bool use_zk_flavor = (i == raw_steps.size() - 1);
247 bbapi::wire::ChonkCheckPrecomputedVk{
248 .circuit = bbapi::wire::CircuitInput{ .name = step.function_name,
249 .bytecode = step.bytecode,
250 .verification_key = step.vk },
251 .use_zk_flavor = use_zk_flavor,
254 if (!response.valid) {
255 info(
"VK mismatch detected for function ", step.function_name);
257 info(
"Computed VK differs from precomputed VK in ivc-inputs.msgpack");
260 info(
"Updating VK in ivc-inputs.msgpack with computed value");
261 step.vk = response.actual_vk;
273 const std::filesystem::path& bytecode_path,
274 const std::filesystem::path& output_path)
277 write_chonk_vk(
get_bytecode(bytecode_path), output_path, flags);
281 [[maybe_unused]]
const std::filesystem::path& bytecode_path,
282 [[maybe_unused]]
const std::filesystem::path& witness_path)
292 std::string functions_string =
"{\"functions\": [\n ";
299 bbapi::wire::ChonkStats{
300 .circuit = bbapi::wire::CircuitInputNoVK{ .name =
"ivc_circuit", .bytecode =
std::move(
bytecode) },
301 .include_gates_per_opcode = include_gates_per_opcode,
306 std::string gates_per_opcode_str;
307 if (include_gates_per_opcode && !response.gates_per_opcode.empty()) {
308 for (
size_t j = 0; j < response.gates_per_opcode.size(); j++) {
309 gates_per_opcode_str +=
std::to_string(response.gates_per_opcode[j]);
310 if (j != response.gates_per_opcode.size() - 1) {
311 gates_per_opcode_str +=
",";
315 auto result_string =
format(
316 "{\n \"acir_opcodes\": ",
317 response.acir_opcodes,
318 ",\n \"circuit_size\": ",
319 response.circuit_size,
320 (include_gates_per_opcode ?
format(
",\n \"gates_per_opcode\": [", gates_per_opcode_str,
"]") :
""),
322 functions_string =
format(functions_string, result_string);
void write_bytes_to_stdout(const std::vector< uint8_t > &data)
Writes raw bytes of the vector to stdout.
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.
bool prove_and_verify(const std::filesystem::path &input_path)
Test/debug function: prove and verify in one call (bypasses serialization).
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.
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
Verify a Chonk proof against a verification key.
void write_vk(const Flags &flags, const std::filesystem::path &bytecode_path, const std::filesystem::path &output_path) override
Compute and write a MegaHonk verification key for a circuit to be accumulated in Chonk.
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.
bool check(const Flags &flags, const std::filesystem::path &bytecode_path, const std::filesystem::path &witness_path) override
void gates(const Flags &flags, const std::filesystem::path &bytecode_path) override
Output gate count statistics for a circuit.
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 write_solidity_verifier(const Flags &flags, const std::filesystem::path &output_path, const std::filesystem::path &vk_path) override
Verifier for Chonk IVC proofs (both native and recursive).
Output verify(const Proof &proof)
Verify a Chonk proof.
static std::vector< uint8_t > compress_chonk_proof(const ChonkProof &proof)
std::string format(Args... args)
std::vector< uint8_t > get_bytecode(const std::string &bytecodePath)
wire::ChonkBatchVerifyResponse handle_chonk_batch_verify(BBApiRequest &, wire::ChonkBatchVerify &&cmd)
wire::ChonkVerifyResponse handle_chonk_verify(BBApiRequest &, wire::ChonkVerify &&cmd)
wire::ChonkProveResponse handle_chonk_prove(BBApiRequest &request, wire::ChonkProve &&)
VkPolicy
Policy for handling verification keys during IVC accumulation.
wire::ChonkProof chonk_proof_to_wire(const ChonkProof &d)
wire::ChonkAccumulateResponse handle_chonk_accumulate(BBApiRequest &request, wire::ChonkAccumulate &&cmd)
wire::ChonkLoadResponse handle_chonk_load(BBApiRequest &request, wire::ChonkLoad &&cmd)
ChonkProof chonk_proof_from_wire(wire::ChonkProof &&w)
wire::ChonkStartResponse handle_chonk_start(BBApiRequest &request, wire::ChonkStart &&cmd)
VkPolicy parse_vk_policy(const std::string &policy)
Convert VK policy string to enum for internal use.
wire::ChonkComputeVkResponse handle_chonk_compute_vk(BBApiRequest &, wire::ChonkComputeVk &&cmd)
wire::ChonkCheckPrecomputedVkResponse handle_chonk_check_precomputed_vk(BBApiRequest &, wire::ChonkCheckPrecomputedVk &&cmd)
std::vector< bb::fr > fr_vec_from_wire(const std::vector< Fr > &w)
wire::ChonkStatsResponse handle_chonk_stats(BBApiRequest &, wire::ChonkStats &&cmd)
Entry point for Barretenberg command-line interface.
void chonk_gate_count(const std::string &bytecode_path, bool include_gates_per_opcode)
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::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)
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 include_gates_per_opcode
std::string output_format
static ChonkProof_ from_field_elements(const std::vector< FF > &fields)
Reconstruct proof from field elements.
static void compress_and_save(std::vector< PrivateExecutionStepRaw > &&steps, const std::filesystem::path &output_path)
static std::vector< PrivateExecutionStepRaw > load_and_decompress(const std::filesystem::path &input_path)
Parsed private execution steps ready for Chonk accumulation.
std::shared_ptr< Chonk > accumulate()
Creates a Chonk instance and accumulates each circuit in the folding stack. Uses precomputed VKs when...
void parse(std::vector< PrivateExecutionStepRaw > &&steps)
Converts PrivateExecutionStepRaw entries (which contain raw bytecode/witness bytes) into structured A...
static std::string build(const std::vector< T > &fields, const std::string &vk_hash, const std::string &scheme)
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)