From ddfb0081e458e456d671ecbeccf00d5498ed82f6 Mon Sep 17 00:00:00 2001 From: Polina Chernyshova Date: Tue, 28 Mar 2023 12:03:03 +0000 Subject: [PATCH] update test_component usage #116 --- .../blueprint/utils/satisfiability_check.hpp | 2 +- .../algebra/fields/plonk/non_native/range.cpp | 21 ++++++---- test/algebra/fields/plonk/range_check.cpp | 21 ++++++---- test/hashes/plonk/decomposition.cpp | 32 ++++++++++----- .../plonk/bool_scalar_multiplication.cpp | 28 +++++++------ .../plonk/scalar_non_native_range.cpp | 26 +++++++----- test/test_plonk_component.hpp | 40 ++++++++++++++++--- 7 files changed, 115 insertions(+), 55 deletions(-) diff --git a/include/nil/blueprint/utils/satisfiability_check.hpp b/include/nil/blueprint/utils/satisfiability_check.hpp index 99d72a0d6f..a32dbad8b7 100644 --- a/include/nil/blueprint/utils/satisfiability_check.hpp +++ b/include/nil/blueprint/utils/satisfiability_check.hpp @@ -44,7 +44,7 @@ namespace nil { bool is_satisfied(circuit> bp, crypto3::zk::snark::plonk_assignment_table assignments) { + ArithmetizationParams> assignments){ const std::vector>> gates = bp.gates(); diff --git a/test/algebra/fields/plonk/non_native/range.cpp b/test/algebra/fields/plonk/non_native/range.cpp index 07e85ac7d1..cdc9b99459 100644 --- a/test/algebra/fields/plonk/non_native/range.cpp +++ b/test/algebra/fields/plonk/non_native/range.cpp @@ -47,7 +47,7 @@ using namespace nil; template void test_field_range(std::vector public_input, - bool must_pass = true){ + bool expected_to_pass){ constexpr std::size_t WitnessColumns = 9; constexpr std::size_t PublicInputColumns = 1; @@ -84,8 +84,13 @@ 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, must_pass); + if (expected_to_pass) { + crypto3::test_component( + component_instance, public_input, result_check, instance_input); + } else { + crypto3::test_component_to_fail( + component_instance, public_input, result_check, instance_input); + } } BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) @@ -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( - {455245345345345, 523553453454343, 68753453534534689, 54355345344544}); + {455245345345345, 523553453454343, 68753453534534689, 54355345344544}, true); test_field_range( create_public_input_1_value( chop_non_native(1) - )); + ), true); test_field_range( create_public_input_1_value( chop_non_native(0) - )); + ), true); test_field_range( create_public_input_1_value( chop_non_native(-1) - )); + ), true); nil::crypto3::random::algebraic_engine rand; boost::random::mt19937 seed_seq; @@ -118,7 +123,7 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test0) { test_field_range( create_public_input_1_value( chop_non_native(rand()) - )); + ), true); } } diff --git a/test/algebra/fields/plonk/range_check.cpp b/test/algebra/fields/plonk/range_check.cpp index efb02608b5..bfdca7906a 100644 --- a/test/algebra/fields/plonk/range_check.cpp +++ b/test/algebra/fields/plonk/range_check.cpp @@ -46,7 +46,7 @@ template void test_range_check(std::vector 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; @@ -76,16 +76,21 @@ 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, must_pass); + if (expected_to_pass) { + nil::crypto3::test_component + (component_instance, public_input, result_check, instance_input); + } else { + nil::crypto3::test_component_to_fail + (component_instance, public_input, result_check, instance_input); + } } template void test_range_check_specific_inputs(){ - test_range_check({0}); - test_range_check({1}); - test_range_check({35000}); - test_range_check({0xFFFFFFFFFFFFFFFF_cppui256}); + test_range_check({0}, true); + test_range_check({1}, true); + test_range_check({35000}, true); + test_range_check({0xFFFFFFFFFFFFFFFF_cppui256}, true); } template @@ -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({input_scalar}); + test_range_check({input_scalar}, true); } } diff --git a/test/hashes/plonk/decomposition.cpp b/test/hashes/plonk/decomposition.cpp index eca68dae20..418498a46f 100644 --- a/test/hashes/plonk/decomposition.cpp +++ b/test/hashes/plonk/decomposition.cpp @@ -45,7 +45,7 @@ using namespace nil; template void test_decomposition(std::vector public_input, std::vector expected_res, - bool must_pass = true) { + bool expected_to_pass) { constexpr std::size_t WitnessColumns = 9; constexpr std::size_t PublicInputColumns = 1; @@ -77,8 +77,13 @@ 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, must_pass); + if (expected_to_pass) { + crypto3::test_component( + component_instance, public_input, result_check, instance_input); + } else { + crypto3::test_component_to_fail( + component_instance, public_input, result_check, instance_input); + } } BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) @@ -122,15 +127,18 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_test0) { test_decomposition( {0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255}, - calculate_decomposition({0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255})); + calculate_decomposition({0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255}), + true); test_decomposition( {0, 0}, - calculate_decomposition({0, 0})); + calculate_decomposition({0, 0}), + true); test_decomposition( {0xffffffffffffffffffffffffffffffff_cppui255, 0xffffffffffffffffffffffffffffffff_cppui255}, - calculate_decomposition({0xffffffffffffffffffffffffffffffff_cppui255, 0xffffffffffffffffffffffffffffffff_cppui255})); + calculate_decomposition({0xffffffffffffffffffffffffffffffff_cppui255, 0xffffffffffffffffffffffffffffffff_cppui255}), + true); } BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_must_fail) { @@ -140,21 +148,25 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_must_fail) { test_decomposition( {0, bad}, - calculate_decomposition({0, bad}), false); + calculate_decomposition({0, bad}), + false); test_decomposition( {bad, 0}, - calculate_decomposition({bad, 0}), false); + calculate_decomposition({bad, 0}), + false); bad = 0x4000000000000000000000000000000000000000000000000000000000000000_cppui255; test_decomposition( {0, bad}, - calculate_decomposition({0, bad}), false); + calculate_decomposition({0, bad}), + false); test_decomposition( {bad, 0}, - calculate_decomposition({bad, 0}), false); + 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 c54c6ab431..0d37daf91b 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, bool must_pass = true){ + std::vector expected_res, bool expected_to_pass){ 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 - 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])); @@ -127,14 +127,18 @@ void test_bool_scalar_multiplication(std::vector( - component_instance, public_input, result_check, instance_input, must_pass); + if (expected_to_pass) { + crypto3::test_component( + component_instance, public_input, result_check, instance_input); + } else { + crypto3::test_component_to_fail( + component_instance, public_input, result_check, instance_input); + } } template void test_bool_scalar_multiplication_usable (typename NonNativeCurveType::template g1_type::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 public_input = create_public_input( chop_non_native(point.X), @@ -148,7 +152,7 @@ void test_bool_scalar_multiplication_usable (typename NonNativeCurveType::templa } public_input.push_back(scalar_bool); - test_bool_scalar_multiplication(public_input, expected_res, must_pass); + test_bool_scalar_multiplication(public_input, expected_res, expected_to_pass); } constexpr static const std::size_t random_tests_amount = 3; @@ -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({0,1}, 1); - test_bool_scalar_multiplication_usable({0,1}, 0); + test_bool_scalar_multiplication_usable({0,1}, 1, true); + test_bool_scalar_multiplication_usable({0,1}, 0, true); for (std::size_t i = 0; i < random_tests_amount; i++) { - test_bool_scalar_multiplication_usable(rand(), 1); - test_bool_scalar_multiplication_usable(rand(), 0); + test_bool_scalar_multiplication_usable(rand(), 1, true); + test_bool_scalar_multiplication_usable(rand(), 0, true); } } diff --git a/test/non_native/plonk/scalar_non_native_range.cpp b/test/non_native/plonk/scalar_non_native_range.cpp index d1bf1e6241..8a961a15c2 100644 --- a/test/non_native/plonk/scalar_non_native_range.cpp +++ b/test/non_native/plonk/scalar_non_native_range.cpp @@ -44,7 +44,8 @@ using namespace nil; template -void test_scalar_non_native_range(std::vector public_input, bool must_pass = true){ +void test_scalar_non_native_range(std::vector public_input, + bool expected_to_pass){ using ed25519_type = crypto3::algebra::curves::ed25519; constexpr std::size_t WitnessColumns = 9; @@ -74,8 +75,13 @@ void test_scalar_non_native_range(std::vector( - component_instance, public_input, result_check, instance_input, must_pass); + if (expected_to_pass) { + crypto3::test_component( + component_instance, public_input, result_check, instance_input); + } else { + crypto3::test_component_to_fail( + component_instance, public_input, result_check, instance_input); + } } constexpr static const std::size_t random_tests_amount = 10; @@ -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( - {45524}); + {45524}, true); } BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test1) { @@ -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( - {typename field_type::value_type(ed25519_scalar_modulus-1)}); + {typename field_type::value_type(ed25519_scalar_modulus-1)}, true); test_scalar_non_native_range( - {typename field_type::value_type(ones)}); + {typename field_type::value_type(ones)}, true); - test_scalar_non_native_range({1}); + test_scalar_non_native_range({1}, true); - test_scalar_non_native_range({0}); + test_scalar_non_native_range({0}, true); nil::crypto3::random::algebraic_engine rand; boost::random::mt19937 seed_seq; @@ -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({r}); + test_scalar_non_native_range({r}, true); } } @@ -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({typename field_type::value_type(ed25519_scalar_modulus + overage)}, false); + test_scalar_non_native_range({typename field_type::value_type(ed25519_scalar_modulus + overage)}, false); // false positive } test_scalar_non_native_range({-1}, false); } diff --git a/test/test_plonk_component.hpp b/test/test_plonk_component.hpp index af98b7f0d1..66a911c50c 100644 --- a/test/test_plonk_component.hpp +++ b/test/test_plonk_component.hpp @@ -103,7 +103,7 @@ namespace nil { auto prepare_component(ComponentType component_instance, const PublicInputContainerType &public_input, const FunctorResultCheck &result_check, typename ComponentType::input_type instance_input, - bool must_pass = true) { + bool expected_to_pass) { using ArithmetizationType = zk::snark::plonk_constraint_system; using component_type = ComponentType; @@ -135,7 +135,7 @@ namespace nil { profiling(assignment); #endif - assert(blueprint::is_satisfied(bp, assignment) == must_pass); + assert(blueprint::is_satisfied(bp, assignment) == expected_to_pass); return std::make_tuple(desc, bp, assignment); } @@ -145,14 +145,14 @@ namespace nil { typename std::enable_if< std::is_same::value_type>::value>::type - test_component(ComponentType component_instance, const PublicInputContainerType &public_input, + test_component_inner(ComponentType component_instance, const PublicInputContainerType &public_input, FunctorResultCheck result_check, typename ComponentType::input_type instance_input, - bool must_pass = true) { + bool expected_to_pass) { auto [desc, bp, assignments] = prepare_component(component_instance, public_input, result_check, instance_input, must_pass); + FunctorResultCheck>(component_instance, public_input, result_check, instance_input, expected_to_pass); #ifdef BLUEPRINT_PLACEHOLDER_PROOF_GEN_ENABLED using placeholder_params = @@ -183,13 +183,41 @@ namespace nil { bool verifier_res = zk::snark::placeholder_verifier::process( public_preprocessed_data, proof, bp, fri_params); - if (must_pass) { + if (expected_to_pass) { BOOST_CHECK(verifier_res); } else { BOOST_CHECK(!verifier_res); } #endif } + + template + typename std::enable_if< + std::is_same::value_type>::value>::type + test_component(ComponentType component_instance, const PublicInputContainerType &public_input, + FunctorResultCheck result_check, + typename ComponentType::input_type instance_input) { + return test_component_inner(component_instance, public_input, + result_check, instance_input, + true); + } + + template + typename std::enable_if< + std::is_same::value_type>::value>::type + test_component_to_fail(ComponentType component_instance, const PublicInputContainerType &public_input, + FunctorResultCheck result_check, + typename ComponentType::input_type instance_input) { + return test_component_inner(component_instance, public_input, + result_check, instance_input, + false); + } } // namespace crypto3 } // namespace nil