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#include "api_avm.hpp"
2
3#include <filesystem>
4
10
11namespace bb {
12
13// AVM is enabled in this build
14const bool avm_enabled = true;
15
16namespace {
17
18void print_avm_stats()
19{
20#ifdef AVM_TRACK_STATS
21 info("------- STATS -------");
22 const auto& stats = ::bb::avm2::Stats::get();
23 const int levels = std::getenv("AVM_STATS_DEPTH") != nullptr ? std::stoi(std::getenv("AVM_STATS_DEPTH")) : 2;
24 info(stats.to_string(levels));
25#endif
26}
27
28} // namespace
29
30void avm_prove(const std::filesystem::path& inputs_path, const std::filesystem::path& output_path)
31{
32 avm2::AvmAPI avm;
34 auto [proof, vk] = avm.prove(inputs);
35
36 // NOTE: As opposed to Avm1 and other proof systems, the public inputs are NOT part of the proof.
37 write_file(output_path / "proof", to_buffer(proof));
38 write_file(output_path / "vk", vk);
39
40 print_avm_stats();
41
42 // NOTE: Temporarily we also verify after proving.
43 // The reasoning is that proving will always pass unless it crashes.
44 // We want to return an exit code != 0 if the proof is invalid so that the prover client saves the inputs.
45 info("verifying...");
46 bool res = avm.verify(proof, inputs.public_inputs, vk);
47 info("verification: ", res ? "success" : "failure");
48 if (!res) {
49 throw std::runtime_error("Generated proof is invalid!!!!!");
50 }
51}
52
53void avm_check_circuit(const std::filesystem::path& inputs_path)
54{
55 avm2::AvmAPI avm;
57
58 bool res = avm.check_circuit(inputs);
59 info("circuit check: ", res ? "success" : "failure");
60
61 print_avm_stats();
62}
63
64// NOTE: The proof should NOT include the public inputs.
65bool avm_verify(const std::filesystem::path& proof_path,
66 const std::filesystem::path& public_inputs_path,
67 const std::filesystem::path& vk_path)
68{
69 const auto proof = many_from_buffer<fr>(read_file(proof_path));
70 std::vector<uint8_t> vk_bytes = read_file(vk_path);
71 auto public_inputs = avm2::PublicInputs::from(read_file(public_inputs_path));
72
73 avm2::AvmAPI avm;
74 bool res = avm.verify(proof, public_inputs, vk_bytes);
75 info("verification: ", res ? "success" : "failure");
76
77 print_avm_stats();
78 return res;
79}
80
81void avm_simulate(const std::filesystem::path& inputs_path)
82{
83 // This includes input deserialization as well.
84 AVM_TRACK_TIME("command/avm_simulate", {
85 avm2::AvmAPI avm;
88 });
89
90 print_avm_stats();
91}
92
93void avm_write_verification_key(const std::filesystem::path& output_path)
94{
95 avm2::AvmAPI avm;
96 auto vk = avm.get_verification_key();
97 info("Writing AVM verification key to: ", output_path / "vk");
98 write_file(output_path / "vk", vk);
99}
100
101} // namespace bb
AvmVerificationKey get_verification_key()
Definition avm_api.cpp:73
bool check_circuit(const ProvingInputs &inputs)
Definition avm_api.cpp:36
bool verify(const AvmProof &proof, const PublicInputs &pi, const AvmVerificationKey &vk_data)
Definition avm_api.cpp:66
std::pair< AvmProof, AvmVerificationKey > prove(const ProvingInputs &inputs)
Definition avm_api.cpp:13
TxSimulationResult simulate_with_hinted_dbs(const AvmProvingInputs &inputs)
static Stats & get()
Definition stats.cpp:10
void info(Args... args)
Definition log.hpp:75
AvmProvingInputs inputs
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void avm_simulate(const std::filesystem::path &inputs_path)
Simulates an public transaction.
Definition api_avm.cpp:81
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:65
void avm_write_verification_key(const std::filesystem::path &output_path)
Writes an avm (incomplete) verification key to a file.
Definition api_avm.cpp:93
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:30
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)
Stub - throws runtime error if called.
Definition api_avm.cpp:53
void write_file(const std::string &filename, std::vector< uint8_t > const &data)
Definition file_io.hpp:58
const bool avm_enabled
Definition api_avm.cpp:14
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)
#define AVM_TRACK_TIME(key, body)
Definition stats.hpp:16
static AvmProvingInputs from(const std::vector< uint8_t > &data)
Definition avm_io.cpp:122
static PublicInputs from(const std::vector< uint8_t > &data)
Msgpack deserialization.
Definition avm_io.cpp:115