barretenberg
Loading...
Searching...
No Matches
field2_declarations.hpp
1#pragma once
2
3#include "barretenberg/numeric/uint256/uint256.hpp"
4
5// forward declare Engine
6namespace numeric::random {
7class Engine;
8}
9
10namespace barretenberg {
11template <class base_field, class Params> struct alignas(32) field2 {
12 public:
13 constexpr field2(const base_field& a = base_field::zero(), const base_field& b = base_field::zero())
14 : c0(a)
15 , c1(b)
16 {}
17
18 constexpr field2(const field2& other) noexcept
19 : c0(other.c0)
20 , c1(other.c1)
21 {}
22 constexpr field2(field2&& other) noexcept
23 : c0(other.c0)
24 , c1(other.c1)
25 {}
26
27 constexpr field2& operator=(const field2& other) noexcept
28 {
29 if (this == &other) {
30 return *this;
31 }
32 c0 = other.c0;
33 c1 = other.c1;
34 return *this;
35 }
36
37 constexpr field2& operator=(field2&& other) noexcept
38 {
39 if (this == &other) {
40 return *this;
41 }
42 c0 = other.c0;
43 c1 = other.c1;
44 return *this;
45 }
46
47 constexpr ~field2() noexcept = default;
48
49 base_field c0;
50 base_field c1;
51
52 static constexpr uint256_t modulus = base_field::modulus;
53
54 static constexpr field2 zero() { return field2{ base_field::zero(), base_field::zero() }; }
55 static constexpr field2 one() { return field2{ base_field::one(), base_field::zero() }; }
56 static constexpr field2 twist_coeff_b() { return field2{ Params::twist_coeff_b_0, Params::twist_coeff_b_1 }; }
57 static constexpr field2 twist_mul_by_q_x()
58 {
59 return field2{ Params::twist_mul_by_q_x_0, Params::twist_mul_by_q_x_1 };
60 }
61 static constexpr field2 twist_mul_by_q_y()
62 {
63 return field2{ Params::twist_mul_by_q_y_0, Params::twist_mul_by_q_y_1 };
64 }
65 static constexpr field2 cube_root_of_unity()
66 {
67 return field2{ Params::twist_cube_root_0, Params::twist_cube_root_1 };
68 }
69
70 constexpr field2 operator*(const field2& other) const noexcept;
71 constexpr field2 operator+(const field2& other) const noexcept;
72 constexpr field2 operator-(const field2& other) const noexcept;
73 constexpr field2 operator-() const noexcept;
74 constexpr field2 operator/(const field2& other) const noexcept;
75
76 constexpr field2 operator*=(const field2& other) noexcept;
77 constexpr field2 operator+=(const field2& other) noexcept;
78 constexpr field2 operator-=(const field2& other) noexcept;
79 constexpr field2 operator/=(const field2& other) noexcept;
80
81 constexpr field2 mul_by_fq(const base_field& a) const noexcept
82 {
83 field2 r{ a * c0, a * c1 };
84 return r;
85 }
86
87 constexpr bool operator==(const field2& other) const noexcept;
88 constexpr bool operator!=(const field2& other) const noexcept { return !(*this == other); }
89 constexpr field2 sqr() const noexcept;
90 constexpr void self_sqr() noexcept;
91
92 constexpr field2 pow(const uint256_t& exponent) const noexcept;
93 constexpr field2 pow(uint64_t exponent) const noexcept;
94
95 constexpr field2 invert() const noexcept;
96
97 constexpr void self_neg() noexcept;
98 constexpr field2 to_montgomery_form() const noexcept;
99 constexpr field2 from_montgomery_form() const noexcept;
100
101 constexpr void self_to_montgomery_form() noexcept;
102 constexpr void self_from_montgomery_form() noexcept;
103
104 constexpr void self_conditional_negate(uint64_t predicate) noexcept;
105
106 constexpr field2 reduce_once() const noexcept;
107 constexpr void self_reduce_once() noexcept;
108
109 constexpr void self_set_msb() noexcept;
110 [[nodiscard]] constexpr bool is_msb_set() const noexcept;
111 [[nodiscard]] constexpr uint64_t is_msb_set_word() const noexcept;
112
113 [[nodiscard]] constexpr bool is_zero() const noexcept;
114
115 constexpr field2 frobenius_map() const noexcept;
116 constexpr void self_frobenius_map() noexcept;
117
118 static field2 random_element(numeric::random::Engine* engine = nullptr);
119 static void serialize_to_buffer(const field2& value, uint8_t* buffer)
120 {
121 base_field::serialize_to_buffer(value.c0, buffer);
122 base_field::serialize_to_buffer(value.c1, buffer + sizeof(base_field));
123 }
124
125 static field2 serialize_from_buffer(uint8_t* buffer)
126 {
127 field2 result{ base_field::zero(), base_field::zero() };
128 result.c0 = base_field::serialize_from_buffer(buffer);
129 result.c1 = base_field::serialize_from_buffer(buffer + sizeof(base_field));
130
131 return result;
132 }
133
134 friend std::ostream& operator<<(std::ostream& os, const field2& a)
135 {
136 os << a.c0 << " , " << a.c1;
137 return os;
138 }
139};
140
141template <typename B, typename base_field, typename Params> void read(B& it, field2<base_field, Params>& value)
142{
143 using serialize::read;
144 read(it, value.c0);
145 read(it, value.c1);
146}
147template <typename B, typename base_field, typename Params> void write(B& buf, field2<base_field, Params> const& value)
148{
149 using serialize::write;
150 write(buf, value.c0);
151 write(buf, value.c1);
152}
153} // namespace barretenberg
Definition: engine.hpp:10
Definition: uint256.hpp:25
constexpr_utils defines some helper methods that perform some stl-equivalent operations but in a cons...
Definition: constexpr_utils.hpp:16
Definition: field2_declarations.hpp:11