Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 6d28737

Browse files
ayashunskyIluvmagick
authored andcommitted
code cleaning and file placement
1 parent 25ab668 commit 6d28737

File tree

7 files changed

+84
-38
lines changed

7 files changed

+84
-38
lines changed
Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
// SOFTWARE.
2323
//---------------------------------------------------------------------------//
24-
// @file Declaration of template function for F_p^{12} field multiplication.
24+
// @file Declaration of F_p^{12} elements over ab abstract entity (to be used with constraints).
2525
// We use towered field extension
2626
// F_p^12 = F_p^6[w]/(w^2 - v),
2727
// F_p^6 = F_p^2[v]/(v^3-(u+1)),
2828
// F_p^2 = F_p[u]/(u^2 - (-1)).
2929
//---------------------------------------------------------------------------//
3030

31-
#ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_PERFORM_FP12_MULT_HPP
32-
#define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_PERFORM_FP12_MULT_HPP
31+
#ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP12_HPP
32+
#define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP12_HPP
3333

3434
namespace nil {
3535
namespace blueprint {
@@ -42,7 +42,7 @@ namespace nil {
4242
std::array<T,12> c;
4343

4444
for(std::size_t i = 0; i < 12; i++) {
45-
c[i] = a[0] - a[0]; // hack because we can't actually write c[i] = 0: type T might have casting problems
45+
c[i] = T(); // assume default constructor creates a "zero" object which is true for constraints and numbers
4646
}
4747

4848
for(std::size_t i = 0; i < 12; i++) {
@@ -79,9 +79,54 @@ namespace nil {
7979
}
8080
return c;
8181
}
82+
template<typename T>
83+
class abstract_fp12_element {
84+
public:
85+
std::array<T,12> data;
86+
87+
T& operator[](std::size_t idx) {
88+
return data[idx];
89+
}
90+
const T& operator[](std::size_t idx) const {
91+
return data[idx];
92+
}
93+
94+
constexpr abstract_fp12_element operator*(const abstract_fp12_element& other) {
95+
std::array<T,12> res = perform_fp12_mult(data,other.data);
96+
return { res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7], res[8], res[9], res[10], res[11] };
97+
}
98+
constexpr abstract_fp12_element operator*(const int x) {
99+
std::array<T,12> res;
100+
for(std::size_t i = 0; i < 12; i++) {
101+
res[i] = data[i] * x;
102+
}
103+
return { res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7], res[8], res[9], res[10], res[11] };
104+
}
105+
friend abstract_fp12_element operator*(const int x, const abstract_fp12_element& e) {
106+
std::array<T,12> res;
107+
for(std::size_t i = 0; i < 12; i++) {
108+
res[i] = e[i] * x;
109+
}
110+
return { res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7], res[8], res[9], res[10], res[11] };
111+
}
112+
constexpr abstract_fp12_element operator+(const abstract_fp12_element& other) {
113+
std::array<T,12> res;
114+
for(std::size_t i = 0; i < 12; i++) {
115+
res[i] = data[i] + other.data[i];
116+
}
117+
return { res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7], res[8], res[9], res[10], res[11] };
118+
}
119+
constexpr abstract_fp12_element operator-(const abstract_fp12_element& other) {
120+
std::array<T,12> res;
121+
for(std::size_t i = 0; i < 12; i++) {
122+
res[i] = data[i] - other.data[i];
123+
}
124+
return { res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7], res[8], res[9], res[10], res[11] };
125+
}
126+
};
82127
} // namespace detail
83128
} // namespace components
84129
} // namespace blueprint
85130
} // namespace nil
86131

87-
#endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_PERFORM_FP12_MULT_HPP
132+
#endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP12_HPP

include/nil/blueprint/components/algebra/fields/plonk/non_native/fp12_frobenius_map.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
#include <nil/blueprint/component.hpp>
4848
#include <nil/blueprint/manifest.hpp>
4949

