Skip to content

Commit

Permalink
update test_component usage #116
Browse files Browse the repository at this point in the history
  • Loading branch information
tshchelovek committed Mar 28, 2023
1 parent 0ae4057 commit ddfb008
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 55 deletions.
2 changes: 1 addition & 1 deletion include/nil/blueprint/utils/satisfiability_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace nil {
bool is_satisfied(circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType,
ArithmetizationParams>> bp,
crypto3::zk::snark::plonk_assignment_table<BlueprintFieldType,
ArithmetizationParams> assignments) {
ArithmetizationParams> assignments){

const std::vector<crypto3::zk::snark::plonk_gate<BlueprintFieldType, crypto3::zk::snark::plonk_constraint<BlueprintFieldType>>> gates =
bp.gates();
Expand Down
21 changes: 13 additions & 8 deletions test/algebra/fields/plonk/non_native/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ using namespace nil;

template <typename BlueprintFieldType>
void test_field_range(std::vector<typename BlueprintFieldType::value_type> public_input,
bool must_pass = true){
bool expected_to_pass){

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
Expand Down Expand Up @@ -84,8 +84,13 @@ void test_field_range(std::vector<typename BlueprintFieldType::value_type> publi

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

crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input, must_pass);
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, instance_input);
}
}

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
Expand All @@ -95,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 @@ -118,7 +123,7 @@ 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);
}
}

Expand Down
21 changes: 13 additions & 8 deletions test/algebra/fields/plonk/range_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

template <typename BlueprintFieldType>
void test_range_check(std::vector<typename BlueprintFieldType::value_type> public_input,
bool must_pass = true){
bool expected_to_pass){
constexpr std::size_t WitnessColumns = 15;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 1;
Expand Down Expand Up @@ -76,16 +76,21 @@ void test_range_check(std::vector<typename BlueprintFieldType::value_type> publi

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

nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
(component_instance, public_input, result_check, instance_input, must_pass);
if (expected_to_pass) {
nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
(component_instance, public_input, result_check, instance_input);
} 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 @@ -100,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
32 changes: 22 additions & 10 deletions test/hashes/plonk/decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using namespace nil;
template <typename BlueprintFieldType>
void test_decomposition(std::vector<typename BlueprintFieldType::value_type> public_input,
std::vector<typename BlueprintFieldType::value_type> expected_res,
bool must_pass = true) {
bool expected_to_pass) {

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
Expand Down Expand Up @@ -77,8 +77,13 @@ void test_decomposition(std::vector<typename BlueprintFieldType::value_type> pub

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

crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input, must_pass);
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, instance_input);
}
}

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
Expand Down Expand Up @@ -122,15 +127,18 @@ 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) {
Expand All @@ -140,21 +148,25 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_must_fail) {

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

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

bad = 0x4000000000000000000000000000000000000000000000000000000000000000_cppui255;

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

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

BOOST_AUTO_TEST_SUITE_END()
28 changes: 16 additions & 12 deletions test/non_native/plonk/bool_scalar_multiplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ using namespace nil;

template <typename BlueprintFieldType, typename NonNativeCurveType>
void test_bool_scalar_multiplication(std::vector<typename BlueprintFieldType::value_type> public_input,
std::vector<typename BlueprintFieldType::value_type> expected_res, bool must_pass = true){
std::vector<typename BlueprintFieldType::value_type> expected_res, bool expected_to_pass){

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
Expand Down Expand Up @@ -74,7 +74,7 @@ void test_bool_scalar_multiplication(std::vector<typename BlueprintFieldType::va

typename component_type::input_type instance_input = {{T_x, T_y}, var(0, 8, false, var::column_type::public_input)};

auto result_check = [&expected_res, public_input, must_pass](AssignmentType &assignment,
auto result_check = [&expected_res, public_input, expected_to_pass](AssignmentType &assignment,
typename component_type::result_type &real_res) {

#ifdef BLUEPRINT_PLONK_PROFILING_ENABLED
Expand Down Expand Up @@ -117,7 +117,7 @@ void test_bool_scalar_multiplication(std::vector<typename BlueprintFieldType::va
// std::cout << "(" << glue_non_native<BlueprintFieldType, NonNativeFieldType>(real_y).data << ")" << std::endl;
#endif

if (must_pass) {
if (expected_to_pass) {
for(std::size_t i = 0; i < 4; i++) {
assert(expected_res[i] == var_value(assignment, real_res.output.x[i]));
assert(expected_res[i+4] == var_value(assignment, real_res.output.y[i]));
Expand All @@ -127,14 +127,18 @@ void test_bool_scalar_multiplication(std::vector<typename BlueprintFieldType::va

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

crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input, must_pass);
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, instance_input);
}
}

template<typename FieldType, typename NonNativeCurveType>
void test_bool_scalar_multiplication_usable (typename NonNativeCurveType::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type point,
typename FieldType::value_type scalar_bool,
bool must_pass = true) {
typename FieldType::value_type scalar_bool, bool expected_to_pass) {

std::vector<typename FieldType::value_type> public_input = create_public_input<FieldType, typename NonNativeCurveType::base_field_type>(
chop_non_native<FieldType, typename NonNativeCurveType::base_field_type>(point.X),
Expand All @@ -148,7 +152,7 @@ void test_bool_scalar_multiplication_usable (typename NonNativeCurveType::templa
}
public_input.push_back(scalar_bool);

test_bool_scalar_multiplication<FieldType, NonNativeCurveType>(public_input, expected_res, must_pass);
test_bool_scalar_multiplication<FieldType, NonNativeCurveType>(public_input, expected_res, expected_to_pass);
}

constexpr static const std::size_t random_tests_amount = 3;
Expand All @@ -164,12 +168,12 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_bool_scalar_mul_test1) {
boost::random::mt19937 seed_seq;
rand.seed(seed_seq);

test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 1);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 0);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 1, true);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 0, true);

for (std::size_t i = 0; i < random_tests_amount; i++) {
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 1);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 0);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 1, true);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 0, true);
}
}

