Skip to content

Commit d090073

Browse files
author
AztecBot
committed
Merge branch 'next' into merge-train/fairies
2 parents 434c543 + 480c552 commit d090073

26 files changed

Lines changed: 1399 additions & 1130 deletions

File tree

barretenberg/cpp/src/barretenberg/api/api_avm.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
11
#pragma once
22
#include <filesystem>
3+
#include <vector>
4+
5+
#include "barretenberg/ecc/curves/bn254/fr.hpp"
36

47
namespace bb {
58

9+
/**
10+
* @brief Result of in-memory AVM proving.
11+
*/
12+
struct AvmProveResult {
13+
std::vector<bb::fr> proof;
14+
};
15+
16+
/**
17+
* @brief Prove an AVM transaction from serialized inputs (msgpack bytes).
18+
* Callers that need to verify the proof should call avm_verify_from_bytes separately.
19+
*/
20+
AvmProveResult avm_prove_from_bytes(std::vector<uint8_t> inputs);
21+
22+
/**
23+
* @brief Verify an AVM proof from serialized data.
24+
* @param proof The proof as a vector of field elements.
25+
* @param public_inputs Serialized public inputs (msgpack bytes).
26+
* @return true if verification succeeds.
27+
*/
28+
bool avm_verify_from_bytes(std::vector<bb::fr> proof, std::vector<uint8_t> public_inputs);
29+
30+
/**
31+
* @brief Check the AVM circuit from serialized inputs (msgpack bytes).
32+
* @return true if the circuit check passes.
33+
*/
34+
bool avm_check_circuit_from_bytes(std::vector<uint8_t> inputs);
35+
636
// Global flag indicating AVM support is available
737
extern const bool avm_enabled;
838

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "barretenberg/bbapi/bbapi_avm.hpp"
2+
#include "barretenberg/api/api_avm.hpp"
3+
#include "barretenberg/vm2/tooling/stats.hpp"
4+
5+
namespace bb::bbapi {
6+
7+
namespace {
8+
9+
// Reset the AVM per-stage timings registry so the snapshot we return reflects only this call.
10+
void reset_avm_stats()
11+
{
12+
::bb::avm2::Stats::get().reset();
13+
}
14+
15+
// Take a snapshot of the AVM per-stage timings registry and convert it to the wire-format struct.
16+
std::vector<AvmStat> snapshot_avm_stats()
17+
{
18+
auto snapshot = ::bb::avm2::Stats::get().snapshot();
19+
std::vector<AvmStat> result;
20+
result.reserve(snapshot.size());
21+
for (auto& [name, value] : snapshot) {
22+
result.push_back(AvmStat{ .name = std::move(name), .value_ms = value });
23+
}
24+
return result;
25+
}
26+
27+
} // namespace
28+
29+
AvmProve::Response AvmProve::execute(const BBApiRequest& /*request*/) &&
30+
{
31+
reset_avm_stats();
32+
auto result = avm_prove_from_bytes(std::move(inputs));
33+
return Response{
34+
.proof = std::move(result.proof),
35+
.stats = snapshot_avm_stats(),
36+
};
37+
}
38+
39+
AvmVerify::Response AvmVerify::execute(const BBApiRequest& /*request*/) &&
40+
{
41+
bool verified = avm_verify_from_bytes(std::move(proof), std::move(public_inputs));
42+
return Response{ .verified = verified };
43+
}
44+
45+
AvmCheckCircuit::Response AvmCheckCircuit::execute(const BBApiRequest& /*request*/) &&
46+
{
47+
reset_avm_stats();
48+
bool passed = avm_check_circuit_from_bytes(std::move(inputs));
49+
return Response{ .passed = passed, .stats = snapshot_avm_stats() };
50+
}
51+
52+
} // namespace bb::bbapi
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#pragma once
2+
/**
3+
* @file bbapi_avm.hpp
4+
* @brief AVM-specific command definitions for the Barretenberg RPC API.
5+
*
6+
* This file contains command structures for AVM operations including proving,
7+
* verification, and circuit checking. When built with bb (non-AVM), these
8+
* commands return an error response. When built with bb-avm, they work normally.
9+
*/
10+
#include "barretenberg/bbapi/bbapi_shared.hpp"
11+
#include "barretenberg/common/named_union.hpp"
12+
#include "barretenberg/ecc/curves/bn254/fr.hpp"
13+
#include "barretenberg/serialize/msgpack.hpp"
14+
#include <cstdint>
15+
#include <string>
16+
#include <vector>
17+
18+
namespace bb::bbapi {
19+
20+
/**
21+
* @struct AvmStat
22+
* @brief A single AVM per-stage timing entry. `value_ms` is wall-clock milliseconds captured by
23+
* bb::avm2::Stats during a prove or check-circuit call.
24+
*/
25+
struct AvmStat {
26+
static constexpr const char MSGPACK_SCHEMA_NAME[] = "AvmStat";
27+
28+
std::string name;
29+
uint64_t value_ms;
30+
SERIALIZATION_FIELDS(name, value_ms);
31+
bool operator==(const AvmStat&) const = default;
32+
};
33+
34+
/**
35+
* @struct AvmProve
36+
* @brief Prove an AVM transaction from serialized inputs.
37+
* The inputs are opaque msgpack bytes of AvmProvingInputs. Callers should call AvmVerify
38+
* separately if they need to verify the resulting proof.
39+
*/
40+
struct AvmProve {
41+
static constexpr const char MSGPACK_SCHEMA_NAME[] = "AvmProve";
42+
43+
struct Response {
44+
static constexpr const char MSGPACK_SCHEMA_NAME[] = "AvmProveResponse";
45+
46+
std::vector<bb::fr> proof;
47+
std::vector<AvmStat> stats;
48+
SERIALIZATION_FIELDS(proof, stats);
49+
bool operator==(const Response&) const = default;
50+
};
51+
52+
std::vector<uint8_t> inputs;
53+
SERIALIZATION_FIELDS(inputs);
54+
Response execute(const BBApiRequest& request = {}) &&;
55+
bool operator==(const AvmProve&) const = default;
56+
};
57+
58+
/**
59+
* @struct AvmVerify
60+
* @brief Verify an AVM proof against serialized public inputs.
61+
*/
62+
struct AvmVerify {
63+
static constexpr const char MSGPACK_SCHEMA_NAME[] = "AvmVerify";
64+
65+
struct Response {
66+
static constexpr const char MSGPACK_SCHEMA_NAME[] = "AvmVerifyResponse";
67+
68+
bool verified;
69+
SERIALIZATION_FIELDS(verified);
70+
bool operator==(const Response&) const = default;
71+
};
72+
73+
std::vector<bb::fr> proof;
74+
std::vector<uint8_t> public_inputs;
75+
SERIALIZATION_FIELDS(proof, public_inputs);
76+
Response execute(const BBApiRequest& request = {}) &&;
77+
bool operator==(const AvmVerify&) const = default;
78+
};
79+
80+
/**
81+
* @struct AvmCheckCircuit
82+
* @brief Check the AVM circuit from serialized inputs.
83+
*/
84+
struct AvmCheckCircuit {
85+
static constexpr const char MSGPACK_SCHEMA_NAME[] = "AvmCheckCircuit";
86+
87+
struct Response {
88+
static constexpr const char MSGPACK_SCHEMA_NAME[] = "AvmCheckCircuitResponse";
89+
90+
bool passed;
91+
std::vector<AvmStat> stats;
92+
SERIALIZATION_FIELDS(passed, stats);
93+
bool operator==(const Response&) const = default;
94+
};
95+
96+
std::vector<uint8_t> inputs;
97+
SERIALIZATION_FIELDS(inputs);
98+
Response execute(const BBApiRequest& request = {}) &&;
99+
bool operator==(const AvmCheckCircuit&) const = default;
100+
};
101+
102+
} // namespace bb::bbapi

barretenberg/cpp/src/barretenberg/bbapi/bbapi_execute.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "barretenberg/bbapi/bbapi_avm.hpp"
34
#include "barretenberg/bbapi/bbapi_chonk.hpp"
45
#include "barretenberg/bbapi/bbapi_crypto.hpp"
56
#include "barretenberg/bbapi/bbapi_ecc.hpp"
@@ -13,7 +14,10 @@
1314

1415
namespace bb::bbapi {
1516

16-
using Command = NamedUnion<CircuitProve,
17+
using Command = NamedUnion<AvmProve,
18+
AvmVerify,
19+
AvmCheckCircuit,
20+
CircuitProve,
1721
CircuitComputeVk,
1822
CircuitStats,
1923
CircuitVerify,
@@ -73,6 +77,9 @@ using Command = NamedUnion<CircuitProve,
7377
Shutdown>;
7478

7579
using CommandResponse = NamedUnion<ErrorResponse,
80+
AvmProve::Response,
81+
AvmVerify::Response,
82+
AvmCheckCircuit::Response,
7683
CircuitProve::Response,
7784
CircuitComputeVk::Response,
7885
CircuitStats::Response,

barretenberg/cpp/src/barretenberg/vm2/api_avm.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <filesystem>
44

5+
#include "barretenberg/api/api_avm.hpp"
56
#include "barretenberg/api/file_io.hpp"
67
#include "barretenberg/common/map.hpp"
78
#include "barretenberg/vm2/avm_api.hpp"
@@ -94,4 +95,39 @@ void avm_write_verification_key(const std::filesystem::path& output_path)
9495
write_file(output_path / "vk", vk);
9596
}
9697

98+
AvmProveResult avm_prove_from_bytes(std::vector<uint8_t> inputs)
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+
109+
bool 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+
121+
bool 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+
97133
} // namespace bb

