From 207a935bd99c07479bf339c88230d0efb11e7937 Mon Sep 17 00:00:00 2001 From: Polina Chernyshova Date: Mon, 20 Mar 2023 12:28:38 +0000 Subject: [PATCH] added support of tests-to-fail #116 --- cmake/modules | 2 +- .../algebra/fields/plonk/range_check.hpp | 2 -- .../blueprint/utils/satisfiability_check.hpp | 15 ++++++-- .../algebra/fields/plonk/non_native/range.cpp | 11 +++--- test/algebra/fields/plonk/range_check.cpp | 15 ++++---- test/hashes/plonk/decomposition.cpp | 13 +++---- .../plonk/bool_scalar_multiplication.cpp | 34 +++++++++++-------- .../plonk/scalar_non_native_range.cpp | 8 ++--- test/test_plonk_component.hpp | 10 +++--- 9 files changed, 64 insertions(+), 46 deletions(-) diff --git a/cmake/modules b/cmake/modules index 5e6b354eaa..29b5fb09f8 160000 --- a/cmake/modules +++ b/cmake/modules @@ -1 +1 @@ -Subproject commit 5e6b354eaa69f6a275992e08220e925c34ba0a19 +Subproject commit 29b5fb09f8fc5cdcf249186df9dc48d1ea6a36b7 diff --git a/include/nil/blueprint/components/algebra/fields/plonk/range_check.hpp b/include/nil/blueprint/components/algebra/fields/plonk/range_check.hpp index 20dd856f5b..a4228a71da 100644 --- a/include/nil/blueprint/components/algebra/fields/plonk/range_check.hpp +++ b/include/nil/blueprint/components/algebra/fields/plonk/range_check.hpp @@ -189,8 +189,6 @@ namespace nil { row++; } - typename BlueprintFieldType::value_type x_reconstructed = assignment.witness(component.W(0), row - 1); - BOOST_ASSERT(x_reconstructed == x); BOOST_ASSERT(row == start_row_index + component.rows_amount); return typename plonk_range_check::result_type(component, start_row_index); diff --git a/include/nil/blueprint/utils/satisfiability_check.hpp b/include/nil/blueprint/utils/satisfiability_check.hpp index a32dbad8b7..d156fa087b 100644 --- a/include/nil/blueprint/utils/satisfiability_check.hpp +++ b/include/nil/blueprint/utils/satisfiability_check.hpp @@ -44,7 +44,8 @@ namespace nil { bool is_satisfied(circuit> bp, crypto3::zk::snark::plonk_assignment_table assignments){ + ArithmetizationParams> assignments, + bool must_pass = true) { const std::vector>> gates = bp.gates(); @@ -68,7 +69,11 @@ namespace nil { if (!constraint_result.is_zero()) { std::cout << "Constraint " << j << " from gate " << i << "on row " << selector_row << " is not satisfied." << std::endl; - return false; + if (must_pass) { + return false; + } else { + return true; + } } } } @@ -79,7 +84,11 @@ namespace nil { if (var_value(assignments, copy_constraints[i].first) != var_value(assignments, copy_constraints[i].second)){ std::cout << "Copy constraint number " << i << " is not satisfied." << std::endl; - return false; + if (must_pass) { + return false; + } else { + return true; + } } } diff --git a/test/algebra/fields/plonk/non_native/range.cpp b/test/algebra/fields/plonk/non_native/range.cpp index 73c0573a74..07e85ac7d1 100644 --- a/test/algebra/fields/plonk/non_native/range.cpp +++ b/test/algebra/fields/plonk/non_native/range.cpp @@ -46,7 +46,8 @@ using namespace nil; template -void test_field_range(std::vector public_input){ +void test_field_range(std::vector public_input, + bool must_pass = true){ constexpr std::size_t WitnessColumns = 9; constexpr std::size_t PublicInputColumns = 1; @@ -84,7 +85,7 @@ void test_field_range(std::vector publi component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{}); crypto3::test_component( - component_instance, public_input, result_check, instance_input); + component_instance, public_input, result_check, instance_input, must_pass); } BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) @@ -126,11 +127,13 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test_must_fail) { using field_type = crypto3::algebra::curves::pallas::base_field_type; test_field_range( //ed25519 modulus - {0x3ffffffffffffffed_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255} + {0x3ffffffffffffffed_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255}, + false ); test_field_range( - {0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255} + {0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255}, + false ); } diff --git a/test/algebra/fields/plonk/range_check.cpp b/test/algebra/fields/plonk/range_check.cpp index c22040e3e4..efb02608b5 100644 --- a/test/algebra/fields/plonk/range_check.cpp +++ b/test/algebra/fields/plonk/range_check.cpp @@ -45,7 +45,8 @@ #include "test_plonk_component.hpp" template -void test_range_check(std::vector public_input){ +void test_range_check(std::vector public_input, + bool must_pass = true){ constexpr std::size_t WitnessColumns = 15; constexpr std::size_t PublicInputColumns = 1; constexpr std::size_t ConstantColumns = 1; @@ -75,7 +76,8 @@ void test_range_check(std::vector 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_instance, public_input, result_check, instance_input); + nil::crypto3::test_component + (component_instance, public_input, result_check, instance_input, must_pass); } template @@ -128,9 +130,9 @@ BOOST_AUTO_TEST_SUITE_END() template void test_range_check_fail_specific_inputs(){ - test_range_check({-1}); - test_range_check({0x10000000000000000_cppui256}); - test_range_check({0x4000000000000000000000000000000000000000000000000000000000000000_cppui256}); + test_range_check({-1}, false); + test_range_check({0x10000000000000000_cppui256}, false); + test_range_check({0x4000000000000000000000000000000000000000000000000000000000000000_cppui256}, false); } template @@ -147,12 +149,11 @@ 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({input_scalar}); + test_range_check({input_scalar}, false); } } BOOST_AUTO_TEST_SUITE(blueprint_plonk_fields_range_check_fail_test_suite) -// TODO: we need to check that component fails on the wrong input. Don't have such feature yet BOOST_AUTO_TEST_CASE(blueprint_plonk_fields_range_check_fail_bls12) { using field_type = nil::crypto3::algebra::fields::bls12_fr<381>; diff --git a/test/hashes/plonk/decomposition.cpp b/test/hashes/plonk/decomposition.cpp index 5e45a047d9..eca68dae20 100644 --- a/test/hashes/plonk/decomposition.cpp +++ b/test/hashes/plonk/decomposition.cpp @@ -44,7 +44,8 @@ using namespace nil; template void test_decomposition(std::vector public_input, - std::vector expected_res) { + std::vector expected_res, + bool must_pass = true) { constexpr std::size_t WitnessColumns = 9; constexpr std::size_t PublicInputColumns = 1; @@ -77,7 +78,7 @@ void test_decomposition(std::vector pub component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{}); crypto3::test_component( - component_instance, public_input, result_check, instance_input); + component_instance, public_input, result_check, instance_input, must_pass); } BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) @@ -139,21 +140,21 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_must_fail) { test_decomposition( {0, bad}, - calculate_decomposition({0, bad})); + calculate_decomposition({0, bad}), false); test_decomposition( {bad, 0}, - calculate_decomposition({bad, 0})); + calculate_decomposition({bad, 0}), false); bad = 0x4000000000000000000000000000000000000000000000000000000000000000_cppui255; test_decomposition( {0, bad}, - calculate_decomposition({0, bad})); + calculate_decomposition({0, bad}), false); test_decomposition( {bad, 0}, - calculate_decomposition({bad, 0})); + calculate_decomposition({bad, 0}), false); } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/non_native/plonk/bool_scalar_multiplication.cpp b/test/non_native/plonk/bool_scalar_multiplication.cpp index 459589947a..c54c6ab431 100644 --- a/test/non_native/plonk/bool_scalar_multiplication.cpp +++ b/test/non_native/plonk/bool_scalar_multiplication.cpp @@ -46,7 +46,7 @@ using namespace nil; template void test_bool_scalar_multiplication(std::vector public_input, - std::vector expected_res){ + std::vector expected_res, bool must_pass = true){ constexpr std::size_t WitnessColumns = 9; constexpr std::size_t PublicInputColumns = 1; @@ -74,7 +74,7 @@ void test_bool_scalar_multiplication(std::vector(real_y).data << ")" << std::endl; #endif - 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])); + if (must_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])); + } } }; component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{}); crypto3::test_component( - component_instance, public_input, result_check, instance_input); + component_instance, public_input, result_check, instance_input, must_pass); } template -void test_bool_scalar_multiplication_usable (typename NonNativeCurveType::template g1_type::value_type point, typename FieldType::value_type scalar_bool) { +void test_bool_scalar_multiplication_usable (typename NonNativeCurveType::template g1_type::value_type point, + typename FieldType::value_type scalar_bool, + bool must_pass = true) { std::vector public_input = create_public_input( chop_non_native(point.X), @@ -144,7 +148,7 @@ void test_bool_scalar_multiplication_usable (typename NonNativeCurveType::templa } public_input.push_back(scalar_bool); - test_bool_scalar_multiplication(public_input, expected_res); + test_bool_scalar_multiplication(public_input, expected_res, must_pass); } constexpr static const std::size_t random_tests_amount = 3; @@ -169,7 +173,7 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_bool_scalar_mul_test1) { } } -BOOST_AUTO_TEST_CASE(blueprint_non_native_bool_scalar_mul_must_fail) { // test should pass if component fails on wrong data, feature is not implemented yet +BOOST_AUTO_TEST_CASE(blueprint_non_native_bool_scalar_mul_must_fail) { using field_type = typename crypto3::algebra::curves::pallas::base_field_type; using non_native_curve_type = crypto3::algebra::curves::ed25519; using non_native_field_type = non_native_curve_type::base_field_type; @@ -178,13 +182,13 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_bool_scalar_mul_must_fail) { // test s boost::random::mt19937 seed_seq; rand.seed(seed_seq); - test_bool_scalar_multiplication_usable({0,1}, 2); - test_bool_scalar_multiplication_usable({0,1}, 10); - test_bool_scalar_multiplication_usable({0,1}, -1); + test_bool_scalar_multiplication_usable({0,1}, 2, false); + test_bool_scalar_multiplication_usable({0,1}, 10, false); + test_bool_scalar_multiplication_usable({0,1}, -1, false); - test_bool_scalar_multiplication_usable(rand(), 2); - test_bool_scalar_multiplication_usable(rand(), 10); - test_bool_scalar_multiplication_usable(rand(), -1); + test_bool_scalar_multiplication_usable(rand(), 2, false); + test_bool_scalar_multiplication_usable(rand(), 10, false); + test_bool_scalar_multiplication_usable(rand(), -1, false); } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/non_native/plonk/scalar_non_native_range.cpp b/test/non_native/plonk/scalar_non_native_range.cpp index f8d747c8dd..d1bf1e6241 100644 --- a/test/non_native/plonk/scalar_non_native_range.cpp +++ b/test/non_native/plonk/scalar_non_native_range.cpp @@ -44,7 +44,7 @@ using namespace nil; template -void test_scalar_non_native_range(std::vector public_input){ +void test_scalar_non_native_range(std::vector public_input, bool must_pass = true){ using ed25519_type = crypto3::algebra::curves::ed25519; constexpr std::size_t WitnessColumns = 9; @@ -75,7 +75,7 @@ void test_scalar_non_native_range(std::vector( - component_instance, public_input, result_check, instance_input); + component_instance, public_input, result_check, instance_input, must_pass); } constexpr static const std::size_t random_tests_amount = 10; @@ -136,9 +136,9 @@ 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({typename field_type::value_type(ed25519_scalar_modulus + overage)}); + test_scalar_non_native_range({typename field_type::value_type(ed25519_scalar_modulus + overage)}, false); } - test_scalar_non_native_range({-1}); + test_scalar_non_native_range({-1}, false); } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/test_plonk_component.hpp b/test/test_plonk_component.hpp index 32cf074b10..1ff9cec803 100644 --- a/test/test_plonk_component.hpp +++ b/test/test_plonk_component.hpp @@ -102,7 +102,8 @@ namespace nil { bool>::type = true> auto prepare_component(ComponentType component_instance, const PublicInputContainerType &public_input, const FunctorResultCheck &result_check, - typename ComponentType::input_type instance_input) { + typename ComponentType::input_type instance_input, + bool must_pass = true) { using ArithmetizationType = zk::snark::plonk_constraint_system; using component_type = ComponentType; @@ -134,7 +135,7 @@ namespace nil { profiling(assignment); #endif - assert(blueprint::is_satisfied(bp, assignment)); + assert(blueprint::is_satisfied(bp, assignment, must_pass)); return std::make_tuple(desc, bp, assignment); } @@ -146,11 +147,12 @@ namespace nil { typename std::iterator_traits::value_type>::value>::type test_component(ComponentType component_instance, const PublicInputContainerType &public_input, FunctorResultCheck result_check, - typename ComponentType::input_type instance_input) { + typename ComponentType::input_type instance_input, + bool must_pass = true) { auto [desc, bp, assignments] = prepare_component(component_instance, public_input, result_check, instance_input); + FunctorResultCheck>(component_instance, public_input, result_check, instance_input, must_pass); #ifdef BLUEPRINT_PLACEHOLDER_PROOF_GEN_ENABLED using placeholder_params =