Skip to content

Commit

Permalink
returned true/false #116
Browse files Browse the repository at this point in the history
  • Loading branch information
tshchelovek committed Apr 13, 2023
1 parent 909e58e commit 8b688a3
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 381 deletions.
69 changes: 16 additions & 53 deletions test/algebra/fields/plonk/non_native/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
using namespace nil;

template <typename BlueprintFieldType>
auto test_field_range_inner(std::vector<typename BlueprintFieldType::value_type> public_input){
void test_field_range(std::vector<typename BlueprintFieldType::value_type> public_input,
bool expected_to_pass){

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
Expand Down Expand Up @@ -83,51 +84,13 @@ auto test_field_range_inner(std::vector<typename BlueprintFieldType::value_type>

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{});

return std::tuple(component_instance, instance_input, result_check);
}

template <typename BlueprintFieldType>
void test_field_range(std::vector<typename BlueprintFieldType::value_type> public_input){
constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 0;
constexpr std::size_t SelectorColumns = 1;
using ArithmetizationParams =
crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = blueprint::assignment<ArithmetizationType>;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using component_type = blueprint::components::range<ArithmetizationType,
typename crypto3::algebra::fields::curve25519_base_field, 9, blueprint::basic_non_native_policy<BlueprintFieldType>>;

auto [component_instance, instance_input, result_check] = test_field_range_inner<BlueprintFieldType>(public_input);

crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
if (expected_to_pass) {
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input);
}

template <typename BlueprintFieldType>
void test_field_range_to_fail(std::vector<typename BlueprintFieldType::value_type> public_input){
constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 0;
constexpr std::size_t SelectorColumns = 1;
using ArithmetizationParams =
crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = blueprint::assignment<ArithmetizationType>;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using component_type = blueprint::components::range<ArithmetizationType,
typename crypto3::algebra::fields::curve25519_base_field, 9, blueprint::basic_non_native_policy<BlueprintFieldType>>;

auto [component_instance, instance_input, result_check] = test_field_range_inner<BlueprintFieldType>(public_input);

crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
} else {
crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input);
}
}

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
Expand All @@ -137,20 +100,20 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test0) {
using field_type = crypto3::algebra::curves::pallas::base_field_type;

test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
{455245345345345, 523553453454343, 68753453534534689, 54355345344544});
{455245345345345, 523553453454343, 68753453534534689, 54355345344544}, true);

test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
create_public_input_1_value<field_type, non_native_field_type>(
chop_non_native<field_type, non_native_field_type>(1)
));
), true);
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
create_public_input_1_value<field_type, non_native_field_type>(
chop_non_native<field_type, non_native_field_type>(0)
));
), true);
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
create_public_input_1_value<field_type, non_native_field_type>(
chop_non_native<field_type, non_native_field_type>(-1)
));
), true);

nil::crypto3::random::algebraic_engine<non_native_field_type> rand;
boost::random::mt19937 seed_seq;
Expand All @@ -160,20 +123,20 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test0) {
test_field_range<field_type>(
create_public_input_1_value<field_type, non_native_field_type>(
chop_non_native<field_type, non_native_field_type>(rand())
));
), true);
}
}

BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test_must_fail) {
using non_native_field_type = typename crypto3::algebra::fields::curve25519_base_field;
using field_type = crypto3::algebra::curves::pallas::base_field_type;

test_field_range_to_fail<typename crypto3::algebra::curves::pallas::base_field_type>( //ed25519 modulus
{0x3ffffffffffffffed_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255}
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>( //ed25519 modulus
{0x3ffffffffffffffed_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255}, false
);

test_field_range_to_fail<typename crypto3::algebra::curves::pallas::base_field_type>(
{0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255}
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
{0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255}, false
);

}
Expand Down
73 changes: 18 additions & 55 deletions test/algebra/fields/plonk/range_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@
#include "test_plonk_component.hpp"

template <typename BlueprintFieldType>
auto test_range_check_inner(std::vector<typename BlueprintFieldType::value_type> public_input){
auto test_range_check(std::vector<typename BlueprintFieldType::value_type> public_input,
const bool expected_to_pass) {
constexpr std::size_t WitnessColumns = 15;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 1;
constexpr std::size_t SelectorColumns = 1;
constexpr std::size_t R = 64;
using ArithmetizationParams = nil::crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns,
PublicInputColumns, ConstantColumns, SelectorColumns>;
PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = nil::blueprint::assignment<ArithmetizationType>;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using component_type = nil::blueprint::components::range_check<ArithmetizationType, R, 15>;
using component_type = nil::blueprint::components::range_check<ArithmetizationType, R, WitnessColumns>;
using var = nil::crypto3::zk::snark::plonk_variable<BlueprintFieldType>;

var x(0, 0, false, var::column_type::public_input);
Expand All @@ -75,59 +76,21 @@ auto test_range_check_inner(std::vector<typename BlueprintFieldType::value_type>

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},{0},{0});

return std::make_tuple(component_instance, instance_input, result_check);
}

template <typename BlueprintFieldType>
void test_range_check(std::vector<typename BlueprintFieldType::value_type> public_input){
constexpr std::size_t WitnessColumns = 15;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 1;
constexpr std::size_t SelectorColumns = 1;
constexpr std::size_t R = 64;
using ArithmetizationParams = nil::crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns,
PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = nil::blueprint::assignment<ArithmetizationType>;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using component_type = nil::blueprint::components::range_check<ArithmetizationType, R, 15>;

auto [component_instance, instance_input, result_check] = test_range_check_inner<BlueprintFieldType>(public_input);

nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
if (expected_to_pass) {
nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
(component_instance, public_input, result_check, instance_input);
}

template <typename BlueprintFieldType>
void test_range_check_to_fail(std::vector<typename BlueprintFieldType::value_type> public_input){
constexpr std::size_t WitnessColumns = 15;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 1;
constexpr std::size_t SelectorColumns = 1;
constexpr std::size_t R = 64;
using ArithmetizationParams = nil::crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns,
PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = nil::blueprint::assignment<ArithmetizationType>;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using component_type = nil::blueprint::components::range_check<ArithmetizationType, R, 15>;

auto [component_instance, instance_input, result_check] = test_range_check_inner<BlueprintFieldType>(public_input);

nil::crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
} else {
nil::crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
(component_instance, public_input, result_check, instance_input);
}
}

template<typename FieldType>
void test_range_check_specific_inputs(){
test_range_check<FieldType>({0});
test_range_check<FieldType>({1});
test_range_check<FieldType>({35000});
test_range_check<FieldType>({0xFFFFFFFFFFFFFFFF_cppui256});
test_range_check<FieldType>({0}, true);
test_range_check<FieldType>({1}, true);
test_range_check<FieldType>({35000}, true);
test_range_check<FieldType>({0xFFFFFFFFFFFFFFFF_cppui256}, true);
}

template<typename FieldType, std::size_t RandomTestsAmount>
Expand All @@ -142,7 +105,7 @@ void test_range_check_random_inputs(){
typename FieldType::integral_type input_integral = typename FieldType::integral_type(input.data);
input_integral = input_integral & 0xFFFFFFFFFFFFFFFF_cppui255;
typename FieldType::value_type input_scalar = input_integral;
test_range_check<FieldType>({input_scalar});
test_range_check<FieldType>({input_scalar}, true);
}
}

Expand Down Expand Up @@ -172,9 +135,9 @@ BOOST_AUTO_TEST_SUITE_END()

template<typename FieldType>
void test_range_check_fail_specific_inputs(){
test_range_check_to_fail<FieldType>({-1});
test_range_check_to_fail<FieldType>({0x10000000000000000_cppui256});
test_range_check_to_fail<FieldType>({0x4000000000000000000000000000000000000000000000000000000000000000_cppui256});
test_range_check<FieldType>({-1}, false);
test_range_check<FieldType>({0x10000000000000000_cppui256}, false);
test_range_check<FieldType>({0x4000000000000000000000000000000000000000000000000000000000000000_cppui256}, false);
}

template<typename FieldType, std::size_t RandomTestsAmount>
Expand All @@ -191,7 +154,7 @@ void test_range_check_fail_random_inputs(){
}
typename FieldType::integral_type input_integral = typename FieldType::integral_type(input.data);
typename FieldType::value_type input_scalar = input_integral;
test_range_check_to_fail<FieldType>({input_scalar});
test_range_check<FieldType>({input_scalar}, false);
}
}

Expand Down
99 changes: 30 additions & 69 deletions test/hashes/plonk/decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
using namespace nil;

