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 {
31
32// Threshold for "large" moduli (>= 2^254). When the top limb of the modulus is >= 2^62,
33// intermediate arithmetic results can overflow 256 bits, requiring different reduction strategies (enacted via
34// constexpr branching).
35//
36// There is a further difference: internally, when limb[3] <MODULUS_TOP_LIMB_LARGE_THRESHOLD, we allow for coarse
37// representation of the elements; this means that we assume the underlying unsigned integer to be in the range [0, 2p).
38//
39// On the other hand, for moduli with limb[3] > MODULUS_TOP_LIMB_LARGE_THRESHOLD, the uint256_t element
40// derived from the limbs is arbitrary (and is in particular NOT guaranteed to be in the range [0, p)). In particular
41// one sees this in the `add` functionality.
42
43// To speed up multiplication, we internally represent all elements in MONTGOMERY form. This means that the underlying 4
44// limbs represent a * R modulo p. (See the documentation in \ref field_docs["field documentation"]).
45//
46// In Barretenberg, the main workhorse fields are the base and scalar fields of BN-254, which are "small" moduli: they
47// are each 254 bits. The field algorithms for them are constant-time.
48static constexpr uint64_t MODULUS_TOP_LIMB_LARGE_THRESHOLD = 0x4000000000000000ULL; // 2^62
49
55template <class Params_> struct alignas(32) field {
56 public:
57 using View = field;
59 using Params = Params_;
60 using in_buf = const uint8_t*;
61 using vec_in_buf = const uint8_t*;
62 using out_buf = uint8_t*;
63 using vec_out_buf = uint8_t**;
64
65 // The number of element required to represent field<Params_> in the public inputs of a circuit
66 static constexpr size_t PUBLIC_INPUTS_SIZE = Params::PUBLIC_INPUTS_SIZE;
67
68#if defined(__wasm__) || !defined(__SIZEOF_INT128__)
69#define WASM_NUM_LIMBS 9
70#define WASM_LIMB_BITS 29
71#endif
72
73 // We don't initialize data in the default constructor since we'd lose a lot of time on huge array initializations.
74 // Other alternatives have been noted, such as casting to get around constructors where they matter,
75 // however it is felt that sanitizer tools (e.g. MSAN) can detect garbage well, whereas doing
76 // hacky casts where needed would require rework to critical algos like MSM, FFT, Sumcheck.
77 // Instead, the recommended solution is use an explicit {} where initialization is important:
78 // field f; // not initialized
79 // field f{}; // zero-initialized
80 // std::array<field, N> arr; // not initialized, good for huge N
81 // std::array<field, N> arr {}; // zero-initialized, preferable for moderate N
82 field() = default;
83
84 constexpr field(const numeric::uint256_t& input) noexcept
85 : data{ input.data[0], input.data[1], input.data[2], input.data[3] }
86 {
88 }
89
90 constexpr field(const uint128_t& input) noexcept
91 : field(static_cast<uint256_t>(input))
92 {}
93
94 // NOLINTNEXTLINE (unsigned long is platform dependent, which we want in this case)
95 constexpr field(const unsigned long input) noexcept
96 : data{ input, 0, 0, 0 }
97 {
99 }
100
101 constexpr field(const unsigned int input) noexcept
102 : data{ input, 0, 0, 0 }
103 {
105 }
106
107 // NOLINTNEXTLINE (unsigned long long is platform dependent, which we want in this case)
108 constexpr field(const unsigned long long input) noexcept
109 : data{ input, 0, 0, 0 }
110 {
112 }
113
114 constexpr field(const int input) noexcept
115 : data{ 0, 0, 0, 0 }
116 {
117 if (input < 0) {
118 data[0] = static_cast<uint64_t>(-input);
119 data[1] = 0;
120 data[2] = 0;
121 data[3] = 0;
123 self_neg();
125 } else {
126 data[0] = static_cast<uint64_t>(input);
127 data[1] = 0;
128 data[2] = 0;
129 data[3] = 0;
131 }
132 }
140 constexpr field(const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t d) noexcept
141 : data{ a, b, c, d } {};
142
149 constexpr explicit field(const uint512_t& input) noexcept
150 {
151 uint256_t value = (input % modulus).lo;
152 data[0] = value.data[0];
153 data[1] = value.data[1];
154 data[2] = value.data[2];
155 data[3] = value.data[3];
157 }
158
159 constexpr explicit field(std::string input) noexcept
160 {
161 uint256_t value(input);
162 *this = field(value);
163 }
164
165 // Conversion operators to primitive types.
166 // Note: from_montgomery_form() may return values bigger than p (in the range of [0, 2p) for 254-bit fields,
167 // arbitrary 256-bit number for 256-bit fields.)
168 // We call reduce_once() to ensure canonical [0, p) representation.
169
170 constexpr explicit operator bool() const
171 {
173 if (out.data[0] != 0 && out.data[0] != 1 && out.data[1] != 0 && out.data[2] != 0 && out.data[3] != 0) {
174 bb::assert_failure("Cannot convert field element to bool unless it is 0 or 1");
175 }
176 return static_cast<bool>(out.data[0]);
177 }
178
179 constexpr explicit operator uint8_t() const
180 {
182 return static_cast<uint8_t>(out.data[0]);
183 }
184
185 constexpr explicit operator uint16_t() const
186 {
188 return static_cast<uint16_t>(out.data[0]);
189 }
190
191 constexpr explicit operator uint32_t() const
192 {
194 return static_cast<uint32_t>(out.data[0]);
195 }
196
197 constexpr explicit operator uint64_t() const
198 {
200 return out.data[0];
201 }
202
203 constexpr explicit operator uint128_t() const
204 {
206 uint128_t lo = out.data[0];
207 uint128_t hi = out.data[1];
208 return (hi << 64) | lo;
209 }
210
211 constexpr operator uint256_t() const noexcept
212 {
214 return uint256_t(out.data[0], out.data[1], out.data[2], out.data[3]);
215 }
216
217 [[nodiscard]] constexpr uint256_t uint256_t_no_montgomery_conversion() const noexcept
218 {
219 return { data[0], data[1], data[2], data[3] };
220 }
221
222 constexpr field(const field& other) noexcept = default;
223 constexpr field(field&& other) noexcept = default;
224 constexpr field& operator=(const field& other) & noexcept = default;
225 constexpr field& operator=(field&& other) & noexcept = default;
226 constexpr ~field() noexcept = default;
227 alignas(32) uint64_t data[4]; // NOLINT
228
229 static constexpr uint256_t modulus =
230 uint256_t{ Params::modulus_0, Params::modulus_1, Params::modulus_2, Params::modulus_3 };
231#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
232 static constexpr uint256_t r_squared_uint{
233 Params_::r_squared_0, Params_::r_squared_1, Params_::r_squared_2, Params_::r_squared_3
234 };
235#else
236 static constexpr uint256_t r_squared_uint{
237 Params_::r_squared_wasm_0, Params_::r_squared_wasm_1, Params_::r_squared_wasm_2, Params_::r_squared_wasm_3
238 };
239 static constexpr std::array<uint64_t, 9> wasm_modulus = { Params::modulus_wasm_0, Params::modulus_wasm_1,
240 Params::modulus_wasm_2, Params::modulus_wasm_3,
241 Params::modulus_wasm_4, Params::modulus_wasm_5,
242 Params::modulus_wasm_6, Params::modulus_wasm_7,
243 Params::modulus_wasm_8 };
244 static constexpr std::array<uint64_t, 9> wasm_r_inv = {
245 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,
246 Params::r_inv_wasm_5, Params::r_inv_wasm_6, Params::r_inv_wasm_7, Params::r_inv_wasm_8
247 };
248
249#endif
250 static constexpr field cube_root_of_unity()
251 {
252 // endomorphism i.e. lambda * [P] = (beta * x, y)
253 if constexpr (Params::cube_root_0 != 0) {
254#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
255 constexpr field result{
256 Params::cube_root_0, Params::cube_root_1, Params::cube_root_2, Params::cube_root_3
257 };
258#else
259 constexpr field result{
260 Params::cube_root_wasm_0, Params::cube_root_wasm_1, Params::cube_root_wasm_2, Params::cube_root_wasm_3
261 };
262#endif
263 return result;
264 } else {
265 constexpr field two_inv = field(2).invert();
266 constexpr field numerator = (-field(3)).sqrt() - field(1);
267 constexpr field result = two_inv * numerator;
268 return result;
269 }
270 }
271
272 static constexpr field zero() { return field(0, 0, 0, 0); }
273 static constexpr field neg_one() { return -field(1); }
274 static constexpr field one() { return field(1); }
275
277 {
278#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
279 const field result{
280 Params::coset_generators_0[7],
281 Params::coset_generators_1[7],
282 Params::coset_generators_2[7],
283 Params::coset_generators_3[7],
284 };
285#else
286 const field result{
287 Params::coset_generators_wasm_0[7],
288 Params::coset_generators_wasm_1[7],
289 Params::coset_generators_wasm_2[7],
290 Params::coset_generators_wasm_3[7],
291 };
292#endif
293
294 return result;
295 }
296
297 static constexpr field tag_coset_generator()
298 {
299#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
300 const field result{
301 Params::coset_generators_0[6],
302 Params::coset_generators_1[6],
303 Params::coset_generators_2[6],
304 Params::coset_generators_3[6],
305 };
306#else
307 const field result{
308 Params::coset_generators_wasm_0[6],
309 Params::coset_generators_wasm_1[6],
310 Params::coset_generators_wasm_2[6],
311 Params::coset_generators_wasm_3[6],
312 };
313#endif
314
315 return result;
316 }
317
318 template <size_t idx> static constexpr field coset_generator()
319 {
320 static_assert(idx < 7);
321#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
322 const field result{
323 Params::coset_generators_0[idx],
324 Params::coset_generators_1[idx],
325 Params::coset_generators_2[idx],
326 Params::coset_generators_3[idx],
327 };
328#else
329 const field result{
330 Params::coset_generators_wasm_0[idx],
331 Params::coset_generators_wasm_1[idx],
332 Params::coset_generators_wasm_2[idx],
333 Params::coset_generators_wasm_3[idx],
334 };
335#endif
336
337 return result;
338 }
339
340 BB_INLINE constexpr field operator*(const field& other) const noexcept;
341 BB_INLINE constexpr field operator+(const field& other) const noexcept;
342 BB_INLINE constexpr field operator-(const field& other) const noexcept;
343 BB_INLINE constexpr field operator-() const noexcept;
344 constexpr field operator/(const field& other) const noexcept;
345
346 // prefix increment (++x)
347 BB_INLINE constexpr field operator++() noexcept;
348 // postfix increment (x++)
349 // NOLINTNEXTLINE
350 BB_INLINE constexpr field operator++(int) noexcept;
351
352 BB_INLINE constexpr field& operator*=(const field& other) & noexcept;
353 BB_INLINE constexpr field& operator+=(const field& other) & noexcept;
354 BB_INLINE constexpr field& operator-=(const field& other) & noexcept;
355 constexpr field& operator/=(const field& other) & noexcept;
356
357 // NOTE: comparison operators exist so that `field` is comparible with stl methods that require them.
358 // (e.g. std::sort)
359 // Finite fields do not have an explicit ordering, these should *NEVER* be used in algebraic algorithms.
360 BB_INLINE constexpr bool operator>(const field& other) const noexcept;
361 BB_INLINE constexpr bool operator<(const field& other) const noexcept;
362 BB_INLINE constexpr bool operator==(const field& other) const noexcept;
363 BB_INLINE constexpr bool operator!=(const field& other) const noexcept;
364
365 BB_INLINE constexpr field to_montgomery_form() const noexcept;
366 BB_INLINE constexpr field from_montgomery_form() const noexcept;
367 // Reduced versions guarantee output is in canonical form [0, p)
368 BB_INLINE constexpr field to_montgomery_form_reduced() const noexcept;
369 BB_INLINE constexpr field from_montgomery_form_reduced() const noexcept;
370
371 BB_INLINE constexpr field sqr() const noexcept;
372 BB_INLINE constexpr void self_sqr() & noexcept;
373
374 BB_INLINE constexpr field pow(const uint256_t& exponent) const noexcept;
375 BB_INLINE constexpr field pow(uint64_t exponent) const noexcept;
376 // STARKNET: next line was commented as stark252 violates the assertion
377 // static_assert(Params::modulus_0 != 1);
378 static constexpr uint256_t modulus_minus_two =
379 uint256_t(Params::modulus_0 - 2ULL, Params::modulus_1, Params::modulus_2, Params::modulus_3);
380 constexpr field invert() const noexcept;
381 template <typename C>
382 // has size() and operator[].
383 requires requires(C& c) {
384 { c.size() } -> std::convertible_to<size_t>;
385 { c[0] };
386 }
387 static void batch_invert(C& coeffs) noexcept;
388 static void batch_invert(field* coeffs, size_t n) noexcept;
389 static void batch_invert(std::span<field> coeffs) noexcept;
395 constexpr std::pair<bool, field> sqrt() const noexcept
396 requires((Params_::modulus_0 & 0x3UL) == 0x3UL);
397 constexpr std::pair<bool, field> sqrt() const noexcept
398 requires((Params_::modulus_0 & 0x3UL) != 0x3UL);
399 BB_INLINE constexpr void self_neg() & noexcept;
400
401 BB_INLINE constexpr void self_to_montgomery_form() & noexcept;
402 BB_INLINE constexpr void self_from_montgomery_form() & noexcept;
403 // Reduced versions guarantee output is in canonical form [0, p)
404 BB_INLINE constexpr void self_to_montgomery_form_reduced() & noexcept;
405 BB_INLINE constexpr void self_from_montgomery_form_reduced() & noexcept;
406
407 BB_INLINE constexpr void self_conditional_negate(uint64_t predicate) & noexcept;
408
409 BB_INLINE constexpr field reduce_once() const noexcept;
410 BB_INLINE constexpr void self_reduce_once() & noexcept;
411
412 BB_INLINE constexpr void self_set_msb() & noexcept;
413 [[nodiscard]] BB_INLINE constexpr bool is_msb_set() const noexcept;
414 [[nodiscard]] BB_INLINE constexpr uint64_t is_msb_set_word() const noexcept;
415
416 [[nodiscard]] BB_INLINE constexpr bool is_zero() const noexcept;
417
418 static constexpr field get_root_of_unity(size_t subgroup_size) noexcept;
419
420 static void serialize_to_buffer(const field& value, uint8_t* buffer) { write(buffer, value); }
421
422 static field serialize_from_buffer(const uint8_t* buffer) { return from_buffer<field>(buffer); }
423
424 template <class V> static field reconstruct_from_public(const std::span<const field<V>, PUBLIC_INPUTS_SIZE>& limbs);
425
426 [[nodiscard]] BB_INLINE std::vector<uint8_t> to_buffer() const { return ::to_buffer(*this); }
427
428 struct wide_array {
429 uint64_t data[8]; // NOLINT
430 };
431 BB_INLINE constexpr wide_array mul_512(const field& other) const noexcept;
432 BB_INLINE constexpr wide_array sqr_512() const noexcept;
433
434 BB_INLINE constexpr field conditionally_subtract_from_double_modulus(const uint64_t predicate) const noexcept
435 {
436 if (predicate != 0) {
437 constexpr field p{
439 };
440 return p - *this;
441 }
442 return *this;
443 }
444
469 static void split_into_endomorphism_scalars(const field& k, field& k1, field& k2)
470 {
471 // if the modulus is a >= 255-bit integer, we need to use a basis where g1, g2 have been shifted by 2^384
472 if constexpr (Params::modulus_3 >= MODULUS_TOP_LIMB_LARGE_THRESHOLD) {
474 } else {
476 k1.data[0] = ret.first[0];
477 k1.data[1] = ret.first[1];
478
479#if !defined(__clang__) && defined(__GNUC__)
480#pragma GCC diagnostic push
481#pragma GCC diagnostic ignored "-Warray-bounds"
482#endif
483 k2.data[0] = ret.second[0]; // NOLINT
484 k2.data[1] = ret.second[1];
485#if !defined(__clang__) && defined(__GNUC__)
486#pragma GCC diagnostic pop
487#endif
488 }
489 }
490
491 // NOTE: this form is only usable if the modulus is 254 bits or less, otherwise see
492 // split_into_endomorphism_scalars_384.
493 // DOES NOT assume that the input is reduced; it can be in coarse form.
494 // TODO(https://github.com/AztecProtocol/barretenberg/issues/851): Unify these APIs.
496 {
497 static_assert(Params::modulus_3 < MODULUS_TOP_LIMB_LARGE_THRESHOLD);
498 field input = k.reduce_once();
499
500 constexpr field endo_g1 = { Params::endo_g1_lo, Params::endo_g1_mid, Params::endo_g1_hi, 0 };
501
502 constexpr field endo_g2 = { Params::endo_g2_lo, Params::endo_g2_mid, 0, 0 };
503
504 constexpr field endo_minus_b1 = { Params::endo_minus_b1_lo, Params::endo_minus_b1_mid, 0, 0 };
505
506 constexpr field endo_b2 = { Params::endo_b2_lo, Params::endo_b2_mid, 0, 0 };
507
508 // compute c1 = (g2 * k) >> 256
509 wide_array c1 = endo_g2.mul_512(input);
510 // compute c2 = (g1 * k) >> 256
511 wide_array c2 = endo_g1.mul_512(input);
512
513 // (the bit shifts are implicit, as we only utilize the high limbs of c1, c2
514
515 field c1_hi = {
516 c1.data[4], c1.data[5], c1.data[6], c1.data[7]
517 }; // *(field*)((uintptr_t)(&c1) + (4 * sizeof(uint64_t)));
518 field c2_hi = {
519 c2.data[4], c2.data[5], c2.data[6], c2.data[7]
520 }; // *(field*)((uintptr_t)(&c2) + (4 * sizeof(uint64_t)));
521
522 // compute q1 = c1 * -b1
523 wide_array q1 = c1_hi.mul_512(endo_minus_b1);
524 // compute q2 = c2 * b2
525 wide_array q2 = c2_hi.mul_512(endo_b2);
526
527 // FIX: Avoid using 512-bit multiplication as its not necessary.
528 // c1_hi, c2_hi can be uint256_t's and the final result (without montgomery reduction)
529 // could be casted to a field.
530 field q1_lo{ q1.data[0], q1.data[1], q1.data[2], q1.data[3] };
531 field q2_lo{ q2.data[0], q2.data[1], q2.data[2], q2.data[3] };
532
533 field t1 = (q2_lo - q1_lo).reduce_once();
534
535 // k2 (= t1) can be slightly negative for ~2^{-64} of inputs.
536 // When negative, t1 = k2 + r is 254 bits (upper limbs nonzero).
537 // Fix: decrement c1 by 1, equivalent to adding |b1| to k2.
538 // This shifts k2 by +|b1| (~127 bits, now positive) and k1 by -a1 (~64 bits),
539 // keeping both within 128 bits. See endomorphism_scalars.py for more details.
540 if (t1.data[2] != 0 || t1.data[3] != 0) {
541 t1 = (t1 + endo_minus_b1).reduce_once();
542 }
543
544 field beta = cube_root_of_unity();
545 field t2 = (t1 * beta + input).reduce_once();
546 return {
547 { t2.data[0], t2.data[1] },
548 { t1.data[0], t1.data[1] },
549 };
550 }
551
552 static void split_into_endomorphism_scalars_384(const field& input, field& k1_out, field& k2_out)
553 {
554 constexpr field minus_b1f{
555 Params::endo_minus_b1_lo,
556 Params::endo_minus_b1_mid,
557 0,
558 0,
559 };
560 constexpr field b2f{
561 Params::endo_b2_lo,
562 Params::endo_b2_mid,
563 0,
564 0,
565 };
566 constexpr uint256_t g1{
567 Params::endo_g1_lo,
568 Params::endo_g1_mid,
569 Params::endo_g1_hi,
570 Params::endo_g1_hihi,
571 };
572 constexpr uint256_t g2{
573 Params::endo_g2_lo,
574 Params::endo_g2_mid,
575 Params::endo_g2_hi,
576 Params::endo_g2_hihi,
577 };
578
579 field kf = input.reduce_once();
580 uint256_t k{ kf.data[0], kf.data[1], kf.data[2], kf.data[3] };
581
582 uint512_t c1 = (uint512_t(k) * static_cast<uint512_t>(g1)) >> 384;
583 uint512_t c2 = (uint512_t(k) * static_cast<uint512_t>(g2)) >> 384;
584
585 field c1f{ c1.lo.data[0], c1.lo.data[1], c1.lo.data[2], c1.lo.data[3] };
586 field c2f{ c2.lo.data[0], c2.lo.data[1], c2.lo.data[2], c2.lo.data[3] };
587
588 c1f.self_to_montgomery_form();
589 c2f.self_to_montgomery_form();
590 c1f = c1f * minus_b1f;
591 c2f = c2f * b2f;
592 field r2f = c1f - c2f;
593 field beta = cube_root_of_unity();
594 field r1f = input.reduce_once() - r2f * beta;
595 k1_out = r1f;
596 k2_out = -r2f;
597 }
598
599 // static constexpr auto coset_generators = compute_coset_generators();
600 // static constexpr std::array<field, 15> coset_generators = compute_coset_generators((1 << 30U));
601
602 friend std::ostream& operator<<(std::ostream& os, const field& a)
603 {
605 std::ios_base::fmtflags f(os.flags());
606 os << std::hex << "0x" << std::setfill('0') << std::setw(16) << out.data[3] << std::setw(16) << out.data[2]
607 << std::setw(16) << out.data[1] << std::setw(16) << out.data[0];
608 os.flags(f);
609 return os;
610 }
611
612 BB_INLINE static void __copy(const field& a, field& r) noexcept { r = a; } // NOLINT
613 BB_INLINE static void __swap(field& src, field& dest) noexcept // NOLINT
614 {
615 field T = dest;
616 dest = src;
617 src = T;
618 }
619
620 static field random_element(numeric::RNG* engine = nullptr) noexcept;
621
622 // For serialization
623 void msgpack_pack(auto& packer) const;
624 void msgpack_unpack(auto o);
625 void msgpack_schema(auto& packer) const { packer.pack_alias(Params::schema_name, "bin32"); }
626
628 static constexpr uint256_t not_modulus = -modulus;
630
631 struct wnaf_table {
632 uint8_t windows[64]; // NOLINT
633
634 constexpr wnaf_table(const uint256_t& target)
635 : windows{
636 static_cast<uint8_t>(target.data[0] & 15), static_cast<uint8_t>((target.data[0] >> 4) & 15),
637 static_cast<uint8_t>((target.data[0] >> 8) & 15), static_cast<uint8_t>((target.data[0] >> 12) & 15),
638 static_cast<uint8_t>((target.data[0] >> 16) & 15), static_cast<uint8_t>((target.data[0] >> 20) & 15),
639 static_cast<uint8_t>((target.data[0] >> 24) & 15), static_cast<uint8_t>((target.data[0] >> 28) & 15),
640 static_cast<uint8_t>((target.data[0] >> 32) & 15), static_cast<uint8_t>((target.data[0] >> 36) & 15),
641 static_cast<uint8_t>((target.data[0] >> 40) & 15), static_cast<uint8_t>((target.data[0] >> 44) & 15),
642 static_cast<uint8_t>((target.data[0] >> 48) & 15), static_cast<uint8_t>((target.data[0] >> 52) & 15),
643 static_cast<uint8_t>((target.data[0] >> 56) & 15), static_cast<uint8_t>((target.data[0] >> 60) & 15),
644 static_cast<uint8_t>(target.data[1] & 15), static_cast<uint8_t>((target.data[1] >> 4) & 15),
645 static_cast<uint8_t>((target.data[1] >> 8) & 15), static_cast<uint8_t>((target.data[1] >> 12) & 15),
646 static_cast<uint8_t>((target.data[1] >> 16) & 15), static_cast<uint8_t>((target.data[1] >> 20) & 15),
647 static_cast<uint8_t>((target.data[1] >> 24) & 15), static_cast<uint8_t>((target.data[1] >> 28) & 15),
648 static_cast<uint8_t>((target.data[1] >> 32) & 15), static_cast<uint8_t>((target.data[1] >> 36) & 15),
649 static_cast<uint8_t>((target.data[1] >> 40) & 15), static_cast<uint8_t>((target.data[1] >> 44) & 15),
650 static_cast<uint8_t>((target.data[1] >> 48) & 15), static_cast<uint8_t>((target.data[1] >> 52) & 15),
651 static_cast<uint8_t>((target.data[1] >> 56) & 15), static_cast<uint8_t>((target.data[1] >> 60) & 15),
652 static_cast<uint8_t>(target.data[2] & 15), static_cast<uint8_t>((target.data[2] >> 4) & 15),
653 static_cast<uint8_t>((target.data[2] >> 8) & 15), static_cast<uint8_t>((target.data[2] >> 12) & 15),
654 static_cast<uint8_t>((target.data[2] >> 16) & 15), static_cast<uint8_t>((target.data[2] >> 20) & 15),
655 static_cast<uint8_t>((target.data[2] >> 24) & 15), static_cast<uint8_t>((target.data[2] >> 28) & 15),
656 static_cast<uint8_t>((target.data[2] >> 32) & 15), static_cast<uint8_t>((target.data[2] >> 36) & 15),
657 static_cast<uint8_t>((target.data[2] >> 40) & 15), static_cast<uint8_t>((target.data[2] >> 44) & 15),
658 static_cast<uint8_t>((target.data[2] >> 48) & 15), static_cast<uint8_t>((target.data[2] >> 52) & 15),
659 static_cast<uint8_t>((target.data[2] >> 56) & 15), static_cast<uint8_t>((target.data[2] >> 60) & 15),
660 static_cast<uint8_t>(target.data[3] & 15), static_cast<uint8_t>((target.data[3] >> 4) & 15),
661 static_cast<uint8_t>((target.data[3] >> 8) & 15), static_cast<uint8_t>((target.data[3] >> 12) & 15),
662 static_cast<uint8_t>((target.data[3] >> 16) & 15), static_cast<uint8_t>((target.data[3] >> 20) & 15),
663 static_cast<uint8_t>((target.data[3] >> 24) & 15), static_cast<uint8_t>((target.data[3] >> 28) & 15),
664 static_cast<uint8_t>((target.data[3] >> 32) & 15), static_cast<uint8_t>((target.data[3] >> 36) & 15),
665 static_cast<uint8_t>((target.data[3] >> 40) & 15), static_cast<uint8_t>((target.data[3] >> 44) & 15),
666 static_cast<uint8_t>((target.data[3] >> 48) & 15), static_cast<uint8_t>((target.data[3] >> 52) & 15),
667 static_cast<uint8_t>((target.data[3] >> 56) & 15), static_cast<uint8_t>((target.data[3] >> 60) & 15)
668 }
669 {}
670 };
671
672#if defined(__wasm__) || !defined(__SIZEOF_INT128__)
673 BB_INLINE static constexpr void wasm_madd(uint64_t& left_limb,
674 const std::array<uint64_t, WASM_NUM_LIMBS>& right_limbs,
675 uint64_t& result_0,
676 uint64_t& result_1,
677 uint64_t& result_2,
678 uint64_t& result_3,
679 uint64_t& result_4,
680 uint64_t& result_5,
681 uint64_t& result_6,
682 uint64_t& result_7,
683 uint64_t& result_8);
684 BB_INLINE static constexpr void wasm_reduce(uint64_t& result_0,
685 uint64_t& result_1,
686 uint64_t& result_2,
687 uint64_t& result_3,
688 uint64_t& result_4,
689 uint64_t& result_5,
690 uint64_t& result_6,
691 uint64_t& result_7,
692 uint64_t& result_8);
693 BB_INLINE static constexpr void wasm_reduce_yuval(uint64_t& result_0,
694 uint64_t& result_1,
695 uint64_t& result_2,
696 uint64_t& result_3,
697 uint64_t& result_4,
698 uint64_t& result_5,
699 uint64_t& result_6,
700 uint64_t& result_7,
701 uint64_t& result_8,
702 uint64_t& result_9);
703 BB_INLINE static constexpr std::array<uint64_t, WASM_NUM_LIMBS> wasm_convert(const uint64_t* data);
704#endif
705 BB_INLINE static constexpr std::pair<uint64_t, uint64_t> mul_wide(uint64_t a, uint64_t b) noexcept;
706
707 BB_INLINE static constexpr uint64_t mac(
708 uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t& carry_out) noexcept;
709
710 BB_INLINE static constexpr void mac(
711 uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t& out, uint64_t& carry_out) noexcept;
712
713 BB_INLINE static constexpr uint64_t mac_mini(uint64_t a, uint64_t b, uint64_t c, uint64_t& out) noexcept;
714
715 BB_INLINE static constexpr void mac_mini(
716 uint64_t a, uint64_t b, uint64_t c, uint64_t& out, uint64_t& carry_out) noexcept;
717
718 BB_INLINE static constexpr uint64_t mac_discard_lo(uint64_t a, uint64_t b, uint64_t c) noexcept;
719
720 BB_INLINE static constexpr uint64_t addc(uint64_t a, uint64_t b, uint64_t carry_in, uint64_t& carry_out) noexcept;
721
722 BB_INLINE static constexpr uint64_t sbb(uint64_t a, uint64_t b, uint64_t borrow_in, uint64_t& borrow_out) noexcept;
723
724 BB_INLINE static constexpr uint64_t square_accumulate(uint64_t a,
725 uint64_t b,
726 uint64_t c,
727 uint64_t carry_in_lo,
728 uint64_t carry_in_hi,
729 uint64_t& carry_lo,
730 uint64_t& carry_hi) noexcept;
731 BB_INLINE constexpr field reduce() const noexcept;
732 BB_INLINE constexpr field add(const field& other) const noexcept;
733 BB_INLINE constexpr field subtract(const field& other) const noexcept;
734 BB_INLINE constexpr field montgomery_mul(const field& other) const noexcept;
735 BB_INLINE constexpr field montgomery_mul_big(const field& other) const noexcept;
736 BB_INLINE constexpr field montgomery_square() const noexcept;
737
738#if (BBERG_NO_ASM == 0)
739 BB_INLINE static field asm_mul_with_coarse_reduction(const field& a, const field& b) noexcept;
740 BB_INLINE static field asm_sqr_with_coarse_reduction(const field& a) noexcept;
741 BB_INLINE static field asm_add_with_coarse_reduction(const field& a, const field& b) noexcept;
742 BB_INLINE static field asm_sub_with_coarse_reduction(const field& a, const field& b) noexcept;
743 BB_INLINE static void asm_self_mul_with_coarse_reduction(const field& a, const field& b) noexcept;
744 BB_INLINE static void asm_self_sqr_with_coarse_reduction(const field& a) noexcept;
745 BB_INLINE static void asm_self_add_with_coarse_reduction(const field& a, const field& b) noexcept;
746 BB_INLINE static void asm_self_sub_with_coarse_reduction(const field& a, const field& b) noexcept;
747
748 BB_INLINE static void asm_conditional_negate(field& r, uint64_t predicate) noexcept;
749 BB_INLINE static field asm_reduce_once(const field& a) noexcept;
750 BB_INLINE static void asm_self_reduce_once(const field& a) noexcept;
751 static constexpr uint64_t zero_reference = 0x00ULL;
752#endif
753 static constexpr size_t COSET_GENERATOR_SIZE = 15;
754 constexpr field tonelli_shanks_sqrt() const noexcept;
755 static constexpr size_t primitive_root_log_size() noexcept;
756
757#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
758 static constexpr uint128_t lo_mask = 0xffffffffffffffffUL;
759#endif
760};
761
762template <typename B, typename Params> void read(B& it, field<Params>& value)
763{
764 using serialize::read;
765 field<Params> result{ 0, 0, 0, 0 };
766 read(it, result.data[3]);
767 read(it, result.data[2]);
768 read(it, result.data[1]);
769 read(it, result.data[0]);
770 value = result.to_montgomery_form();
771}
772template <typename B, typename Params> void write(B& buf, field<Params> const& value)
773{
774 using serialize::write;
776 write(buf, input.data[3]);
777 write(buf, input.data[2]);
778 write(buf, input.data[1]);
779 write(buf, input.data[0]);
780}
781
782} // namespace bb
783
784// Define hash function for field elements, e.g., so that it can be used in maps.
785// See https://en.cppreference.com/w/cpp/utility/hash .
786template <typename Params> struct std::hash<bb::field<Params>> {
787 std::size_t operator()(const bb::field<Params>& ff) const noexcept
788 {
789 // Just like in equality, we need to reduce the field element before hashing.
790 auto reduced = ff.reduce_once();
791 return bb::utils::hash_as_tuple(reduced.data[0], reduced.data[1], reduced.data[2], reduced.data[3]);
792 }
793};
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Definition group.hpp:36
#define BB_INLINE
FF a
FF b
uint8_t const * buf
Definition data_store.hpp:9
numeric::RNG & engine
std::unique_ptr< uint8_t[]> buffer
Definition engine.cpp:50
uintx< uint256_t > uint512_t
Definition uintx.hpp:306
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:45
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
BB_INLINE constexpr field from_montgomery_form_reduced() const noexcept
static constexpr std::array< uint64_t, 9 > wasm_modulus
static constexpr field get_root_of_unity(size_t subgroup_size) noexcept
BB_INLINE constexpr void self_to_montgomery_form_reduced() &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
BB_INLINE constexpr void self_from_montgomery_form_reduced() &noexcept
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
unsigned 64-bit subtract-with-borrow that takes in borrow_in value in the size-2 set {0,...
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
Compute uint128_t(a * b + c + carry_in), where the inputs are all uint64_t. Return the top 64 bits.
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
unsigned 64-bit add-with-carry that takes in a carry_in and a carry_out bit and rewrites the latter.
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 calling this...
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
cast four uint64_t as a field
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 accumulate 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
Computes a + 2 * b * c + carry_in_lo + 2^64 * carry_in_hi, in the form of returning a uint64_t and mo...
BB_INLINE constexpr field to_montgomery_form_reduced() const 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
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
BB_INLINE constexpr bool is_zero() const noexcept
static void batch_invert(C &coeffs) noexcept
Batch invert a collection of field elements using Montgomery's trick.
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
Squaring via a variant of the Montgomery algorithm, where we roughly take advantage of the repeated t...
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
reduce once, i.e., if the value is bigger than the modulus, subtract off the modulus once.
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