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: Planned, auditors: [Raju], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
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 if (out.data[0] != 0 && out.data[0] != 1) {
144 bb::assert_failure("Cannot convert field element to bool unless it is 0 or 1");
145 }
146 return static_cast<bool>(out.data[0]);
147 }
148
149 constexpr explicit operator uint8_t() const
150 {
152 return static_cast<uint8_t>(out.data[0]);
153 }
154
155 constexpr explicit operator uint16_t() const
156 {
158 return static_cast<uint16_t>(out.data[0]);
159 }
160
161 constexpr explicit operator uint32_t() const
162 {
164 return static_cast<uint32_t>(out.data[0]);
165 }
166
167 constexpr explicit operator uint64_t() const
168 {
170 return out.data[0];
171 }
172
173 constexpr explicit operator uint128_t() const
174 {
176 uint128_t lo = out.data[0];
177 uint128_t hi = out.data[1];
178 return (hi << 64) | lo;
179 }
180
181 constexpr operator uint256_t() const noexcept
182 {
184 return uint256_t(out.data[0], out.data[1], out.data[2], out.data[3]);
185 }
186
187 [[nodiscard]] constexpr uint256_t uint256_t_no_montgomery_conversion() const noexcept
188 {
189 return { data[0], data[1], data[2], data[3] };
190 }
191
192 constexpr field(const field& other) noexcept = default;
193 constexpr field(field&& other) noexcept = default;
194 constexpr field& operator=(const field& other) & noexcept = default;
195 constexpr field& operator=(field&& other) & noexcept = default;
196 constexpr ~field() noexcept = default;
197 alignas(32) uint64_t data[4]; // NOLINT
198
199 static constexpr uint256_t modulus =
200 uint256_t{ Params::modulus_0, Params::modulus_1, Params::modulus_2, Params::modulus_3 };
201#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
202 static constexpr uint256_t r_squared_uint{
203 Params_::r_squared_0, Params_::r_squared_1, Params_::r_squared_2, Params_::r_squared_3
204 };
205#else
206 static constexpr uint256_t r_squared_uint{
207 Params_::r_squared_wasm_0, Params_::r_squared_wasm_1, Params_::r_squared_wasm_2, Params_::r_squared_wasm_3
208 };
209 static constexpr std::array<uint64_t, 9> wasm_modulus = { Params::modulus_wasm_0, Params::modulus_wasm_1,
210 Params::modulus_wasm_2, Params::modulus_wasm_3,
211 Params::modulus_wasm_4, Params::modulus_wasm_5,
212 Params::modulus_wasm_6, Params::modulus_wasm_7,
213 Params::modulus_wasm_8 };
214 static constexpr std::array<uint64_t, 9> wasm_r_inv = {
215 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,
216 Params::r_inv_wasm_5, Params::r_inv_wasm_6, Params::r_inv_wasm_7, Params::r_inv_wasm_8
217 };
218
219#endif
220 static constexpr field cube_root_of_unity()
221 {
222 // endomorphism i.e. lambda * [P] = (beta * x, y)
223 if constexpr (Params::cube_root_0 != 0) {
224#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
225 constexpr field result{
226 Params::cube_root_0, Params::cube_root_1, Params::cube_root_2, Params::cube_root_3
227 };
228#else
229 constexpr field result{
230 Params::cube_root_wasm_0, Params::cube_root_wasm_1, Params::cube_root_wasm_2, Params::cube_root_wasm_3
231 };
232#endif
233 return result;
234 } else {
235 constexpr field two_inv = field(2).invert();
236 constexpr field numerator = (-field(3)).sqrt() - field(1);
237 constexpr field result = two_inv * numerator;
238 return result;
239 }
240 }
241
242 static constexpr field zero() { return field(0, 0, 0, 0); }
243 static constexpr field neg_one() { return -field(1); }
244 static constexpr field one() { return field(1); }
245
247 {
248#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
249 const field result{
250 Params::coset_generators_0[7],
251 Params::coset_generators_1[7],
252 Params::coset_generators_2[7],
253 Params::coset_generators_3[7],
254 };
255#else
256 const field result{
257 Params::coset_generators_wasm_0[7],
258 Params::coset_generators_wasm_1[7],
259 Params::coset_generators_wasm_2[7],
260 Params::coset_generators_wasm_3[7],
261 };
262#endif
263
264 return result;
265 }
266
267 static constexpr field tag_coset_generator()
268 {
269#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
270 const field result{
271 Params::coset_generators_0[6],
272 Params::coset_generators_1[6],
273 Params::coset_generators_2[6],
274 Params::coset_generators_3[6],
275 };
276#else
277 const field result{
278 Params::coset_generators_wasm_0[6],
279 Params::coset_generators_wasm_1[6],
280 Params::coset_generators_wasm_2[6],
281 Params::coset_generators_wasm_3[6],
282 };
283#endif
284
285 return result;
286 }
287
288 template <size_t idx> static constexpr field coset_generator()
289 {
290 static_assert(idx < 7);
291#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
292 const field result{
293 Params::coset_generators_0[idx],
294 Params::coset_generators_1[idx],
295 Params::coset_generators_2[idx],
296 Params::coset_generators_3[idx],
297 };
298#else
299 const field result{
300 Params::coset_generators_wasm_0[idx],
301 Params::coset_generators_wasm_1[idx],
302 Params::coset_generators_wasm_2[idx],
303 Params::coset_generators_wasm_3[idx],
304 };
305#endif
306
307 return result;
308 }
309
310 BB_INLINE constexpr field operator*(const field& other) const noexcept;
311 BB_INLINE constexpr field operator+(const field& other) const noexcept;
312 BB_INLINE constexpr field operator-(const field& other) const noexcept;
313 BB_INLINE constexpr field operator-() const noexcept;
314 constexpr field operator/(const field& other) const noexcept;
315
316 // prefix increment (++x)
317 BB_INLINE constexpr field operator++() noexcept;
318 // postfix increment (x++)
319 // NOLINTNEXTLINE
320 BB_INLINE constexpr field operator++(int) noexcept;
321
322 BB_INLINE constexpr field& operator*=(const field& other) & noexcept;
323 BB_INLINE constexpr field& operator+=(const field& other) & noexcept;
324 BB_INLINE constexpr field& operator-=(const field& other) & noexcept;
325 constexpr field& operator/=(const field& other) & noexcept;
326
327 // NOTE: comparison operators exist so that `field` is comparible with stl methods that require them.
328 // (e.g. std::sort)
329 // Finite fields do not have an explicit ordering, these should *NEVER* be used in algebraic algorithms.
330 BB_INLINE constexpr bool operator>(const field& other) const noexcept;
331 BB_INLINE constexpr bool operator<(const field& other) const noexcept;
332 BB_INLINE constexpr bool operator==(const field& other) const noexcept;
333 BB_INLINE constexpr bool operator!=(const field& other) const noexcept;
334
335 BB_INLINE constexpr field to_montgomery_form() const noexcept;
336 BB_INLINE constexpr field from_montgomery_form() const noexcept;
337
338 BB_INLINE constexpr field sqr() const noexcept;
339 BB_INLINE constexpr void self_sqr() & noexcept;
340
341 BB_INLINE constexpr field pow(const uint256_t& exponent) const noexcept;
342 BB_INLINE constexpr field pow(uint64_t exponent) const noexcept;
343 // STARKNET: next line was commented as stark252 violates the assertion
344 // static_assert(Params::modulus_0 != 1);
345 static constexpr uint256_t modulus_minus_two =
346 uint256_t(Params::modulus_0 - 2ULL, Params::modulus_1, Params::modulus_2, Params::modulus_3);
347 constexpr field invert() const noexcept;
348 template <typename C>
349 // has size() and operator[].
350 requires requires(C& c) {
351 { c.size() } -> std::convertible_to<size_t>;
352 { c[0] };
353 }
354 static void batch_invert(C& coeffs) noexcept;
355 static void batch_invert(field* coeffs, size_t n) noexcept;
356 static void batch_invert(std::span<field> coeffs) noexcept;
362 constexpr std::pair<bool, field> sqrt() const noexcept
363 requires((Params_::modulus_0 & 0x3UL) == 0x3UL);
364 constexpr std::pair<bool, field> sqrt() const noexcept
365 requires((Params_::modulus_0 & 0x3UL) != 0x3UL);
366 BB_INLINE constexpr void self_neg() & noexcept;
367
368 BB_INLINE constexpr void self_to_montgomery_form() & noexcept;
369 BB_INLINE constexpr void self_from_montgomery_form() & noexcept;
370
371 BB_INLINE constexpr void self_conditional_negate(uint64_t predicate) & noexcept;
372
373 BB_INLINE constexpr field reduce_once() const noexcept;
374 BB_INLINE constexpr void self_reduce_once() & noexcept;
375
376 BB_INLINE constexpr void self_set_msb() & noexcept;
377 [[nodiscard]] BB_INLINE constexpr bool is_msb_set() const noexcept;
378 [[nodiscard]] BB_INLINE constexpr uint64_t is_msb_set_word() const noexcept;
379
380 [[nodiscard]] BB_INLINE constexpr bool is_zero() const noexcept;
381
382 static constexpr field get_root_of_unity(size_t subgroup_size) noexcept;
383
384 static void serialize_to_buffer(const field& value, uint8_t* buffer) { write(buffer, value); }
385
386 static field serialize_from_buffer(const uint8_t* buffer) { return from_buffer<field>(buffer); }
387
388 template <class V> static field reconstruct_from_public(const std::span<const field<V>, PUBLIC_INPUTS_SIZE>& limbs);
389
390 [[nodiscard]] BB_INLINE std::vector<uint8_t> to_buffer() const { return ::to_buffer(*this); }
391
392 struct wide_array {
393 uint64_t data[8]; // NOLINT
394 };
395 BB_INLINE constexpr wide_array mul_512(const field& other) const noexcept;
396 BB_INLINE constexpr wide_array sqr_512() const noexcept;
397
398 BB_INLINE constexpr field conditionally_subtract_from_double_modulus(const uint64_t predicate) const noexcept
399 {
400 if (predicate != 0) {
401 constexpr field p{
403 };
404 return p - *this;
405 }
406 return *this;
407 }
408
433 static void split_into_endomorphism_scalars(const field& k, field& k1, field& k2)
434 {
435 // if the modulus is a >= 255-bit integer, we need to use a basis where g1, g2 have been shifted by 2^384
436 if constexpr (Params::modulus_3 >= 0x4000000000000000ULL) {
438 } else {
440 k1.data[0] = ret.first[0];
441 k1.data[1] = ret.first[1];
442
443 // TODO(https://github.com/AztecProtocol/barretenberg/issues/851): We should move away from this hack by
444 // returning pair of uint64_t[2] instead of a half-set field
445#if !defined(__clang__) && defined(__GNUC__)
446#pragma GCC diagnostic push
447#pragma GCC diagnostic ignored "-Warray-bounds"
448#endif
449 k2.data[0] = ret.second[0]; // NOLINT
450 k2.data[1] = ret.second[1];
451#if !defined(__clang__) && defined(__GNUC__)
452#pragma GCC diagnostic pop
453#endif
454 }
455 }
456
457 // NOTE: this form is only usable if the modulus is 254 bits or less, otherwise see
458 // split_into_endomorphism_scalars_384.
459 // TODO(https://github.com/AztecProtocol/barretenberg/issues/851): Unify these APIs.
461 {
462 static_assert(Params::modulus_3 < 0x4000000000000000ULL);
463 field input = k.reduce_once();
464
465 constexpr field endo_g1 = { Params::endo_g1_lo, Params::endo_g1_mid, Params::endo_g1_hi, 0 };
466
467 constexpr field endo_g2 = { Params::endo_g2_lo, Params::endo_g2_mid, 0, 0 };
468
469 constexpr field endo_minus_b1 = { Params::endo_minus_b1_lo, Params::endo_minus_b1_mid, 0, 0 };
470
471 constexpr field endo_b2 = { Params::endo_b2_lo, Params::endo_b2_mid, 0, 0 };
472
473 // compute c1 = (g2 * k) >> 256
474 wide_array c1 = endo_g2.mul_512(input);
475 // compute c2 = (g1 * k) >> 256
476 wide_array c2 = endo_g1.mul_512(input);
477
478 // (the bit shifts are implicit, as we only utilize the high limbs of c1, c2
479
480 field c1_hi = {
481 c1.data[4], c1.data[5], c1.data[6], c1.data[7]
482 }; // *(field*)((uintptr_t)(&c1) + (4 * sizeof(uint64_t)));
483 field c2_hi = {
484 c2.data[4], c2.data[5], c2.data[6], c2.data[7]
485 }; // *(field*)((uintptr_t)(&c2) + (4 * sizeof(uint64_t)));
486
487 // compute q1 = c1 * -b1
488 wide_array q1 = c1_hi.mul_512(endo_minus_b1);
489 // compute q2 = c2 * b2
490 wide_array q2 = c2_hi.mul_512(endo_b2);
491
492 // FIX: Avoid using 512-bit multiplication as its not necessary.
493 // c1_hi, c2_hi can be uint256_t's and the final result (without montgomery reduction)
494 // could be casted to a field.
495 field q1_lo{ q1.data[0], q1.data[1], q1.data[2], q1.data[3] };
496 field q2_lo{ q2.data[0], q2.data[1], q2.data[2], q2.data[3] };
497
498 field t1 = (q2_lo - q1_lo).reduce_once();
499 field beta = cube_root_of_unity();
500 field t2 = (t1 * beta + input).reduce_once();
501 return {
502 { t2.data[0], t2.data[1] },
503 { t1.data[0], t1.data[1] },
504 };
505 }
506
507 static void split_into_endomorphism_scalars_384(const field& input, field& k1_out, field& k2_out)
508 {
509 constexpr field minus_b1f{
510 Params::endo_minus_b1_lo,
511 Params::endo_minus_b1_mid,
512 0,
513 0,
514 };
515 constexpr field b2f{
516 Params::endo_b2_lo,
517 Params::endo_b2_mid,
518 0,
519 0,
520 };
521 constexpr uint256_t g1{
522 Params::endo_g1_lo,
523 Params::endo_g1_mid,
524 Params::endo_g1_hi,
525 Params::endo_g1_hihi,
526 };
527 constexpr uint256_t g2{
528 Params::endo_g2_lo,
529 Params::endo_g2_mid,
530 Params::endo_g2_hi,
531 Params::endo_g2_hihi,
532 };
533
534 field kf = input.reduce_once();
535 uint256_t k{ kf.data[0], kf.data[1], kf.data[2], kf.data[3] };
536
537 uint512_t c1 = (uint512_t(k) * static_cast<uint512_t>(g1)) >> 384;
538 uint512_t c2 = (uint512_t(k) * static_cast<uint512_t>(g2)) >> 384;
539
540 field c1f{ c1.lo.data[0], c1.lo.data[1], c1.lo.data[2], c1.lo.data[3] };
541 field c2f{ c2.lo.data[0], c2.lo.data[1], c2.lo.data[2], c2.lo.data[3] };
542
543 c1f.self_to_montgomery_form();
544 c2f.self_to_montgomery_form();
545 c1f = c1f * minus_b1f;
546 c2f = c2f * b2f;
547 field r2f = c1f - c2f;
548 field beta = cube_root_of_unity();
549 field r1f = input.reduce_once() - r2f * beta;
550 k1_out = r1f;
551 k2_out = -r2f;
552 }
553
554 // static constexpr auto coset_generators = compute_coset_generators();
555 // static constexpr std::array<field, 15> coset_generators = compute_coset_generators((1 << 30U));
556
557 friend std::ostream& operator<<(std::ostream& os, const field& a)
558 {
560 std::ios_base::fmtflags f(os.flags());
561 os << std::hex << "0x" << std::setfill('0') << std::setw(16) << out.data[3] << std::setw(16) << out.data[2]
562 << std::setw(16) << out.data[1] << std::setw(16) << out.data[0];
563 os.flags(f);
564 return os;
565 }
566
567 BB_INLINE static void __copy(const field& a, field& r) noexcept { r = a; } // NOLINT
568 BB_INLINE static void __swap(field& src, field& dest) noexcept // NOLINT
569 {
570 field T = dest;
571 dest = src;
572 src = T;
573 }
574
575 static field random_element(numeric::RNG* engine = nullptr) noexcept;
576
577 static constexpr field multiplicative_generator() noexcept;
578
579 // For serialization
580 void msgpack_pack(auto& packer) const;
581 void msgpack_unpack(auto o);
582 void msgpack_schema(auto& packer) const { packer.pack_alias(Params::schema_name, "bin32"); }
583
585 static constexpr uint256_t not_modulus = -modulus;
587
588 struct wnaf_table {
589 uint8_t windows[64]; // NOLINT
590
591 constexpr wnaf_table(const uint256_t& target)
592 : windows{
593 static_cast<uint8_t>(target.data[0] & 15), static_cast<uint8_t>((target.data[0] >> 4) & 15),
594 static_cast<uint8_t>((target.data[0] >> 8) & 15), static_cast<uint8_t>((target.data[0] >> 12) & 15),
595 static_cast<uint8_t>((target.data[0] >> 16) & 15), static_cast<uint8_t>((target.data[0] >> 20) & 15),
596 static_cast<uint8_t>((target.data[0] >> 24) & 15), static_cast<uint8_t>((target.data[0] >> 28) & 15),
597 static_cast<uint8_t>((target.data[0] >> 32) & 15), static_cast<uint8_t>((target.data[0] >> 36) & 15),
598 static_cast<uint8_t>((target.data[0] >> 40) & 15), static_cast<uint8_t>((target.data[0] >> 44) & 15),
599 static_cast<uint8_t>((target.data[0] >> 48) & 15), static_cast<uint8_t>((target.data[0] >> 52) & 15),
600 static_cast<uint8_t>((target.data[0] >> 56) & 15), static_cast<uint8_t>((target.data[0] >> 60) & 15),
601 static_cast<uint8_t>(target.data[1] & 15), static_cast<uint8_t>((target.data[1] >> 4) & 15),
602 static_cast<uint8_t>((target.data[1] >> 8) & 15), static_cast<uint8_t>((target.data[1] >> 12) & 15),
603 static_cast<uint8_t>((target.data[1] >> 16) & 15), static_cast<uint8_t>((target.data[1] >> 20) & 15),
604 static_cast<uint8_t>((target.data[1] >> 24) & 15), static_cast<uint8_t>((target.data[1] >> 28) & 15),
605 static_cast<uint8_t>((target.data[1] >> 32) & 15), static_cast<uint8_t>((target.data[1] >> 36) & 15),
606 static_cast<uint8_t>((target.data[1] >> 40) & 15), static_cast<uint8_t>((target.data[1] >> 44) & 15),
607 static_cast<uint8_t>((target.data[1] >> 48) & 15), static_cast<uint8_t>((target.data[1] >> 52) & 15),
608 static_cast<uint8_t>((target.data[1] >> 56) & 15), static_cast<uint8_t>((target.data[1] >> 60) & 15),
609 static_cast<uint8_t>(target.data[2] & 15), static_cast<uint8_t>((target.data[2] >> 4) & 15),
610 static_cast<uint8_t>((target.data[2] >> 8) & 15), static_cast<uint8_t>((target.data[2] >> 12) & 15),
611 static_cast<uint8_t>((target.data[2] >> 16) & 15), static_cast<uint8_t>((target.data[2] >> 20) & 15),
612 static_cast<uint8_t>((target.data[2] >> 24) & 15), static_cast<uint8_t>((target.data[2] >> 28) & 15),
613 static_cast<uint8_t>((target.data[2] >> 32) & 15), static_cast<uint8_t>((target.data[2] >> 36) & 15),
614 static_cast<uint8_t>((target.data[2] >> 40) & 15), static_cast<uint8_t>((target.data[2] >> 44) & 15),
615 static_cast<uint8_t>((target.data[2] >> 48) & 15), static_cast<uint8_t>((target.data[2] >> 52) & 15),
616 static_cast<uint8_t>((target.data[2] >> 56) & 15), static_cast<uint8_t>((target.data[2] >> 60) & 15),
617 static_cast<uint8_t>(target.data[3] & 15), static_cast<uint8_t>((target.data[3] >> 4) & 15),
618 static_cast<uint8_t>((target.data[3] >> 8) & 15), static_cast<uint8_t>((target.data[3] >> 12) & 15),
619 static_cast<uint8_t>((target.data[3] >> 16) & 15), static_cast<uint8_t>((target.data[3] >> 20) & 15),
620 static_cast<uint8_t>((target.data[3] >> 24) & 15), static_cast<uint8_t>((target.data[3] >> 28) & 15),
621 static_cast<uint8_t>((target.data[3] >> 32) & 15), static_cast<uint8_t>((target.data[3] >> 36) & 15),
622 static_cast<uint8_t>((target.data[3] >> 40) & 15), static_cast<uint8_t>((target.data[3] >> 44) & 15),
623 static_cast<uint8_t>((target.data[3] >> 48) & 15), static_cast<uint8_t>((target.data[3] >> 52) & 15),
624 static_cast<uint8_t>((target.data[3] >> 56) & 15), static_cast<uint8_t>((target.data[3] >> 60) & 15)
625 }
626 {}
627 };
628
629#if defined(__wasm__) || !defined(__SIZEOF_INT128__)
630 BB_INLINE static constexpr void wasm_madd(uint64_t& left_limb,
631 const std::array<uint64_t, WASM_NUM_LIMBS>& right_limbs,
632 uint64_t& result_0,
633 uint64_t& result_1,
634 uint64_t& result_2,
635 uint64_t& result_3,
636 uint64_t& result_4,
637 uint64_t& result_5,
638 uint64_t& result_6,
639 uint64_t& result_7,
640 uint64_t& result_8);
641 BB_INLINE static constexpr void wasm_reduce(uint64_t& result_0,
642 uint64_t& result_1,
643 uint64_t& result_2,
644 uint64_t& result_3,
645 uint64_t& result_4,
646 uint64_t& result_5,
647 uint64_t& result_6,
648 uint64_t& result_7,
649 uint64_t& result_8);
650 BB_INLINE static constexpr void wasm_reduce_yuval(uint64_t& result_0,
651 uint64_t& result_1,
652 uint64_t& result_2,
653 uint64_t& result_3,
654 uint64_t& result_4,
655 uint64_t& result_5,
656 uint64_t& result_6,
657 uint64_t& result_7,
658 uint64_t& result_8,
659 uint64_t& result_9);
660 BB_INLINE static constexpr std::array<uint64_t, WASM_NUM_LIMBS> wasm_convert(const uint64_t* data);
661#endif
662 BB_INLINE static constexpr std::pair<uint64_t, uint64_t> mul_wide(uint64_t a, uint64_t b) noexcept;
663
664 BB_INLINE static constexpr uint64_t mac(
665 uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t& carry_out) noexcept;
666
667 BB_INLINE static constexpr void mac(
668 uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t& out, uint64_t& carry_out) noexcept;
669
670 BB_INLINE static constexpr uint64_t mac_mini(uint64_t a, uint64_t b, uint64_t c, uint64_t& out) noexcept;
671
672 BB_INLINE static constexpr void mac_mini(
673 uint64_t a, uint64_t b, uint64_t c, uint64_t& out, uint64_t& carry_out) noexcept;
674
675 BB_INLINE static constexpr uint64_t mac_discard_lo(uint64_t a, uint64_t b, uint64_t c) noexcept;
676
677 BB_INLINE static constexpr uint64_t addc(uint64_t a, uint64_t b, uint64_t carry_in, uint64_t& carry_out) noexcept;
678
679 BB_INLINE static constexpr uint64_t sbb(uint64_t a, uint64_t b, uint64_t borrow_in, uint64_t& borrow_out) noexcept;
680
681 BB_INLINE static constexpr uint64_t square_accumulate(uint64_t a,
682 uint64_t b,
683 uint64_t c,
684 uint64_t carry_in_lo,
685 uint64_t carry_in_hi,
686 uint64_t& carry_lo,
687 uint64_t& carry_hi) noexcept;
688 BB_INLINE constexpr field reduce() const noexcept;
689 BB_INLINE constexpr field add(const field& other) const noexcept;
690 BB_INLINE constexpr field subtract(const field& other) const noexcept;
691 BB_INLINE constexpr field subtract_coarse(const field& other) const noexcept;
692 BB_INLINE constexpr field montgomery_mul(const field& other) const noexcept;
693 BB_INLINE constexpr field montgomery_mul_big(const field& other) const noexcept;
694 BB_INLINE constexpr field montgomery_square() const noexcept;
695
696#if (BBERG_NO_ASM == 0)
697 BB_INLINE static field asm_mul(const field& a, const field& b) noexcept;
698 BB_INLINE static field asm_sqr(const field& a) noexcept;
699 BB_INLINE static field asm_add(const field& a, const field& b) noexcept;
700 BB_INLINE static field asm_sub(const field& a, const field& b) noexcept;
701 BB_INLINE static field asm_mul_with_coarse_reduction(const field& a, const field& b) noexcept;
702 BB_INLINE static field asm_sqr_with_coarse_reduction(const field& a) noexcept;
703 BB_INLINE static field asm_add_with_coarse_reduction(const field& a, const field& b) noexcept;
704 BB_INLINE static field asm_sub_with_coarse_reduction(const field& a, const field& b) noexcept;
705 BB_INLINE static field asm_add_without_reduction(const field& a, const field& b) noexcept;
706 BB_INLINE static void asm_self_sqr(const field& a) noexcept;
707 BB_INLINE static void asm_self_add(const field& a, const field& b) noexcept;
708 BB_INLINE static void asm_self_sub(const field& a, const field& b) noexcept;
709 BB_INLINE static void asm_self_mul_with_coarse_reduction(const field& a, const field& b) noexcept;
710 BB_INLINE static void asm_self_sqr_with_coarse_reduction(const field& a) noexcept;
711 BB_INLINE static void asm_self_add_with_coarse_reduction(const field& a, const field& b) noexcept;
712 BB_INLINE static void asm_self_sub_with_coarse_reduction(const field& a, const field& b) noexcept;
713 BB_INLINE static void asm_self_add_without_reduction(const field& a, const field& b) noexcept;
714
715 BB_INLINE static void asm_conditional_negate(field& r, uint64_t predicate) noexcept;
716 BB_INLINE static field asm_reduce_once(const field& a) noexcept;
717 BB_INLINE static void asm_self_reduce_once(const field& a) noexcept;
718 static constexpr uint64_t zero_reference = 0x00ULL;
719#endif
720 static constexpr size_t COSET_GENERATOR_SIZE = 15;
721 constexpr field tonelli_shanks_sqrt() const noexcept;
722 static constexpr size_t primitive_root_log_size() noexcept;
723 static constexpr std::array<field, COSET_GENERATOR_SIZE> compute_coset_generators() noexcept;
724
725#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
726 static constexpr uint128_t lo_mask = 0xffffffffffffffffUL;
727#endif
728};
729
730template <typename B, typename Params> void read(B& it, field<Params>& value)
731{
732 using serialize::read;
733 field<Params> result{ 0, 0, 0, 0 };
734 read(it, result.data[3]);
735 read(it, result.data[2]);
736 read(it, result.data[1]);
737 read(it, result.data[0]);
738 value = result.to_montgomery_form();
739}
740template <typename B, typename Params> void write(B& buf, field<Params> const& value)
741{
742 using serialize::write;
743 const field input = value.from_montgomery_form();
744 write(buf, input.data[3]);
745 write(buf, input.data[2]);
746 write(buf, input.data[1]);
747 write(buf, input.data[0]);
748}
749
750} // namespace bb
751
752// Define hash function for field elements, e.g., so that it can be used in maps.
753// See https://en.cppreference.com/w/cpp/utility/hash .
754template <typename Params> struct std::hash<bb::field<Params>> {
755 std::size_t operator()(const bb::field<Params>& ff) const noexcept
756 {
757 // Just like in equality, we need to reduce the field element before hashing.
758 auto reduced = ff.reduce_once();
759 return bb::utils::hash_as_tuple(reduced.data[0], reduced.data[1], reduced.data[2], reduced.data[3]);
760 }
761};
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
numeric::RNG & engine
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.
Definition api.hpp:5
group< fq2, fr, Bn254G2Params > g2
Definition g2.hpp:39
group< fq, fr, Bn254G1Params > g1
Definition g1.hpp:33
void read(B &it, field2< base_field, Params > &value)
void write(B &buf, field2< base_field, Params > const &value)
void assert_failure(std::string const &err)
Definition assert.cpp:11
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