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: 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
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 }; }
66 {
67 return field2{ Params::frobenius_on_twisted_curve_x_0, Params::frobenius_on_twisted_curve_x_1 };
68 }
70 {
71 return field2{ Params::frobenius_on_twisted_curve_y_0, Params::frobenius_on_twisted_curve_y_1 };
72 }
73
74 constexpr field2 operator*(const field2& other) const noexcept;
75 constexpr field2 operator+(const field2& other) const noexcept;
76 constexpr field2 operator-(const field2& other) const noexcept;
77 constexpr field2 operator-() const noexcept;
78 constexpr field2 operator/(const field2& other) const noexcept;
79
80 constexpr field2 operator*=(const field2& other) noexcept;
81 constexpr field2 operator+=(const field2& other) noexcept;
82 constexpr field2 operator-=(const field2& other) noexcept;
83 constexpr field2 operator/=(const field2& other) noexcept;
84
85 constexpr field2 mul_by_fq(const base_field& a) const noexcept
86 {
87 field2 r{ a * c0, a * c1 };
88 return r;
89 }
90
91 constexpr bool operator==(const field2& other) const noexcept;
92 constexpr bool operator!=(const field2& other) const noexcept { return !(*this == other); }
93 constexpr field2 sqr() const noexcept;
94 constexpr void self_sqr() noexcept;
95
96 constexpr field2 pow(const uint256_t& exponent) const noexcept;
97 constexpr field2 pow(uint64_t exponent) const noexcept;
98
99 constexpr field2 invert() const noexcept;
100
101 constexpr void self_neg() noexcept;
102 constexpr field2 to_montgomery_form() const noexcept;
103 constexpr field2 from_montgomery_form() const noexcept;
104
105 constexpr void self_to_montgomery_form() noexcept;
106 constexpr void self_from_montgomery_form() noexcept;
107
108 constexpr void self_conditional_negate(uint64_t predicate) noexcept;
109
110 constexpr field2 reduce_once() const noexcept;
111 constexpr void self_reduce_once() noexcept;
112
113 constexpr void self_set_msb() noexcept;
114 [[nodiscard]] constexpr bool is_msb_set() const noexcept;
115 [[nodiscard]] constexpr uint64_t is_msb_set_word() const noexcept;
116
117 [[nodiscard]] constexpr bool is_zero() const noexcept;
118
119 constexpr field2 frobenius_map() const noexcept;
120 constexpr void self_frobenius_map() noexcept;
121
122 static field2 random_element(numeric::RNG* engine = nullptr);
123 static void serialize_to_buffer(const field2& value, uint8_t* buffer)
124 {
125 base_field::serialize_to_buffer(value.c0, buffer);
126 base_field::serialize_to_buffer(value.c1, buffer + sizeof(base_field));
127 }
128
130 {
131 field2 result{ base_field::zero(), base_field::zero() };
132 result.c0 = base_field::serialize_from_buffer(buffer);
133 result.c1 = base_field::serialize_from_buffer(buffer + sizeof(base_field));
134
135 return result;
136 }
137
138 friend std::ostream& operator<<(std::ostream& os, const field2& a)
139 {
140 os << a.c0 << " , " << a.c1;
141 return os;
142 }
143};
144
145template <typename B, typename base_field, typename Params> void read(B& it, field2<base_field, Params>& value)
146{
147 using serialize::read;
148 read(it, value.c0);
149 read(it, value.c1);
150}
151template <typename B, typename base_field, typename Params> void write(B& buf, field2<base_field, Params> const& value)
152{
153 using serialize::write;
154 write(buf, value.c0);
155 write(buf, value.c1);
156}
157} // namespace bb
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
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
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:166
constexpr void self_conditional_negate(uint64_t predicate) noexcept
Definition field2.hpp:161
static field2 serialize_from_buffer(uint8_t *buffer)
constexpr void self_to_montgomery_form() noexcept
Definition field2.hpp:97
constexpr bool operator!=(const field2 &other) const noexcept
constexpr bool operator==(const field2 &other) const noexcept
Definition field2.hpp:186
constexpr field2 sqr() const noexcept
Definition field2.hpp:74
constexpr field2 to_montgomery_form() const noexcept
Definition field2.hpp:87
constexpr void self_from_montgomery_form() noexcept
Definition field2.hpp:103
constexpr field2 mul_by_fq(const base_field &a) const noexcept
constexpr void self_reduce_once() noexcept
Definition field2.hpp:115
constexpr void self_neg() noexcept
Definition field2.hpp:121
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 frobenius_on_twisted_curve_y()
static constexpr field2 zero()
constexpr field2(const field2 &other) noexcept
constexpr void self_frobenius_map() noexcept
Definition field2.hpp:196
constexpr field2 & operator=(const field2 &other) noexcept
constexpr field2 invert() const noexcept
Definition field2.hpp:154
static constexpr uint256_t modulus
static constexpr field2 twist_coeff_b()
constexpr bool is_msb_set() const noexcept
Definition field2.hpp:171
static field2 random_element(numeric::RNG *engine=nullptr)
Definition field2.hpp:201
constexpr field2 from_montgomery_form() const noexcept
Definition field2.hpp:92
static constexpr field2 frobenius_on_twisted_curve_x()
constexpr bool is_zero() const noexcept
Definition field2.hpp:181
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:127
constexpr field2 & operator=(field2 &&other) noexcept
constexpr field2 reduce_once() const noexcept
Definition field2.hpp:109
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:176
constexpr field2 operator*(const field2 &other) const noexcept
Definition field2.hpp:18
constexpr field2 frobenius_map() const noexcept
Definition field2.hpp:191
constexpr ~field2() noexcept=default