36 throw_or_abort(
"write_chonk_vk: --circuit_kind must be one of 'app' / 'kernel' / 'hiding' (got '" + s +
"')");
37 __builtin_unreachable();
45void write_chonk_vk(std::vector<uint8_t>
bytecode,
const std::filesystem::path& output_path,
const API::Flags& flags)
50 const bool is_stdout = output_path ==
"-";
56 write_file(output_path /
"vk.json", std::vector<uint8_t>(json_content.begin(), json_content.end()));
57 info(
"VK (JSON) saved to ", output_path /
"vk.json");
59 write_file(output_path /
"vk", response.bytes);
65 const std::filesystem::path& input_path,
66 const std::filesystem::path& output_dir)
73 std::vector<CircuitKind> kinds;
74 kinds.reserve(raw_steps.size());
75 for (
const auto& step : raw_steps) {
76 kinds.push_back(step.kind);
79 info(
"Chonk: starting with ", raw_steps.size(),
" circuits");
80 for (
size_t i = 0; i < raw_steps.size(); ++i) {
81 const auto& step = raw_steps[i];
83 .
circuit = { .
name = step.function_name, .bytecode = step.bytecode, .verification_key = step.vk },
89 info(
"Chonk: accumulating " + step.function_name);
95 const bool output_to_stdout = output_dir ==
"-";
97 const auto write_proof = [&]() {
99 if (output_to_stdout) {
100 vinfo(
"writing Chonk proof to stdout");
103 vinfo(
"writing Chonk proof (JSON) in directory ", output_dir);
106 write_file(output_dir /
"proof.json", std::vector<uint8_t>(json_content.begin(), json_content.end()));
107 info(
"Proof (JSON) saved to ", output_dir /
"proof.json");
109 vinfo(
"writing Chonk proof in directory ", output_dir);
117 vinfo(
"writing Chonk vk in directory ", output_dir);
119 Flags hiding_flags = flags;
121 write_chonk_vk(raw_steps[raw_steps.size() - 1].bytecode, output_dir, hiding_flags);
126 [[maybe_unused]]
const std::filesystem::path& public_inputs_path,
127 const std::filesystem::path& proof_path,
128 const std::filesystem::path& vk_path)
131 auto proof_fields = many_from_buffer<fr>(
read_file(proof_path));
137 return response.valid;
147 for (
size_t i = 0;; ++i) {
151 if (!std::filesystem::exists(proof_file) || !std::filesystem::exists(vk_file)) {
155 auto proof_fields = many_from_buffer<fr>(
read_file(proof_file));
160 if (proofs.empty()) {
161 throw_or_abort(
"batch_verify: no proof_0/vk_0 pairs found in " + proofs_dir.string());
164 info(
"ChonkAPI::batch_verify - found ", proofs.size(),
" proof/vk pairs in ", proofs_dir.string());
167 return response.valid;
172 auto proof_fields = many_from_buffer<fr>(
read_file(proof_path));
177 std::string json =
"{\n"
178 " \"compressed_proof_size_bytes\": " +
183 if (output_path ==
"-") {
186 write_file(output_path, std::vector<uint8_t>(json.begin(), json.end()));
188 auto compressed_proof_path = output_path;
189 compressed_proof_path.replace_extension(
".bin");
190 write_file(compressed_proof_path, compressed);
191 info(
"Proof stats written to ", output_path);
204 auto proof = ivc->prove();
205 auto vk_and_hash = ivc->get_hiding_kernel_vk_and_hash();
207 const bool verified = verifier.
verify(proof);
218 [[maybe_unused]]
const std::filesystem::path& output_path,
219 [[maybe_unused]]
const std::filesystem::path& vk_path)
232 bool check_failed =
false;
233 for (
size_t i = 0; i < raw_steps.size(); ++i) {
234 auto& step = raw_steps[i];
235 if (step.vk.empty()) {
236 info(
"FAIL: Expected precomputed vk for function ", step.function_name);
241 .
circuit = { .
name = step.function_name, .bytecode = step.bytecode, .verification_key = step.vk },
246 if (!response.valid) {
247 info(
"VK mismatch detected for function ", step.function_name);
249 info(
"Computed VK differs from precomputed VK in ivc-inputs.msgpack");
252 info(
"Updating VK in ivc-inputs.msgpack with computed value");
253 step.vk = response.actual_vk;
265 const std::filesystem::path& bytecode_path,
266 const std::filesystem::path& output_path)
269 write_chonk_vk(
get_bytecode(bytecode_path), output_path, flags);
273 [[maybe_unused]]
const std::filesystem::path& bytecode_path,
274 [[maybe_unused]]
const std::filesystem::path& witness_path)
284 std::string functions_string =
"{\"functions\": [\n ";
290 .include_gates_per_opcode = include_gates_per_opcode }
295 std::string gates_per_opcode_str;
296 if (include_gates_per_opcode && !response.gates_per_opcode.empty()) {
297 for (
size_t j = 0; j < response.gates_per_opcode.size(); j++) {
298 gates_per_opcode_str +=
std::to_string(response.gates_per_opcode[j]);
299 if (j != response.gates_per_opcode.size() - 1) {
300 gates_per_opcode_str +=
",";
304 auto result_string =
format(
305 "{\n \"acir_opcodes\": ",
306 response.acir_opcodes,
307 ",\n \"circuit_size\": ",
308 response.circuit_size,
309 (include_gates_per_opcode ?
format(
",\n \"gates_per_opcode\": [", gates_per_opcode_str,
"]") :
""),
311 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.
#define BB_BENCH_NAME(name)
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 > bytecode
std::vector< uint8_t > get_bytecode(const std::string &bytecodePath)
VkPolicy
Policy for handling verification keys during IVC accumulation.
VkPolicy parse_vk_policy(const std::string &policy)
Convert VK policy string to enum for internal use.
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.
std::vector< FF > to_field_elements() const
Serialize proof to field elements (native mode)
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)
Accumulate the previously loaded circuit into the IVC proof.
std::vector< uint8_t > witness
Serialized witness data for the last loaded circuit.
Batch-verify multiple Chonk proofs with batched IPA SRS MSMs.
std::vector< ChonkProof > proofs
Verify that a precomputed verification key matches the circuit.
CircuitInput circuit
Circuit with its precomputed verification key.
Compute MegaHonk verification key for a circuit to be accumulated in Chonk.
Load a circuit into the Chonk instance for accumulation.
CircuitInput circuit
Circuit to be loaded with its bytecode and verification key.
ChonkProof proof
Complete IVC proof for all accumulated circuits.
Generate a proof for all accumulated circuits.
Response execute(BBApiRequest &request) &&
Initialize a new Chonk instance for incremental proof accumulation.
std::vector< CircuitKind > kinds
Get gate counts for a circuit.
CircuitInputNoVK circuit
The circuit to analyze.
Verify a Chonk proof with its verification key.
ChonkProof proof
The Chonk proof to verify.
void throw_or_abort(std::string const &err)