Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit ddfb008

Browse files
committed
update test_component usage #116
1 parent 0ae4057 commit ddfb008

File tree

7 files changed

+115
-55
lines changed

7 files changed

+115
-55
lines changed

include/nil/blueprint/utils/satisfiability_check.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace nil {
4444
bool is_satisfied(circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType,
4545
ArithmetizationParams>> bp,
4646
crypto3::zk::snark::plonk_assignment_table<BlueprintFieldType,
47-
ArithmetizationParams> assignments) {
47+
ArithmetizationParams> assignments){
4848

4949
const std::vector<crypto3::zk::snark::plonk_gate<BlueprintFieldType, crypto3::zk::snark::plonk_constraint<BlueprintFieldType>>> gates =
5050
bp.gates();

test/algebra/fields/plonk/non_native/range.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ using namespace nil;
4747

4848
template <typename BlueprintFieldType>
4949
void test_field_range(std::vector<typename BlueprintFieldType::value_type> public_input,
50-
bool must_pass = true){
50+
bool expected_to_pass){
5151

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

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

87-
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
88-
component_instance, public_input, result_check, instance_input, must_pass);
87+
if (expected_to_pass) {
88+
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
89+
component_instance, public_input, result_check, instance_input);
90+
} else {
91+
crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
92+
component_instance, public_input, result_check, instance_input);
93+
}
8994
}
9095

9196
BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
@@ -95,20 +100,20 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test0) {
95100
using field_type = crypto3::algebra::curves::pallas::base_field_type;
96101

97102
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
98-
{455245345345345, 523553453454343, 68753453534534689, 54355345344544});
103+
{455245345345345, 523553453454343, 68753453534534689, 54355345344544}, true);
99104

100105
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
101106
create_public_input_1_value<field_type, non_native_field_type>(
102107
chop_non_native<field_type, non_native_field_type>(1)
103-
));
108+
), true);
104109
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
105110
create_public_input_1_value<field_type, non_native_field_type>(
106111
chop_non_native<field_type, non_native_field_type>(0)
107-
));
112+
), true);
108113
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
109114
create_public_input_1_value<field_type, non_native_field_type>(
110115
chop_non_native<field_type, non_native_field_type>(-1)
111-
));
116+
), true);
112117

113118
nil::crypto3::random::algebraic_engine<non_native_field_type> rand;
114119
boost::random::mt19937 seed_seq;
@@ -118,7 +123,7 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test0) {
118123
test_field_range<field_type>(
119124
create_public_input_1_value<field_type, non_native_field_type>(
120125
chop_non_native<field_type, non_native_field_type>(rand())
121-
));
126+
), true);
122127
}
123128
}
124129

test/algebra/fields/plonk/range_check.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
template <typename BlueprintFieldType>
4848
void test_range_check(std::vector<typename BlueprintFieldType::value_type> public_input,
49-
bool must_pass = true){
49+
bool expected_to_pass){
5050
constexpr std::size_t WitnessColumns = 15;
5151
constexpr std::size_t PublicInputColumns = 1;
5252
constexpr std::size_t ConstantColumns = 1;
@@ -76,16 +76,21 @@ void test_range_check(std::vector<typename BlueprintFieldType::value_type> publi
7676

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

79-
nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
80-
(component_instance, public_input, result_check, instance_input, must_pass);
79+
if (expected_to_pass) {
80+
nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
81+
(component_instance, public_input, result_check, instance_input);
82+
} else {
83+
nil::crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>
84+
(component_instance, public_input, result_check, instance_input);
85+
}
8186
}
8287

8388
template<typename FieldType>
8489
void test_range_check_specific_inputs(){
85-
test_range_check<FieldType>({0});
86-
test_range_check<FieldType>({1});
87-
test_range_check<FieldType>({35000});
88-
test_range_check<FieldType>({0xFFFFFFFFFFFFFFFF_cppui256});
90+
test_range_check<FieldType>({0}, true);
91+
test_range_check<FieldType>({1}, true);
92+
test_range_check<FieldType>({35000}, true);
93+
test_range_check<FieldType>({0xFFFFFFFFFFFFFFFF_cppui256}, true);
8994
}
9095

9196
template<typename FieldType, std::size_t RandomTestsAmount>
@@ -100,7 +105,7 @@ void test_range_check_random_inputs(){
100105
typename FieldType::integral_type input_integral = typename FieldType::integral_type(input.data);
101106
input_integral = input_integral & 0xFFFFFFFFFFFFFFFF_cppui255;
102107
typename FieldType::value_type input_scalar = input_integral;
103-
test_range_check<FieldType>({input_scalar});
108+
test_range_check<FieldType>({input_scalar}, true);
104109
}
105110
}
106111

