Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
field2.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
17namespace bb {
18template <class base, class T> constexpr field2<base, T> field2<base, T>::operator*(const field2& other) const noexcept
19{
20 // no funny primes please! we assume -1 is not a quadratic residue
21 static_assert((base::modulus.data[0] & 0x3UL) == 0x3UL);
22 base t1 = c0 * other.c0;
23 base t2 = c1 * other.c1;
24 base t3 = c0 + c1;
25 base t4 = other.c0 + other.c1;
26
27 return { t1 - t2, t3 * t4 - (t1 + t2) };
28}
29
30template <class base, class T> constexpr field2<base, T> field2<base, T>::operator+(const field2& other) const noexcept
31{
32 return { c0 + other.c0, c1 + other.c1 };
33}
34
35template <class base, class T> constexpr field2<base, T> field2<base, T>::operator-(const field2& other) const noexcept
36{
37 return { c0 - other.c0, c1 - other.c1 };
38}
39
40template <class base, class T> constexpr field2<base, T> field2<base, T>::operator-() const noexcept
41{
42 return { -c0, -c1 };
43}
44
45template <class base, class T> constexpr field2<base, T> field2<base, T>::operator/(const field2& other) const noexcept
46{
47 return operator*(other.invert());
48}
49
50template <class base, class T> constexpr field2<base, T> field2<base, T>::operator*=(const field2& other) noexcept
51{
52 *this = operator*(other);
53 return *this;
54}
55
56template <class base, class T> constexpr field2<base, T> field2<base, T>::operator+=(const field2& other) noexcept
57{
58 *this = operator+(other);
59 return *this;
60}
61
62template <class base, class T> constexpr field2<base, T> field2<base, T>::operator-=(const field2& other) noexcept
63{
64 *this = operator-(other);
65 return *this;
66}
67
68template <class base, class T> constexpr field2<base, T> field2<base, T>::operator/=(const field2& other) noexcept
69{
70 *this = operator/(other);
71 return *this;
72}
73
74template <class base, class T> constexpr field2<base, T> field2<base, T>::sqr() const noexcept
75{
76 base t1 = (c0 * c1);
77 return { (c0 + c1) * (c0 - c1), t1 + t1 };
78}
79
80template <class base, class T> constexpr void field2<base, T>::self_sqr() noexcept
81{
82 *this = sqr();
83}
84
85template <class base, class T> constexpr field2<base, T> field2<base, T>::to_montgomery_form() const noexcept
86{
87 return { c0.to_montgomery_form(), c1.to_montgomery_form() };
88}
89
90template <class base, class T> constexpr field2<base, T> field2<base, T>::from_montgomery_form() const noexcept
91{
92 return { c0.from_montgomery_form(), c1.from_montgomery_form() };
93}
94
95template <class base, class T> constexpr void field2<base, T>::self_to_montgomery_form() noexcept
96{
98 c1.self_to_montgomery_form();
99}
100
101template <class base, class T> constexpr void field2<base, T>::self_from_montgomery_form() noexcept
102{
103 c0.self_from_montgomery_form();
104 c1.self_from_montgomery_form();
105}
106
107template <class base, class T> constexpr field2<base, T> field2<base, T>::reduce_once() const noexcept
108{
109 return *this;
110 // return { c0.reduce_once(), c1.reduce_once() };
111}
112
113template <class base, class T> constexpr void field2<base, T>::self_reduce_once() noexcept
114{
115 // c0.self_reduce_once();
116 // c1.self_reduce_once();
117}
118
119template <class base, class T> constexpr void field2<base, T>::self_neg() noexcept
120{
121 c0.self_neg();
122 c1.self_neg();
123}
124
125template <class base, class T> constexpr field2<base, T> field2<base, T>::pow(const uint256_t& exponent) const noexcept
126{
127
128 field2 accumulator = *this;
129 field2 to_mul = *this;
130 const uint64_t maximum_set_bit = exponent.get_msb();
131
132 for (int i = static_cast<int>(maximum_set_bit) - 1; i >= 0; --i) {
133 accumulator.self_sqr();
134 if (exponent.get_bit(static_cast<uint64_t>(i))) {
135 accumulator *= to_mul;
136 }
137 }
138
139 if (*this == zero()) {
140 accumulator = zero();
141 } else if (exponent == uint256_t(0)) {
142 accumulator = one();
143 }
144 return accumulator;
145}
146
147template <class base, class T> constexpr field2<base, T> field2<base, T>::pow(const uint64_t exponent) const noexcept
148{
149 return pow({ exponent, 0, 0, 0 });
150}
151
152template <class base, class T> constexpr field2<base, T> field2<base, T>::invert() const noexcept
153{
154 base t3 = (c0.sqr() + c1.sqr()).invert();
155 return { c0 * t3, -(c1 * t3) };
156}
157
158template <class base, class T>
159constexpr void field2<base, T>::self_conditional_negate(const uint64_t predicate) noexcept
160{
161 *this = predicate != 0U ? -(*this) : *this;
162}
163
164template <class base, class T> constexpr void field2<base, T>::self_set_msb() noexcept
165{
166 c0.data[3] = 0ULL | (1ULL << 63ULL);
167}
168
169template <class base, class T> constexpr bool field2<base, T>::is_msb_set() const noexcept
170{
171 return (c0.data[3] >> 63ULL) == 1ULL;
172}
173
174template <class base, class T> constexpr uint64_t field2<base, T>::is_msb_set_word() const noexcept
175{
176 return (c0.data[3] >> 63ULL);
177}
178
179template <class base, class T> constexpr bool field2<base, T>::is_zero() const noexcept
180{
181 return (c0.is_zero() && c1.is_zero());
182}
183
184template <class base, class T> constexpr bool field2<base, T>::operator==(const field2& other) const noexcept
185{
186 return (c0 == other.c0) && (c1 == other.c1);
187}
188
189template <class base, class T> constexpr field2<base, T> field2<base, T>::frobenius_map() const noexcept
190{
191 return { c0, -c1 };
192}
193
194template <class base, class T> constexpr void field2<base, T>::self_frobenius_map() noexcept
195{
196 c1.self_neg();
197}
198
200{
201 return { base::random_element(engine), base::random_element(engine) };
202}
203} // namespace bb
numeric::RNG & engine
Entry point for Barretenberg command-line interface.
Univariate< Fr, domain_end, domain_start, skip_count > operator+(const Fr &ff, const Univariate< Fr, domain_end, domain_start, skip_count > &uv)
Univariate< Fr, domain_end, domain_start, skip_count > operator*(const Fr &ff, const Univariate< Fr, domain_end, domain_start, skip_count > &uv)
Univariate< Fr, domain_end, domain_start, skip_count > operator-(const Fr &ff, const Univariate< Fr, domain_end, domain_start, skip_count > &uv)
constexpr void self_set_msb() noexcept
Definition field2.hpp:164
constexpr void self_conditional_negate(uint64_t predicate) noexcept
Definition field2.hpp:159
constexpr void self_to_montgomery_form() noexcept
Definition field2.hpp:95
constexpr bool operator==(const field2 &other) const noexcept
Definition field2.hpp:184
constexpr field2 sqr() const noexcept
Definition field2.hpp:74
constexpr field2 operator/=(const field2 &other) noexcept
Definition field2.hpp:68
constexpr field2 operator-=(const field2 &other) noexcept
Definition field2.hpp:62
constexpr field2 to_montgomery_form() const noexcept
Definition field2.hpp:85
constexpr void self_from_montgomery_form() noexcept
Definition field2.hpp:101
constexpr void self_reduce_once() noexcept
Definition field2.hpp:113
constexpr void self_neg() noexcept
Definition field2.hpp:119
constexpr field2 operator*=(const field2 &other) noexcept
Definition field2.hpp:50
constexpr field2 operator-() const noexcept
Definition field2.hpp:40
constexpr field2 operator+(const field2 &other) const noexcept
Definition field2.hpp:30
constexpr void self_frobenius_map() noexcept
Definition field2.hpp:194
constexpr field2 invert() const noexcept
Definition field2.hpp:152
constexpr bool is_msb_set() const noexcept
Definition field2.hpp:169
constexpr field2 operator+=(const field2 &other) noexcept
Definition field2.hpp:56
constexpr field2 operator/(const field2 &other) const noexcept
Definition field2.hpp:45
static field2 random_element(numeric::RNG *engine=nullptr)
Definition field2.hpp:199
constexpr field2 from_montgomery_form() const noexcept
Definition field2.hpp:90
constexpr bool is_zero() const noexcept
Definition field2.hpp:179
constexpr void self_sqr() noexcept
Definition field2.hpp:80
constexpr field2 pow(const uint256_t &exponent) const noexcept
Definition field2.hpp:125
constexpr field2 reduce_once() const noexcept
Definition field2.hpp:107
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