Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
api_avm.cpp
Go to the documentation of this file.
1#ifndef DISABLE_AZTEC_VM
3
4#include <filesystem>
5
11
12namespace bb {
13namespace {
14
15void print_avm_stats()
16{
17#ifdef AVM_TRACK_STATS
18 info("------- STATS -------");
19 const auto& stats = ::bb::avm2::Stats::get();
20 const int levels = std::getenv("AVM_STATS_DEPTH") != nullptr ? std::stoi(std::getenv("AVM_STATS_DEPTH")) : 2;
21 info(stats.to_string(levels));
22#endif
23}
24
25} // namespace
26
27void avm_prove(const std::filesystem::path& inputs_path, const std::filesystem::path& output_path)
28{
29 avm2::AvmAPI avm;
30 auto inputs = avm2::AvmAPI::ProvingInputs::from(read_file(inputs_path));
31 auto [proof, vk] = avm.prove(inputs);
32
33 // NOTE: As opposed to Avm1 and other proof systems, the public inputs are NOT part of the proof.
34 write_file(output_path / "proof", to_buffer(proof));
35 write_file(output_path / "vk", vk);
36
37 print_avm_stats();
38
39 // NOTE: Temporarily we also verify after proving.
40 // The reasoning is that proving will always pass unless it crashes.
41 // We want to return an exit code != 0 if the proof is invalid so that the prover client saves the inputs.
42 info("verifying...");
43 bool res = avm.verify(proof, inputs.publicInputs, vk);
44 info("verification: ", res ? "success" : "failure");
45 if (!res) {
46 throw std::runtime_error("Generated proof is invalid!!!!!");
47 }
48}
49
50void avm_check_circuit(const std::filesystem::path& inputs_path)
51{
52 avm2::AvmAPI avm;
53 auto inputs = avm2::AvmAPI::ProvingInputs::from(read_file(inputs_path));
54
55 bool res = avm.check_circuit(inputs);
56 info("circuit check: ", res ? "success" : "failure");
57
58 print_avm_stats();
59}
60
61// NOTE: The proof should NOT include the public inputs.
62bool avm_verify(const std::filesystem::path& proof_path,
63 const std::filesystem::path& public_inputs_path,
64 const std::filesystem::path& vk_path)
65{
66 const auto proof = many_from_buffer<fr>(read_file(proof_path));
67 std::vector<uint8_t> vk_bytes = read_file(vk_path);
68 auto public_inputs = avm2::PublicInputs::from(read_file(public_inputs_path));
69
70 avm2::AvmAPI avm;
71 bool res = avm.verify(proof, public_inputs, vk_bytes);
72 info("verification: ", res ? "success" : "failure");
73
74 print_avm_stats();
75 return res;
76}
77
78} // namespace bb
79#endif
bool check_circuit(const ProvingInputs &inputs)
Definition avm_api.cpp:35
bool verify(const AvmProof &proof, const PublicInputs &pi, const AvmVerificationKey &vk_data)
Definition avm_api.cpp:64
std::pair< AvmProof, AvmVerificationKey > prove(const ProvingInputs &inputs)
Definition avm_api.cpp:13
static Stats & get()
Definition stats.cpp:10
void info(Args... args)
Definition log.hpp:70
Entry point for Barretenberg command-line interface.
bool avm_verify(const std::filesystem::path &proof_path, const std::filesystem::path &public_inputs_path, const std::filesystem::path &vk_path)
Verifies an avm proof and writes the result to stdout.
Definition api_avm.cpp:62
void avm_prove(const std::filesystem::path &inputs_path, const std::filesystem::path &output_path)
Writes an avm proof and corresponding (incomplete) verification key to files.
Definition api_avm.cpp:27
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:29
void avm_check_circuit(const std::filesystem::path &inputs_path)
Definition api_avm.cpp:50
void write_file(const std::string &filename, std::vector< uint8_t > const &data)
Definition file_io.hpp:58
VerifierCommitmentKey< Curve > vk
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< uint8_t > to_buffer(T const &value)
static AvmProvingInputs from(const std::vector< uint8_t > &data)
static PublicInputs from(const std::vector< uint8_t > &data)
Msgpack deserialization.