Skip to content

Commit

Permalink
work in progress #355
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Apr 5, 2024
1 parent 27bcbe1 commit a32574c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP2_HPP
#define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP2_HPP

#include <array>

namespace nil {
namespace blueprint {
namespace components {
Expand All @@ -47,8 +49,8 @@ namespace nil {


constexpr abstract_fp2_element operator*(const abstract_fp2_element& other) {
return { data[0] * other.data[0] + UnderlyingFieldType::non_residue * data[1] * other.data[1],
data[0] * other.data[1] + data[1] * other.data[0]};
return { data[0] * other[0] + UnderlyingFieldType::non_residue * data[1] * other[1],
data[0] * other[1] + data[1] * other[0]};
}
constexpr abstract_fp2_element operator*(const int x) {
return { data[0]*x, data[1]*x };
Expand All @@ -57,10 +59,10 @@ namespace nil {
return { e[0]*x, e[1]*x };
}
constexpr abstract_fp2_element operator+(const abstract_fp2_element& other) {
return { data[0] + other.data[0], data[1] + other.data[1] };
return { data[0] + other[0], data[1] + other[1] };
}
constexpr abstract_fp2_element operator-(const abstract_fp2_element& other) {
return { data[0] - other.data[0], data[1] - other.data[1] };
return { data[0] - other[0], data[1] - other[1] };
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP3_HPP
#define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_ABSTRACT_FP3_HPP

#include <array>

namespace nil {
namespace blueprint {
namespace components {
Expand All @@ -45,10 +47,11 @@ namespace nil {
}

constexpr abstract_fp3_element operator*(abstract_fp3_element const& other) {
auto s = UnderlyingFieldType::non_residue;
return {
data[0] * other.data[0] + UnderlyingFieldType::non_residue * data[1] * other.data[2] + UnderlyingFieldType::non_residue * data[2] * other.data[1],
data[0] * other.data[1] + data[1] * other.data[0] + UnderlyingFieldType::non_residue * data[2] * other.data[2],
data[0] * other.data[2] + data[1] * other.data[1] + data[2] * other.data[0]
data[0]*other[0] + s*(data[1]*other[2] + data[2]*other[1]),
data[0]*other[1] + data[1]*other[0] + s*data[2]*other[2],
data[0]*other[2] + data[1]*other[1] + data[2]*other[0]
};
}

Expand All @@ -59,10 +62,10 @@ namespace nil {
return { e[0]*x, e[1]*x, e[2]*x };
}
constexpr abstract_fp3_element operator+(abstract_fp3_element const& other) {
return { data[0] + other.data[0], data[1] + other.data[1], data[2] + other.data[2] };
return { data[0] + other[0], data[1] + other[1], data[2] + other[2] };
}
constexpr abstract_fp3_element operator-(abstract_fp3_element const& other) {
return { data[0] - other.data[0], data[1] - other.data[1], data[2] - other.data[2] };
return { data[0] - other[0], data[1] - other[1], data[2] - other[2] };
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,8 @@
#include <nil/blueprint/component.hpp>
#include <nil/blueprint/manifest.hpp>

#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/abstract_fp4.hpp>
/*
#include <nil/blueprint/components/algebra/fields/plonk/non_native/fp12_fixed_power.hpp>
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/fp12_frobenius_coefs.hpp>
*/

/*
#include <nil/blueprint/components/algebra/pairing/weierstrass/plonk/detail/fp12_power_t.hpp>
#include <nil/blueprint/components/algebra/pairing/weierstrass/plonk/detail/fp12_power_tminus1sq_over3.hpp>
*/
#include <nil/blueprint/components/algebra/fields/plonk/non_native/detail/mnt4_fp4.hpp>

namespace nil {
namespace blueprint {
namespace components {
Expand Down Expand Up @@ -90,7 +82,7 @@ namespace nil {
static manifest_type manifest = manifest_type(
std::shared_ptr<manifest_param>(new manifest_single_value_param(12)),
false
).merge_with(fixed_power_type::get_manifest());
)/*.merge_with(fixed_power_type::get_manifest())*/;

return manifest;
}
Expand Down Expand Up @@ -118,14 +110,15 @@ namespace nil {
};

struct result_type {
std::array<var,12> output;
std::array<var, 4> output;

result_type(const bn_exponentiation &component, std::uint32_t start_row_index) {
result_type(const mnt4_exponentiation &component, std::uint32_t start_row_index) {
std::size_t last_row = start_row_index + component.rows_amount - 1;

for(std::size_t i = 0; i < 12; i++) {
/*
for(std::size_t i = 0; i < 4; i++) {
output[i] = var(component.W(i), last_row, false, var::column_type::witness);
}
*/
}

std::vector<std::reference_wrapper<var>> all_vars() {
Expand All @@ -137,16 +130,16 @@ namespace nil {
};

template<typename ContainerType>
explicit bn_exponentiation(ContainerType witness, unsigned long long T_) :
explicit mnt4_exponentiation(ContainerType witness, unsigned long long T_) :
component_type(witness, {}, {}, get_manifest()), T(T_) {};

template<typename WitnessContainerType, typename ConstantContainerType,
typename PublicInputContainerType>
bn_exponentiation(WitnessContainerType witness, ConstantContainerType constant,
mnt4_exponentiation(WitnessContainerType witness, ConstantContainerType constant,
PublicInputContainerType public_input, unsigned long long T_) :
component_type(witness, constant, public_input, get_manifest()), T(T_) {};

bn_exponentiation(
mnt4_exponentiation(
std::initializer_list<typename component_type::witness_container_type::value_type>
witnesses,
std::initializer_list<typename component_type::constant_container_type::value_type>
Expand All @@ -157,18 +150,18 @@ namespace nil {
};

template<typename BlueprintFieldType>
using plonk_bn_exponentiation =
bn_exponentiation<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>;
using plonk_mnt4_exponentiation =
mnt4_exponentiation<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>;

template<typename BlueprintFieldType>
typename plonk_bn_exponentiation<BlueprintFieldType>::result_type generate_assignments(
const plonk_bn_exponentiation<BlueprintFieldType> &component,
typename plonk_mnt4_exponentiation<BlueprintFieldType>::result_type generate_assignments(
const plonk_mnt4_exponentiation<BlueprintFieldType> &component,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
&assignment,
const typename plonk_bn_exponentiation<BlueprintFieldType>::input_type
const typename plonk_mnt4_exponentiation<BlueprintFieldType>::input_type
&instance_input,
const std::uint32_t start_row_index) {
using component_type = plonk_bn_exponentiation<BlueprintFieldType>;
using component_type = plonk_mnt4_exponentiation<BlueprintFieldType>;
using var = typename component_type::var;
using value_type = typename BlueprintFieldType::value_type;
using fixed_power_type = typename component_type::fixed_power_type;
Expand All @@ -178,47 +171,51 @@ namespace nil {

typename BlueprintFieldType::integral_type field_p = BlueprintFieldType::modulus;

std::array<value_type,12> x;
std::array<value_type, 4> x;

for(std::size_t i = 0; i < 12; i++) {
for(std::size_t i = 0; i < 4; i++) {
x[i] = var_value(assignment, instance_input.x[i]);
}

using policy_type_fp12 = crypto3::algebra::fields::fp12_2over3over2<BlueprintFieldType>;
using fp12_element = typename policy_type_fp12::value_type;
using policy_type_fp4 = crypto3::algebra::fields::fp4<BlueprintFieldType>;
using fp4_element = typename policy_type_fp4::value_type;

fp12_element X = fp12_element({ {x[0],x[1]}, {x[2],x[3]}, {x[4],x[5]} }, { {x[6],x[7]}, {x[8],x[9]}, {x[10],x[11]} }),
F, A, B, C, D;
fp4_element
elt = fp4_element({ {x[0],x[1]}, {x[2],x[3]}, });

std::size_t row = 0;
auto fill_row = [&assignment, &component, &start_row_index, &row](fp12_element V) {
for(std::size_t i = 0; i < 12; i++) {
assignment.witness(component.W(i),start_row_index + row) = V.data[i/6].data[(i % 6)/2].data[i % 2];
auto fill_row = [&assignment, &component, &start_row_index, &row](fp4_element V) {
for(std::size_t i = 0; i < 4; i++) {
assignment.witness(component.W(i),start_row_index + row) = V.data[i/2].data[i % 2];
}
row++;
};
auto row_vars = [&component, &start_row_index](std::size_t input_row) {
std::array<var,12> transfer_vars;
for(std::size_t i = 0; i < 12; i++) {
std::array<var, 4> transfer_vars;
for(std::size_t i = 0; i < 4; i++) {
transfer_vars[i] = var(component.W(i),start_row_index + input_row,false);
}
return transfer_vars;
};

auto use_power_t = [&assignment, &start_row_index, &row, &power_t_instance](std::array<var,12> transfer_vars) {
typename fixed_power_type::input_type block_input = {transfer_vars};
typename fixed_power_type::result_type block_res =
generate_assignments(power_t_instance, assignment, block_input, start_row_index + row);
row += power_t_instance.rows_amount;
return block_res.output;
};
auto vars_to_fp12 = [&assignment](std::array<var,12> o) {
std::array<value_type,12> v;
for(std::size_t i = 0; i < 12; i++) {

auto vars_to_fp4 = [&assignment](std::array<var, 4> o) {
std::array<value_type, 4> v;
for(std::size_t i = 0; i < 4; i++) {
v[i] = var_value(assignment, o[i]);
}
return fp12_element({ {v[0],v[1]}, {v[2],v[3]}, {v[4],v[5]} }, { {v[6],v[7]}, {v[8],v[9]}, {v[10],v[11]} });
return fp4_element({ {v[0],v[1]}, {v[2],v[3]}, });
};



fill_row(X.inversed()); // 0: x^{-1}
fill_row(X); // 1: x
fill_row(X.pow(field_p).pow(field_p).pow(field_p)); // 2: x^{p^3}
Expand Down Expand Up @@ -272,23 +269,22 @@ namespace nil {
fill_row(F.pow(4)); // 3R+46: f^4
D = D * F.pow(4); fill_row(D); // 3R+47: f^{p^3} c^{6t^2+1} b (f^{p+1})^9 a f^4

return typename plonk_bn_exponentiation<BlueprintFieldType>::result_type(
return typename plonk_mnt4_exponentiation<BlueprintFieldType>::result_type(
component, start_row_index);
}

template<typename BlueprintFieldType>
std::vector<std::size_t> generate_gates(
const plonk_bn_exponentiation<BlueprintFieldType> &component,
const plonk_mnt4_exponentiation<BlueprintFieldType> &component,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
&assignment,
const typename plonk_bn_exponentiation<BlueprintFieldType>::input_type
const typename plonk_mnt4_exponentiation<BlueprintFieldType>::input_type
&instance_input) {

using var = typename plonk_bn_exponentiation<BlueprintFieldType>::var;
using var = typename plonk_mnt4_exponentiation<BlueprintFieldType>::var;
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;
using policy_type_fp12 = crypto3::algebra::fields::fp12_2over3over2<BlueprintFieldType>;

using fp12_constraint = detail::abstract_fp12_element<constraint_type,BlueprintFieldType>;

std::vector<std::size_t> gate_list = {};
Expand Down Expand Up @@ -375,14 +371,14 @@ namespace nil {

template<typename BlueprintFieldType>
void generate_copy_constraints(
const plonk_bn_exponentiation<BlueprintFieldType> &component,
const plonk_mnt4_exponentiation<BlueprintFieldType> &component,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
&assignment,
const typename plonk_bn_exponentiation<BlueprintFieldType>::input_type &instance_input,
const typename plonk_mnt4_exponentiation<BlueprintFieldType>::input_type &instance_input,
const std::size_t start_row_index, std::size_t R) { // R = number of rows in external sub-circuit

using var = typename plonk_bn_exponentiation<BlueprintFieldType>::var;
using var = typename plonk_mnt4_exponentiation<BlueprintFieldType>::var;

// initial data in row 1
for(std::size_t i = 0; i < 12; i++) {
Expand All @@ -402,15 +398,15 @@ namespace nil {
}

template<typename BlueprintFieldType>
typename plonk_bn_exponentiation<BlueprintFieldType>::result_type generate_circuit(
const plonk_bn_exponentiation<BlueprintFieldType> &component,
typename plonk_mnt4_exponentiation<BlueprintFieldType>::result_type generate_circuit(
const plonk_mnt4_exponentiation<BlueprintFieldType> &component,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
&assignment,
const typename plonk_bn_exponentiation<BlueprintFieldType>::input_type &instance_input,
const typename plonk_mnt4_exponentiation<BlueprintFieldType>::input_type &instance_input,
const std::size_t start_row_index) {

using component_type = plonk_bn_exponentiation<BlueprintFieldType>;
using component_type = plonk_mnt4_exponentiation<BlueprintFieldType>;
using var = typename component_type::var;
using fixed_power_type = typename component_type::fixed_power_type;

Expand Down Expand Up @@ -475,11 +471,11 @@ namespace nil {

generate_copy_constraints(component, bp, assignment, instance_input, start_row_index, R);

return typename plonk_bn_exponentiation<BlueprintFieldType>::result_type(
return typename plonk_mnt4_exponentiation<BlueprintFieldType>::result_type(
component, start_row_index);
}
} // namespace components
} // namespace blueprint
} // namespace nil

#endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_BN_EXPONENTIATION_HPP
#endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_mnt4_exponentiation_HPP

0 comments on commit a32574c

Please sign in to comment.