14constexpr const char* CRS_PRIMARY_URL =
"http://crs.aztec-cdn.foundation/g1.dat";
16constexpr const char* CRS_FALLBACK_URL =
"http://crs.aztec-labs.com/g1.dat";
18std::vector<uint8_t> download_bn254_g1_data(
size_t num_points,
19 const std::string& primary_url,
20 const std::string& fallback_url)
28 size_t aligned_points = ((
num_points + points_per_chunk - 1) / points_per_chunk) * points_per_chunk;
29 if (aligned_points > max_aligned_points) {
30 aligned_points = max_aligned_points;
33 size_t download_points =
std::max(aligned_points, num_points);
39 std::vector<uint8_t>
data;
43 }
catch (
const std::exception& e) {
44 vinfo(
"Primary CRS download failed: ", e.what(),
". Trying fallback...");
50 static_cast<void>(fallback_url);
58 auto first_element = from_buffer<bb::g1::affine_element>(
data, 0);
60 throw_or_abort(
"Downloaded BN254 G1 CRS first element does not match expected point.");
77 const std::string& primary_url,
78 const std::string& fallback_url)
80 std::filesystem::create_directories(path);
82 auto g1_path = path /
"bn254_g1.dat";
83 auto lock_path = path /
"crs.lock";
89 if (g1_downloaded_points >= num_points) {
90 vinfo(
"using cached bn254 crs with num points ",
std::to_string(g1_downloaded_points),
" at ", g1_path);
93 for (
size_t i = 0; i < num_points; ++i) {
99 if (!allow_download && g1_downloaded_points == 0) {
100 throw_or_abort(
"bn254 g1 data not found and download not allowed in this context");
101 }
else if (!allow_download) {
103 g1_downloaded_points,
106 " were requested but download not allowed in this context"));
111 if (g1_downloaded_points >= num_points) {
112 vinfo(
"using cached bn254 crs with num points ",
std::to_string(g1_downloaded_points),
" at ", g1_path);
115 for (
size_t i = 0; i < num_points; ++i) {
121 vinfo(
"downloading bn254 crs...");
122 auto data = download_bn254_g1_data(num_points, primary_url, fallback_url);
126 for (
size_t i = 0; i < num_points; ++i) {
137 return get_bn254_g1_data(path, num_points, allow_download, CRS_PRIMARY_URL, CRS_FALLBACK_URL);
group_elements::affine_element< Fq, Fr, Params > affine_element
std::string format(Args... args)
const std::vector< MemoryValue > data
void verify_bn254_crs_integrity(const std::vector< uint8_t > &data)
Verify downloaded CRS data against embedded SHA256 chunk hashes.
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 size_t CRS_NUM_FULL_CHUNKS
constexpr g1::affine_element BN254_G1_FIRST_ELEMENT
Expected first G1 element from BN254 CRS.
constexpr size_t CRS_HASH_CHUNK_SIZE
SHA256 hashes for integrity verification of downloaded BN254 CRS G1 data.
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::vector< uint8_t > const &data)
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)
void throw_or_abort(std::string const &err)