Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
grumpkin_srs_gen.cpp
Go to the documentation of this file.
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4
10
11using namespace bb;
12
13const std::string protocol_name = "BARRETENBERG_GRUMPKIN_IPA_CRS";
22int main(int argc, char** argv)
23{
24 std::vector<std::string> args(argv, argv + argc);
25 if (args.size() <= 1) {
26 info("usage: ", args[0], " <subgroup_size> [output_srs_path]");
27 return 1;
28 }
29
30 const size_t subgroup_size = static_cast<size_t>(atoi(args[1].c_str())); // NOLINT
31 const std::filesystem::path srs_path = (args.size() > 2) ? args[2] : "./";
32 std::filesystem::create_directories(srs_path);
33
35
36 parallel_for_range(subgroup_size, [&](size_t start, size_t end) {
37 std::vector<uint8_t> hash_input;
38 for (size_t point_idx = start; point_idx < end; ++point_idx) {
39 bool rational_point_found = false;
40 size_t attempt = 0;
41 while (!rational_point_found) {
42 hash_input.clear();
43 // We hash
44 // |BARRETENBERG_GRUMPKIN_IPA_CRS|POINT_INDEX_IN_LITTLE_ENDIAN|POINT_ATTEMPT_INDEX_IN_LITTLE_ENDIAN|
45 std::copy(protocol_name.begin(), protocol_name.end(), std::back_inserter(hash_input));
46 uint64_t point_index_le_order = htonll(static_cast<uint64_t>(point_idx));
47 uint64_t point_attempt_le_order = htonll(static_cast<uint64_t>(attempt));
48 hash_input.insert(hash_input.end(),
49 reinterpret_cast<uint8_t*>(&point_index_le_order),
50 reinterpret_cast<uint8_t*>(&point_index_le_order) + sizeof(uint64_t));
51 hash_input.insert(hash_input.end(),
52 reinterpret_cast<uint8_t*>(&point_attempt_le_order),
53 reinterpret_cast<uint8_t*>(&point_attempt_le_order) + sizeof(uint64_t));
54 auto hash_result = crypto::sha256(hash_input);
55 uint256_t hash_result_uint(
56 ntohll(*reinterpret_cast<uint64_t*>(hash_result.data())),
57 ntohll(*reinterpret_cast<uint64_t*>(hash_result.data() + sizeof(uint64_t))),
58 ntohll(*reinterpret_cast<uint64_t*>(hash_result.data() + 2 * sizeof(uint64_t))),
59 ntohll(*reinterpret_cast<uint64_t*>(hash_result.data() + 3 * sizeof(uint64_t))));
60 // We try to get a point from the resulting hash
61 auto crs_element = grumpkin::g1::affine_element::from_compressed(hash_result_uint);
62 // If the points coordinates are (0,0) then the compressed representation didn't land on an actual point
63 // (happens half of the time) and we need to continue searching
64 if (!crs_element.x.is_zero() || !crs_element.y.is_zero()) {
65 rational_point_found = true;
66 // Note: there used to be a mutex here, however there is no need as this is just a write to a
67 // computed (exclusive to this thread) memory location
68 srs.at(point_idx) = static_cast<grumpkin::g1::affine_element>(crs_element);
69 break;
70 }
71 attempt += 1;
72 }
73 }
74 });
75
76 write_file(srs_path / "grumpkin_g1.dat", to_buffer(srs));
77
78 return 0;
79}
void info(Args... args)
Definition log.hpp:70
int main(int argc, char **argv)
Generates a monomial basis Grumpkin SRS.
const std::string protocol_name
Sha256Hash sha256(const ByteContainer &input)
Definition sha256.cpp:142
Entry point for Barretenberg command-line interface.
void write_file(const std::string &filename, std::vector< uint8_t > const &data)
Definition file_io.hpp:58
void parallel_for_range(size_t num_points, const std::function< void(size_t, size_t)> &func, size_t no_multhreading_if_less_or_equal)
Split a loop into several loops running in parallel.
Definition thread.cpp:102
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< uint8_t > to_buffer(T const &value)