Skip to content

Commit

Permalink
code cleaning and file placement
Browse files Browse the repository at this point in the history
  • Loading branch information
ayashunsky authored and Iluvmagick committed Feb 19, 2024
1 parent cb97623 commit 3097c92
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//
// @file Declaration of template function for F_p^{12} field multiplication.
// @file Declaration of F_p^{12} elements over ab abstract entity (to be used with constraints).
// We use towered field extension
// F_p^12 = F_p^6[w]/(w^2 - v),
// F_p^6 = F_p^2[v]/(v^3-(u+1)),
// F_p^2 = F_p[u]/(u^2 - (-1)).
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_PERFORM_FP12_MULT_HPP
#define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_PERFORM_FP12_MULT_HPP
#ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP12_HPP
#define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP12_HPP

namespace nil {
namespace blueprint {
Expand All @@ -42,7 +42,7 @@ namespace nil {
std::array<T,12> c;

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

for(std::size_t i = 0; i < 12; i++) {
Expand Down Expand Up @@ -79,9 +79,54 @@ namespace nil {
}
return c;
}
template<typename T>
class abstract_fp12_element {
public:
std::array<T,12> data;

T& operator[](std::size_t idx) {
return data[idx];
}
const T& operator[](std::size_t idx) const {
return data[idx];
}

constexpr abstract_fp12_element operator*(const abstract_fp12_element& other) {
std::array<T,12> res = perform_fp12_mult(data,other.data);
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] };
}
constexpr abstract_fp12_element operator*(const int x) {
std::array<T,12> res;
for(std::size_t i = 0; i < 12; i++) {
res[i] = data[i] * x;
}
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] };
}
friend abstract_fp12_element operator*(const int x, const abstract_fp12_element& e) {
std::array<T,12> res;
for(std::size_t i = 0; i < 12; i++) {
res[i] = e[i] * x;
}
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] };
}
constexpr abstract_fp12_element operator+(const abstract_fp12_element& other) {
std::array<T,12> res;
for(std::size_t i = 0; i < 12; i++) {
res[i] = data[i] + other.data[i];
}
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] };
}
constexpr abstract_fp12_element operator-(const abstract_fp12_element& other) {
std::array<T,12> res;
for(std::size_t i = 0; i < 12; i++) {
res[i] = data[i] - other.data[i];
}
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] };
}
};
} // namespace detail
} // namespace components
} // namespace blueprint
} // namespace nil

#endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_PERFORM_FP12_MULT_HPP
#endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP12_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
#include <nil/blueprint/component.hpp>
#include <nil/blueprint/manifest.hpp>

// #include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/perform_fp12_mult.hpp>

namespace nil {
namespace blueprint {
namespace components {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <nil/blueprint/component.hpp>
#include <nil/blueprint/manifest.hpp>

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

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

using detail::perform_fp12_mult;

template<typename ArithmetizationType, typename BlueprintFieldType>
class fp12_inversion;

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

using fp12_constraint = detail::abstract_fp12_element<constraint_type>;

const std::size_t WA = component.witness_amount();

std::array<constraint_type,12> X, Y, C;
fp12_constraint X, Y, C;

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

C = perform_fp12_mult(X,Y);
std::vector<constraint_type> Cs = { C[0] - 1 };
for(std::size_t i = 1; i < 12; i++) {
Cs.push_back(C[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include <nil/blueprint/component.hpp>
#include <nil/blueprint/manifest.hpp>

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

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

using detail::perform_fp12_mult;

template<typename ArithmetizationType, typename BlueprintFieldType>
class fp12_multiplication;

Expand Down Expand Up @@ -163,8 +161,6 @@ namespace nil {

std::array<value_type,12> a;
std::array<value_type,12> b;
std::array<value_type,12> c;


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

c = perform_fp12_mult(a,b);
using policy_type_fp12 = crypto3::algebra::fields::fp12_2over3over2<BlueprintFieldType>;
using fp12_element = typename policy_type_fp12::value_type;

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]} }),
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]} }),
C = A*B;

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

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

using fp12_constraint = detail::abstract_fp12_element<constraint_type>;

const std::size_t WA = component.witness_amount();
const int shift = -(WA < 24); // if WA is small we use 3 rows, and need to shift everything

std::array<constraint_type,12> A, B, C;
fp12_constraint A, B, C;

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

C = perform_fp12_mult(A,B);
std::vector<constraint_type> Cs = {};
for(std::size_t i = 0; i < 12; i++) {
Cs.push_back(C[i] - var(component.W((i+24) % WA), (i+24)/WA + shift, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include <nil/blueprint/component.hpp>
#include <nil/blueprint/manifest.hpp>

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

namespace nil {
namespace blueprint {
Expand Down Expand Up @@ -201,26 +201,28 @@ namespace nil {
using var = typename plonk_fp12_small_power<BlueprintFieldType, ArithmetizationParams, Power>::var;
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;

using fp12_constraint = detail::abstract_fp12_element<constraint_type>;

const std::size_t WA = component.witness_amount();

std::array<constraint_type,12> X, Y, C;
fp12_constraint X, Y, C;

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

C = perform_fp12_mult(X,X); // 2
C = X * X;
switch(Power) {
case square: {
break;
}
case cube: {
C = perform_fp12_mult(C,X); // 3
C = C * X; // 3
break;
}
case power4: {
C = perform_fp12_mult(C,C); // 4
C = C * C; // 4
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <nil/blueprint/component.hpp>
#include <nil/blueprint/manifest.hpp>

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

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

template<typename ArithmetizationType, typename BlueprintFieldType>
class fp12_power_t;
Expand Down Expand Up @@ -250,17 +249,19 @@ namespace nil {
using var = typename plonk_fp12_power_t<BlueprintFieldType, ArithmetizationParams>::var;
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;

using fp12_constraint = detail::abstract_fp12_element<constraint_type>;

const std::size_t WA = component.witness_amount();
std::vector<std::size_t> gate_list = {}; // 5 gate ids (if WA==12, the last two are the same)

std::array<constraint_type,12> X, Y, Z, C;
fp12_constraint X, Y, Z, C;

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

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

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

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

std::vector<constraint_type> pow4_1_constrs = {};
for(std::size_t i = 0; i < 12; i++) {
Expand All @@ -315,8 +314,7 @@ namespace nil {
X[i] = var(component.W((i+12) % WA), -1, true);
Y[i] = var(component.W(i), 0, true);
}
C = perform_fp12_mult(X,X);
C = perform_fp12_mult(C,C);
C = (X * X) * (X * X);

std::vector<constraint_type> pow4_2_constrs = {};
for(std::size_t i = 0; i < 12; i++) {
Expand Down
2 changes: 1 addition & 1 deletion test/algebra/fields/plonk/non_native/fp12_arithmetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <nil/blueprint/components/algebra/fields/plonk/non_native/fp12_inversion.hpp>
#include <nil/blueprint/components/algebra/fields/plonk/non_native/fp12_small_power.hpp>
#include <nil/blueprint/components/algebra/fields/plonk/non_native/fp12_frobenius_map.hpp>
#include <nil/blueprint/components/algebra/fields/plonk/non_native/fp12_power_t.hpp>
#include <nil/blueprint/components/algebra/pairing/weierstrass/plonk/detail/fp12_power_t.hpp>

#include "../../../../test_plonk_component.hpp"

Expand Down

0 comments on commit 3097c92

Please sign in to comment.