Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
precomputed_generators.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "group.hpp"
5#include <cstddef>
6#include <cxxabi.h>
7#include <span>
8#include <sstream>
9namespace bb::detail {
10// Each 'generators' array is specialized in the precomputed_generators_*_impl.hpp files.
11
12// Compile-time string
13template <std::size_t N> struct DomainSeparator {
14 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
15 constexpr DomainSeparator(const char (&str)[N])
16 {
17 for (std::size_t i = 0; i < N; ++i) {
18 value[i] = str[i];
19 }
20 }
21 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
22 char value[N];
23};
24
25// Interface for the generator constants.
26// Each 'generators' array is specialized in the precomputed_generators_*_impl.hpp files for each type domain separator,
27// type, num, starting index tuple that we have in use.
28template <DomainSeparator domain_separator,
29 typename AffineElement,
30 std::size_t num_generators,
31 std::size_t starting_index>
33 // If this is being invoked, there is a missing specialization in the _impl.hpp files.
35 {
36 // Add dependency on template parameters to force lazy evaluation of the static_assert.
37 // This will always evaluate to false if called.
38 static_assert(domain_separator.value[0] == '0',
39 "Should not be called! Relevant precomputed_generators_*_impl.hpp file not included OR does not "
40 "have this specialization.");
41 };
42};
43} // namespace bb::detail
44
45namespace bb {
46
47template <typename Group,
48 detail::DomainSeparator domain_separator, // the object itself
49 std::size_t num_generators,
50 std::size_t starting_index = 0>
52{
53 return detail::PrecomputedGenerators<domain_separator,
54 typename Group::affine_element,
55 num_generators,
56 starting_index>::get_generators();
57}
58
59template <typename Group,
60 detail::DomainSeparator domain_separator, // the object itself
61 std::size_t num_generators,
62 std::size_t starting_index = 0>
64{
65 const auto precomputed = detail::PrecomputedGenerators<domain_separator,
66 typename Group::affine_element,
67 num_generators,
68 starting_index>::get_generators();
69 const auto generators = Group::derive_generators(domain_separator.value, num_generators, starting_index);
70 if (precomputed.size() != generators.size()) {
71 info("Precomputed generators size mismatch");
72 return false;
73 }
74 for (auto [p, g] : zip_view(precomputed, generators)) {
75 if (p != g) {
76 info("WARNING: Generators do not match precomputed generators! THESE SHOULD GENERALLY NOT CHANGE, "
77 "SOMETHING MAY BE WRONG ON A MORE FUNDAMENTAL LEVEL!");
78 info("WARNING: IF YOU REALLY ARE SURE THESE NEED TO BE CHANGED: Include the below code in "
79 "precomputed_generators_*_impl.hpp to hardcode the generators");
80
81 // Demangle the AffineElement type name to get a human-readable string.
82 char* demangled_name_c_str =
83 abi::__cxa_demangle(typeid(typename Group::affine_element).name(), nullptr, nullptr, nullptr);
84 std::string type_name_str =
85 demangled_name_c_str != nullptr
86 ? demangled_name_c_str
87 : typeid(typename Group::affine_element).name(); // Fallback if demangling fails
88
89 // Print the header of the class specialization
90 info("template <> class PrecomputedGenerators<\"",
91 domain_separator.value, // domain_separator.value is char[N]
92 "\", ",
93 type_name_str,
94 ", ",
95 num_generators,
96 ", ",
97 starting_index,
98 "> {");
99
100 // Print the public section and method signature
101 info("with these values for generators: ");
102 for (size_t i = 0; i < generators.size(); ++i) {
103 std::stringstream x_stream;
104 x_stream << generators[i].x;
105 std::stringstream y_stream;
106 y_stream << generators[i].y;
107 const char* suffix = (i == generators.size() - 1) ? "" : ",";
108 info(" { uint256_t(\"", x_stream.str(), "\"), uint256_t(\"", y_stream.str(), "\") }", suffix);
109 }
110 info(" }");
111 info("};");
112
113 // Free the memory allocated by abi::__cxa_demangle
114 if (demangled_name_c_str != nullptr) {
115 std::free(demangled_name_c_str); // NOLINT
116 }
117 return false;
118 }
119 }
120 return true;
121}
122} // namespace bb
void info(Args... args)
Definition log.hpp:70
Entry point for Barretenberg command-line interface.
bool check_precomputed_generators()
constexpr std::span< const typename Group::affine_element > get_precomputed_generators()
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
constexpr DomainSeparator(const char(&str)[N])
static constexpr std::span< AffineElement > get_generators()