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.
5
6namespace {
7const std::string protocol_name = "BARRETENBERG_GRUMPKIN_IPA_CRS";
8}
9
10namespace bb::srs {
11
13{
15
16 parallel_for_range(num_points, [&](size_t start, size_t end) {
17 std::vector<uint8_t> hash_input;
18 for (size_t point_idx = start; point_idx < end; ++point_idx) {
19 bool rational_point_found = false;
20 size_t attempt = 0;
21 while (!rational_point_found) {
22 hash_input.clear();
23 // We hash
24 // |BARRETENBERG_GRUMPKIN_IPA_CRS|POINT_INDEX_IN_LITTLE_ENDIAN|POINT_ATTEMPT_INDEX_IN_LITTLE_ENDIAN|
25 std::copy(protocol_name.begin(), protocol_name.end(), std::back_inserter(hash_input));
26 uint64_t point_index_le_order = htonll(static_cast<uint64_t>(point_idx));
27 uint64_t point_attempt_le_order = htonll(static_cast<uint64_t>(attempt));
28 hash_input.insert(hash_input.end(),
29 reinterpret_cast<uint8_t*>(&point_index_le_order),
30 reinterpret_cast<uint8_t*>(&point_index_le_order) + sizeof(uint64_t));
31 hash_input.insert(hash_input.end(),
32 reinterpret_cast<uint8_t*>(&point_attempt_le_order),
33 reinterpret_cast<uint8_t*>(&point_attempt_le_order) + sizeof(uint64_t));
34 auto hash_result = crypto::sha256(hash_input);
35 uint256_t hash_result_uint(
36 ntohll(*reinterpret_cast<uint64_t*>(hash_result.data())),
37 ntohll(*reinterpret_cast<uint64_t*>(hash_result.data() + sizeof(uint64_t))),
38 ntohll(*reinterpret_cast<uint64_t*>(hash_result.data() + 2 * sizeof(uint64_t))),
39 ntohll(*reinterpret_cast<uint64_t*>(hash_result.data() + 3 * sizeof(uint64_t))));
40 // We try to get a point from the resulting hash
41 auto crs_element = grumpkin::g1::affine_element::from_compressed(hash_result_uint);
42 // If the points coordinates are (0,0) then the compressed representation didn't land on an actual point
43 // (happens half of the time) and we need to continue searching
44 if (!crs_element.x.is_zero() || !crs_element.y.is_zero()) {
45 rational_point_found = true;
46 // Note: there used to be a mutex here, however there is no need as this is just a write to a
47 // computed (exclusive to this thread) memory location
48 srs.at(point_idx) = static_cast<grumpkin::g1::affine_element>(crs_element);
49 break;
50 }
51 attempt += 1;
52 }
53 }
54 });
55
56 return srs;
57}
58
59} // namespace bb::srs
const std::string protocol_name
Sha256Hash sha256(const ByteContainer &input)
Definition sha256.cpp:142
std::vector< grumpkin::g1::affine_element > generate_grumpkin_srs(size_t num_points)
Generates a monomial basis Grumpkin SRS on-the-fly.
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:141
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13