template <typename BlueprintFieldType>
auto test_decomposition_inner(std::vector<typename BlueprintFieldType::value_type> public_input,
std::vector<typename BlueprintFieldType::value_type> expected_res) {
void test_decomposition(std::vector<typename BlueprintFieldType::value_type> public_input,
std::vector<typename BlueprintFieldType::value_type> expected_res,
const bool expected_to_pass) {

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
Expand All @@ -67,71 +68,24 @@ auto test_decomposition_inner(std::vector<typename BlueprintFieldType::value_typ

typename component_type::input_type instance_input = {input_state_var};

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{});

return std::make_tuple(component_instance, instance_input);
}

template <typename BlueprintFieldType>
void test_decomposition(std::vector<typename BlueprintFieldType::value_type> public_input,
std::vector<typename BlueprintFieldType::value_type> expected_res) {

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 0;
constexpr std::size_t SelectorColumns = 1;
using hash_type = crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 40;

using ArithmetizationParams =
crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using var = crypto3::zk::snark::plonk_variable<BlueprintFieldType>;

using AssignmentType = blueprint::assignment<ArithmetizationType>;
using component_type = blueprint::components::decomposition<ArithmetizationType,
BlueprintFieldType, 9>;

auto [component_instance, instance_input] = test_decomposition_inner<BlueprintFieldType>(public_input, expected_res);

auto result_check = [&expected_res](AssignmentType &assignment,
typename component_type::result_type &real_res) {
for (std::size_t i = 0; i < real_res.output.size(); i++){
assert(expected_res[i] == var_value(assignment, real_res.output[i]));
}
};

crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input);
}

template <typename BlueprintFieldType>
void test_decomposition_to_fail(std::vector<typename BlueprintFieldType::value_type> public_input,
std::vector<typename BlueprintFieldType::value_type> expected_res) {

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 0;
constexpr std::size_t SelectorColumns = 1;
using hash_type = crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 40;

using ArithmetizationParams =
crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using var = crypto3::zk::snark::plonk_variable<BlueprintFieldType>;

using AssignmentType = blueprint::assignment<ArithmetizationType>;
using component_type = blueprint::components::decomposition<ArithmetizationType,
BlueprintFieldType, 9>;

auto [component_instance, instance_input] = test_decomposition_inner<BlueprintFieldType>(public_input, expected_res);

auto result_check = [&expected_res](AssignmentType &assignment,
auto result_check_to_fail = [&expected_res](AssignmentType &assignment,
typename component_type::result_type &real_res) { };

crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{});

if (expected_to_pass) {
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input);
} else {
crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check_to_fail, instance_input);
}
}

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
Expand Down Expand Up @@ -175,39 +129,46 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_test0) {

test_decomposition<field_type>(
{0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255},
calculate_decomposition<field_type>({0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255}));
calculate_decomposition<field_type>({0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255}),
true);

test_decomposition<field_type>(
{0, 0},
calculate_decomposition<field_type>({0, 0}));
calculate_decomposition<field_type>({0, 0}),
true);

test_decomposition<field_type>(
{0xffffffffffffffffffffffffffffffff_cppui255, 0xffffffffffffffffffffffffffffffff_cppui255},
calculate_decomposition<field_type>({0xffffffffffffffffffffffffffffffff_cppui255, 0xffffffffffffffffffffffffffffffff_cppui255}));
calculate_decomposition<field_type>({0xffffffffffffffffffffffffffffffff_cppui255, 0xffffffffffffffffffffffffffffffff_cppui255}),
true);
}

BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_must_fail) {
using field_type = typename crypto3::algebra::curves::pallas::base_field_type;

typename field_type::value_type bad = 0x100000000000000000000000000000000_cppui255;

test_decomposition_to_fail<field_type>(
test_decomposition<field_type>(
{0, bad},
calculate_decomposition<field_type>({0, bad}));
calculate_decomposition<field_type>({0, bad}),
false);

test_decomposition_to_fail<field_type>(
test_decomposition<field_type>(
{bad, 0},
calculate_decomposition<field_type>({bad, 0}));
calculate_decomposition<field_type>({bad, 0}),
false);

bad = 0x4000000000000000000000000000000000000000000000000000000000000000_cppui255;

test_decomposition_to_fail<field_type>(
test_decomposition<field_type>(
{0, bad},
calculate_decomposition<field_type>({0, bad}));
calculate_decomposition<field_type>({0, bad}),
false);

test_decomposition_to_fail<field_type>(
test_decomposition<field_type>(
{bad, 0},
calculate_decomposition<field_type>({bad, 0}));
calculate_decomposition<field_type>({bad, 0}),
false);
}

BOOST_AUTO_TEST_SUITE_END()
Loading

0 comments on commit 8b688a3

Please sign in to comment.