Skip to content

Commit

Permalink
Added component manifest. Changed component interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iluvmagick authored and nkaskov committed Sep 11, 2023
1 parent 646c326 commit 1e9ceab
Show file tree
Hide file tree
Showing 89 changed files with 5,829 additions and 3,857 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
blueprint_algebra_fields_plonk_exponentiation_test,
blueprint_algebra_curves_plonk_unified_addition_test,
blueprint_algebra_curves_plonk_variable_base_scalar_mul_test,
blueprint_algebra_curves_plonk_decomposed_variable_base_scalar_mul_test,
blueprint_verifiers_kimchi_sponge_oracles_test,
blueprint_hashes_plonk_poseidon_test,
blueprint_algebra_curves_plonk_endo_scalar_test,
Expand Down Expand Up @@ -53,6 +52,7 @@ jobs:
blueprint_hashes_plonk_sha256_test,
blueprint_hashes_plonk_sha512_test,
blueprint_algebra_fields_plonk_sqrt_test,
blueprint_manifest_test,
] # Tests to execute
steps:
- uses: cachix/install-nix-action@v23
Expand Down
5 changes: 4 additions & 1 deletion include/nil/blueprint/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ namespace nil {
} // namespace blueprint
} // namespace nil

#define BLUEPRINT_RELEASE_ASSERT( expr ) \
( (expr) ? (void)0 : nil::blueprint::detail::blueprint_assert( __LINE__, __FILE__, #expr))

#ifdef BLUEPRINT_DEBUG_ENABLED
#define BLUEPRINT_ASSERT( expr ) \
( (expr) ? (void)0 : nil::blueprint::detail::blueprint_assert( __LINE__, __FILE__, #expr))
BLUEPRINT_RELEASE_ASSERT( expr )
#else
#define BLUEPRINT_ASSERT( expr ) ((void)0)
#endif
Expand Down
23 changes: 14 additions & 9 deletions include/nil/blueprint/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
#ifndef CRYPTO3_BLUEPRINT_COMPONENT_HPP
#define CRYPTO3_BLUEPRINT_COMPONENT_HPP

#include <string>

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

#include <nil/blueprint/detail/get_component_id.hpp>
#include <nil/blueprint/manifest.hpp>
#include <nil/blueprint/assert.hpp>

namespace nil {
namespace blueprint {
Expand All @@ -42,16 +45,16 @@ namespace nil {
template<typename ArithmetizationType>
class component{};

template<typename BlueprintFieldType, typename ArithmetizationParams, std::uint32_t WitnessAmount,
std::uint32_t ConstantAmount, std::uint32_t PublicInputAmount>
template<typename BlueprintFieldType, typename ArithmetizationParams,
std::uint32_t ConstantAmount, std::uint32_t PublicInputAmount>
class plonk_component:
public component<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> {
protected:

using witness_container_type = std::array<std::uint32_t, WitnessAmount>;
using witness_container_type = std::vector<std::uint32_t>;
using constant_container_type = std::array<std::uint32_t, ConstantAmount>;
using public_input_container_type = std::array<std::uint32_t, PublicInputAmount>;

using manifest_type = nil::blueprint::plonk_component_manifest;
public:

witness_container_type _W;
Expand All @@ -67,7 +70,7 @@ namespace nil {
* @param[in] internal witness signed index. For -1, last witness assumed.
*/
typename witness_container_type::value_type W(std::int32_t index) const {
return _W[(WitnessAmount + index)%WitnessAmount];
return _W[(_W.size() + index) % _W.size()];
}

/**
Expand Down Expand Up @@ -104,10 +107,13 @@ namespace nil {
template <typename WitnessContainerType, typename ConstantContainerType,
typename PublicInputContainerType>
plonk_component(WitnessContainerType witness, ConstantContainerType constant,
PublicInputContainerType public_input) {
std::copy_n(std::make_move_iterator(witness.begin()), WitnessAmount, _W.begin());
PublicInputContainerType public_input, const manifest_type &manifest) {
_W.resize(witness.size());
std::copy_n(std::make_move_iterator(witness.begin()), witness.size(), _W.begin());
std::copy_n(std::make_move_iterator(constant.begin()), ConstantAmount, _C.begin());
std::copy_n(std::make_move_iterator(public_input.begin()), PublicInputAmount, _PI.begin());

BLUEPRINT_RELEASE_ASSERT(manifest.check_manifest(*this));
}

std::size_t witness_amount() const {
Expand Down Expand Up @@ -160,7 +166,6 @@ namespace nil {

virtual detail::blueprint_component_id_type get_id() const { return std::string(""); };
};

} // namespace components
} // namespace blueprint
} // namespace nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,57 @@
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
#include <nil/blueprint/component.hpp>
#include <nil/blueprint/manifest.hpp>
#include <nil/blueprint/basic_non_native_policy.hpp>

namespace nil {
namespace blueprint {
namespace components {

template<typename ArithmetizationType, typename Ed25519Type, std::uint32_t WitnessesAmount,
typename NonNativePolicyType>
template<typename ArithmetizationType, typename Ed25519Type, typename NonNativePolicyType>
class bool_scalar_multiplication;

template<typename BlueprintFieldType, typename ArithmetizationParams>
class bool_scalar_multiplication<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
typename crypto3::algebra::curves::ed25519, 9, basic_non_native_policy<BlueprintFieldType>>:
public plonk_component<BlueprintFieldType, ArithmetizationParams, 9, 0, 0> {
typename crypto3::algebra::curves::ed25519, basic_non_native_policy<BlueprintFieldType>>:
public plonk_component<BlueprintFieldType, ArithmetizationParams, 0, 0> {

constexpr static const std::uint32_t WitnessesAmount = 9;

using component_type = plonk_component<BlueprintFieldType, ArithmetizationParams, WitnessesAmount, 0, 0>;
using component_type = plonk_component<BlueprintFieldType, ArithmetizationParams, 0, 0>;
using operating_field_type = typename crypto3::algebra::fields::curve25519_base_field;
using non_native_policy_type = basic_non_native_policy<BlueprintFieldType>;

public:

using var = typename component_type::var;
using manifest_type = nil::blueprint::plonk_component_manifest;

class gate_manifest_type : public component_gate_manifest {
public:
std::uint32_t gates_amount() const override {
return bool_scalar_multiplication::gates_amount;
}
};

constexpr static const std::size_t rows_amount = 2;
static gate_manifest get_gate_manifest(std::size_t witness_amount,
std::size_t lookup_column_amount) {
static gate_manifest manifest = gate_manifest(gate_manifest_type());
return manifest;
}

static manifest_type get_manifest() {
static manifest_type manifest = manifest_type(
std::shared_ptr<manifest_param>(
new manifest_single_value_param(9)),
false
);
return manifest;
}

constexpr static std::size_t get_rows_amount(std::size_t witness_amount,
std::size_t lookup_column_amount) {
return 2;
}

const std::size_t rows_amount = get_rows_amount(this->witness_amount(), 0);

constexpr static const std::size_t gates_amount = 1;

Expand Down Expand Up @@ -96,43 +121,42 @@ namespace nil {

template <typename ContainerType>
bool_scalar_multiplication(ContainerType witness):
component_type(witness, {}, {}){};
component_type(witness, {}, {}, get_manifest()){};

template <typename WitnessContainerType, typename ConstantContainerType,
typename PublicInputContainerType>
bool_scalar_multiplication(WitnessContainerType witness, ConstantContainerType constant,
PublicInputContainerType public_input):
component_type(witness, constant, public_input){};
component_type(witness, constant, public_input, get_manifest()){};

bool_scalar_multiplication(std::initializer_list<
typename component_type::witness_container_type::value_type> witnesses,
std::initializer_list<
typename component_type::constant_container_type::value_type> constants,
std::initializer_list<
typename component_type::public_input_container_type::value_type> public_inputs):
component_type(witnesses, constants, public_inputs){};
component_type(witnesses, constants, public_inputs, get_manifest()){};

};

template<typename BlueprintFieldType,
typename ArithmetizationParams,
std::int32_t WitnessesAmount>
typename ArithmetizationParams>
using plonk_bool_scalar_multiplication =
bool_scalar_multiplication<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
typename crypto3::algebra::curves::ed25519,
WitnessesAmount,
basic_non_native_policy<BlueprintFieldType>>;

template<typename BlueprintFieldType, typename ArithmetizationParams>
typename plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams, 9>::result_type
typename plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams>::result_type
generate_assignments(
const plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams, 9> &component,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &assignment,
const plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams> &component,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>
&assignment,
const typename plonk_bool_scalar_multiplication<BlueprintFieldType,
ArithmetizationParams, 9>::input_type instance_input,
ArithmetizationParams>::input_type &instance_input,
const std::uint32_t start_row_index) {
using Ed25519Type = typename crypto3::algebra::curves::ed25519;
using var = typename plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams, 9>::var;
using var = typename plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams>::var;

std::size_t row = start_row_index;
typename Ed25519Type::base_field_type::integral_type b = typename Ed25519Type::base_field_type::integral_type(var_value(assignment, instance_input.k).data);
Expand Down Expand Up @@ -164,19 +188,21 @@ namespace nil {
assignment.witness(component.W(7), row) = b * T_x_array[2];
assignment.witness(component.W(8), row) = b * T_x_array[3];
std::array<var, 4> Q_x = {var(component.W(5), row), var(component.W(6), row), var(component.W(7), row), var(component.W(8), row)};
return typename plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams, 9>::result_type(
component, start_row_index);

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

template<typename BlueprintFieldType, typename ArithmetizationParams>
void generate_gates(
const plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams, 9> &component,
const plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams> &component,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &assignment,
const typename plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams, 9>::input_type &instance_input,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>
&assignment,
const typename plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams>::input_type
&instance_input,
const std::size_t first_selector_index) {
using var = typename plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams, 9>::var;
using var = typename plonk_bool_scalar_multiplication<BlueprintFieldType, ArithmetizationParams>::var;

auto constraint_9 = bp.add_constraint(
var(component.W(4), 0)*( var(component.W(4), 0) - 1));
Expand All @@ -199,21 +225,23 @@ namespace nil {
auto constraint_8 = bp.add_constraint(
var(component.W(8), +1) - var(component.W(3), +1) * var(component.W(4), +1));

bp.add_gate(first_selector_index,
{constraint_9, constraint_10,
bp.add_gate(first_selector_index,
{constraint_9, constraint_10,
constraint_1, constraint_2, constraint_3, constraint_4,
constraint_5, constraint_6, constraint_7, constraint_8});

}

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

std::size_t row = start_row_index;

Expand All @@ -230,12 +258,13 @@ namespace nil {
}

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

auto selector_iterator = assignment.find_selector(component);
Expand All @@ -251,7 +280,7 @@ namespace nil {

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

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

Expand Down
Loading

0 comments on commit 1e9ceab

Please sign in to comment.