Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
get_bn254_crs.cpp
Go to the documentation of this file.
1#include "get_bn254_crs.hpp"
7#include "bn254_crs_data.hpp"
8#include "http_download.hpp"
9
10namespace {
11std::vector<uint8_t> download_bn254_g1_data(size_t num_points)
12{
13 size_t g1_end = (num_points * sizeof(bb::g1::affine_element)) - 1;
14
15 // Download via HTTP with Range header
16 auto data = bb::srs::http_download("http://crs.aztec.network/g1.dat", 0, g1_end);
17
18 if (data.size() < sizeof(bb::g1::affine_element)) {
19 throw_or_abort("Downloaded g1 data is too small");
20 }
21
22 // Verify first element matches our expected point.
23 auto first_element = from_buffer<bb::g1::affine_element>(data, 0);
24 if (first_element != bb::srs::BN254_G1_FIRST_ELEMENT) {
25 throw_or_abort("Downloaded BN254 G1 CRS first element does not match expected point.");
26 }
27
28 // Verify second element if we have enough data
29 if (data.size() >= 2 * sizeof(bb::g1::affine_element)) {
30 auto second_element = from_buffer<bb::g1::affine_element>(data, sizeof(bb::g1::affine_element));
31 if (second_element != bb::srs::get_bn254_g1_second_element()) {
32 throw_or_abort("Downloaded BN254 G1 CRS second element does not match expected point.");
33 }
34 }
35
36 return data;
37}
38} // namespace
39
40namespace bb {
41std::vector<g1::affine_element> get_bn254_g1_data(const std::filesystem::path& path,
42 size_t num_points,
43 bool allow_download)
44{
45 std::filesystem::create_directories(path);
46
47 auto g1_path = path / "bn254_g1.dat";
48 auto lock_path = path / "crs.lock";
49 // Acquire exclusive lock to prevent simultaneous downloads
50 FileLockGuard lock(lock_path.string());
51
52 size_t g1_downloaded_points = get_file_size(g1_path) / sizeof(g1::affine_element);
53
54 if (g1_downloaded_points >= num_points) {
55 vinfo("using cached bn254 crs with num points ", std::to_string(g1_downloaded_points), " at ", g1_path);
56 auto data = read_file(g1_path, num_points * sizeof(g1::affine_element));
57 auto points = std::vector<g1::affine_element>(num_points);
58 for (size_t i = 0; i < num_points; ++i) {
59 points[i] = from_buffer<g1::affine_element>(data, i * sizeof(g1::affine_element));
60 }
61 return points;
62 }
63
64 if (!allow_download && g1_downloaded_points == 0) {
65 throw_or_abort("bn254 g1 data not found and download not allowed in this context");
66 } else if (!allow_download) {
67 throw_or_abort(format("bn254 g1 data had ",
68 g1_downloaded_points,
69 " points and ",
70 num_points,
71 " were requested but download not allowed in this context"));
72 }
73
74 // Double-check after acquiring lock (another process may have downloaded while we waited)
75 g1_downloaded_points = get_file_size(g1_path) / sizeof(g1::affine_element);
76 if (g1_downloaded_points >= num_points) {
77 vinfo("using cached bn254 crs with num points ", std::to_string(g1_downloaded_points), " at ", g1_path);
78 auto data = read_file(g1_path, num_points * sizeof(g1::affine_element));
79 auto points = std::vector<g1::affine_element>(num_points);
80 for (size_t i = 0; i < num_points; ++i) {
81 points[i] = from_buffer<g1::affine_element>(data, i * sizeof(g1::affine_element));
82 }
83 return points;
84 }
85
86 vinfo("downloading bn254 crs...");
87 auto data = download_bn254_g1_data(num_points);
88 write_file(g1_path, data);
89
90 auto points = std::vector<g1::affine_element>(num_points);
91 for (size_t i = 0; i < num_points; ++i) {
92 points[i] = from_buffer<g1::affine_element>(data, i * sizeof(g1::affine_element));
93 }
94 return points;
95}
96
97} // namespace bb
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:42
std::string format(Args... args)
Definition log.hpp:21
#define vinfo(...)
Definition log.hpp:79
const std::vector< FF > data
std::vector< uint8_t > http_download(const std::string &url, size_t start_byte=0, size_t end_byte=0)
Download data from a URL with optional Range header support.
constexpr g1::affine_element BN254_G1_FIRST_ELEMENT
Expected first G1 element from BN254 CRS.
g1::affine_element get_bn254_g1_second_element()
Expected second G1 element from BN254 CRS.
Entry point for Barretenberg command-line interface.
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:29
std::vector< g1::affine_element > get_bn254_g1_data(const std::filesystem::path &path, size_t num_points, bool allow_download)
void write_file(const std::string &filename, std::vector< uint8_t > const &data)
Definition file_io.hpp:58
size_t get_file_size(std::string const &filename)
Definition file_io.hpp:17
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
void throw_or_abort(std::string const &err)