Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
get_grumpkin_crs.cpp
Go to the documentation of this file.
7
8namespace {
9std::vector<uint8_t> download_grumpkin_g1_data(size_t num_points)
10{
11 size_t g1_end = num_points * sizeof(bb::curve::Grumpkin::AffineElement) - 1;
12 std::string url = "https://crs.aztec.network/grumpkin_g1.dat";
13
14 // IMPORTANT: this currently uses a shell, DO NOT let user-controlled strings here.
15 std::string command = "curl -s -H \"Range: bytes=0-" + std::to_string(g1_end) + "\" '" + url + "'";
16
17 auto data = bb::exec_pipe(command);
18 if (data.size() < g1_end) {
19 THROW std::runtime_error("Failed to download grumpkin g1 data.");
20 }
21
22 return data;
23}
24} // namespace
25
26namespace bb {
28 size_t num_points,
29 bool allow_download)
30{
31 // TODO(AD): per Charlie this should just download and replace the flat file portion atomically so we have no race
32 // condition
33 std::filesystem::create_directories(path);
34 auto g1_path = path / "grumpkin_g1.flat.dat";
35 size_t g1_downloaded_points = get_file_size(g1_path) / sizeof(curve::Grumpkin::AffineElement);
36 if (g1_downloaded_points >= num_points) {
37 vinfo("using cached grumpkin crs with num points ", g1_downloaded_points, " at: ", g1_path);
38 auto data = read_file(g1_path, num_points * sizeof(curve::Grumpkin::AffineElement));
40 for (uint32_t i = 0; i < num_points; ++i) {
41 points[i] = from_buffer<curve::Grumpkin::AffineElement>(data, i * sizeof(curve::Grumpkin::AffineElement));
42 }
43 if (points[0].on_curve()) {
44 return points;
45 }
46 }
47 if (!allow_download && g1_downloaded_points == 0) {
48 throw_or_abort("grumpkin g1 data not found and download not allowed in this context");
49 } else if (!allow_download) {
50 throw_or_abort(format("grumpkin g1 data had ",
51 g1_downloaded_points,
52 " points and ",
53 num_points,
54 " were requested but download not allowed in this context"));
55 }
56 vinfo("downloading grumpkin crs...");
57 auto data = download_grumpkin_g1_data(num_points);
58 write_file(path / "grumpkin_g1.flat.dat", data);
59
61 for (uint32_t i = 0; i < num_points; ++i) {
62 points[i] = from_buffer<curve::Grumpkin::AffineElement>(data, i * sizeof(curve::Grumpkin::AffineElement));
63 }
64 return points;
65}
66} // namespace bb
typename Group::affine_element AffineElement
Definition grumpkin.hpp:56
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.
std::vector< curve::Grumpkin::AffineElement > get_grumpkin_g1_data(const std::filesystem::path &path, size_t num_points, bool allow_download)
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:29
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)
#define THROW