Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
field2_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
8
10
11// forward declare RNG
12namespace bb::numeric {
13class RNG;
14}
15
16namespace bb {
17template <class base_field, class Params> struct alignas(32) field2 {
18 public:
19 static constexpr size_t PUBLIC_INPUTS_SIZE = base_field::PUBLIC_INPUTS_SIZE + base_field::PUBLIC_INPUTS_SIZE;
20
21 constexpr field2(const base_field& a = base_field::zero(), const base_field& b = base_field::zero())
22 : c0(a)
23 , c1(b)
24 {}
25
26 constexpr field2(const field2& other) noexcept
27 : c0(other.c0)
28 , c1(other.c1)
29 {}
30 constexpr field2(field2&& other) noexcept
31 : c0(other.c0)
32 , c1(other.c1)
33 {}
34
35 constexpr field2& operator=(const field2& other) noexcept
36 {
37 if (this == &other) {
38 return *this;
39 }
40 c0 = other.c0;
41 c1 = other.c1;
42 return *this;
43 }
44
45 constexpr field2& operator=(field2&& other) noexcept
46 {
47 if (this == &other) {
48 return *this;
49 }
50 c0 = other.c0;
51 c1 = other.c1;
52 return *this;
53 }
54
55 constexpr ~field2() noexcept = default;
56
57 base_field c0;
58 base_field c1;
59
60 static constexpr uint256_t modulus = base_field::modulus;
61
62 static constexpr field2 zero() { return field2{ base_field::zero(), base_field::zero() }; }
63 static constexpr field2 one() { return field2{ base_field::one(), base_field::zero() }; }
64 static constexpr field2 twist_coeff_b() { return field2{ Params::twist_coeff_b_0, Params::twist_coeff_b_1 }; }
65 static constexpr field2 twist_mul_by_q_x()
66 {
67 return field2{ Params::twist_mul_by_q_x_0, Params::twist_mul_by_q_x_1 };
68 }
69 static constexpr field2 twist_mul_by_q_y()
70 {
71 return field2{ Params::twist_mul_by_q_y_0, Params::twist_mul_by_q_y_1 };
72 }
73 static constexpr field2 cube_root_of_unity()
74 {
75 return field2{ Params::twist_cube_root_0, Params::twist_cube_root_1 };
76 }
77
78 constexpr field2 operator*(const field2& other) const noexcept;
79 constexpr field2 operator+(const field2& other) const noexcept;
80 constexpr field2 operator-(const field2& other) const noexcept;
81 constexpr field2 operator-() const noexcept;
82 constexpr field2 operator/(const field2& other) const noexcept;
83
84 constexpr field2 operator*=(const field2& other) noexcept;
85 constexpr field2 operator+=(const field2& other) noexcept;
86 constexpr field2 operator-=(const field2& other) noexcept;
87 constexpr field2 operator/=(const field2& other) noexcept;
88
89 constexpr field2 mul_by_fq(const base_field& a) const noexcept
90 {
91 field2 r{ a * c0, a * c1 };
92 return r;
93 }
94
95 constexpr bool operator==(const field2& other) const noexcept;
96 constexpr bool operator!=(const field2& other) const noexcept { return !(*this == other); }
97 constexpr field2 sqr() const noexcept;
98 constexpr void self_sqr() noexcept;
99
100 constexpr field2 pow(const uint256_t& exponent) const noexcept;
101 constexpr field2 pow(uint64_t exponent) const noexcept;
102
103 constexpr field2 invert() const noexcept;
104
105 constexpr void self_neg() noexcept;
106 constexpr field2 to_montgomery_form() const noexcept;
107 constexpr field2 from_montgomery_form() const noexcept;
108
109 constexpr void self_to_montgomery_form() noexcept;
110 constexpr void self_from_montgomery_form() noexcept;
111
112 constexpr void self_conditional_negate(uint64_t predicate) noexcept;
113
114 constexpr field2 reduce_once() const noexcept;
115 constexpr void self_reduce_once() noexcept;
116
117 constexpr void self_set_msb() noexcept;
118 [[nodiscard]] constexpr bool is_msb_set() const noexcept;
119 [[nodiscard]] constexpr uint64_t is_msb_set_word() const noexcept;
120
121 [[nodiscard]] constexpr bool is_zero() const noexcept;
122
123 constexpr field2 frobenius_map() const noexcept;
124 constexpr void self_frobenius_map() noexcept;
125
126 static field2 random_element(numeric::RNG* engine = nullptr);
127 static void serialize_to_buffer(const field2& value, uint8_t* buffer)
128 {
129 base_field::serialize_to_buffer(value.c0, buffer);
130 base_field::serialize_to_buffer(value.c1, buffer + sizeof(base_field));
131 }
132
134 {
135 field2 result{ base_field::zero(), base_field::zero() };
136 result.c0 = base_field::serialize_from_buffer(buffer);
137 result.c1 = base_field::serialize_from_buffer(buffer + sizeof(base_field));
138
139 return result;
140 }
141
142 friend std::ostream& operator<<(std::ostream& os, const field2& a)
143 {
144 os << a.c0 << " , " << a.c1;
145 return os;
146 }
147};
148
149template <typename B, typename base_field, typename Params> void read(B& it, field2<base_field, Params>& value)
150{
151 using serialize::read;
152 read(it, value.c0);
153 read(it, value.c1);
154}
155template <typename B, typename base_field, typename Params> void write(B& buf, field2<base_field, Params> const& value)
156{
157 using serialize::write;
158 write(buf, value.c0);
159 write(buf, value.c1);
160}
161} // namespace bb
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
Entry point for Barretenberg command-line interface.
void read(B &it, field2< base_field, Params > &value)
void write(B &buf, field2< base_field, Params > const &value)
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...
constexpr void self_set_msb() noexcept
Definition field2.hpp:164
constexpr void self_conditional_negate(uint64_t predicate) noexcept
Definition field2.hpp:159
static field2 serialize_from_buffer(uint8_t *buffer)
constexpr void self_to_montgomery_form() noexcept
Definition field2.hpp:95
constexpr bool operator!=(const field2 &other) const noexcept
constexpr bool operator==(const field2 &other) const noexcept
Definition field2.hpp:184
constexpr field2 sqr() const noexcept
Definition field2.hpp:74
static constexpr field2 twist_mul_by_q_y()
constexpr field2 to_montgomery_form() const noexcept
Definition field2.hpp:85
constexpr void self_from_montgomery_form() noexcept
Definition field2.hpp:101
constexpr field2 mul_by_fq(const base_field &a) const noexcept
constexpr void self_reduce_once() noexcept
Definition field2.hpp:113
constexpr void self_neg() noexcept
Definition field2.hpp:119
static constexpr field2 one()
constexpr field2 operator-() const noexcept
Definition field2.hpp:40
constexpr field2 operator+(const field2 &other) const noexcept
Definition field2.hpp:30
static constexpr size_t PUBLIC_INPUTS_SIZE
static constexpr field2 twist_mul_by_q_x()
static constexpr field2 zero()
constexpr field2(const field2 &other) noexcept
constexpr void self_frobenius_map() noexcept
Definition field2.hpp:194
constexpr field2 & operator=(const field2 &other) noexcept
constexpr field2 invert() const noexcept
Definition field2.hpp:152
static constexpr uint256_t modulus
static constexpr field2 twist_coeff_b()
constexpr bool is_msb_set() const noexcept
Definition field2.hpp:169
static field2 random_element(numeric::RNG *engine=nullptr)
Definition field2.hpp:199
constexpr field2 from_montgomery_form() const noexcept
Definition field2.hpp:90
static constexpr field2 cube_root_of_unity()
constexpr bool is_zero() const noexcept
Definition field2.hpp:179
constexpr field2(const base_field &a=base_field::zero(), const base_field &b=base_field::zero())
friend std::ostream & operator<<(std::ostream &os, const field2 &a)
constexpr void self_sqr() noexcept
Definition field2.hpp:80
constexpr field2 pow(const uint256_t &exponent) const noexcept
Definition field2.hpp:125
constexpr field2 & operator=(field2 &&other) noexcept
constexpr field2 reduce_once() const noexcept
Definition field2.hpp:107
static void serialize_to_buffer(const field2 &value, uint8_t *buffer)
constexpr field2(field2 &&other) noexcept
constexpr uint64_t is_msb_set_word() const noexcept
Definition field2.hpp:174
constexpr field2 operator*(const field2 &other) const noexcept
Definition field2.hpp:18
constexpr field2 frobenius_map() const noexcept
Definition field2.hpp:189
constexpr ~field2() noexcept=default