Skip to content

Commit

Permalink
PLONK components updated. #16
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaskov committed Dec 24, 2021
1 parent 7e58df7 commit 895fa9f
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,51 @@
#ifndef CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_FIXED_BASE_SCALAR_MUL_COMPONENT_5_WIRES_HPP
#define CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_FIXED_BASE_SCALAR_MUL_COMPONENT_5_WIRES_HPP

#include <nil/crypto3/math/detail/field_utils.hpp>

#include <nil/marshalling/algorithms/unpack.hpp>

#include <nil/crypto3/zk/components/blueprint.hpp>
#include <nil/crypto3/zk/components/component.hpp>

namespace nil {
namespace crypto3 {
namespace zk {
namespace components {

template<typename TBlueprintField, typename CurveType,
template<typename CurveType,
typename CurveType::template g1_type<>::value_type B,
typename TArithmetization,
std::size_t W0 = 4, std::size_t W1 = 0, std::size_t W2 = 1, std::size_t W3 = 2,
std::size_t W4 = 3, CurveType::g1_type<>::value_type B>
class element_g1_fixed_base_scalar_mul_plonk : public component<TBlueprintField> {
typedef snark::plonk_constraint_system<TBlueprintField> arithmetization_type;

typedef blueprint<arithmetization_type, TBlueprintField> blueprint_type;
std::size_t W4 = 3>
class element_g1_fixed_base_scalar_mul;

template<typename TBlueprintField, std::size_t WiresAmount, typename CurveType,
typename CurveType::template g1_type<>::value_type B,
std::size_t W0, std::size_t W1, std::size_t W2, std::size_t W3,
std::size_t W4>
class element_g1_fixed_base_scalar_mul<
CurveType, B,
snark::plonk_constraint_system<TBlueprintField, WiresAmount>,
W0, W1, W2, W3, W4> :
public component<TBlueprintField> {
typedef snark::plonk_constraint_system<TBlueprintField, WiresAmount> arithmetization_type;

typedef blueprint<arithmetization_type> blueprint_type;

typename blueprint_type::row_index_type j;
public:

element_g1_fixed_base_scalar_mul_plonk(blueprint_type &bp) :
component<FieldType>(bp){
element_g1_fixed_base_scalar_mul(blueprint_type &bp) :
component<TBlueprintField>(bp){

j = bp.allocate_rows(85);
j = this->bp.allocate_rows(85);
}
private:
typename CurveType::g1_type<>::value_type omega(
typename CurveType::template g1_type<>::value_type omega(
std::size_t s, std::size_t i){

return (i * 8**s)*B;
return (i * math::detail::get_power_of_two(3*s))*B;
}

void generate_phi1_gate(
Expand All @@ -66,7 +83,7 @@ namespace nil {
typename blueprint_type::variable_type x_4,
std::array<typename CurveType::base_field_type, 7> u) {

bp.add_gate(row_index,
this->bp.add_gate(row_index,
x_3 * (-u[0] * x_2 * x_1 + u[0] * x_1 + u[0] * x_2
- u[0] + u[2] * x_1 * x_2 - u[2]* x_2 + u[4] * x_1 * x_2
- u[4]* x_2 -u[6] * x_1 * x_2 + u[1] * x_2 * x_1
Expand All @@ -85,7 +102,7 @@ namespace nil {
typename blueprint_type::variable_type x_4,
std::array<typename CurveType::base_field_type, 7> v) {

bp.add_gate(row_index,
this->bp.add_gate(row_index,
x_3 * (-v[0] * x_2 * x_1 + v[0] * x_1 + v[0] * x_2
- v[0] + v[2] * x_1 * x_2 -v[2] * x_2 + v[4] * x_1 * x_2
- v[4] * x_2 - v[6] * x_1 * x_2 + v[1] * x_2 * x_1
Expand All @@ -104,7 +121,7 @@ namespace nil {
typename blueprint_type::variable_type x_4,
typename blueprint_type::variable_type x_5,
typename blueprint_type::variable_type x_6) {
bp.add_gate(row_index, x_1 * (1 + CurveType::d * x_3*x_4*x_5*x_6) - (x_3*x_6 + x_4*x_5));
this->bp.add_gate(row_index, x_1 * (1 + CurveType::d * x_3*x_4*x_5*x_6) - (x_3*x_6 + x_4*x_5));
}

void generate_phi4_gate(
Expand All @@ -115,20 +132,20 @@ namespace nil {
typename blueprint_type::variable_type x_4,
typename blueprint_type::variable_type x_5,
typename blueprint_type::variable_type x_6) {
bp.add_gate(row_index, x_2 * (1 - CurveType::d * x_3*x_4*x_5*x_6) - (x_3*x_5 + x_4*x_6));
this->bp.add_gate(row_index, x_2 * (1 - CurveType::d * x_3*x_4*x_5*x_6) - (x_3*x_5 + x_4*x_6));
}
public:
void generate_gates() {

constexpr static const typename blueprint_type::variable_type w_o_jp2(W0,
constexpr static const typename blueprint_type::variable_type w_o_jm2(W0,
blueprint_type::variable_type::rotation_type::pre_previous);
constexpr static const typename blueprint_type::variable_type w_1_jp2(W1,
constexpr static const typename blueprint_type::variable_type w_1_jm2(W1,
blueprint_type::variable_type::rotation_type::pre_previous);
constexpr static const typename blueprint_type::variable_type w_2_jp2(W2,
constexpr static const typename blueprint_type::variable_type w_2_jm2(W2,
blueprint_type::variable_type::rotation_type::pre_previous);
constexpr static const typename blueprint_type::variable_type w_3_jp2(W3,
constexpr static const typename blueprint_type::variable_type w_3_jm2(W3,
blueprint_type::variable_type::rotation_type::pre_previous);
constexpr static const typename blueprint_type::variable_type w_4_jp2(W4,
constexpr static const typename blueprint_type::variable_type w_4_jm2(W4,
blueprint_type::variable_type::rotation_type::pre_previous);

constexpr static const typename blueprint_type::variable_type w_o_jm1(W0,
Expand Down Expand Up @@ -175,26 +192,26 @@ namespace nil {
constexpr static const typename blueprint_type::variable_type w_4_jp2(W4,
blueprint_type::variable_type::rotation_type::after_next);

bp.add_gate({j, j+2}, w_1_j * (w_1_j - 1));
bp.add_gate({j, j+2}, w_2_j * (w_2_j - 1));
bp.add_gate({j, j+1, j+3}, w_3_j * (w_3_j - 1));
bp.add_gate({j+2, j+3}, w_4_j * (w_4_j - 1));
this->bp.add_gate({j, j+2}, w_1_j * (w_1_j - 1));
this->bp.add_gate({j, j+2}, w_2_j * (w_2_j - 1));
this->bp.add_gate({j, j+1, j+3}, w_3_j * (w_3_j - 1));
this->bp.add_gate({j+2, j+3}, w_4_j * (w_4_j - 1));

// j=0
bp.add_gate(j, w_o_j - (w_1_j*4 + w_2_j*2 + w_3_j));
this->bp.add_gate(j, w_o_j - (w_1_j*4 + w_2_j*2 + w_3_j));

generate_phi3_gate(j, w_1_jp1, w_2_jp1, w_4_j, w_o_jp1, w_4_jp1, w_3_jp2);
generate_phi4_gate(j, w_1_jp1, w_2_jp1, w_4_j, w_o_jp1, w_4_jp1, w_3_jp2);

// j+z, z=0 mod 5, z!=0
for (typename blueprint_type::row_index_type z = 5, z <= 84; z+=5){
for (typename blueprint_type::row_index_type z = 5; z <= 84; z+=5){

bp.add_gate(j + z, w_o_j - (w_1_j*4 + w_2_j*2 + w_3_j + w_o_jm1 * 8));
this->bp.add_gate(j + z, w_o_j - (w_1_j*4 + w_2_j*2 + w_3_j + w_o_jm1 * 8));

std::array<typename CurveType::base_field_type::value_type, 7> u;
std::array<typename CurveType::base_field_type::value_type, 7> v;
for (std::size_t i=0; i<7; i++){
typename CurveType::g1_type<>::value_type omega = omega(3*z/5, i);
typename CurveType::template g1_type<>::value_type omega = omega(3*z/5, i);
u[i] = omega.X;
v[i] = omega.Y;
}
Expand All @@ -206,14 +223,14 @@ namespace nil {
}

// j+z, z=2 mod 5
for (typename blueprint_type::row_index_type z = 2, z <= 84; z+=5){
for (typename blueprint_type::row_index_type z = 2; z <= 84; z+=5){

bp.add_gate(j + z, w_o_j - (w_1_j*4 + w_2_j*2 + w_3_jm1 + w_o_jm2 * 8));
this->bp.add_gate(j + z, w_o_j - (w_1_j*4 + w_2_j*2 + w_3_jm1 + w_o_jm2 * 8));

std::array<typename CurveType::base_field_type::value_type, 7> u;
std::array<typename CurveType::base_field_type::value_type, 7> v;
for (std::size_t i=0; i<7; i++){
typename CurveType::g1_type<>::value_type omega = omega(3*(z-2)/5, i);
typename CurveType::template g1_type<>::value_type omega = omega(3*(z-2)/5, i);
u[i] = omega.X;
v[i] = omega.Y;
}
Expand All @@ -225,12 +242,12 @@ namespace nil {
}

// j+z, z=3 mod 5
for (typename blueprint_type::row_index_type z = 3, z <= 84; z+=5){
for (typename blueprint_type::row_index_type z = 3; z <= 84; z+=5){

std::array<typename CurveType::base_field_type::value_type, 7> u;
std::array<typename CurveType::base_field_type::value_type, 7> v;
for (std::size_t i=0; i<7; i++){
typename CurveType::g1_type<>::value_type omega = omega(3*(z-3)/5, i);
typename CurveType::template g1_type<>::value_type omega = omega(3*(z-3)/5, i);
u[i] = omega.X;
v[i] = omega.Y;
}
Expand All @@ -240,34 +257,34 @@ namespace nil {
}

// j+z, z=4 mod 5
for (typename blueprint_type::row_index_type z = 4, z <= 84; z+=5){
for (typename blueprint_type::row_index_type z = 4; z <= 84; z+=5){

bp.add_gate(j + z - 1, w_o_jp1 - (w_4_jm1*4 + w_3_jm2*2 + w_4_jm2 + w_o_jm1 * 8));
this->bp.add_gate(j + z - 1, w_o_jp1 - (w_4_jm1*4 + w_3_jm2*2 + w_4_jm2 + w_o_jm1 * 8));

generate_phi3_gate(j+z, w_1_jm2, w_2_j, w_1_jm1, w_2_jm1, w_4_jp1, w_o_jp2);
generate_phi4_gate(j+z, w_1_jm2, w_2_j, w_1_jm1, w_2_jm1, w_4_jp1, w_o_jp2);
}
}

void generate_assignments(typename CurveType::scalar_field_type::value_type &a,
typename CurveType::g1_type<>::value_type &P) {
typename CurveType::template g1_type<>::value_type &P) {

std::array<bool, 9> b = marshalling::unpack(a);

bp.val(W1, j) = b[0];
bp.val(W2, j) = b[1];
bp.val(W3, j) = b[2];
this->bp.val(W1, j) = b[0];
this->bp.val(W2, j) = b[1];
this->bp.val(W3, j) = b[2];

bp.val(W1, j+1) = P.X;
bp.val(W2, j+1) = P.Y;
bp.val(W3, j+1) = b[3];
this->bp.val(W1, j+1) = P.X;
this->bp.val(W2, j+1) = P.Y;
this->bp.val(W3, j+1) = b[3];

bp.val(W1, j+2) = b[4];
bp.val(W2, j+2) = b[5];
bp.val(W4, j+2) = b[6];
this->bp.val(W1, j+2) = b[4];
this->bp.val(W2, j+2) = b[5];
this->bp.val(W4, j+2) = b[6];

bp.val(W3, j+3) = b[7];
bp.val(W4, j+3) = b[8];
this->bp.val(W3, j+3) = b[7];
this->bp.val(W4, j+3) = b[8];
}
};

Expand Down
Loading

0 comments on commit 895fa9f

Please sign in to comment.