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: 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
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 *this;
112 // return { c0.reduce_once(), c1.reduce_once() };
115template <class base, class T> constexpr void field2<base, T>::self_reduce_once() noexcept
116{
117 // c0.self_reduce_once();
118 // c1.self_reduce_once();
121template <class base, class T> constexpr void field2<base, T>::self_neg() noexcept
123 c0.self_neg();
124 c1.self_neg();
125}
126
127template <class base, class T> constexpr field2<base, T> field2<base, T>::pow(const uint256_t& exponent) const noexcept
128{
129
130 field2 accumulator = *this;
131 field2 to_mul = *this;
132 const uint64_t maximum_set_bit = exponent.get_msb();
133
134 for (int i = static_cast<int>(maximum_set_bit) - 1; i >= 0; --i) {
135 accumulator.self_sqr();
136 if (exponent.get_bit(static_cast<uint64_t>(i))) {
137 accumulator *= to_mul;
138 }
139 }
140
141 if (*this == zero()) {
142 accumulator = zero();
143 } else if (exponent == uint256_t(0)) {
144 accumulator = one();
145 }
146 return accumulator;
147}
148
149template <class base, class T> constexpr field2<base, T> field2<base, T>::pow(const uint64_t exponent) const noexcept
150{
151 return pow({ exponent, 0, 0, 0 });
152}
153
154template <class base, class T> constexpr field2<base, T> field2<base, T>::invert() const noexcept
155{
156 base t3 = (c0.sqr() + c1.sqr()).invert();
157 return { c0 * t3, -(c1 * t3) };
158}
159
160template <class base, class T>
161constexpr void field2<base, T>::self_conditional_negate(const uint64_t predicate) noexcept
162{
163 *this = predicate != 0U ? -(*this) : *this;
164}
165
166template <class base, class T> constexpr void field2<base, T>::self_set_msb() noexcept
167{
168 c0.data[3] = 0ULL | (1ULL << 63ULL);
169}
170
171template <class base, class T> constexpr bool field2<base, T>::is_msb_set() const noexcept
172{
173 return (c0.data[3] >> 63ULL) == 1ULL;
174}
175
176template <class base, class T> constexpr uint64_t field2<base, T>::is_msb_set_word() const noexcept
177{
178 return (c0.data[3] >> 63ULL);
179}
180
181template <class base, class T> constexpr bool field2<base, T>::is_zero() const noexcept
182{
183 return (c0.is_zero() && c1.is_zero());
184}
185
186template <class base, class T> constexpr bool field2<base, T>::operator==(const field2& other) const noexcept
187{
188 return (c0 == other.c0) && (c1 == other.c1);
189}
190
191template <class base, class T> constexpr field2<base, T> field2<base, T>::frobenius_map() const noexcept
192{
193 return { c0, -c1 };
194}
195
196template <class base, class T> constexpr void field2<base, T>::self_frobenius_map() noexcept
197{
198 c1.self_neg();
199}
200
202{
203 return { base::random_element(engine), base::random_element(engine) };
204}
205} // 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:166
constexpr void self_conditional_negate(uint64_t predicate) noexcept
Definition field2.hpp:161
constexpr void self_to_montgomery_form() noexcept
Definition field2.hpp:97
constexpr bool operator==(const field2 &other) const noexcept
Definition field2.hpp:186
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:115
constexpr void self_neg() noexcept
Definition field2.hpp:121
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:196
constexpr field2 invert() const noexcept
Definition field2.hpp:154
constexpr bool is_msb_set() const noexcept
Definition field2.hpp:171
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:201
constexpr field2 from_montgomery_form() const noexcept
Definition field2.hpp:92
constexpr bool is_zero() const noexcept
Definition field2.hpp:181
constexpr void self_sqr() noexcept
Definition field2.hpp:80
constexpr field2 pow(const uint256_t &exponent) const noexcept
Definition field2.hpp:127
constexpr field2 reduce_once() const noexcept
Definition field2.hpp:109
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