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
11
12namespace bb {
13
14// AVM is enabled in this build
15const bool avm_enabled = true;
16
17namespace {
18
19void print_avm_stats()
20{
21#ifdef AVM_TRACK_STATS
22 info("------- STATS -------");
23 const auto& stats = ::bb::avm2::Stats::get();
24 const int levels = std::getenv("AVM_STATS_DEPTH") != nullptr ? std::stoi(std::getenv("AVM_STATS_DEPTH")) : 2;
25 info(stats.to_string(levels));
26#endif
27}
28
29} // namespace
30
31void avm_prove(const std::filesystem::path& inputs_path, const std::filesystem::path& output_path)
32{
33 avm2::AvmAPI avm;
35 auto proof = avm.prove(inputs);
36
37 // NOTE: As opposed to other proof systems, the public inputs are NOT part of the proof.
38 write_file(output_path / "proof", to_buffer(proof));
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);
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, const std::filesystem::path& public_inputs_path)
66{
67 const auto proof = many_from_buffer<fr>(read_file(proof_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);
72 info("verification: ", res ? "success" : "failure");
73
74 print_avm_stats();
75 return res;
76}
77
78void avm_simulate(const std::filesystem::path& inputs_path)
79{
80 // This includes input deserialization as well.
81 AVM_TRACK_TIME("command/avm_simulate", {
82 avm2::AvmAPI avm;
85 });
86
87 print_avm_stats();
88}
89
90void avm_write_verification_key(const std::filesystem::path& output_path)
91{
92 avm2::AvmAPI avm;
93 auto vk = avm.get_verification_key();
94 info("Writing AVM verification key to: ", output_path / "vk");
95 write_file(output_path / "vk", vk);
96}
97
99{
100 avm2::AvmAPI avm;
101 auto proving_inputs = avm2::AvmAPI::ProvingInputs::from(inputs);
102 auto proof = avm.prove(proving_inputs);
103
104 print_avm_stats();
105
106 return AvmProveResult{ .proof = std::move(proof) };
107}
108
109bool avm_verify_from_bytes(std::vector<bb::fr> proof, std::vector<uint8_t> public_inputs)
110{
111 auto pi = avm2::PublicInputs::from(public_inputs);
112
113 avm2::AvmAPI avm;
114 bool res = avm.verify(proof, pi);
115 info("verification: ", res ? "success" : "failure");
116
117 print_avm_stats();
118 return res;
119}
120
121bool avm_check_circuit_from_bytes(std::vector<uint8_t> inputs)
122{
123 avm2::AvmAPI avm;
124 auto proving_inputs = avm2::AvmAPI::ProvingInputs::from(inputs);
125
126 bool res = avm.check_circuit(proving_inputs);
127 info("circuit check: ", res ? "success" : "failure");
128
129 print_avm_stats();
130 return res;
131}
132
133} // 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
#define info(...)
Definition log.hpp:93
AvmProvingInputs inputs
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
bool avm_check_circuit_from_bytes(std::vector< uint8_t > inputs)
Check the AVM circuit from serialized inputs (msgpack bytes).
Definition api_avm.cpp:121
void avm_simulate(const std::filesystem::path &inputs_path)
Simulates an public transaction.
Definition api_avm.cpp:78
bool avm_verify_from_bytes(std::vector< bb::fr > proof, std::vector< uint8_t > public_inputs)
Verify an AVM proof from serialized data.
Definition api_avm.cpp:109
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: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:90
AvmProveResult avm_prove_from_bytes(std::vector< uint8_t > inputs)
Prove an AVM transaction from serialized inputs (msgpack bytes). Callers that need to verify the proo...
Definition api_avm.cpp:98
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:31
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:31
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::span< const uint8_t > data)
Definition file_io.hpp:101
const bool avm_enabled
Definition api_avm.cpp:15
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:18
Result of in-memory AVM proving.
Definition api_avm.hpp:12
std::vector< bb::fr > proof
Definition api_avm.hpp:13
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