Expand Down
26 changes: 16 additions & 10 deletions test/non_native/plonk/scalar_non_native_range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
using namespace nil;

template <typename BlueprintFieldType>
void test_scalar_non_native_range(std::vector<typename BlueprintFieldType::value_type> public_input, bool must_pass = true){
void test_scalar_non_native_range(std::vector<typename BlueprintFieldType::value_type> public_input,
bool expected_to_pass){

using ed25519_type = crypto3::algebra::curves::ed25519;
constexpr std::size_t WitnessColumns = 9;
Expand Down Expand Up @@ -74,8 +75,13 @@ void test_scalar_non_native_range(std::vector<typename BlueprintFieldType::value

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

crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input, must_pass);
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, instance_input);
}
}

constexpr static const std::size_t random_tests_amount = 10;
Expand All @@ -84,7 +90,7 @@ BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test0) {
test_scalar_non_native_range<typename crypto3::algebra::curves::pallas::base_field_type>(
{45524});
{45524}, true);
}

BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test1) {
Expand All @@ -95,14 +101,14 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test1) {
typename field_type::value_type ones = 0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui255;

test_scalar_non_native_range<field_type>(
{typename field_type::value_type(ed25519_scalar_modulus-1)});
{typename field_type::value_type(ed25519_scalar_modulus-1)}, true);

test_scalar_non_native_range<field_type>(
{typename field_type::value_type(ones)});
{typename field_type::value_type(ones)}, true);

test_scalar_non_native_range<field_type>({1});
test_scalar_non_native_range<field_type>({1}, true);

test_scalar_non_native_range<field_type>({0});
test_scalar_non_native_range<field_type>({0}, true);

nil::crypto3::random::algebraic_engine<field_type> rand;
boost::random::mt19937 seed_seq;
Expand All @@ -117,7 +123,7 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test1) {
r_integral = typename field_type::integral_type(r.data);
r_integral = r_integral % ed25519_scalar_modulus;
r = typename field_type::value_type(r_integral);
test_scalar_non_native_range<field_type>({r});
test_scalar_non_native_range<field_type>({r}, true);
}
}

Expand All @@ -136,7 +142,7 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test_must_fail) {

for (std::size_t i = 0; i < random_tests_amount; i++) {
overage = (typename field_type::integral_type(rand().data)) % ed25519_scalar_overage;
test_scalar_non_native_range<field_type>({typename field_type::value_type(ed25519_scalar_modulus + overage)}, false);
test_scalar_non_native_range<field_type>({typename field_type::value_type(ed25519_scalar_modulus + overage)}, false); // false positive
}
test_scalar_non_native_range<field_type>({-1}, false);
}
Expand Down
Loading

0 comments on commit ddfb008

Please sign in to comment.