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 = avm.prove(inputs);
35
36 // NOTE: As opposed to other proof systems, the public inputs are NOT part of the proof.
37 write_file(output_path / "proof", to_buffer(proof));
38
39 print_avm_stats();
40
41 // NOTE: Temporarily we also verify after proving.
42 // The reasoning is that proving will always pass unless it crashes.
43 // We want to return an exit code != 0 if the proof is invalid so that the prover client saves the inputs.
44 info("verifying...");
45 bool res = avm.verify(proof, inputs.public_inputs);
46 info("verification: ", res ? "success" : "failure");
47 if (!res) {
48 throw std::runtime_error("Generated proof is invalid!!!!!");
49 }
50}
51
52void avm_check_circuit(const std::filesystem::path& inputs_path)
53{
54 avm2::AvmAPI avm;
56
57 bool res = avm.check_circuit(inputs);
58 info("circuit check: ", res ? "success" : "failure");
59
60 print_avm_stats();
61}
62
63// NOTE: The proof should NOT include the public inputs.
64bool avm_verify(const std::filesystem::path& proof_path, const std::filesystem::path& public_inputs_path)
65{
66 const auto proof = many_from_buffer<fr>(read_file(proof_path));
67 auto public_inputs = avm2::PublicInputs::from(read_file(public_inputs_path));
68
69 avm2::AvmAPI avm;
70 bool res = avm.verify(proof, public_inputs);
71 info("verification: ", res ? "success" : "failure");
72
73 print_avm_stats();
74 return res;
75}
76
77void avm_simulate(const std::filesystem::path& inputs_path)
78{
79 // This includes input deserialization as well.
80 AVM_TRACK_TIME("command/avm_simulate", {
81 avm2::AvmAPI avm;
84 });
85
86 print_avm_stats();
87}
88
89void avm_write_verification_key(const std::filesystem::path& output_path)
90{
91 avm2::AvmAPI avm;
92 auto vk = avm.get_verification_key();
93 info("Writing AVM verification key to: ", output_path / "vk");
94 write_file(output_path / "vk", vk);
95}
96
97} // 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)
Definition avm_api.cpp:66
AvmProof 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:89
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:77
bool avm_verify(const std::filesystem::path &proof_path, const std::filesystem::path &public_inputs_path)
Verifies an avm proof and writes the result to stdout.
Definition api_avm.cpp:64
void avm_write_verification_key(const std::filesystem::path &output_path)
Writes an avm (incomplete) verification key to a file.
Definition api_avm.cpp:89
void avm_prove(const std::filesystem::path &inputs_path, const std::filesystem::path &output_path)
Writes an avm proof to a file.
Definition api_avm.cpp:30
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:30
void avm_check_circuit(const std::filesystem::path &inputs_path)
Stub - throws runtime error if called.
Definition api_avm.cpp:52
void write_file(const std::string &filename, std::vector< uint8_t > const &data)
Definition file_io.hpp:59
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