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 {
11// Primary CRS URL (Cloudflare R2)
12constexpr const char* CRS_PRIMARY_URL = "http://crs.aztec-cdn.foundation/g1.dat";
13// Fallback CRS URL (AWS S3)
14constexpr const char* CRS_FALLBACK_URL = "http://crs.aztec-labs.com/g1.dat";
15
16std::vector<uint8_t> download_bn254_g1_data(size_t num_points,
17 const std::string& primary_url,
18 const std::string& fallback_url)
19{
20 size_t g1_end = (num_points * sizeof(bb::g1::affine_element)) - 1;
21
22 // Try primary URL first, with fallback on failure.
23 // Note: WASM is compiled with -fno-exceptions, so try/catch is not available.
24 // In practice, WASM never calls this function - it initializes CRS via srs_init_srs from JavaScript.
25 std::vector<uint8_t> data;
26#ifndef __wasm__
27 try {
28 data = bb::srs::http_download(primary_url, 0, g1_end);
29 } catch (const std::exception& e) {
30 vinfo("Primary CRS download failed: ", e.what(), ". Trying fallback...");
31 data = bb::srs::http_download(fallback_url, 0, g1_end);
32 }
33#else
34 // WASM fallback: just try primary (will abort on failure)
35 data = bb::srs::http_download(primary_url, 0, g1_end);
36 static_cast<void>(fallback_url);
37#endif
38
39 if (data.size() < sizeof(bb::g1::affine_element)) {
40 throw_or_abort("Downloaded g1 data is too small");
41 }
42
43 // Verify first element matches our expected point.
44 auto first_element = from_buffer<bb::g1::affine_element>(data, 0);
45 if (first_element != bb::srs::BN254_G1_FIRST_ELEMENT) {
46 throw_or_abort("Downloaded BN254 G1 CRS first element does not match expected point.");
47 }
48
49 // Verify second element if we have enough data
50 if (data.size() >= 2 * sizeof(bb::g1::affine_element)) {
51 auto second_element = from_buffer<bb::g1::affine_element>(data, sizeof(bb::g1::affine_element));
52 if (second_element != bb::srs::get_bn254_g1_second_element()) {
53 throw_or_abort("Downloaded BN254 G1 CRS second element does not match expected point.");
54 }
55 }
56
57 return data;
58}
59} // namespace
60
61namespace bb {
62
63// Main implementation with configurable URLs
64std::vector<g1::affine_element> get_bn254_g1_data(const std::filesystem::path& path,
65 size_t num_points,
66 bool allow_download,
67 const std::string& primary_url,
68 const std::string& fallback_url)
69{
70 std::filesystem::create_directories(path);
71
72 auto g1_path = path / "bn254_g1.dat";
73 auto lock_path = path / "crs.lock";
74 // Acquire exclusive lock to prevent simultaneous downloads
75 FileLockGuard lock(lock_path.string());
76
77 size_t g1_downloaded_points = get_file_size(g1_path) / sizeof(g1::affine_element);
78
79 if (g1_downloaded_points >= num_points) {
80 vinfo("using cached bn254 crs with num points ", std::to_string(g1_downloaded_points), " at ", g1_path);
81 auto data = read_file(g1_path, num_points * sizeof(g1::affine_element));
82 auto points = std::vector<g1::affine_element>(num_points);
83 for (size_t i = 0; i < num_points; ++i) {
84 points[i] = from_buffer<g1::affine_element>(data, i * sizeof(g1::affine_element));
85 }
86 return points;
87 }
88
89 if (!allow_download && g1_downloaded_points == 0) {
90 throw_or_abort("bn254 g1 data not found and download not allowed in this context");
91 } else if (!allow_download) {
92 throw_or_abort(format("bn254 g1 data had ",
93 g1_downloaded_points,
94 " points and ",
95 num_points,
96 " were requested but download not allowed in this context"));
97 }
98
99 // Double-check after acquiring lock (another process may have downloaded while we waited)
100 g1_downloaded_points = get_file_size(g1_path) / sizeof(g1::affine_element);
101 if (g1_downloaded_points >= num_points) {
102 vinfo("using cached bn254 crs with num points ", std::to_string(g1_downloaded_points), " at ", g1_path);
103 auto data = read_file(g1_path, num_points * sizeof(g1::affine_element));
104 auto points = std::vector<g1::affine_element>(num_points);
105 for (size_t i = 0; i < num_points; ++i) {
106 points[i] = from_buffer<g1::affine_element>(data, i * sizeof(g1::affine_element));
107 }
108 return points;
109 }
110
111 vinfo("downloading bn254 crs...");
112 auto data = download_bn254_g1_data(num_points, primary_url, fallback_url);
113 write_file(g1_path, data);
114
115 auto points = std::vector<g1::affine_element>(num_points);
116 for (size_t i = 0; i < num_points; ++i) {
117 points[i] = from_buffer<g1::affine_element>(data, i * sizeof(g1::affine_element));
118 }
119 return points;
120}
121
122// Default overload using production URLs
123std::vector<g1::affine_element> get_bn254_g1_data(const std::filesystem::path& path,
124 size_t num_points,
125 bool allow_download)
126{
127 return get_bn254_g1_data(path, num_points, allow_download, CRS_PRIMARY_URL, CRS_FALLBACK_URL);
128}
129
130} // namespace bb
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:42
std::string format(Args... args)
Definition log.hpp:23
#define vinfo(...)
Definition log.hpp:94
const std::vector< MemoryValue > data
const size_t num_points
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.
Definition api.hpp:5
std::vector< g1::affine_element > get_bn254_g1_data(const std::filesystem::path &path, size_t num_points, bool allow_download, const std::string &primary_url, const std::string &fallback_url)
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:30
void write_file(const std::string &filename, std::vector< uint8_t > const &data)
Definition file_io.hpp:59
size_t get_file_size(std::string const &filename)
Definition file_io.hpp:18
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)