Skip to content

Commit

Permalink
Updated bls for new interfraces.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iluvmagick committed Mar 11, 2024
1 parent 6d28737 commit 7a44b6a
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 332 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ jobs:
blueprint_verifiers_placeholder_f1_loop_test,
blueprint_verifiers_placeholder_f3_loop_test,
blueprint_verifiers_placeholder_gate_component_test,
blueprint_algebra_curves_plonk_bls12_g2_test,
blueprint_algebra_fields_plonk_non_native_fp12_arithmetic_test,
blueprint_proxy_test
blueprint_mock_mocked_components_test
] # Tests to execute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_BLS12_G2_POINT_ADDITION_HPP
#define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_BLS12_G2_POINT_ADDITION_HPP

#include <functional>

#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>

#include <nil/blueprint/blueprint/plonk/assignment.hpp>
Expand Down Expand Up @@ -58,16 +60,15 @@ namespace nil {
// +--+--+--+--+--+--+---+---+--+--+--+--+
//

template<typename ArithmetizationType, typename BlueprintFieldType>
template<typename ArithmetizationType>
class bls12_g2_point_addition;

template<typename BlueprintFieldType, typename ArithmetizationParams>
class bls12_g2_point_addition<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
BlueprintFieldType>
: public plonk_component<BlueprintFieldType, ArithmetizationParams, 0, 0> {
template<typename BlueprintFieldType>
class bls12_g2_point_addition<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
: public plonk_component<BlueprintFieldType> {

public:
using component_type = plonk_component<BlueprintFieldType, ArithmetizationParams, 0, 0>;
using component_type = plonk_component<BlueprintFieldType>;

using var = typename component_type::var;
using manifest_type = plonk_component_manifest;
Expand Down Expand Up @@ -104,8 +105,8 @@ namespace nil {
struct input_type {
std::array<var,4> P, Q;

std::vector<var> all_vars() const {
std::vector<var> res = {};
std::vector<std::reference_wrapper<var>> all_vars() {
std::vector<std::reference_wrapper<var>> res = {};
for(auto & e : P) { res.push_back(e); }
for(auto & e : Q) { res.push_back(e); }
return res;
Expand Down Expand Up @@ -150,23 +151,20 @@ namespace nil {
component_type(witnesses, constants, public_inputs, get_manifest()) {};
};

template<typename BlueprintFieldType, typename ArithmetizationParams>
template<typename BlueprintFieldType>
using plonk_bls12_g2_point_addition =
bls12_g2_point_addition<
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
BlueprintFieldType>;
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>;

template<typename BlueprintFieldType, typename ArithmetizationParams>
typename plonk_bls12_g2_point_addition<BlueprintFieldType, ArithmetizationParams>::result_type generate_assignments(
const plonk_bls12_g2_point_addition<BlueprintFieldType, ArithmetizationParams> &component,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>
template<typename BlueprintFieldType>
typename plonk_bls12_g2_point_addition<BlueprintFieldType>::result_type generate_assignments(
const plonk_bls12_g2_point_addition<BlueprintFieldType> &component,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
&assignment,
const typename plonk_bls12_g2_point_addition<BlueprintFieldType, ArithmetizationParams>::input_type
const typename plonk_bls12_g2_point_addition<BlueprintFieldType>::input_type
&instance_input,
const std::uint32_t start_row_index) {

using value_type = typename BlueprintFieldType::value_type;

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

using policy_type_fp2 = crypto3::algebra::fields::fp2<BlueprintFieldType>;
Expand Down Expand Up @@ -222,22 +220,22 @@ namespace nil {
assignment.witness(component.W((22 + i) % WA),start_row_index + (WA < 24)) = yR.data[i];
}

return typename plonk_bls12_g2_point_addition<BlueprintFieldType, ArithmetizationParams>::result_type(
return typename plonk_bls12_g2_point_addition<BlueprintFieldType>::result_type(
component, start_row_index);
}

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

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

using var = typename plonk_bls12_g2_point_addition<BlueprintFieldType, ArithmetizationParams>::var;
using var = typename plonk_bls12_g2_point_addition<BlueprintFieldType>::var;
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;

// Fp2 field over constraints:
Expand Down Expand Up @@ -325,31 +323,29 @@ namespace nil {
return bp.add_gate(Cs);
}

template<typename BlueprintFieldType, typename ArithmetizationParams>
template<typename BlueprintFieldType>
void generate_copy_constraints(
const plonk_bls12_g2_point_addition<BlueprintFieldType, ArithmetizationParams> &component,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>
const plonk_bls12_g2_point_addition<BlueprintFieldType> &component,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
&assignment,
const typename plonk_bls12_g2_point_addition<BlueprintFieldType, ArithmetizationParams>::input_type &instance_input,
const typename plonk_bls12_g2_point_addition<BlueprintFieldType>::input_type &instance_input,
const std::size_t start_row_index) {

using var = typename plonk_bls12_g2_point_addition<BlueprintFieldType, ArithmetizationParams>::var;

const std::size_t WA = component.witness_amount();
using var = typename plonk_bls12_g2_point_addition<BlueprintFieldType>::var;

for(std::size_t i = 0; i < 4; i++) {
bp.add_copy_constraint({var(component.W(i), start_row_index, false), instance_input.P[i]});
}
}

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

std::size_t selector_index = generate_gates(component, bp, assignment, instance_input);
Expand All @@ -358,7 +354,7 @@ namespace nil {

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

return typename plonk_bls12_g2_point_addition<BlueprintFieldType, ArithmetizationParams>::result_type(
return typename plonk_bls12_g2_point_addition<BlueprintFieldType>::result_type(
component, start_row_index);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,15 @@ namespace nil {
// Input: (xP, yP) = P[4]
// Output: (xR, yR) = R[4], R = [2]P as element of E'(F_p^2)

template<typename ArithmetizationType, typename BlueprintFieldType>
template<typename ArithmetizationType>
class bls12_g2_point_double;

template<typename BlueprintFieldType, typename ArithmetizationParams>
class bls12_g2_point_double<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
BlueprintFieldType>
: public plonk_component<BlueprintFieldType, ArithmetizationParams, 0, 0> {
template<typename BlueprintFieldType>
class bls12_g2_point_double<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
: public plonk_component<BlueprintFieldType> {

public:
using component_type = plonk_component<BlueprintFieldType, ArithmetizationParams, 0, 0>;
using component_type = plonk_component<BlueprintFieldType>;

using var = typename component_type::var;
using manifest_type = plonk_component_manifest;
Expand Down Expand Up @@ -95,8 +94,8 @@ namespace nil {
struct input_type {
std::array<var,4> P;

std::vector<var> all_vars() const {
std::vector<var> res = {};
std::vector<std::reference_wrapper<var>> all_vars() {
std::vector<std::reference_wrapper<var>> res = {};
for(auto & e : P) { res.push_back(e); }
return res;
}
Expand Down Expand Up @@ -138,23 +137,19 @@ namespace nil {
component_type(witnesses, constants, public_inputs, get_manifest()) {};
};

template<typename BlueprintFieldType, typename ArithmetizationParams>
template<typename BlueprintFieldType>
using plonk_bls12_g2_point_double =
bls12_g2_point_double<
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
BlueprintFieldType>;

template<typename BlueprintFieldType, typename ArithmetizationParams>
typename plonk_bls12_g2_point_double<BlueprintFieldType, ArithmetizationParams>::result_type generate_assignments(
const plonk_bls12_g2_point_double<BlueprintFieldType, ArithmetizationParams> &component,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>
bls12_g2_point_double<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>;

template<typename BlueprintFieldType>
typename plonk_bls12_g2_point_double<BlueprintFieldType>::result_type generate_assignments(
const plonk_bls12_g2_point_double<BlueprintFieldType> &component,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
&assignment,
const typename plonk_bls12_g2_point_double<BlueprintFieldType, ArithmetizationParams>::input_type
const typename plonk_bls12_g2_point_double<BlueprintFieldType>::input_type
&instance_input,
const std::uint32_t start_row_index) {

using value_type = typename BlueprintFieldType::value_type;

using policy_type_fp2 = crypto3::algebra::fields::fp2<BlueprintFieldType>;
using fp2_element = typename policy_type_fp2::value_type;

Expand All @@ -176,20 +171,20 @@ namespace nil {
assignment.witness(component.W(8 + i),start_row_index) = yR.data[i];
}

return typename plonk_bls12_g2_point_double<BlueprintFieldType, ArithmetizationParams>::result_type(
return typename plonk_bls12_g2_point_double<BlueprintFieldType>::result_type(
component, start_row_index);
}

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

using var = typename plonk_bls12_g2_point_double<BlueprintFieldType, ArithmetizationParams>::var;
using var = typename plonk_bls12_g2_point_double<BlueprintFieldType>::var;
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;

using fp2_constraint = detail::abstract_fp2_element<constraint_type>;
Expand Down Expand Up @@ -233,31 +228,29 @@ namespace nil {
return bp.add_gate(Cs);
}

template<typename BlueprintFieldType, typename ArithmetizationParams>
template<typename BlueprintFieldType>
void generate_copy_constraints(
const plonk_bls12_g2_point_double<BlueprintFieldType, ArithmetizationParams> &component,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>
const plonk_bls12_g2_point_double<BlueprintFieldType> &component,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
&assignment,
const typename plonk_bls12_g2_point_double<BlueprintFieldType, ArithmetizationParams>::input_type &instance_input,
const typename plonk_bls12_g2_point_double<BlueprintFieldType>::input_type &instance_input,
const std::size_t start_row_index) {

using var = typename plonk_bls12_g2_point_double<BlueprintFieldType, ArithmetizationParams>::var;

const std::size_t WA = component.witness_amount();
using var = typename plonk_bls12_g2_point_double<BlueprintFieldType>::var;

for(std::size_t i = 0; i < 4; i++) {
bp.add_copy_constraint({var(component.W(i), start_row_index, false), instance_input.P[i]});
}
}

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

std::size_t selector_index = generate_gates(component, bp, assignment, instance_input);
Expand All @@ -266,7 +259,7 @@ namespace nil {

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

return typename plonk_bls12_g2_point_double<BlueprintFieldType, ArithmetizationParams>::result_type(
return typename plonk_bls12_g2_point_double<BlueprintFieldType>::result_type(
component, start_row_index);
}

Expand Down
Loading

0 comments on commit 7a44b6a

Please sign in to comment.