Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
field_declarations.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
14#include <array>
15#include <cstdint>
16#include <iostream>
17#include <random>
18#include <span>
19
20#ifndef DISABLE_ASM
21#ifdef __BMI2__
22#define BBERG_NO_ASM 0
23#else
24#define BBERG_NO_ASM 1
25#endif
26#else
27#define BBERG_NO_ASM 1
28#endif
29
30namespace bb {
36template <class Params_> struct alignas(32) field {
37 public:
38 using View = field;
40 using Params = Params_;
41 using in_buf = const uint8_t*;
42 using vec_in_buf = const uint8_t*;
43 using out_buf = uint8_t*;
44 using vec_out_buf = uint8_t**;
45
46 // The number of element required to represent field<Params_> in the public inputs of a circuit
47 static constexpr size_t PUBLIC_INPUTS_SIZE = Params::PUBLIC_INPUTS_SIZE;
48
49#if defined(__wasm__) || !defined(__SIZEOF_INT128__)
50#define WASM_NUM_LIMBS 9
51#define WASM_LIMB_BITS 29
52#endif
53
54 // We don't initialize data in the default constructor since we'd lose a lot of time on huge array initializations.
55 // Other alternatives have been noted, such as casting to get around constructors where they matter,
56 // however it is felt that sanitizer tools (e.g. MSAN) can detect garbage well, whereas doing
57 // hacky casts where needed would require rework to critical algos like MSM, FFT, Sumcheck.
58 // Instead, the recommended solution is use an explicit {} where initialization is important:
59 // field f; // not initialized
60 // field f{}; // zero-initialized
61 // std::array<field, N> arr; // not initialized, good for huge N
62 // std::array<field, N> arr {}; // zero-initialized, preferable for moderate N
63 field() = default;
64
65 constexpr field(const numeric::uint256_t& input) noexcept
66 : data{ input.data[0], input.data[1], input.data[2], input.data[3] }
67 {
69 }
70
71 constexpr field(const uint128_t& input) noexcept
73 {}
74
75 // NOLINTNEXTLINE (unsigned long is platform dependent, which we want in this case)
76 constexpr field(const unsigned long input) noexcept
77 : data{ input, 0, 0, 0 }
78 {
80 }
81
82 constexpr field(const unsigned int input) noexcept
83 : data{ input, 0, 0, 0 }
84 {
86 }
87
88 // NOLINTNEXTLINE (unsigned long long is platform dependent, which we want in this case)
89 constexpr field(const unsigned long long input) noexcept
90 : data{ input, 0, 0, 0 }
91 {
93 }
94
95 constexpr field(const int input) noexcept
96 : data{ 0, 0, 0, 0 }
97 {
98 if (input < 0) {
99 data[0] = static_cast<uint64_t>(-input);
100 data[1] = 0;
101 data[2] = 0;
102 data[3] = 0;
104 self_neg();
106 } else {
107 data[0] = static_cast<uint64_t>(input);
108 data[1] = 0;
109 data[2] = 0;
110 data[3] = 0;
112 }
113 }
114
115 constexpr field(const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t d) noexcept
116 : data{ a, b, c, d } {};
117
124 constexpr explicit field(const uint512_t& input) noexcept
125 {
126 uint256_t value = (input % modulus).lo;
127 data[0] = value.data[0];
128 data[1] = value.data[1];
129 data[2] = value.data[2];
130 data[3] = value.data[3];
132 }
133
134 constexpr explicit field(std::string input) noexcept
135 {
136 uint256_t value(input);
137 *this = field(value);
138 }
139
140 constexpr explicit operator bool() const
141 {
143 ASSERT_IN_CONSTEXPR(out.data[0] == 0 || out.data[0] == 1);
144 return static_cast<bool>(out.data[0]);
145 }
146
147 constexpr explicit operator uint8_t() const
148 {
150 return static_cast<uint8_t>(out.data[0]);
151 }
152
153 constexpr explicit operator uint16_t() const
154 {
156 return static_cast<uint16_t>(out.data[0]);
157 }
158
159 constexpr explicit operator uint32_t() const
160 {
162 return static_cast<uint32_t>(out.data[0]);
163 }
164
165 constexpr explicit operator uint64_t() const
166 {
168 return out.data[0];
169 }
170
171 constexpr explicit operator uint128_t() const
172 {
174 uint128_t lo = out.data[0];
175 uint128_t hi = out.data[1];
176 return (hi << 64) | lo;
177 }
178
179 constexpr operator uint256_t() const noexcept
180 {
182 return uint256_t(out.data[0], out.data[1], out.data[2], out.data[3]);
183 }
184
185 [[nodiscard]] constexpr uint256_t uint256_t_no_montgomery_conversion() const noexcept
186 {
187 return { data[0], data[1], data[2], data[3] };
188 }
189
190 constexpr field(const field& other) noexcept = default;
191 constexpr field(field&& other) noexcept = default;
192 constexpr field& operator=(const field& other) & noexcept = default;
193 constexpr field& operator=(field&& other) & noexcept = default;
194 constexpr ~field() noexcept = default;
195 alignas(32) uint64_t data[4]; // NOLINT
196
197 static constexpr uint256_t modulus =
198 uint256_t{ Params::modulus_0, Params::modulus_1, Params::modulus_2, Params::modulus_3 };
199#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
200 static constexpr uint256_t r_squared_uint{
201 Params_::r_squared_0, Params_::r_squared_1, Params_::r_squared_2, Params_::r_squared_3
202 };
203#else
204 static constexpr uint256_t r_squared_uint{
205 Params_::r_squared_wasm_0, Params_::r_squared_wasm_1, Params_::r_squared_wasm_2, Params_::r_squared_wasm_3
206 };
207 static constexpr std::array<uint64_t, 9> wasm_modulus = { Params::modulus_wasm_0, Params::modulus_wasm_1,
208 Params::modulus_wasm_2, Params::modulus_wasm_3,
209 Params::modulus_wasm_4, Params::modulus_wasm_5,
210 Params::modulus_wasm_6, Params::modulus_wasm_7,
211 Params::modulus_wasm_8 };
212 static constexpr std::array<uint64_t, 9> wasm_r_inv = {
213 Params::r_inv_wasm_0, Params::r_inv_wasm_1, Params::r_inv_wasm_2, Params::r_inv_wasm_3, Params::r_inv_wasm_4,
214 Params::r_inv_wasm_5, Params::r_inv_wasm_6, Params::r_inv_wasm_7, Params::r_inv_wasm_8
215 };
216
217#endif
218 static constexpr field cube_root_of_unity()
219 {
220 // endomorphism i.e. lambda * [P] = (beta * x, y)
221 if constexpr (Params::cube_root_0 != 0) {
222#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
223 constexpr field result{
224 Params::cube_root_0, Params::cube_root_1, Params::cube_root_2, Params::cube_root_3
225 };
226#else
227 constexpr field result{
228 Params::cube_root_wasm_0, Params::cube_root_wasm_1, Params::cube_root_wasm_2, Params::cube_root_wasm_3
229 };
230#endif
231 return result;
232 } else {
233 constexpr field two_inv = field(2).invert();
234 constexpr field numerator = (-field(3)).sqrt() - field(1);
235 constexpr field result = two_inv * numerator;
236 return result;
237 }
238 }
239
240 static constexpr field zero() { return field(0, 0, 0, 0); }
241 static constexpr field neg_one() { return -field(1); }
242 static constexpr field one() { return field(1); }
243
245 {
246#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
247 const field result{
248 Params::coset_generators_0[7],
249 Params::coset_generators_1[7],
250 Params::coset_generators_2[7],
251 Params::coset_generators_3[7],
252 };
253#else
254 const field result{
255 Params::coset_generators_wasm_0[7],
256 Params::coset_generators_wasm_1[7],
257 Params::coset_generators_wasm_2[7],
258 Params::coset_generators_wasm_3[7],
259 };
260#endif
261
262 return result;
263 }
264
265 static constexpr field tag_coset_generator()
266 {
267#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
268 const field result{
269 Params::coset_generators_0[6],
270 Params::coset_generators_1[6],
271 Params::coset_generators_2[6],
272 Params::coset_generators_3[6],
273 };
274#else
275 const field result{
276 Params::coset_generators_wasm_0[6],
277 Params::coset_generators_wasm_1[6],
278 Params::coset_generators_wasm_2[6],
279 Params::coset_generators_wasm_3[6],
280 };
281#endif
282
283 return result;
284 }
285
286 template <size_t idx> static constexpr field coset_generator()
287 {
288 static_assert(idx < 7);
289#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
290 const field result{
291 Params::coset_generators_0[idx],
292 Params::coset_generators_1[idx],
293 Params::coset_generators_2[idx],
294 Params::coset_generators_3[idx],
295 };
296#else
297 const field result{
298 Params::coset_generators_wasm_0[idx],
299 Params::coset_generators_wasm_1[idx],
300 Params::coset_generators_wasm_2[idx],
301 Params::coset_generators_wasm_3[idx],
302 };
303#endif
304
305 return result;
306 }
307
308 BB_INLINE constexpr field operator*(const field& other) const noexcept;
309 BB_INLINE constexpr field operator+(const field& other) const noexcept;
310 BB_INLINE constexpr field operator-(const field& other) const noexcept;
311 BB_INLINE constexpr field operator-() const noexcept;
312 constexpr field operator/(const field& other) const noexcept;
313
314 // prefix increment (++x)
315 BB_INLINE constexpr field operator++() noexcept;
316 // postfix increment (x++)
317 // NOLINTNEXTLINE
318 BB_INLINE constexpr field operator++(int) noexcept;
319
320 BB_INLINE constexpr field& operator*=(const field& other) & noexcept;
321 BB_INLINE constexpr field& operator+=(const field& other) & noexcept;
322 BB_INLINE constexpr field& operator-=(const field& other) & noexcept;
323 constexpr field& operator/=(const field& other) & noexcept;
324
325 // NOTE: comparison operators exist so that `field` is comparible with stl methods that require them.
326 // (e.g. std::sort)
327 // Finite fields do not have an explicit ordering, these should *NEVER* be used in algebraic algorithms.
328 BB_INLINE constexpr bool operator>(const field& other) const noexcept;
329 BB_INLINE constexpr bool operator<(const field& other) const noexcept;
330 BB_INLINE constexpr bool operator==(const field& other) const noexcept;
331 BB_INLINE constexpr bool operator!=(const field& other) const noexcept;
332
333 BB_INLINE constexpr field to_montgomery_form() const noexcept;
334 BB_INLINE constexpr field from_montgomery_form() const noexcept;
335
336 BB_INLINE constexpr field sqr() const noexcept;
337 BB_INLINE constexpr void self_sqr() & noexcept;
338
339 BB_INLINE constexpr field pow(const uint256_t& exponent) const noexcept;
340 BB_INLINE constexpr field pow(uint64_t exponent) const noexcept;
341 // STARKNET: next line was commented as stark252 violates the assertion
342 // static_assert(Params::modulus_0 != 1);
343 static constexpr uint256_t modulus_minus_two =
344 uint256_t(Params::modulus_0 - 2ULL, Params::modulus_1, Params::modulus_2, Params::modulus_3);
345 constexpr field invert() const noexcept;
346 template <typename C>
347 // has size() and operator[].
348 requires requires(C& c) {
349 { c.size() } -> std::convertible_to<size_t>;
350 { c[0] };
351 }
352 static void batch_invert(C& coeffs) noexcept;
353 static void batch_invert(field* coeffs, size_t n) noexcept;
354 static void batch_invert(std::span<field> coeffs) noexcept;
360 constexpr std::pair<bool, field> sqrt() const noexcept
361 requires((Params_::modulus_0 & 0x3UL) == 0x3UL);
362 constexpr std::pair<bool, field> sqrt() const noexcept
363 requires((Params_::modulus_0 & 0x3UL) != 0x3UL);
364 BB_INLINE constexpr void self_neg() & noexcept;
365
366 BB_INLINE constexpr void self_to_montgomery_form() & noexcept;
367 BB_INLINE constexpr void self_from_montgomery_form() & noexcept;
368
369 BB_INLINE constexpr void self_conditional_negate(uint64_t predicate) & noexcept;
370
371 BB_INLINE constexpr field reduce_once() const noexcept;
372 BB_INLINE constexpr void self_reduce_once() & noexcept;
373
374 BB_INLINE constexpr void self_set_msb() & noexcept;
375 [[nodiscard]] BB_INLINE constexpr bool is_msb_set() const noexcept;
376 [[nodiscard]] BB_INLINE constexpr uint64_t is_msb_set_word() const noexcept;
377
378 [[nodiscard]] BB_INLINE constexpr bool is_zero() const noexcept;
379
380 static constexpr field get_root_of_unity(size_t subgroup_size) noexcept;
381
382 static void serialize_to_buffer(const field& value, uint8_t* buffer) { write(buffer, value); }
383
384 static field serialize_from_buffer(const uint8_t* buffer) { return from_buffer<field>(buffer); }
385
386 template <class V> static field reconstruct_from_public(const std::span<const field<V>, PUBLIC_INPUTS_SIZE>& limbs);
387
388 [[nodiscard]] BB_INLINE std::vector<uint8_t> to_buffer() const { return ::to_buffer(*this); }
389
390 struct wide_array {
391 uint64_t data[8]; // NOLINT
392 };
393 BB_INLINE constexpr wide_array mul_512(const field& other) const noexcept;
394 BB_INLINE constexpr wide_array sqr_512() const noexcept;
395
396 BB_INLINE constexpr field conditionally_subtract_from_double_modulus(const uint64_t predicate) const noexcept
397 {
398 if (predicate != 0) {
399 constexpr field p{
401 };
402 return p - *this;
403 }
404 return *this;
405 }
406
431 static void split_into_endomorphism_scalars(const field& k, field& k1, field& k2)
432 {
433 // if the modulus is a >= 255-bit integer, we need to use a basis where g1, g2 have been shifted by 2^384
434 if constexpr (Params::modulus_3 >= 0x4000000000000000ULL) {
436 } else {
438 k1.data[0] = ret.first[0];
439 k1.data[1] = ret.first[1];
440
441 // TODO(https://github.com/AztecProtocol/barretenberg/issues/851): We should move away from this hack by
442 // returning pair of uint64_t[2] instead of a half-set field
443#if !defined(__clang__) && defined(__GNUC__)
444#pragma GCC diagnostic push
445#pragma GCC diagnostic ignored "-Warray-bounds"
446#endif
447 k2.data[0] = ret.second[0]; // NOLINT
448 k2.data[1] = ret.second[1];
449#if !defined(__clang__) && defined(__GNUC__)
450#pragma GCC diagnostic pop
451#endif
452 }
453 }
454
455 // NOTE: this form is only usable if the modulus is 254 bits or less, otherwise see
456 // split_into_endomorphism_scalars_384.
457 // TODO(https://github.com/AztecProtocol/barretenberg/issues/851): Unify these APIs.
459 {
460 static_assert(Params::modulus_3 < 0x4000000000000000ULL);
461 field input = k.reduce_once();
462
463 constexpr field endo_g1 = { Params::endo_g1_lo, Params::endo_g1_mid, Params::endo_g1_hi, 0 };
464
465 constexpr field endo_g2 = { Params::endo_g2_lo, Params::endo_g2_mid, 0, 0 };
466
467 constexpr field endo_minus_b1 = { Params::endo_minus_b1_lo, Params::endo_minus_b1_mid, 0, 0 };
468
469 constexpr field endo_b2 = { Params::endo_b2_lo, Params::endo_b2_mid, 0, 0 };
470
471 // compute c1 = (g2 * k) >> 256
472 wide_array c1 = endo_g2.mul_512(input);
473 // compute c2 = (g1 * k) >> 256
474 wide_array c2 = endo_g1.mul_512(input);
475
476 // (the bit shifts are implicit, as we only utilize the high limbs of c1, c2
477
478 field c1_hi = {
479 c1.data[4], c1.data[5], c1.data[6], c1.data[7]
480 }; // *(field*)((uintptr_t)(&c1) + (4 * sizeof(uint64_t)));
481 field c2_hi = {
482 c2.data[4], c2.data[5], c2.data[6], c2.data[7]
483 }; // *(field*)((uintptr_t)(&c2) + (4 * sizeof(uint64_t)));
484
485 // compute q1 = c1 * -b1
486 wide_array q1 = c1_hi.mul_512(endo_minus_b1);
487 // compute q2 = c2 * b2
488 wide_array q2 = c2_hi.mul_512(endo_b2);
489
490 // FIX: Avoid using 512-bit multiplication as its not necessary.
491 // c1_hi, c2_hi can be uint256_t's and the final result (without montgomery reduction)
492 // could be casted to a field.
493 field q1_lo{ q1.data[0], q1.data[1], q1.data[2], q1.data[3] };
494 field q2_lo{ q2.data[0], q2.data[1], q2.data[2], q2.data[3] };
495
496 field t1 = (q2_lo - q1_lo).reduce_once();
497 field beta = cube_root_of_unity();
498 field t2 = (t1 * beta + input).reduce_once();
499 return {
500 { t2.data[0], t2.data[1] },
501 { t1.data[0], t1.data[1] },
502 };
503 }
504
505 static void split_into_endomorphism_scalars_384(const field& input, field& k1_out, field& k2_out)
506 {
507 constexpr field minus_b1f{
508 Params::endo_minus_b1_lo,
509 Params::endo_minus_b1_mid,
510 0,
511 0,
512 };
513 constexpr field b2f{
514 Params::endo_b2_lo,
515 Params::endo_b2_mid,
516 0,
517 0,
518 };
519 constexpr uint256_t g1{
520 Params::endo_g1_lo,
521 Params::endo_g1_mid,
522 Params::endo_g1_hi,
523 Params::endo_g1_hihi,
524 };
525 constexpr uint256_t g2{
526 Params::endo_g2_lo,
527 Params::endo_g2_mid,
528 Params::endo_g2_hi,
529 Params::endo_g2_hihi,
530 };
531
532 field kf = input.reduce_once();
533 uint256_t k{ kf.data[0], kf.data[1], kf.data[2], kf.data[3] };
534
535 uint512_t c1 = (uint512_t(k) * static_cast<uint512_t>(g1)) >> 384;
536 uint512_t c2 = (uint512_t(k) * static_cast<uint512_t>(g2)) >> 384;
537
538 field c1f{ c1.lo.data[0], c1.lo.data[1], c1.lo.data[2], c1.lo.data[3] };
539 field c2f{ c2.lo.data[0], c2.lo.data[1], c2.lo.data[2], c2.lo.data[3] };
540
541 c1f.self_to_montgomery_form();
542 c2f.self_to_montgomery_form();
543 c1f = c1f * minus_b1f;
544 c2f = c2f * b2f;
545 field r2f = c1f - c2f;
546 field beta = cube_root_of_unity();
547 field r1f = input.reduce_once() - r2f * beta;
548 k1_out = r1f;
549 k2_out = -r2f;
550 }
551
552 // static constexpr auto coset_generators = compute_coset_generators();
553 // static constexpr std::array<field, 15> coset_generators = compute_coset_generators((1 << 30U));
554
555 friend std::ostream& operator<<(std::ostream& os, const field& a)
556 {
558 std::ios_base::fmtflags f(os.flags());
559 os << std::hex << "0x" << std::setfill('0') << std::setw(16) << out.data[3] << std::setw(16) << out.data[2]
560 << std::setw(16) << out.data[1] << std::setw(16) << out.data[0];
561 os.flags(f);
562 return os;
563 }
564
565 BB_INLINE static void __copy(const field& a, field& r) noexcept { r = a; } // NOLINT
566 BB_INLINE static void __swap(field& src, field& dest) noexcept // NOLINT
567 {
568 field T = dest;
569 dest = src;
570 src = T;
571 }
572
573 static field random_element(numeric::RNG* engine = nullptr) noexcept;
574
575 static constexpr field multiplicative_generator() noexcept;
576
577 // For serialization
578 void msgpack_pack(auto& packer) const;
579 void msgpack_unpack(auto o);
580 void msgpack_schema(auto& packer) const { packer.pack_alias(Params::schema_name, "bin32"); }
581
583 static constexpr uint256_t not_modulus = -modulus;
585
586 struct wnaf_table {
587 uint8_t windows[64]; // NOLINT
588
589 constexpr wnaf_table(const uint256_t& target)
590 : windows{
591 static_cast<uint8_t>(target.data[0] & 15), static_cast<uint8_t>((target.data[0] >> 4) & 15),
592 static_cast<uint8_t>((target.data[0] >> 8) & 15), static_cast<uint8_t>((target.data[0] >> 12) & 15),
593 static_cast<uint8_t>((target.data[0] >> 16) & 15), static_cast<uint8_t>((target.data[0] >> 20) & 15),
594 static_cast<uint8_t>((target.data[0] >> 24) & 15), static_cast<uint8_t>((target.data[0] >> 28) & 15),
595 static_cast<uint8_t>((target.data[0] >> 32) & 15), static_cast<uint8_t>((target.data[0] >> 36) & 15),
596 static_cast<uint8_t>((target.data[0] >> 40) & 15), static_cast<uint8_t>((target.data[0] >> 44) & 15),
597 static_cast<uint8_t>((target.data[0] >> 48) & 15), static_cast<uint8_t>((target.data[0] >> 52) & 15),
598 static_cast<uint8_t>((target.data[0] >> 56) & 15), static_cast<uint8_t>((target.data[0] >> 60) & 15),
599 static_cast<uint8_t>(target.data[1] & 15), static_cast<uint8_t>((target.data[1] >> 4) & 15),
600 static_cast<uint8_t>((target.data[1] >> 8) & 15), static_cast<uint8_t>((target.data[1] >> 12) & 15),
601 static_cast<uint8_t>((target.data[1] >> 16) & 15), static_cast<uint8_t>((target.data[1] >> 20) & 15),
602 static_cast<uint8_t>((target.data[1] >> 24) & 15), static_cast<uint8_t>((target.data[1] >> 28) & 15),
603 static_cast<uint8_t>((target.data[1] >> 32) & 15), static_cast<uint8_t>((target.data[1] >> 36) & 15),
604 static_cast<uint8_t>((target.data[1] >> 40) & 15), static_cast<uint8_t>((target.data[1] >> 44) & 15),
605 static_cast<uint8_t>((target.data[1] >> 48) & 15), static_cast<uint8_t>((target.data[1] >> 52) & 15),
606 static_cast<uint8_t>((target.data[1] >> 56) & 15), static_cast<uint8_t>((target.data[1] >> 60) & 15),
607 static_cast<uint8_t>(target.data[2] & 15), static_cast<uint8_t>((target.data[2] >> 4) & 15),
608 static_cast<uint8_t>((target.data[2] >> 8) & 15), static_cast<uint8_t>((target.data[2] >> 12) & 15),
609 static_cast<uint8_t>((target.data[2] >> 16) & 15), static_cast<uint8_t>((target.data[2] >> 20) & 15),
610 static_cast<uint8_t>((target.data[2] >> 24) & 15), static_cast<uint8_t>((target.data[2] >> 28) & 15),
611 static_cast<uint8_t>((target.data[2] >> 32) & 15), static_cast<uint8_t>((target.data[2] >> 36) & 15),
612 static_cast<uint8_t>((target.data[2] >> 40) & 15), static_cast<uint8_t>((target.data[2] >> 44) & 15),
613 static_cast<uint8_t>((target.data[2] >> 48) & 15), static_cast<uint8_t>((target.data[2] >> 52) & 15),
614 static_cast<uint8_t>((target.data[2] >> 56) & 15), static_cast<uint8_t>((target.data[2] >> 60) & 15),
615 static_cast<uint8_t>(target.data[3] & 15), static_cast<uint8_t>((target.data[3] >> 4) & 15),
616 static_cast<uint8_t>((target.data[3] >> 8) & 15), static_cast<uint8_t>((target.data[3] >> 12) & 15),
617 static_cast<uint8_t>((target.data[3] >> 16) & 15), static_cast<uint8_t>((target.data[3] >> 20) & 15),
618 static_cast<uint8_t>((target.data[3] >> 24) & 15), static_cast<uint8_t>((target.data[3] >> 28) & 15),
619 static_cast<uint8_t>((target.data[3] >> 32) & 15), static_cast<uint8_t>((target.data[3] >> 36) & 15),
620 static_cast<uint8_t>((target.data[3] >> 40) & 15), static_cast<uint8_t>((target.data[3] >> 44) & 15),
621 static_cast<uint8_t>((target.data[3] >> 48) & 15), static_cast<uint8_t>((target.data[3] >> 52) & 15),
622 static_cast<uint8_t>((target.data[3] >> 56) & 15), static_cast<uint8_t>((target.data[3] >> 60) & 15)
623 }
624 {}
625 };
626
627#if defined(__wasm__) || !defined(__SIZEOF_INT128__)
628 BB_INLINE static constexpr void wasm_madd(uint64_t& left_limb,
629 const std::array<uint64_t, WASM_NUM_LIMBS>& right_limbs,
630 uint64_t& result_0,
631 uint64_t& result_1,
632 uint64_t& result_2,
633 uint64_t& result_3,
634 uint64_t& result_4,
635 uint64_t& result_5,
636 uint64_t& result_6,
637 uint64_t& result_7,
638 uint64_t& result_8);
639 BB_INLINE static constexpr void wasm_reduce(uint64_t& result_0,
640 uint64_t& result_1,
641 uint64_t& result_2,
642 uint64_t& result_3,
643 uint64_t& result_4,
644 uint64_t& result_5,
645 uint64_t& result_6,
646 uint64_t& result_7,
647 uint64_t& result_8);
648 BB_INLINE static constexpr void wasm_reduce_yuval(uint64_t& result_0,
649 uint64_t& result_1,
650 uint64_t& result_2,
651 uint64_t& result_3,
652 uint64_t& result_4,
653 uint64_t& result_5,
654 uint64_t& result_6,
655 uint64_t& result_7,
656 uint64_t& result_8,
657 uint64_t& result_9);
658 BB_INLINE static constexpr std::array<uint64_t, WASM_NUM_LIMBS> wasm_convert(const uint64_t* data);
659#endif
660 BB_INLINE static constexpr std::pair<uint64_t, uint64_t> mul_wide(uint64_t a, uint64_t b) noexcept;
661
662 BB_INLINE static constexpr uint64_t mac(
663 uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t& carry_out) noexcept;
664
665 BB_INLINE static constexpr void mac(
666 uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t& out, uint64_t& carry_out) noexcept;
667
668 BB_INLINE static constexpr uint64_t mac_mini(uint64_t a, uint64_t b, uint64_t c, uint64_t& out) noexcept;
669
670 BB_INLINE static constexpr void mac_mini(
671 uint64_t a, uint64_t b, uint64_t c, uint64_t& out, uint64_t& carry_out) noexcept;
672
673 BB_INLINE static constexpr uint64_t mac_discard_lo(uint64_t a, uint64_t b, uint64_t c) noexcept;
674
675 BB_INLINE static constexpr uint64_t addc(uint64_t a, uint64_t b, uint64_t carry_in, uint64_t& carry_out) noexcept;
676
677 BB_INLINE static constexpr uint64_t sbb(uint64_t a, uint64_t b, uint64_t borrow_in, uint64_t& borrow_out) noexcept;
678
679 BB_INLINE static constexpr uint64_t square_accumulate(uint64_t a,
680 uint64_t b,
681 uint64_t c,
682 uint64_t carry_in_lo,
683 uint64_t carry_in_hi,
684 uint64_t& carry_lo,
685 uint64_t& carry_hi) noexcept;
686 BB_INLINE constexpr field reduce() const noexcept;
687 BB_INLINE constexpr field add(const field& other) const noexcept;
688 BB_INLINE constexpr field subtract(const field& other) const noexcept;
689 BB_INLINE constexpr field subtract_coarse(const field& other) const noexcept;
690 BB_INLINE constexpr field montgomery_mul(const field& other) const noexcept;
691 BB_INLINE constexpr field montgomery_mul_big(const field& other) const noexcept;
692 BB_INLINE constexpr field montgomery_square() const noexcept;
693
694#if (BBERG_NO_ASM == 0)
695 BB_INLINE static field asm_mul(const field& a, const field& b) noexcept;
696 BB_INLINE static field asm_sqr(const field& a) noexcept;
697 BB_INLINE static field asm_add(const field& a, const field& b) noexcept;
698 BB_INLINE static field asm_sub(const field& a, const field& b) noexcept;
699 BB_INLINE static field asm_mul_with_coarse_reduction(const field& a, const field& b) noexcept;
700 BB_INLINE static field asm_sqr_with_coarse_reduction(const field& a) noexcept;
701 BB_INLINE static field asm_add_with_coarse_reduction(const field& a, const field& b) noexcept;
702 BB_INLINE static field asm_sub_with_coarse_reduction(const field& a, const field& b) noexcept;
703 BB_INLINE static field asm_add_without_reduction(const field& a, const field& b) noexcept;
704 BB_INLINE static void asm_self_sqr(const field& a) noexcept;
705 BB_INLINE static void asm_self_add(const field& a, const field& b) noexcept;
706 BB_INLINE static void asm_self_sub(const field& a, const field& b) noexcept;
707 BB_INLINE static void asm_self_mul_with_coarse_reduction(const field& a, const field& b) noexcept;
708 BB_INLINE static void asm_self_sqr_with_coarse_reduction(const field& a) noexcept;
709 BB_INLINE static void asm_self_add_with_coarse_reduction(const field& a, const field& b) noexcept;
710 BB_INLINE static void asm_self_sub_with_coarse_reduction(const field& a, const field& b) noexcept;
711 BB_INLINE static void asm_self_add_without_reduction(const field& a, const field& b) noexcept;
712
713 BB_INLINE static void asm_conditional_negate(field& r, uint64_t predicate) noexcept;
714 BB_INLINE static field asm_reduce_once(const field& a) noexcept;
715 BB_INLINE static void asm_self_reduce_once(const field& a) noexcept;
716 static constexpr uint64_t zero_reference = 0x00ULL;
717#endif
718 static constexpr size_t COSET_GENERATOR_SIZE = 15;
719 constexpr field tonelli_shanks_sqrt() const noexcept;
720 static constexpr size_t primitive_root_log_size() noexcept;
721 static constexpr std::array<field, COSET_GENERATOR_SIZE> compute_coset_generators() noexcept;
722
723#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
724 static constexpr uint128_t lo_mask = 0xffffffffffffffffUL;
725#endif
726};
727
728template <typename B, typename Params> void read(B& it, field<Params>& value)
729{
730 using serialize::read;
731 field<Params> result{ 0, 0, 0, 0 };
732 read(it, result.data[3]);
733 read(it, result.data[2]);
734 read(it, result.data[1]);
735 read(it, result.data[0]);
736 value = result.to_montgomery_form();
737}
738template <typename B, typename Params> void write(B& buf, field<Params> const& value)
739{
740 using serialize::write;
741 const field input = value.from_montgomery_form();
742 write(buf, input.data[3]);
743 write(buf, input.data[2]);
744 write(buf, input.data[1]);
745 write(buf, input.data[0]);
746}
747
748} // namespace bb
749
750// Define hash function for field elements, e.g., so that it can be used in maps.
751// See https://en.cppreference.com/w/cpp/utility/hash .
752template <typename Params> struct std::hash<bb::field<Params>> {
753 std::size_t operator()(const bb::field<Params>& ff) const noexcept
754 {
755 // Just like in equality, we need to reduce the field element before hashing.
756 auto reduced = ff.reduce_once();
757 return bb::utils::hash_as_tuple(reduced.data[0], reduced.data[1], reduced.data[2], reduced.data[3]);
758 }
759};
#define ASSERT_IN_CONSTEXPR(expression,...)
Definition assert.hpp:68
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Definition group.hpp:36
static constexpr uint256_t from_uint128(const uint128_t a) noexcept
Definition uint256.hpp:94
#define BB_INLINE
FF a
FF b
uint8_t const * buf
Definition data_store.hpp:9
uint8_t buffer[RANDOM_BUFFER_SIZE]
Definition engine.cpp:34
uintx< uint256_t > uint512_t
Definition uintx.hpp:307
size_t hash_as_tuple(const Ts &... ts)
Definition utils.hpp:22
Entry point for Barretenberg command-line interface.
group< fq2, fr, Bn254G2Params > g2
Definition g2.hpp:39
group< fq, fr, Bn254G1Params > g1
Definition g1.hpp:33
void write(std::vector< uint8_t > &buf, ClientIVC::VerificationKey const &vk)
void read(uint8_t const *&it, ClientIVC::VerificationKey &vk)
void read(auto &it, msgpack_concepts::HasMsgPack auto &obj)
Automatically derived read for any object that defines .msgpack() (implicitly defined by MSGPACK_FIEL...
void write(auto &buf, const msgpack_concepts::HasMsgPack auto &obj)
Automatically derived write for any object that defines .msgpack() (implicitly defined by MSGPACK_FIE...
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:44
constexpr wnaf_table(const uint256_t &target)
General class for prime fields see Prime field documentation["field documentation"] for general imple...
static constexpr field cube_root_of_unity()
constexpr field(const int input) noexcept
field()=default
constexpr ~field() noexcept=default
static constexpr std::array< uint64_t, 9 > wasm_modulus
static constexpr field get_root_of_unity(size_t subgroup_size) noexcept
static constexpr field neg_one()
static constexpr field one()
static constexpr uint256_t modulus
BB_INLINE constexpr void self_reduce_once() &noexcept
static BB_INLINE constexpr std::array< uint64_t, WASM_NUM_LIMBS > wasm_convert(const uint64_t *data)
Convert 4 64-bit limbs into 9 29-bit limbs.
constexpr field(const unsigned long long input) noexcept
BB_INLINE constexpr field operator*(const field &other) const noexcept
BB_INLINE constexpr field operator+(const field &other) const noexcept
constexpr field tonelli_shanks_sqrt() const noexcept
Implements an optimized variant of Tonelli-Shanks via lookup tables. Algorithm taken from https://cr....
static BB_INLINE void __swap(field &src, field &dest) noexcept
constexpr field & operator=(const field &other) &noexcept=default
static BB_INLINE constexpr std::pair< uint64_t, uint64_t > mul_wide(uint64_t a, uint64_t b) noexcept
static constexpr uint256_t twice_not_modulus
BB_INLINE constexpr field to_montgomery_form() const noexcept
BB_INLINE constexpr wide_array mul_512(const field &other) const noexcept
static BB_INLINE constexpr uint64_t mac_discard_lo(uint64_t a, uint64_t b, uint64_t c) noexcept
static BB_INLINE constexpr uint64_t sbb(uint64_t a, uint64_t b, uint64_t borrow_in, uint64_t &borrow_out) noexcept
BB_INLINE constexpr field subtract(const field &other) const noexcept
static constexpr field external_coset_generator()
static void split_into_endomorphism_scalars_384(const field &input, field &k1_out, field &k2_out)
BB_INLINE constexpr void self_conditional_negate(uint64_t predicate) &noexcept
void msgpack_schema(auto &packer) const
static constexpr field tag_coset_generator()
BB_INLINE constexpr field pow(const uint256_t &exponent) const noexcept
static BB_INLINE constexpr uint64_t mac(uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t &carry_out) noexcept
static void split_into_endomorphism_scalars(const field &k, field &k1, field &k2)
friend std::ostream & operator<<(std::ostream &os, const field &a)
static BB_INLINE constexpr uint64_t addc(uint64_t a, uint64_t b, uint64_t carry_in, uint64_t &carry_out) noexcept
static constexpr uint256_t r_squared_uint
static BB_INLINE constexpr void wasm_reduce(uint64_t &result_0, uint64_t &result_1, uint64_t &result_2, uint64_t &result_3, uint64_t &result_4, uint64_t &result_5, uint64_t &result_6, uint64_t &result_7, uint64_t &result_8)
Perform 29-bit montgomery reduction on 1 limb (result_0 should be zero modulo 2**29 after this)
static constexpr std::array< field, COSET_GENERATOR_SIZE > compute_coset_generators() noexcept
BB_INLINE constexpr field montgomery_mul_big(const field &other) const noexcept
Mongtomery multiplication for moduli > 2²⁵⁴
static constexpr size_t COSET_GENERATOR_SIZE
static constexpr size_t PUBLIC_INPUTS_SIZE
constexpr field & operator=(field &&other) &noexcept=default
constexpr field(const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t d) noexcept
uint8_t ** vec_out_buf
constexpr field(const uint128_t &input) noexcept
BB_INLINE constexpr void self_sqr() &noexcept
constexpr field(const uint512_t &input) noexcept
Convert a 512-bit big integer into a field element.
constexpr field invert() const noexcept
constexpr field(field &&other) noexcept=default
BB_INLINE constexpr void self_neg() &noexcept
BB_INLINE constexpr bool is_msb_set() const noexcept
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE constexpr field sqr() const noexcept
static BB_INLINE constexpr void wasm_madd(uint64_t &left_limb, const std::array< uint64_t, WASM_NUM_LIMBS > &right_limbs, uint64_t &result_0, uint64_t &result_1, uint64_t &result_2, uint64_t &result_3, uint64_t &result_4, uint64_t &result_5, uint64_t &result_6, uint64_t &result_7, uint64_t &result_8)
Multiply left limb by a sequence of 9 limbs and put into result variables.
static BB_INLINE constexpr uint64_t square_accumulate(uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in_lo, uint64_t carry_in_hi, uint64_t &carry_lo, uint64_t &carry_hi) noexcept
BB_INLINE constexpr field conditionally_subtract_from_double_modulus(const uint64_t predicate) const noexcept
constexpr uint256_t uint256_t_no_montgomery_conversion() const noexcept
static constexpr field coset_generator()
static BB_INLINE constexpr void wasm_reduce_yuval(uint64_t &result_0, uint64_t &result_1, uint64_t &result_2, uint64_t &result_3, uint64_t &result_4, uint64_t &result_5, uint64_t &result_6, uint64_t &result_7, uint64_t &result_8, uint64_t &result_9)
Perform 29-bit montgomery reduction on 1 limb using Yuval's method *.
static field serialize_from_buffer(const uint8_t *buffer)
static constexpr uint256_t modulus_minus_two
static void serialize_to_buffer(const field &value, uint8_t *buffer)
void msgpack_pack(auto &packer) const
BB_INLINE constexpr field subtract_coarse(const field &other) const noexcept
constexpr std::pair< bool, field > sqrt() const noexcept
Compute square root of the field element.
static BB_INLINE void __copy(const field &a, field &r) noexcept
const uint8_t * vec_in_buf
constexpr field(const field &other) noexcept=default
BB_INLINE constexpr void self_from_montgomery_form() &noexcept
static constexpr field multiplicative_generator() noexcept
BB_INLINE constexpr bool is_zero() const noexcept
static void batch_invert(C &coeffs) noexcept
static constexpr uint256_t not_modulus
BB_INLINE constexpr void self_to_montgomery_form() &noexcept
static constexpr std::array< uint64_t, 9 > wasm_r_inv
BB_INLINE constexpr field from_montgomery_form() const noexcept
BB_INLINE constexpr field operator-() const noexcept
constexpr field(const numeric::uint256_t &input) noexcept
BB_INLINE constexpr void self_set_msb() &noexcept
const uint8_t * in_buf
void msgpack_unpack(auto o)
BB_INLINE constexpr field add(const field &other) const noexcept
BB_INLINE std::vector< uint8_t > to_buffer() const
constexpr field(const unsigned int input) noexcept
static constexpr size_t primitive_root_log_size() noexcept
static field reconstruct_from_public(const std::span< const field< V >, PUBLIC_INPUTS_SIZE > &limbs)
BB_INLINE constexpr field montgomery_square() const noexcept
BB_INLINE constexpr field reduce_once() const noexcept
BB_INLINE constexpr field montgomery_mul(const field &other) const noexcept
static std::pair< std::array< uint64_t, 2 >, std::array< uint64_t, 2 > > split_into_endomorphism_scalars(const field &k)
BB_INLINE constexpr field reduce() const noexcept
constexpr field(const unsigned long input) noexcept
BB_INLINE constexpr wide_array sqr_512() const noexcept
static constexpr field zero()
static BB_INLINE constexpr uint64_t mac_mini(uint64_t a, uint64_t b, uint64_t c, uint64_t &out) noexcept
BB_INLINE constexpr uint64_t is_msb_set_word() const noexcept
static constexpr uint256_t twice_modulus
constexpr field(std::string input) noexcept
std::size_t operator()(const bb::field< Params > &ff) const noexcept