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: Completed, 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
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
76 base t1 = (c0 * c1);
77 return { (c0 + c1) * (c0 - c1), t1 + t1 };
79
80template <class base, class T> constexpr void field2<base, T>::self_sqr() noexcept
82 *this = sqr();
84
85// Montgomery form conversions use the reduced variants to ensure each component
86// is in canonical form [0, p) rather than the coarse internal representation [0, 2p).
87template <class base, class T> constexpr field2<base, T> field2<base, T>::to_montgomery_form() const noexcept
88{
89 return { c0.to_montgomery_form_reduced(), c1.to_montgomery_form_reduced() };
90}
92template <class base, class T> constexpr field2<base, T> field2<base, T>::from_montgomery_form() const noexcept
94 return { c0.from_montgomery_form_reduced(), c1.from_montgomery_form_reduced() };
95}
97template <class base, class T> constexpr void field2<base, T>::self_to_montgomery_form() noexcept
98{
99 c0.self_to_montgomery_form_reduced();
100 c1.self_to_montgomery_form_reduced();
103template <class base, class T> constexpr void field2<base, T>::self_from_montgomery_form() noexcept
104{
105 c0.self_from_montgomery_form_reduced();
106 c1.self_from_montgomery_form_reduced();
107}
109template <class base, class T> constexpr field2<base, T> field2<base, T>::reduce_once() const noexcept
111 return { c0.reduce_once(), c1.reduce_once() };
112}
114template <class base, class T> constexpr void field2<base, T>::self_reduce_once() noexcept
116 c0.self_reduce_once();
117 c1.self_reduce_once();
118}
120template <class base, class T> constexpr void field2<base, T>::self_neg() noexcept
121{
122 c0.self_neg();
123 c1.self_neg();
124}
125
126template <class base, class T> constexpr field2<base, T> field2<base, T>::pow(const uint256_t& exponent) const noexcept
127{
128
129 field2 accumulator = *this;
130 field2 to_mul = *this;
131 const uint64_t maximum_set_bit = exponent.get_msb();
132
133 for (int i = static_cast<int>(maximum_set_bit) - 1; i >= 0; --i) {
134 accumulator.self_sqr();
135 if (exponent.get_bit(static_cast<uint64_t>(i))) {
136 accumulator *= to_mul;
137 }
138 }
139
140 if (*this == zero()) {
141 accumulator = zero();
142 } else if (exponent == uint256_t(0)) {
143 accumulator = one();
144 }
145 return accumulator;
146}
147
148template <class base, class T> constexpr field2<base, T> field2<base, T>::pow(const uint64_t exponent) const noexcept
149{
150 return pow({ exponent, 0, 0, 0 });
151}
152
153template <class base, class T> constexpr field2<base, T> field2<base, T>::invert() const noexcept
154{
155 base t3 = (c0.sqr() + c1.sqr()).invert();
156 return { c0 * t3, -(c1 * t3) };
157}
158
159template <class base, class T>
160constexpr void field2<base, T>::self_conditional_negate(const uint64_t predicate) noexcept
161{
162 *this = predicate != 0U ? -(*this) : *this;
163}
164
165template <class base, class T> constexpr void field2<base, T>::self_set_msb() noexcept
166{
167 c0.data[3] = 0ULL | (1ULL << 63ULL);
168}
169
170template <class base, class T> constexpr bool field2<base, T>::is_msb_set() const noexcept
171{
172 return (c0.data[3] >> 63ULL) == 1ULL;
173}
174
175template <class base, class T> constexpr uint64_t field2<base, T>::is_msb_set_word() const noexcept
176{
177 return (c0.data[3] >> 63ULL);
178}
179
180template <class base, class T> constexpr bool field2<base, T>::is_zero() const noexcept
181{
182 return (c0.is_zero() && c1.is_zero());
183}
184
185template <class base, class T> constexpr bool field2<base, T>::operator==(const field2& other) const noexcept
186{
187 return (c0 == other.c0) && (c1 == other.c1);
188}
189
190template <class base, class T> constexpr field2<base, T> field2<base, T>::frobenius_map() const noexcept
191{
192 return { c0, -c1 };
193}
194
195template <class base, class T> constexpr void field2<base, T>::self_frobenius_map() noexcept
196{
197 c1.self_neg();
198}
199
201{
202 return { base::random_element(engine), base::random_element(engine) };
203}
204} // namespace bb
numeric::RNG & engine
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
Univariate< Fr, domain_end > operator+(const Fr &ff, const Univariate< Fr, domain_end > &uv)
Univariate< Fr, domain_end > operator-(const Fr &ff, const Univariate< Fr, domain_end > &uv)
Univariate< Fr, domain_end > operator*(const Fr &ff, const Univariate< Fr, domain_end > &uv)
constexpr void self_set_msb() noexcept
Definition field2.hpp:165
constexpr void self_conditional_negate(uint64_t predicate) noexcept
Definition field2.hpp:160
constexpr void self_to_montgomery_form() noexcept
Definition field2.hpp:97
constexpr bool operator==(const field2 &other) const noexcept
Definition field2.hpp:185
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:87
constexpr void self_from_montgomery_form() noexcept
Definition field2.hpp:103
constexpr void self_reduce_once() noexcept
Definition field2.hpp:114
constexpr void self_neg() noexcept
Definition field2.hpp:120
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:195
constexpr field2 invert() const noexcept
Definition field2.hpp:153
constexpr bool is_msb_set() const noexcept
Definition field2.hpp:170
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:200
constexpr field2 from_montgomery_form() const noexcept
Definition field2.hpp:92
constexpr bool is_zero() const noexcept
Definition field2.hpp:180
constexpr void self_sqr() noexcept
Definition field2.hpp:80
constexpr field2 pow(const uint256_t &exponent) const noexcept
Definition field2.hpp:126
constexpr field2 reduce_once() const noexcept
Definition field2.hpp:109
constexpr uint64_t is_msb_set_word() const noexcept
Definition field2.hpp:175
constexpr field2 operator*(const field2 &other) const noexcept
Definition field2.hpp:18
constexpr field2 frobenius_map() const noexcept
Definition field2.hpp:190