Skip to content

Commit

Permalink
Enable flexible witness amount
Browse files Browse the repository at this point in the history
  • Loading branch information
akokoshn authored and nkaskov committed Oct 10, 2023
1 parent 4d57581 commit 1c93054
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 29 deletions.
4 changes: 2 additions & 2 deletions include/nil/blueprint/comparison/comparison.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ namespace nil {

template<typename BlueprintFieldType, typename ArithmetizationParams>
typename crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>
handle_comparison_component(
handle_comparison_component(
llvm::CmpInst::Predicate p,
const typename crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type> &x,
const typename crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type> &y,
std::size_t Bitness,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>
&assignment,
&assignment,
std::uint32_t start_row,
std::size_t &public_input_idx) {

Expand Down
94 changes: 94 additions & 0 deletions include/nil/blueprint/component_manifest_utilities.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2023 Alexey Kokoshnikov <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ASSIGNER_COMPONENT_MANIFEST_UTILITIES_HPP
#define CRYPTO3_ASSIGNER_COMPONENT_MANIFEST_UTILITIES_HPP

#include <vector>
#include <array>
#include <limits>

#include <nil/blueprint/manifest.hpp>

namespace nil {
namespace blueprint {
namespace detail {

struct FlexibleParameters {
std::vector <std::uint32_t> witness;

FlexibleParameters(std::uint32_t witness_amount) {
witness.resize(witness_amount);
std::iota(witness.begin(), witness.end(), 0); // fill 0, 1, ...
}
};

template<typename ArithmetizationParams>
struct CompilerRestrictions {
inline static compiler_manifest common_restriction_manifest = compiler_manifest(ArithmetizationParams::witness_columns,
std::numeric_limits<std::int32_t>::max() - 1,
std::numeric_limits<std::int32_t>::max(), true);
};

template<typename ComponentType, typename ArithmetizationParams>
struct ManifestReader {
inline static typename ComponentType::manifest_type manifest =
CompilerRestrictions<ArithmetizationParams>::common_restriction_manifest.intersect(ComponentType::get_manifest());

template<typename... Args>
static std::vector <std::pair<std::uint32_t, std::uint32_t>>
get_witness(Args... args) {
ASSERT(manifest.is_satisfiable());
auto witness_amount_ptr = manifest.witness_amount;
std::vector <std::pair<std::uint32_t, std::uint32_t>> values;
for (auto it = witness_amount_ptr->begin();
it != witness_amount_ptr->end(); it++) {
const auto witness_amount = *it;
values.emplace_back(witness_amount,
ComponentType::get_rows_amount(witness_amount,
args...));
}
ASSERT(values.size() > 0);
return values;
}

static typename ComponentType::component_type::constant_container_type
get_constants() {
typename ComponentType::component_type::constant_container_type constants;
std::iota(constants.begin(), constants.end(), 0); // fill 0, 1, ...
return constants;
}

static typename ComponentType::component_type::public_input_container_type
get_public_inputs() {
typename ComponentType::component_type::public_input_container_type public_inputs;
std::iota(public_inputs.begin(), public_inputs.end(), 0); // fill 0, 1, ...
return public_inputs;
}
};
} // namespace detail
} // namespace blueprint
} // namespace nil

#endif // CRYPTO3_ASSIGNER_COMPONENT_MANIFEST_UTILITIES_HPP
7 changes: 5 additions & 2 deletions include/nil/blueprint/curves/addition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include <nil/blueprint/asserts.hpp>
#include <nil/blueprint/stack.hpp>
#include <nil/blueprint/policy/policy_manager.hpp>

namespace nil {
namespace blueprint {
Expand All @@ -66,7 +67,8 @@ namespace nil {

using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using component_type = components::unified_addition<ArithmetizationType, CurveType>;
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

struct var_ec_point {
var X;
Expand Down Expand Up @@ -102,7 +104,8 @@ namespace nil {
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using component_type = components::complete_addition<ArithmetizationType, CurveType,
Ed25519Type, basic_non_native_policy<BlueprintFieldType>>;
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8}, {0}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

using non_native_policy_type = basic_non_native_policy<BlueprintFieldType>;

Expand Down
7 changes: 5 additions & 2 deletions include/nil/blueprint/curves/multiplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include <nil/blueprint/asserts.hpp>
#include <nil/blueprint/stack.hpp>
#include <nil/blueprint/policy/policy_manager.hpp>

namespace nil {
namespace blueprint {
Expand All @@ -67,7 +68,8 @@ namespace nil {
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using component_type = components::curve_element_variable_base_scalar_mul<
ArithmetizationType,CurveType>;
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, {0}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

struct var_ec_point {
var X;
Expand Down Expand Up @@ -104,7 +106,8 @@ namespace nil {
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using component_type = components::variable_base_multiplication<ArithmetizationType, CurveType,
Ed25519Type, basic_non_native_policy<BlueprintFieldType>>;
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8}, {0}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0, 253));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs(), 253, nil::blueprint::components::bit_shift_mode::RIGHT);

using non_native_policy_type = basic_non_native_policy<BlueprintFieldType>;

Expand Down
7 changes: 5 additions & 2 deletions include/nil/blueprint/fields/addition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include <nil/blueprint/asserts.hpp>
#include <nil/blueprint/stack.hpp>
#include <nil/blueprint/policy/policy_manager.hpp>

namespace nil {
namespace blueprint {
Expand All @@ -67,7 +68,8 @@ namespace nil {
using component_type = components::addition<
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
BlueprintFieldType, basic_non_native_policy<BlueprintFieldType>>;
component_type component_instance({0, 1, 2}, {}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

var x = variables[operand0];
var y = variables[operand1];
Expand All @@ -94,7 +96,8 @@ namespace nil {
using component_type = components::addition<
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
OperatingFieldType, basic_non_native_policy<BlueprintFieldType>>;
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8}, {}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

std::vector<var> operand0_vars = vectors[operand0];
std::vector<var> operand1_vars = vectors[operand1];
Expand Down
4 changes: 3 additions & 1 deletion include/nil/blueprint/fields/division.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <nil/blueprint/asserts.hpp>
#include <nil/blueprint/stack.hpp>
#include <nil/blueprint/policy/policy_manager.hpp>

namespace nil {
namespace blueprint {
Expand All @@ -66,7 +67,8 @@ namespace nil {
using component_type = components::division<
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
BlueprintFieldType, basic_non_native_policy<BlueprintFieldType>>;
component_type component_instance({0, 1, 2, 3}, {}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

var x = variables[operand0];
var y = variables[operand1];
Expand Down
7 changes: 5 additions & 2 deletions include/nil/blueprint/fields/multiplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include <nil/blueprint/asserts.hpp>
#include <nil/blueprint/stack.hpp>
#include <nil/blueprint/policy/policy_manager.hpp>

namespace nil {
namespace blueprint {
Expand All @@ -67,7 +68,8 @@ namespace nil {
using component_type = components::multiplication<
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
BlueprintFieldType, basic_non_native_policy<BlueprintFieldType>>;
component_type component_instance({0, 1, 2}, {}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

var x = variables[operand0];
var y = variables[operand1];
Expand All @@ -94,7 +96,8 @@ namespace nil {
using component_type = components::multiplication<
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
OperatingFieldType, basic_non_native_policy<BlueprintFieldType>>;
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8}, {}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

std::vector<var> operand0_vars = vectors[operand0];
std::vector<var> operand1_vars = vectors[operand1];
Expand Down
7 changes: 5 additions & 2 deletions include/nil/blueprint/fields/subtraction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include <nil/blueprint/asserts.hpp>
#include <nil/blueprint/stack.hpp>
#include <nil/blueprint/policy/policy_manager.hpp>

namespace nil {
namespace blueprint {
Expand All @@ -67,7 +68,8 @@ namespace nil {
using component_type = components::subtraction<
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
BlueprintFieldType, basic_non_native_policy<BlueprintFieldType>>;
component_type component_instance({0, 1, 2}, {}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

var x = variables[operand0];
var y = variables[operand1];
Expand All @@ -94,7 +96,8 @@ namespace nil {
using component_type = components::subtraction<
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
OperatingFieldType, basic_non_native_policy<BlueprintFieldType>>;
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8}, {}, {});
const auto p = PolicyManager::get_parameters(ManifestReader<component_type, ArithmetizationParams>::get_witness(0));
component_type component_instance(p.witness, ManifestReader<component_type, ArithmetizationParams>::get_constants(), ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

std::vector<var> operand0_vars = vectors[operand0];
std::vector<var> operand1_vars = vectors[operand1];
Expand Down
6 changes: 5 additions & 1 deletion include/nil/blueprint/hashes/sha2_256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <nil/blueprint/components/hashes/sha2/plonk/sha256.hpp>

#include <nil/blueprint/stack.hpp>
#include <nil/blueprint/policy/policy_manager.hpp>

namespace nil {
namespace blueprint {
Expand All @@ -67,7 +68,10 @@ namespace nil {

typename component_type::input_type instance_input = {input_block_vars};

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8}, {0}, {});
const auto p = detail::PolicyManager::get_parameters(detail::ManifestReader<component_type, ArithmetizationParams>::get_witness(0));

component_type component_instance(p.witness, detail::ManifestReader<component_type, ArithmetizationParams>::get_constants(),
detail::ManifestReader<component_type, ArithmetizationParams>::get_public_inputs());

components::generate_circuit(component_instance, bp, assignmnt, instance_input, start_row);

Expand Down
13 changes: 10 additions & 3 deletions include/nil/blueprint/hashes/sha2_512.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <nil/blueprint/components/algebra/fields/plonk/non_native/reduction.hpp>

#include <nil/blueprint/stack.hpp>
#include <nil/blueprint/policy/policy_manager.hpp>

namespace nil {
namespace blueprint {
Expand Down Expand Up @@ -77,8 +78,10 @@ namespace nil {

typename sha2_512_component_type::input_type sha2_512_instance_input = {R, A, {{input_vars[16], input_vars[17],
input_vars[18], input_vars[19]}}};

sha2_512_component_type sha2_512_component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8}, {0}, {});
const auto p_sha2_512 = detail::PolicyManager::get_parameters(detail::ManifestReader<sha2_512_component_type, ArithmetizationParams>::get_witness(0));
sha2_512_component_type sha2_512_component_instance(p_sha2_512.witness,
detail::ManifestReader<sha2_512_component_type, ArithmetizationParams>::get_constants(),
detail::ManifestReader<sha2_512_component_type, ArithmetizationParams>::get_public_inputs());

components::generate_circuit(sha2_512_component_instance, bp, assignmnt, sha2_512_instance_input, start_row);

Expand All @@ -89,7 +92,11 @@ namespace nil {
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>, BlueprintFieldType,
basic_non_native_policy<BlueprintFieldType>>;

reduction_component_type reduction_component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8}, {0}, {});
const auto p = detail::PolicyManager::get_parameters(detail::ManifestReader<reduction_component_type, ArithmetizationParams>::get_witness(0));

reduction_component_type reduction_component_instance(p.witness,
detail::ManifestReader<reduction_component_type, ArithmetizationParams>::get_constants(),
detail::ManifestReader<reduction_component_type, ArithmetizationParams>::get_public_inputs());

start_row = assignmnt.allocated_rows();

Expand Down
Loading

0 comments on commit 1c93054

Please sign in to comment.