test/hashes/plonk/decomposition.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ using namespace nil;
4545
template <typename BlueprintFieldType>
4646
void test_decomposition(std::vector<typename BlueprintFieldType::value_type> public_input,
4747
std::vector<typename BlueprintFieldType::value_type> expected_res,
48-
bool must_pass = true) {
48+
bool expected_to_pass) {
4949

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

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

80-
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
81-
component_instance, public_input, result_check, instance_input, must_pass);
80+
if (expected_to_pass) {
81+
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
82+
component_instance, public_input, result_check, instance_input);
83+
} else {
84+
crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
85+
component_instance, public_input, result_check, instance_input);
86+
}
8287
}
8388

8489
BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
@@ -122,15 +127,18 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_test0) {
122127

123128
test_decomposition<field_type>(
124129
{0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255},
125-
calculate_decomposition<field_type>({0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255}));
130+
calculate_decomposition<field_type>({0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255}),
131+
true);
126132

127133
test_decomposition<field_type>(
128134
{0, 0},
129-
calculate_decomposition<field_type>({0, 0}));
135+
calculate_decomposition<field_type>({0, 0}),
136+
true);
130137

131138
test_decomposition<field_type>(
132139
{0xffffffffffffffffffffffffffffffff_cppui255, 0xffffffffffffffffffffffffffffffff_cppui255},
133-
calculate_decomposition<field_type>({0xffffffffffffffffffffffffffffffff_cppui255, 0xffffffffffffffffffffffffffffffff_cppui255}));
140+
calculate_decomposition<field_type>({0xffffffffffffffffffffffffffffffff_cppui255, 0xffffffffffffffffffffffffffffffff_cppui255}),
141+
true);
134142
}
135143

136144
BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_must_fail) {
@@ -140,21 +148,25 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_must_fail) {
140148

141149
test_decomposition<field_type>(
142150
{0, bad},
143-
calculate_decomposition<field_type>({0, bad}), false);
151+
calculate_decomposition<field_type>({0, bad}),
152+
false);
144153

145154
test_decomposition<field_type>(
146155
{bad, 0},
147-
calculate_decomposition<field_type>({bad, 0}), false);
156+
calculate_decomposition<field_type>({bad, 0}),
157+
false);
148158

149159
bad = 0x4000000000000000000000000000000000000000000000000000000000000000_cppui255;
150160

151161
test_decomposition<field_type>(
152162
{0, bad},
153-
calculate_decomposition<field_type>({0, bad}), false);
163+
calculate_decomposition<field_type>({0, bad}),
164+
false);
154165

155166
test_decomposition<field_type>(
156167
{bad, 0},
157-
calculate_decomposition<field_type>({bad, 0}), false);
168+
calculate_decomposition<field_type>({bad, 0}),
169+
false);
158170
}
159171

160172
BOOST_AUTO_TEST_SUITE_END()

test/non_native/plonk/bool_scalar_multiplication.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ using namespace nil;
4646

4747
template <typename BlueprintFieldType, typename NonNativeCurveType>
4848
void test_bool_scalar_multiplication(std::vector<typename BlueprintFieldType::value_type> public_input,
49-
std::vector<typename BlueprintFieldType::value_type> expected_res, bool must_pass = true){
49+
std::vector<typename BlueprintFieldType::value_type> expected_res, bool expected_to_pass){
5050

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

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

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

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

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

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

130-
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
131-
component_instance, public_input, result_check, instance_input, must_pass);
130+
if (expected_to_pass) {
131+
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
132+
component_instance, public_input, result_check, instance_input);
133+
} else {
134+
crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
135+
component_instance, public_input, result_check, instance_input);
136+
}
132137
}
133138

