18constexpr const char* CRS_PRIMARY_URL =
"http://crs.aztec-cdn.foundation/g1.dat";
20constexpr const char* CRS_FALLBACK_URL =
"http://crs.aztec-labs.com/g1.dat";
26size_t round_up_to_chunk_boundary(
size_t num_points)
28 if (num_points >= bb::srs::SRS_TOTAL_POINTS) {
29 return bb::srs::SRS_TOTAL_POINTS;
31 size_t rounded = ((
num_points + bb::srs::SRS_CHUNK_SIZE_POINTS - 1) / bb::srs::SRS_CHUNK_SIZE_POINTS) *
32 bb::srs::SRS_CHUNK_SIZE_POINTS;
33 return std::min(rounded, bb::srs::SRS_TOTAL_POINTS);
43void verify_bn254_crs_integrity(
const std::vector<uint8_t>&
data)
45 size_t num_full_chunks =
data.size() / bb::srs::SRS_CHUNK_SIZE_BYTES;
46 size_t chunks_to_verify = std::min(num_full_chunks,
static_cast<size_t>(bb::srs::SRS_NUM_FULL_CHUNKS));
49 const size_t sentinel = bb::srs::SRS_NUM_CHUNKS;
50 std::atomic<size_t> failed_chunk{ sentinel };
53 if (chunks_to_verify > 0) {
55 for (
size_t i : tc.range(chunks_to_verify)) {
60 size_t offset = i * bb::srs::SRS_CHUNK_SIZE_BYTES;
63 if (hash != bb::srs::BN254_G1_CHUNK_HASHES[i]) {
64 size_t expected = sentinel;
72 size_t tail_offset = chunks_to_verify * bb::srs::SRS_CHUNK_SIZE_BYTES;
73 size_t tail_size =
data.size() - tail_offset;
74 if (tail_size > 0 && chunks_to_verify < bb::srs::SRS_NUM_CHUNKS) {
77 if (hash != bb::srs::BN254_G1_CHUNK_HASHES[chunks_to_verify]) {
78 size_t expected = sentinel;
83 size_t bad = failed_chunk.load();
85 size_t offset = bad * bb::srs::SRS_CHUNK_SIZE_BYTES;
90 vinfo(
"verified ", chunks_to_verify + (tail_size > 0 ? 1 : 0),
" BN254 G1 CRS chunks via SHA-256");
93std::vector<uint8_t> download_bn254_g1_data(
size_t num_points,
94 const std::string& primary_url,
95 const std::string& fallback_url)
102 std::vector<uint8_t>
data;
106 }
catch (
const std::exception& e) {
107 vinfo(
"Primary CRS download failed: ", e.what(),
". Trying fallback...");
113 static_cast<void>(fallback_url);
121 auto first_element = from_buffer<bb::g1::affine_element>(
data, 0);
123 throw_or_abort(
"Downloaded BN254 G1 CRS first element does not match expected point.");
127 verify_bn254_crs_integrity(
data);
139 const std::string& primary_url,
140 const std::string& fallback_url)
143 std::filesystem::create_directories(path);
145 auto g1_path = path /
"bn254_g1.dat";
146 auto lock_path = path /
"crs.lock";
152 auto deserialize_points = [](
const std::vector<uint8_t>&
data,
size_t n) {
155 for (
size_t i : tc.
range(n)) {
162 if (g1_downloaded_points >= num_points) {
163 vinfo(
"using cached bn254 crs with num points ",
std::to_string(g1_downloaded_points),
" at ", g1_path);
165 return deserialize_points(
data, num_points);
168 if (!allow_download && g1_downloaded_points == 0) {
169 throw_or_abort(
"bn254 g1 data not found and download not allowed in this context");
170 }
else if (!allow_download) {
172 g1_downloaded_points,
175 " were requested but download not allowed in this context"));
180 if (g1_downloaded_points >= num_points) {
181 vinfo(
"using cached bn254 crs with num points ",
std::to_string(g1_downloaded_points),
" at ", g1_path);
183 return deserialize_points(
data, num_points);
187 size_t download_points = round_up_to_chunk_boundary(num_points);
188 vinfo(
"downloading bn254 crs (", num_points,
" points requested, downloading ", download_points,
")...");
189 auto data = download_bn254_g1_data(download_points, primary_url, fallback_url);
192 return deserialize_points(
data, num_points);
200 return get_bn254_g1_data(path, num_points, allow_download, CRS_PRIMARY_URL, CRS_FALLBACK_URL);
#define BB_BENCH_NAME(name)
group_elements::affine_element< Fq, Fr, Params > affine_element
std::string format(Args... args)
const std::vector< MemoryValue > data
Sha256Hash sha256(const ByteContainer &input)
SHA-256 hash function (FIPS 180-4)
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.
Entry point for Barretenberg command-line interface.
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)
void write_file(const std::string &filename, std::span< const uint8_t > data)
void parallel_for(size_t num_iterations, const std::function< void(size_t)> &func)
size_t get_file_size(std::string const &filename)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
std::string to_string(bb::avm2::ValueTag tag)
auto range(size_t size, size_t offset=0) const
void throw_or_abort(std::string const &err)