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"
5
6namespace {
7std::vector<uint8_t> download_bn254_g1_data(size_t num_points)
8{
9 size_t g1_end = num_points * sizeof(bb::g1::affine_element) - 1;
10
11 std::string url = "https://crs.aztec.network/g1.dat";
12
13 // IMPORTANT: this currently uses a shell, DO NOT let user-controlled strings here.
14 std::string command = "curl -H \"Range: bytes=0-" + std::to_string(g1_end) + "\" '" + url + "'";
15
16 auto data = bb::exec_pipe(command);
17 // Header + num_points * sizeof point.
18 if (data.size() < g1_end) {
19 throw_or_abort("Failed to download g1 data.");
20 }
21
22 return data;
23}
24
25std::vector<uint8_t> download_bn254_g2_data()
26{
27 std::string url = "https://crs.aztec.network/g2.dat";
28 // IMPORTANT: this currently uses a shell, DO NOT let user-controlled strings here.
29 std::string command = "curl '" + url + "'";
30 return bb::exec_pipe(command);
31}
32} // namespace
33
34namespace bb {
35std::vector<g1::affine_element> get_bn254_g1_data(const std::filesystem::path& path,
36 size_t num_points,
37 bool allow_download)
38{
39 // TODO(AD): per Charlie this should just download and replace the flat file portion atomically so we have no race
40 // condition
41 std::filesystem::create_directories(path);
42
43 auto g1_path = path / "bn254_g1.dat";
44 size_t g1_downloaded_points = get_file_size(g1_path) / sizeof(g1::affine_element);
45
46 if (g1_downloaded_points >= num_points) {
47 vinfo("using cached bn254 crs with num points ", std::to_string(g1_downloaded_points), " at ", g1_path);
48 auto data = read_file(g1_path, num_points * sizeof(g1::affine_element));
49 auto points = std::vector<g1::affine_element>(num_points);
50 for (size_t i = 0; i < num_points; ++i) {
51 points[i] = from_buffer<g1::affine_element>(data, i * sizeof(g1::affine_element));
52 }
53 return points;
54 }
55
56 if (!allow_download && g1_downloaded_points == 0) {
57 throw_or_abort("bn254 g1 data not found and download not allowed in this context");
58 } else if (!allow_download) {
59 throw_or_abort(format("bn254 g1 data had ",
60 g1_downloaded_points,
61 " points and ",
62 num_points,
63 " were requested but download not allowed in this context"));
64 }
65 vinfo("downloading bn254 crs...");
66 auto data = download_bn254_g1_data(num_points);
67 write_file(g1_path, data);
68
69 auto points = std::vector<g1::affine_element>(num_points);
70 for (size_t i = 0; i < num_points; ++i) {
71 points[i] = from_buffer<g1::affine_element>(data, i * sizeof(g1::affine_element));
72 }
73 return points;
74}
75
76g2::affine_element get_bn254_g2_data(const std::filesystem::path& path, bool allow_download)
77{
78 std::filesystem::create_directories(path);
79
80 auto g2_path = path / "bn254_g2.dat";
81 size_t g2_file_size = get_file_size(g2_path);
82
83 if (g2_file_size == sizeof(g2::affine_element)) {
84 auto data = read_file(g2_path);
85 return from_buffer<g2::affine_element>(data.data());
86 }
87 if (!allow_download) {
88 throw_or_abort("bn254 g2 data not found and download not allowed in this context");
89 }
90 auto data = download_bn254_g2_data();
91 write_file(g2_path, data);
92 return from_buffer<g2::affine_element>(data.data());
93}
94} // namespace bb
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:42
std::string format(Args... args)
Definition log.hpp:20
void vinfo(Args... args)
Definition log.hpp:76
const std::vector< FF > data
Entry point for Barretenberg command-line interface.
g2::affine_element get_bn254_g2_data(const std::filesystem::path &path, bool allow_download)
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)
std::vector< uint8_t > exec_pipe(const std::string &command)
Definition exec_pipe.hpp:10
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)