Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bbapi_srs.cpp
Go to the documentation of this file.
1
13
14namespace bb::bbapi {
15
17{
18 constexpr size_t COMPRESSED_POINT_SIZE = 32;
19 constexpr size_t UNCOMPRESSED_POINT_SIZE = sizeof(g1::affine_element); // 64
20
21 size_t bytes_per_point = num_points > 0 ? points_buf.size() / num_points : 0;
22 std::vector<g1::affine_element> g1_points(num_points);
23 std::vector<uint8_t> uncompressed_out;
24
25 if (bytes_per_point == UNCOMPRESSED_POINT_SIZE) {
26 // Already uncompressed: fast path with from_buffer
27 parallel_for([&](ThreadChunk chunk) {
28 for (auto i : chunk.range(static_cast<size_t>(num_points))) {
29 g1_points[i] = from_buffer<g1::affine_element>(points_buf.data(), i * UNCOMPRESSED_POINT_SIZE);
30 }
31 });
32 } else if (bytes_per_point == COMPRESSED_POINT_SIZE) {
33 // Compressed: decompress and return uncompressed bytes for caller to cache
34 parallel_for([&](ThreadChunk chunk) {
35 for (auto i : chunk.range(static_cast<size_t>(num_points))) {
36 uint256_t c = from_buffer<uint256_t>(points_buf.data(), i * COMPRESSED_POINT_SIZE);
37 g1_points[i] = g1::affine_element::from_compressed(c);
38 }
39 });
40 // Serialize uncompressed points to return to caller for caching
41 uncompressed_out.resize(static_cast<size_t>(num_points) * UNCOMPRESSED_POINT_SIZE);
42 parallel_for([&](ThreadChunk chunk) {
43 for (auto i : chunk.range(static_cast<size_t>(num_points))) {
44 auto buf = to_buffer(g1_points[i]);
45 std::copy(buf.begin(), buf.end(), &uncompressed_out[i * UNCOMPRESSED_POINT_SIZE]);
46 }
47 });
48 } else {
49 throw_or_abort("SrsInitSrs: invalid points_buf size. Expected 32 or 64 bytes per point, got " +
50 std::to_string(bytes_per_point));
51 }
52
53 // Parse G2 point from buffer (128 bytes). `serialize_from_buffer` validates that the bytes
54 // decode to a curve point but does NOT enforce subgroup membership. BN254 G2 has a non-trivial
55 // cofactor (h2 ≈ 2^254), so a curve point may lie in a small cofactor subgroup of order
56 // dividing h2 rather than the prime-order subgroup of order r. Reject anything outside
57 // the prime-order subgroup before it reaches the SRS factory.
58 auto g2_point_elem = from_buffer<g2::affine_element>(g2_point.data());
59 if (!g2_point_elem.is_in_prime_subgroup()) {
60 throw_or_abort("SrsInitSrs: g2_point is not in the BN254 G2 prime-order subgroup");
61 }
62
63 // Initialize BN254 SRS
64 bb::srs::init_bn254_mem_crs_factory(g1_points, g2_point_elem);
65
66 return { .points_buf = std::move(uncompressed_out) };
67}
68
70{
71 // Validate buffer size before accessing raw pointer
72 const size_t required_size = static_cast<size_t>(num_points) * sizeof(curve::Grumpkin::AffineElement);
73 if (points_buf.size() < required_size) {
74 throw_or_abort("SrsInitGrumpkinSrs: points_buf too small (" + std::to_string(points_buf.size()) +
75 " bytes) for num_points=" + std::to_string(num_points) + " (need " +
76 std::to_string(required_size) + ")");
77 }
78
79 // Parse Grumpkin affine elements from buffer
81 for (uint32_t i = 0; i < num_points; ++i) {
82 points[i] =
83 from_buffer<curve::Grumpkin::AffineElement>(points_buf.data(), i * sizeof(curve::Grumpkin::AffineElement));
84 }
85
86 // Initialize Grumpkin SRS
88
89 return {};
90}
91
92} // namespace bb::bbapi
SRS (Structured Reference String) initialization command definitions for the Barretenberg RPC API.
typename Group::affine_element AffineElement
Definition grumpkin.hpp:64
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:44
#define BB_UNUSED
void init_grumpkin_mem_crs_factory(std::vector< curve::Grumpkin::AffineElement > const &points)
void init_bn254_mem_crs_factory(std::vector< g1::affine_element > const &points, g2::affine_element const &g2_point)
void parallel_for(size_t num_iterations, const std::function< void(size_t)> &func)
Definition thread.cpp:111
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
std::vector< uint8_t > to_buffer(T const &value)
auto range(size_t size, size_t offset=0) const
Definition thread.hpp:152
Response execute(BBApiRequest &request) &&
Definition bbapi_srs.cpp:69
Response execute(BBApiRequest &request) &&
Definition bbapi_srs.cpp:16
void throw_or_abort(std::string const &err)