50-
// #include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/perform_fp12_mult.hpp>
51-
5250
namespace nil {
5351
namespace blueprint {
5452
namespace components {

include/nil/blueprint/components/algebra/fields/plonk/non_native/fp12_inversion.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <nil/blueprint/component.hpp>
4242
#include <nil/blueprint/manifest.hpp>
4343

44-
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/perform_fp12_mult.hpp>
44+
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/abstract_fp12.hpp>
4545

4646
namespace nil {
4747
namespace blueprint {
@@ -50,8 +50,6 @@ namespace nil {
5050
// Input: x[12], x != 0
5151
// Output: y[12]: x*y = 1 as elements of F_p^12
5252

53-
using detail::perform_fp12_mult;
54-
5553
template<typename ArithmetizationType, typename BlueprintFieldType>
5654
class fp12_inversion;
5755

@@ -195,16 +193,18 @@ namespace nil {
195193
using var = typename plonk_fp12_inversion<BlueprintFieldType, ArithmetizationParams>::var;
196194
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;
197195

196+
using fp12_constraint = detail::abstract_fp12_element<constraint_type>;
197+
198198
const std::size_t WA = component.witness_amount();
199199

200-
std::array<constraint_type,12> X, Y, C;
200+
fp12_constraint X, Y, C;
201201

202202
for(std::size_t i = 0; i < 12; i++) {
203203
X[i] = var(component.W(i), 0, true);
204204
Y[i] = var(component.W((i+12) % WA), (i+12)/WA, true);
205205
}
206+
C = X * Y;
206207

207-
C = perform_fp12_mult(X,Y);
208208
std::vector<constraint_type> Cs = { C[0] - 1 };
209209
for(std::size_t i = 1; i < 12; i++) {
210210
Cs.push_back(C[i]);

include/nil/blueprint/components/algebra/fields/plonk/non_native/fp12_multiplication.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include <nil/blueprint/component.hpp>
3939
#include <nil/blueprint/manifest.hpp>
4040

41-
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/perform_fp12_mult.hpp>
41+
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/abstract_fp12.hpp>
4242

4343
namespace nil {
4444
namespace blueprint {
@@ -47,8 +47,6 @@ namespace nil {
4747
// Input: a[12], b[12]
4848
// Output: c[12] = a*b as elements of F_p^12
4949

50-
using detail::perform_fp12_mult;
51-
5250
template<typename ArithmetizationType, typename BlueprintFieldType>
5351
class fp12_multiplication;
5452

@@ -163,8 +161,6 @@ namespace nil {
163161

164162
std::array<value_type,12> a;
165163
std::array<value_type,12> b;
166-
std::array<value_type,12> c;
167-
168164

169165
for(std::size_t i = 0; i < 12; i++) {
170166
a[i] = var_value(assignment, instance_input.a[i]);
@@ -173,10 +169,15 @@ namespace nil {
173169
assignment.witness(component.W((12 + i) % WA),start_row_index + (12 + i)/WA) = b[i];
174170
}
175171

176-
c = perform_fp12_mult(a,b);
172+
using policy_type_fp12 = crypto3::algebra::fields::fp12_2over3over2<BlueprintFieldType>;
173+
using fp12_element = typename policy_type_fp12::value_type;
174+
175+
fp12_element A = fp12_element({ {a[0],a[1]}, {a[2],a[3]}, {a[4],a[5]} }, { {a[6],a[7]}, {a[8],a[9]}, {a[10],a[11]} }),
176+
B = fp12_element({ {b[0],b[1]}, {b[2],b[3]}, {b[4],b[5]} }, { {b[6],b[7]}, {b[8],b[9]}, {b[10],b[11]} }),
177+
C = A*B;
177178

178179
for(std::size_t i = 0; i < 12; i++) {
179-
assignment.witness(component.W((24 + i) % WA),start_row_index + (24 + i)/WA) = c[i];
180+
assignment.witness(component.W((24 + i) % WA),start_row_index + (24 + i)/WA) = C.data[i/6].data[(i % 6)/2].data[i % 2];
180181
}
181182

182183
return typename plonk_fp12_multiplication<BlueprintFieldType, ArithmetizationParams>::result_type(
@@ -195,17 +196,19 @@ namespace nil {
195196
using var = typename plonk_fp12_multiplication<BlueprintFieldType, ArithmetizationParams>::var;
196197
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;
197198

199+
using fp12_constraint = detail::abstract_fp12_element<constraint_type>;
200+
198201
const std::size_t WA = component.witness_amount();
199202
const int shift = -(WA < 24); // if WA is small we use 3 rows, and need to shift everything
200203

201-
std::array<constraint_type,12> A, B, C;
204+
fp12_constraint A, B, C;
202205

203206
for(std::size_t i = 0; i < 12; i++) {
204207
A[i] = var(component.W(i), 0 + shift, true);
205208
B[i] = var(component.W((i+12) % WA), (i+12)/WA + shift, true);
206209
}
210+
C = A * B;
207211

208-
C = perform_fp12_mult(A,B);
209212
std::vector<constraint_type> Cs = {};
210213
for(std::size_t i = 0; i < 12; i++) {
211214
Cs.push_back(C[i] - var(component.W((i+24) % WA), (i+24)/WA + shift, true));

include/nil/blueprint/components/algebra/fields/plonk/non_native/fp12_small_power.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#include <nil/blueprint/component.hpp>
4343
#include <nil/blueprint/manifest.hpp>
4444

45-
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/perform_fp12_mult.hpp>
45+
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/abstract_fp12.hpp>
4646

4747
namespace nil {
4848
namespace blueprint {
@@ -201,26 +201,28 @@ namespace nil {
201201
using var = typename plonk_fp12_small_power<BlueprintFieldType, ArithmetizationParams, Power>::var;
202202
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;
203203

204+
using fp12_constraint = detail::abstract_fp12_element<constraint_type>;
205+
204206
const std::size_t WA = component.witness_amount();
205207

206-
std::array<constraint_type,12> X, Y, C;
208+
fp12_constraint X, Y, C;
207209

208210
for(std::size_t i = 0; i < 12; i++) {
209211
X[i] = var(component.W(i), 0, true);
210212
Y[i] = var(component.W((i+12) % WA), (i+12)/WA, true);
211213
}
212214

213-
C = perform_fp12_mult(X,X); // 2
215+
C = X * X;
214216
switch(Power) {
215217
case square: {
216218
break;
217219
}
218220
case cube: {
219-
C = perform_fp12_mult(C,X); // 3
221+
C = C * X; // 3
220222
break;
221223
}
222224
case power4: {
223-
C = perform_fp12_mult(C,C); // 4
225+
C = C * C; // 4
224226
break;
225227
}
226228
}

include/nil/blueprint/components/algebra/fields/plonk/non_native/fp12_power_t.hpp renamed to include/nil/blueprint/components/algebra/pairing/weierstrass/plonk/detail/fp12_power_t.hpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <nil/blueprint/component.hpp>
4242
#include <nil/blueprint/manifest.hpp>
4343

44-
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/perform_fp12_mult.hpp>
44+
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/abstract_fp12.hpp>
4545

4646
namespace nil {
4747
namespace blueprint {
@@ -60,7 +60,6 @@ namespace nil {
6060
// In the 24-column version we compute two exponents per row,
6161
// writing the value 53760 twice for better alignment of gates.
6262
//
63-
using detail::perform_fp12_mult;
6463

6564
template<typename ArithmetizationType, typename BlueprintFieldType>
6665
class fp12_power_t;
@@ -250,17 +249,19 @@ namespace nil {
250249
using var = typename plonk_fp12_power_t<BlueprintFieldType, ArithmetizationParams>::var;
251250
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;
252251

252+
using fp12_constraint = detail::abstract_fp12_element<constraint_type>;
253+
253254
const std::size_t WA = component.witness_amount();
254255
std::vector<std::size_t> gate_list = {}; // 5 gate ids (if WA==12, the last two are the same)
255256

256-
std::array<constraint_type,12> X, Y, Z, C;
257+
fp12_constraint X, Y, Z, C;
257258

258259
// squaring gate
259260
for(std::size_t i = 0; i < 12; i++) {
260261
X[i] = var(component.W(i), -(WA == 12), true);
261262
Y[i] = var(component.W((i+12) % WA), 0, true);
262263
}
263-
C = perform_fp12_mult(X,X);
264+
C = X * X;
264265

265266
std::vector<constraint_type> square_constrs = {};
266267
for(std::size_t i = 0; i < 12; i++) {
@@ -273,8 +274,7 @@ namespace nil {
273274
X[i] = var(component.W(i), -(WA == 12), true);
274275
Y[i] = var(component.W((i+12) % WA), 0, true);
275276
}
276-
C = perform_fp12_mult(X,X);
277-
C = perform_fp12_mult(C,X);
277+
C = X * X * X;
278278

279279
std::vector<constraint_type> cube_constrs = {};
280280
for(std::size_t i = 0; i < 12; i++) {
@@ -288,7 +288,7 @@ namespace nil {
288288
Y[i] = var(component.W((i+12) % WA), 0, true);
289289
Z[i] = var(component.W(i), 1, true);
290290
}
291-
C = perform_fp12_mult(X,Y);
291+
C = X * Y;
292292

293293
std::vector<constraint_type> mult_constrs = {};
294294
for(std::size_t i = 0; i < 12; i++) {
@@ -301,8 +301,7 @@ namespace nil {
301301
X[i] = var(component.W(i), -(WA == 12), true);
302302
Y[i] = var(component.W((i+12) % WA), 0, true);
303303
}
304-
C = perform_fp12_mult(X,X);
305-
C = perform_fp12_mult(C,C);
304+
C = (X * X) * (X * X);
306305

307306
std::vector<constraint_type> pow4_1_constrs = {};
308307
for(std::size_t i = 0; i < 12; i++) {
@@ -315,8 +314,7 @@ namespace nil {
315314
X[i] = var(component.W((i+12) % WA), -1, true);
316315
Y[i] = var(component.W(i), 0, true);
317316
}
318-
C = perform_fp12_mult(X,X);
319-
C = perform_fp12_mult(C,C);
317+
C = (X * X) * (X * X);
320318

321319
std::vector<constraint_type> pow4_2_constrs = {};
322320
for(std::size_t i = 0; i < 12; i++) {

test/algebra/fields/plonk/non_native/fp12_arithmetic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#include <nil/blueprint/components/algebra/fields/plonk/non_native/fp12_inversion.hpp>
4444
#include <nil/blueprint/components/algebra/fields/plonk/non_native/fp12_small_power.hpp>
4545
#include <nil/blueprint/components/algebra/fields/plonk/non_native/fp12_frobenius_map.hpp>
46-
#include <nil/blueprint/components/algebra/fields/plonk/non_native/fp12_power_t.hpp>
46+
#include <nil/blueprint/components/algebra/pairing/weierstrass/plonk/detail/fp12_power_t.hpp>
4747

4848
#include "../../../../test_plonk_component.hpp"
4949

0 commit comments

Comments
 (0)