134139
template<typename FieldType, typename NonNativeCurveType>
135140
void test_bool_scalar_multiplication_usable (typename NonNativeCurveType::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type point,
136-
typename FieldType::value_type scalar_bool,
137-
bool must_pass = true) {
141+
typename FieldType::value_type scalar_bool, bool expected_to_pass) {
138142

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

151-
test_bool_scalar_multiplication<FieldType, NonNativeCurveType>(public_input, expected_res, must_pass);
155+
test_bool_scalar_multiplication<FieldType, NonNativeCurveType>(public_input, expected_res, expected_to_pass);
152156
}
153157

154158
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) {
164168
boost::random::mt19937 seed_seq;
165169
rand.seed(seed_seq);
166170

167-
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 1);
168-
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 0);
171+
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 1, true);
172+
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>({0,1}, 0, true);
169173

170174
for (std::size_t i = 0; i < random_tests_amount; i++) {
171-
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 1);
172-
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 0);
175+
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 1, true);
176+
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), 0, true);
173177
}
174178
}
175179

test/non_native/plonk/scalar_non_native_range.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
using namespace nil;
4545

4646
template <typename BlueprintFieldType>
47-
void test_scalar_non_native_range(std::vector<typename BlueprintFieldType::value_type> public_input, bool must_pass = true){
47+
void test_scalar_non_native_range(std::vector<typename BlueprintFieldType::value_type> public_input,
48+
bool expected_to_pass){
4849

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

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

77-
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
78-
component_instance, public_input, result_check, instance_input, must_pass);
78+
if (expected_to_pass) {
79+
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
80+
component_instance, public_input, result_check, instance_input);
81+
} else {
82+
crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
83+
component_instance, public_input, result_check, instance_input);
84+
}
7985
}
8086

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

8591
BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test0) {
8692
test_scalar_non_native_range<typename crypto3::algebra::curves::pallas::base_field_type>(
87-
{45524});
93+
{45524}, true);
8894
}
8995

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

97103
test_scalar_non_native_range<field_type>(
98-
{typename field_type::value_type(ed25519_scalar_modulus-1)});
104+
{typename field_type::value_type(ed25519_scalar_modulus-1)}, true);
99105

100106
test_scalar_non_native_range<field_type>(
101-
{typename field_type::value_type(ones)});
107+
{typename field_type::value_type(ones)}, true);
102108

103-
test_scalar_non_native_range<field_type>({1});
109+
test_scalar_non_native_range<field_type>({1}, true);
104110

105-
test_scalar_non_native_range<field_type>({0});
111+
test_scalar_non_native_range<field_type>({0}, true);
106112

107113
nil::crypto3::random::algebraic_engine<field_type> rand;
108114
boost::random::mt19937 seed_seq;
@@ -117,7 +123,7 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test1) {
117123
r_integral = typename field_type::integral_type(r.data);
118124
r_integral = r_integral % ed25519_scalar_modulus;
119125
r = typename field_type::value_type(r_integral);
120-
test_scalar_non_native_range<field_type>({r});
126+
test_scalar_non_native_range<field_type>({r}, true);
121127
}
122128
}
123129

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

137143
for (std::size_t i = 0; i < random_tests_amount; i++) {
138144
overage = (typename field_type::integral_type(rand().data)) % ed25519_scalar_overage;
139-
test_scalar_non_native_range<field_type>({typename field_type::value_type(ed25519_scalar_modulus + overage)}, false);
145+
test_scalar_non_native_range<field_type>({typename field_type::value_type(ed25519_scalar_modulus + overage)}, false); // false positive
140146
}
141147
test_scalar_non_native_range<field_type>({-1}, false);
142148
}

0 commit comments

Comments
 (0)