Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
field_conversion.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
15
16namespace bb::field_conversion {
17
26template <typename T> constexpr size_t calc_num_bn254_frs()
27{
29 return 1;
30 } else if constexpr (IsAnyOf<T, bb::fr, grumpkin::fr>) {
31 return T::Params::NUM_BN254_SCALARS;
33 return 2 * calc_num_bn254_frs<typename T::Fq>();
34 } else {
35 // Array or Univariate
36 return calc_num_bn254_frs<typename T::value_type>() * (std::tuple_size<T>::value);
37 }
38}
39
45template <typename T> constexpr size_t calc_num_uint256_ts()
46{
48 return 1;
50 return 2;
51 } else {
52 // Array or Univariate
53 return calc_num_bn254_frs<typename T::value_type>() * (std::tuple_size<T>::value);
54 }
55}
56
58
70template <typename T> T convert_from_bn254_frs(std::span<const bb::fr> fr_vec)
71{
72 if constexpr (IsAnyOf<T, bool>) {
73 BB_ASSERT_EQ(fr_vec.size(), static_cast<size_t>(1));
74 return static_cast<bool>(fr_vec[0]);
75 } else if constexpr (IsAnyOf<T, uint32_t, uint64_t, bb::fr>) {
76 BB_ASSERT_EQ(fr_vec.size(), static_cast<size_t>(1));
77 return static_cast<T>(fr_vec[0]);
78 } else if constexpr (IsAnyOf<T, grumpkin::fr>) {
79 BB_ASSERT_EQ(fr_vec.size(), static_cast<size_t>(2));
82 using BaseField = typename T::Fq;
83 constexpr size_t BASE_FIELD_SCALAR_SIZE = calc_num_bn254_frs<BaseField>();
84 BB_ASSERT_EQ(fr_vec.size(), 2 * BASE_FIELD_SCALAR_SIZE);
85 T val;
86 val.x = convert_from_bn254_frs<BaseField>(fr_vec.subspan(0, BASE_FIELD_SCALAR_SIZE));
87 val.y = convert_from_bn254_frs<BaseField>(fr_vec.subspan(BASE_FIELD_SCALAR_SIZE, BASE_FIELD_SCALAR_SIZE));
88 if (val.x == BaseField::zero() && val.y == BaseField::zero()) {
89 val.self_set_infinity();
90 }
91 ASSERT(val.on_curve());
92 return val;
93 } else {
94 // Array or Univariate
95 T val;
96 constexpr size_t FieldScalarSize = calc_num_bn254_frs<typename T::value_type>();
97 BB_ASSERT_EQ(fr_vec.size(), FieldScalarSize * std::tuple_size<T>::value);
98 size_t i = 0;
99 for (auto& x : val) {
100 x = convert_from_bn254_frs<typename T::value_type>(fr_vec.subspan(FieldScalarSize * i, FieldScalarSize));
101 ++i;
102 }
103 return val;
104 }
105}
106
107template <typename T> T convert_from_uint256_ts(std::span<const uint256_t> uint256_vec)
108{
109 if constexpr (IsAnyOf<T, bool>) {
110 BB_ASSERT_EQ(uint256_vec.size(), static_cast<size_t>(1));
111 return static_cast<bool>(uint256_vec[0]);
113 BB_ASSERT_EQ(uint256_vec.size(), static_cast<size_t>(1));
114 return static_cast<T>(uint256_vec[0]);
116 using BaseField = typename T::Fq;
117 constexpr size_t NUMBER_OF_ELEMENTS = calc_num_uint256_ts<BaseField>();
118 BB_ASSERT_EQ(uint256_vec.size(), 2 * NUMBER_OF_ELEMENTS);
119 T val;
120 val.x = convert_from_uint256_ts<BaseField>(uint256_vec.subspan(0, NUMBER_OF_ELEMENTS));
121 val.y = convert_from_uint256_ts<BaseField>(uint256_vec.subspan(NUMBER_OF_ELEMENTS, NUMBER_OF_ELEMENTS));
122 if (val.x == BaseField::zero() && val.y == BaseField::zero()) {
123 val.self_set_infinity();
124 }
125 ASSERT(val.on_curve());
126 return val;
127 } else {
128 // Array or Univariate
129 T val;
130 constexpr size_t ElementSize = calc_num_uint256_ts<typename T::value_type>();
131 BB_ASSERT_EQ(uint256_vec.size(), ElementSize * std::tuple_size<T>::value);
132 size_t i = 0;
133 for (auto& x : val) {
134 x = convert_from_uint256_ts<typename T::value_type>(uint256_vec.subspan(ElementSize * i, ElementSize));
135 ++i;
136 }
137 return val;
138 }
139}
140
142
152template <typename T> std::vector<bb::fr> convert_to_bn254_frs(const T& val)
153{
155 std::vector<bb::fr> fr_vec{ val };
156 return fr_vec;
157 } else if constexpr (IsAnyOf<T, grumpkin::fr>) {
160 using BaseField = typename T::Fq;
161
162 std::vector<bb::fr> fr_vec_x;
163 std::vector<bb::fr> fr_vec_y;
164 // When encountering a point at infinity we pass a zero point in the proof to ensure that on the receiving size
165 // there are no inconsistencies whenre constructing and hashing.
166 if (val.is_point_at_infinity()) {
167 fr_vec_x = convert_to_bn254_frs(BaseField::zero());
168 fr_vec_y = convert_to_bn254_frs(BaseField::zero());
169 } else {
170 fr_vec_x = convert_to_bn254_frs(val.x);
171 fr_vec_y = convert_to_bn254_frs(val.y);
172 }
173 std::vector<bb::fr> fr_vec(fr_vec_x.begin(), fr_vec_x.end());
174 fr_vec.insert(fr_vec.end(), fr_vec_y.begin(), fr_vec_y.end());
175 return fr_vec;
176 } else {
177 // Array or Univariate
178 std::vector<bb::fr> fr_vec;
179 for (auto& x : val) {
180 auto tmp_vec = convert_to_bn254_frs(x);
181 fr_vec.insert(fr_vec.end(), tmp_vec.begin(), tmp_vec.end());
182 }
183 return fr_vec;
184 }
185}
186
196template <typename T> std::vector<uint256_t> convert_to_uint256(const T& val)
197{
199 std::vector<uint256_t> uint256_vec{ val };
200 return uint256_vec;
202 using BaseField = typename T::Fq;
203
204 std::vector<uint256_t> uint256_vec_x;
205 std::vector<uint256_t> uint256_vec_y;
206 // When encountering a point at infinity we pass a zero point in the proof to ensure that on the receiving size
207 // there are no inconsistencies whenre constructing and hashing.
208 if (val.is_point_at_infinity()) {
209 uint256_vec_x = convert_to_uint256(BaseField::zero());
210 uint256_vec_y = convert_to_uint256(BaseField::zero());
211 } else {
212 uint256_vec_x = convert_to_uint256(val.x);
213 uint256_vec_y = convert_to_uint256(val.y);
214 }
215 std::vector<uint256_t> uint256_vec(uint256_vec_x.begin(), uint256_vec_x.end());
216 uint256_vec.insert(uint256_vec.end(), uint256_vec_y.begin(), uint256_vec_y.end());
217 return uint256_vec;
218 } else {
219 // Array or Univariate
220 std::vector<uint256_t> uint256_vec;
221 for (auto& x : val) {
222 auto tmp_vec = convert_to_uint256(x);
223 uint256_vec.insert(uint256_vec.end(), tmp_vec.begin(), tmp_vec.end());
224 }
225 return uint256_vec;
226 }
227}
228
230
231template <typename T> T inline convert_challenge(const bb::fr& challenge)
232{
233 if constexpr (std::is_same_v<T, bb::fr>) {
234 return challenge;
235 } else if constexpr (std::is_same_v<T, grumpkin::fr>) {
236 return convert_to_grumpkin_fr(challenge);
237 }
238}
239
240} // namespace bb::field_conversion
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:59
#define ASSERT(expression,...)
Definition assert.hpp:49
std::vector< bb::fr > convert_to_bn254_frs(const T &val)
Conversion from transcript values to bb::frs.
std::vector< bb::fr > convert_grumpkin_fr_to_bn254_frs(const grumpkin::fr &val)
Converts grumpkin::fr to 2 bb::fr elements.
constexpr size_t calc_num_uint256_ts()
Calculates the size of a types in terms of uint256_t.
T convert_from_bn254_frs(std::span< const bb::fr > fr_vec)
Conversions from vector of bb::fr elements to transcript types.
constexpr size_t calc_num_bn254_frs()
Calculates the size of a types in terms of bb::frs.
T convert_challenge(const bb::fr &challenge)
T convert_from_uint256_ts(std::span< const uint256_t > uint256_vec)
grumpkin::fr convert_to_grumpkin_fr(const bb::fr &f)
grumpkin::fr convert_grumpkin_fr_from_bn254_frs(std::span< const bb::fr > fr_vec)
Converts 2 bb::fr elements to grumpkin::fr.
std::vector< uint256_t > convert_to_uint256(const T &val)
Conversion from transcript values to bb::frs.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13