barretenberg/cpp/src/barretenberg/vm2/tooling/stats.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ std::string Stats::to_string(int depth) const
5252
return joined;
5353
}
5454

55+
std::vector<std::pair<std::string, uint64_t>> Stats::snapshot() const
56+
{
57+
std::lock_guard lock(stats_mutex);
58+
return { stats.begin(), stats.end() };
59+
}
60+
5561
} // namespace bb::avm2

barretenberg/cpp/src/barretenberg/vm2/tooling/stats.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <mutex>
66
#include <string>
77
#include <unordered_map>
8+
#include <utility>
9+
#include <vector>
810

911
// To enable stats tracking, compile in RelWithAssert mode.
1012
// cmake --preset $PRESET -DCMAKE_BUILD_TYPE=RelWithAssert
@@ -46,6 +48,9 @@ class Stats {
4648
// prove/logderiv/relation_ms will not be shown.
4749
std::string to_string(int depth = 2) const;
4850

51+
// Returns a copy of the stats as (key, value_ms) pairs. Keys retain the "_ms" suffix added by time().
52+
std::vector<std::pair<std::string, uint64_t>> snapshot() const;
53+
4954
private:
5055
Stats() = default;
5156

barretenberg/cpp/src/barretenberg/vm2_stub/api_avm.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "api_avm.hpp"
2+
#include "barretenberg/api/api_avm.hpp"
23
#include "barretenberg/common/throw_or_abort.hpp"
34
#include <stdexcept>
45

@@ -34,4 +35,20 @@ void avm_write_verification_key([[maybe_unused]] const std::filesystem::path& ou
3435
throw_or_abort("AVM is not supported in this build. Use the 'bb-avm' binary with full AVM support.");
3536
}
3637

38+
AvmProveResult avm_prove_from_bytes([[maybe_unused]] std::vector<uint8_t> inputs)
39+
{
40+
throw_or_abort("AVM is not supported in this build. Use the 'bb-avm' binary with full AVM support.");
41+
}
42+
43+
bool avm_verify_from_bytes([[maybe_unused]] std::vector<bb::fr> proof,
44+
[[maybe_unused]] std::vector<uint8_t> public_inputs)
45+
{
46+
throw_or_abort("AVM is not supported in this build. Use the 'bb-avm' binary with full AVM support.");
47+
}
48+
49+
bool avm_check_circuit_from_bytes([[maybe_unused]] std::vector<uint8_t> inputs)
50+
{
51+
throw_or_abort("AVM is not supported in this build. Use the 'bb-avm' binary with full AVM support.");
52+
}
53+
3754
} // namespace bb
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Stub implementations of bb::avm2::Stats. Linked into binaries that don't pull in vm2_sim
2+
// (e.g. lightweight `bb` and WASM builds), so callers like bbapi_avm.cpp resolve symbols in
3+
// every build mode. The stub no-ops because AVM proving cannot run on these targets anyway.
4+
5+
#include "barretenberg/vm2/tooling/stats.hpp"
6+
7+
namespace bb::avm2 {
8+
9+
Stats& Stats::get()
10+
{
11+
static Stats stats;
12+
return stats;
13+
}
14+
15+
void Stats::reset() {}
16+
17+
void Stats::increment(const std::string& /*key*/, uint64_t /*value*/) {}
18+
19+
void Stats::time(const std::string& /*key*/, const std::function<void()>& f)
20+
{
21+
f();
22+
}
23+
24+
std::string Stats::to_string(int /*depth*/) const
25+
{
26+
return {};
27+
}
28+
29+
std::vector<std::pair<std::string, uint64_t>> Stats::snapshot() const
30+
{
31+
return {};
32+
}
33+
34+
} // namespace bb::avm2

barretenberg/ts/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export { BBApiException } from './bbapi_exception.js';
2121

2222
// Export Point types for use in foundation and other packages
2323
export type {
24+
AvmStat,
2425
Bn254G1Point,
2526
Bn254G2Point,
2627
ChonkProof,

0 commit comments

Comments
 (0)