Skip to content

Commit

Permalink
added support of tests-to-fail #116
Browse files Browse the repository at this point in the history
  • Loading branch information
tshchelovek committed Mar 20, 2023
1 parent 0749eb0 commit 207a935
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 46 deletions.
2 changes: 1 addition & 1 deletion cmake/modules
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlueprintFieldType, ArithmetizationParams, R>::result_type(component, start_row_index);
Expand Down
15 changes: 12 additions & 3 deletions include/nil/blueprint/utils/satisfiability_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ 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,
bool must_pass = true) {

const std::vector<crypto3::zk::snark::plonk_gate<BlueprintFieldType, crypto3::zk::snark::plonk_constraint<BlueprintFieldType>>> gates =
bp.gates();
Expand All @@ -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;
}
}
}
}
Expand All @@ -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;
}
}
}

Expand Down
11 changes: 7 additions & 4 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>
void test_field_range(std::vector<typename BlueprintFieldType::value_type> public_input){
void test_field_range(std::vector<typename BlueprintFieldType::value_type> public_input,
bool must_pass = true){

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
Expand Down Expand Up @@ -84,7 +85,7 @@ 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);
component_instance, public_input, result_check, instance_input, must_pass);
}

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
Expand Down Expand Up @@ -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<typename crypto3::algebra::curves::pallas::base_field_type>( //ed25519 modulus
{0x3ffffffffffffffed_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255}
{0x3ffffffffffffffed_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255},
false
);

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

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

template <typename BlueprintFieldType>
void test_range_check(std::vector<typename BlueprintFieldType::value_type> public_input){
void test_range_check(std::vector<typename BlueprintFieldType::value_type> public_input,
bool must_pass = true){
constexpr std::size_t WitnessColumns = 15;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 1;
Expand Down Expand Up @@ -75,7 +76,8 @@ 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);
nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
(component_instance, public_input, result_check, instance_input, must_pass);
}

template<typename FieldType>
Expand Down Expand Up @@ -128,9 +130,9 @@ BOOST_AUTO_TEST_SUITE_END()

template<typename FieldType>
void test_range_check_fail_specific_inputs(){
test_range_check<FieldType>({-1});
test_range_check<FieldType>({0x10000000000000000_cppui256});
test_range_check<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 @@ -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<FieldType>({input_scalar});
test_range_check<FieldType>({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>;
Expand Down
13 changes: 7 additions & 6 deletions test/hashes/plonk/decomposition.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_decomposition(std::vector<typename BlueprintFieldType::value_type> public_input,
std::vector<typename BlueprintFieldType::value_type> expected_res) {
std::vector<typename BlueprintFieldType::value_type> expected_res,
bool must_pass = true) {

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
Expand Down Expand Up @@ -77,7 +78,7 @@ 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);
component_instance, public_input, result_check, instance_input, must_pass);
}

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
Expand Down Expand Up @@ -139,21 +140,21 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_must_fail) {

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

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

bad = 0x4000000000000000000000000000000000000000000000000000000000000000_cppui255;

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

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

BOOST_AUTO_TEST_SUITE_END()
34 changes: 19 additions & 15 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){
std::vector<typename BlueprintFieldType::value_type> expected_res, bool must_pass = true){

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](AssignmentType &assignment,
auto result_check = [&expected_res, public_input, must_pass](AssignmentType &assignment,
typename component_type::result_type &real_res) {

#ifdef BLUEPRINT_PLONK_PROFILING_ENABLED
Expand Down Expand Up @@ -117,20 +117,24 @@ void test_bool_scalar_multiplication(std::vector<typename BlueprintFieldType::va
// std::cout << "(" << glue_non_native<BlueprintFieldType, NonNativeFieldType>(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_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input);
component_instance, public_input, result_check, instance_input, must_pass);
}

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) {
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) {

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 @@ -144,7 +148,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);
test_bool_scalar_multiplication<FieldType, NonNativeCurveType>(public_input, expected_res, must_pass);
}

constexpr static const std::size_t random_tests_amount = 3;
Expand All @@ -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;
Expand All @@ -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<field_type, non_native_curve_type>({0,1}, 2);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 10);
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}, 2, false);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 10, false);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, -1, false);

test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 2);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 10);
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(), 2, false);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 10, false);
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), -1, false);
}

BOOST_AUTO_TEST_SUITE_END()
8 changes: 4 additions & 4 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,7 @@
using namespace nil;

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

using ed25519_type = crypto3::algebra::curves::ed25519;
constexpr std::size_t WitnessColumns = 9;
Expand Down Expand Up @@ -75,7 +75,7 @@ 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);
component_instance, public_input, result_check, instance_input, must_pass);
}

constexpr static const std::size_t random_tests_amount = 10;
Expand Down Expand Up @@ -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<field_type>({typename field_type::value_type(ed25519_scalar_modulus + 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>({-1});
test_scalar_non_native_range<field_type>({-1}, false);
}

BOOST_AUTO_TEST_SUITE_END()
10 changes: 6 additions & 4 deletions test/test_plonk_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlueprintFieldType, ArithmetizationParams>;
using component_type = ComponentType;
Expand Down Expand Up @@ -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);
}
Expand All @@ -146,11 +147,12 @@ namespace nil {
typename std::iterator_traits<typename PublicInputContainerType::iterator>::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<ComponentType, BlueprintFieldType, ArithmetizationParams, Hash, Lambda,
FunctorResultCheck>(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 =
Expand Down

0 comments on commit 207a935

Please sign in to comment.