28
28
29
29
#include < boost/test/unit_test.hpp>
30
30
31
+ #include < nil/crypto3/algebra/fields/bls12/scalar_field.hpp>
32
+ #include < nil/crypto3/algebra/curves/vesta.hpp>
33
+ #include < nil/crypto3/algebra/fields/arithmetic_params/vesta.hpp>
31
34
#include < nil/crypto3/algebra/curves/pallas.hpp>
32
35
#include < nil/crypto3/algebra/fields/arithmetic_params/pallas.hpp>
33
36
34
37
#include < nil/crypto3/algebra/curves/ed25519.hpp>
35
38
#include < nil/crypto3/algebra/fields/arithmetic_params/ed25519.hpp>
36
- #include < nil/crypto3/algebra/random_element .hpp>
39
+ #include < nil/crypto3/random/algebraic_engine .hpp>
37
40
38
41
#include < nil/crypto3/hash/keccak.hpp>
39
42
40
43
#include < nil/blueprint/blueprint/plonk/circuit.hpp>
41
44
#include < nil/blueprint/blueprint/plonk/assignment.hpp>
42
45
#include < nil/blueprint/components/algebra/fields/plonk/non_native/addition.hpp>
43
46
47
+ #include < ../test/algebra/fields/plonk/non_native/chop_and_glue_non_native.hpp>
48
+
44
49
#include " ../../../../test_plonk_component.hpp"
45
50
46
51
using namespace nil ;
47
52
48
- template <typename BlueprintFieldType>
49
- void test_field_add (std::vector<typename BlueprintFieldType::value_type> public_input){
53
+ template <typename BlueprintFieldType, typename NonNativeFieldType>
54
+ void test_field_add (std::vector<typename BlueprintFieldType::value_type> public_input,
55
+ std::array<typename BlueprintFieldType::value_type, 4 > expected_res){
50
56
51
- using ed25519_type = crypto3::algebra::curves::ed25519;
52
57
constexpr std::size_t WitnessColumns = 9 ;
53
58
constexpr std::size_t PublicInputColumns = 1 ;
54
59
constexpr std::size_t ConstantColumns = 0 ;
@@ -63,7 +68,7 @@ void test_field_add(std::vector<typename BlueprintFieldType::value_type> public_
63
68
using var = crypto3::zk::snark::plonk_variable<BlueprintFieldType>;
64
69
65
70
using component_type = blueprint::components::addition<ArithmetizationType,
66
- typename ed25519_type::base_field_type , 9 , blueprint::basic_non_native_policy<BlueprintFieldType>>;
71
+ NonNativeFieldType , 9 , blueprint::basic_non_native_policy<BlueprintFieldType>>;
67
72
68
73
std::array<var, 4 > input_var_a = {
69
74
var (0 , 0 , false , var::column_type::public_input), var (0 , 1 , false , var::column_type::public_input),
@@ -74,8 +79,40 @@ void test_field_add(std::vector<typename BlueprintFieldType::value_type> public_
74
79
75
80
typename component_type::input_type instance_input = {input_var_a, input_var_b};
76
81
77
- auto result_check = [](AssignmentType &assignment,
82
+ auto result_check = [&expected_res, public_input ](AssignmentType &assignment,
78
83
typename component_type::result_type &real_res) {
84
+ #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED
85
+ std::array<typename BlueprintFieldType::value_type, 4 > x, y, expected_chunks, real_chunks;
86
+ for (std::size_t i = 0 ; i < 4 ; i++) {
87
+ x[i] = public_input[i];
88
+ y[i] = public_input[i+4 ];
89
+ expected_chunks[i] = expected_res[i];
90
+ real_chunks[i] = var_value (assignment, real_res.output [i]);
91
+ }
92
+
93
+ std::cout << std::hex;
94
+
95
+ std::cout << " _________________________________________________________________________________________________________________________________________________\n " ;
96
+ std::cout << " input : " ;
97
+ for (std::size_t i = 0 ; i < 4 ; i++) {std::cout << x[3 -i].data << " " ;}
98
+ std::cout << " (" << glue_non_native<BlueprintFieldType, NonNativeFieldType>(x).data << " )\n " ;
99
+
100
+ std::cout << " " ;
101
+ for (std::size_t i = 0 ; i < 4 ; i++) {std::cout << y[3 -i].data << " " ;}
102
+ std::cout << " (" << glue_non_native<BlueprintFieldType, NonNativeFieldType>(y).data << " )\n " ;
103
+
104
+ std::cout << " expected: " ;
105
+ for (std::size_t i = 0 ; i < 4 ; i++) {std::cout << expected_chunks[3 -i].data << " " ;}
106
+ std::cout << " (" << glue_non_native<BlueprintFieldType, NonNativeFieldType>(expected_chunks).data << " )\n " ;
107
+
108
+ std::cout << " real : " ;
109
+ for (std::size_t i = 0 ; i < 4 ; i++) {std::cout << real_chunks[3 -i].data << " " ;}
110
+ std::cout << " (" << glue_non_native<BlueprintFieldType, NonNativeFieldType>(real_chunks).data << " )\n " ;
111
+ #endif
112
+
113
+ for (std::size_t i = 0 ; i < 4 ; i++) {
114
+ assert (expected_res[i] == var_value (assignment, real_res.output [i]));
115
+ }
79
116
};
80
117
81
118
component_type component_instance ({0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },{},{});
@@ -84,30 +121,57 @@ void test_field_add(std::vector<typename BlueprintFieldType::value_type> public_
84
121
component_instance, public_input, result_check, instance_input);
85
122
}
86
123
87
- BOOST_AUTO_TEST_SUITE (blueprint_plonk_test_suite)
88
-
89
- BOOST_AUTO_TEST_CASE(blueprint_non_native_addition_test0) {
90
- test_field_add<typename crypto3::algebra::curves::pallas::base_field_type>(
91
- {45524 , 52353 , 68769 , 5431 , 3724 , 342453 , 5425 , 54222 });
124
+ template <typename FieldType, typename NonNativeFieldType>
125
+ void test_field_add_useable (typename NonNativeFieldType::value_type a, typename NonNativeFieldType::value_type b){
126
+ using chunked_non_native_type = std::array<typename FieldType::value_type, 4 >;
127
+ chunked_non_native_type first = chop_non_native<FieldType, NonNativeFieldType>(a);
128
+ chunked_non_native_type second = chop_non_native<FieldType, NonNativeFieldType>(b);
129
+ chunked_non_native_type expected_result = chop_non_native<FieldType, NonNativeFieldType>(a + b);
130
+ std::vector<typename FieldType::value_type> public_input = create_public_input<FieldType, NonNativeFieldType>(first, second);
131
+ test_field_add<FieldType, NonNativeFieldType>(public_input, expected_result);
92
132
}
93
133
94
- BOOST_AUTO_TEST_CASE (blueprint_non_native_addition_test1) {
95
-
96
- using ed25519_type = crypto3::algebra::curves::ed25519;
134
+ template <typename FieldType, typename NonNativeFieldType>
135
+ void test_field_add_all_cases (){
136
+ nil::crypto3::random::algebraic_engine<NonNativeFieldType> rand;
137
+ boost::random::mt19937 seed_seq;
138
+ rand.seed (seed_seq);
139
+
140
+ typename NonNativeFieldType::value_type f = 0xf ;
141
+ typename NonNativeFieldType::integral_type f_integral;
142
+ for (std::size_t i = 0 ; i < 63 ; i++) {
143
+ f_integral = typename NonNativeFieldType::integral_type (f.data );
144
+ f_integral = (f_integral << 4 ) + 0xf ;
145
+ f = typename NonNativeFieldType::value_type (f_integral);
146
+ test_field_add_useable<FieldType, NonNativeFieldType>(f, f);
147
+ }
148
+
149
+
150
+ test_field_add_useable<FieldType, NonNativeFieldType>(0 , 0 );
151
+ test_field_add_useable<FieldType, NonNativeFieldType>(1 , 1 );
152
+ test_field_add_useable<FieldType, NonNativeFieldType>(-1 , -1 );
153
+ test_field_add_useable<FieldType, NonNativeFieldType>(1 , -1 );
154
+ test_field_add_useable<FieldType, NonNativeFieldType>(-1 , 0 );
155
+ test_field_add_useable<FieldType, NonNativeFieldType>(1000 , -1000 );
156
+ test_field_add_useable<FieldType, NonNativeFieldType>(
157
+ glue_non_native<FieldType, NonNativeFieldType>({45524 , 52353 , 68769 , 5431 }),
158
+ glue_non_native<FieldType, NonNativeFieldType>({3724 , 342453 , 5425 , 54222 }));
159
+
160
+ test_field_add_useable<FieldType, NonNativeFieldType>(
161
+ glue_non_native<FieldType, NonNativeFieldType>({1 ,1 ,1 ,1 }),
162
+ glue_non_native<FieldType, NonNativeFieldType>({1 ,1 ,1 ,1 }));
163
+
164
+ for (std::size_t i = 0 ; i < 10 ; i++) {
165
+ test_field_add_useable<FieldType, NonNativeFieldType>(rand (), rand ());
166
+ }
97
167
98
- typename ed25519_type::base_field_type::integral_type a =
99
- ed25519_type::base_field_type::integral_type (
100
- crypto3::algebra::random_element<ed25519_type::base_field_type>().data );
101
- typename ed25519_type::base_field_type::integral_type b =
102
- ed25519_type::base_field_type::integral_type (
103
- crypto3::algebra::random_element<ed25519_type::base_field_type>().data );
104
-
105
- typename ed25519_type::base_field_type::integral_type base = 1 ;
106
- typename ed25519_type::base_field_type::integral_type mask = (base << 66 ) - 1 ;
168
+ }
169
+ BOOST_AUTO_TEST_SUITE (blueprint_plonk_test_suite)
107
170
108
- test_field_add<typename crypto3::algebra::curves::pallas::base_field_type>(
109
- {a & mask, (a >> 66 ) & mask, (a >> 132 ) & mask, (a >> 198 ) & mask,
110
- b & mask, (b >> 66 ) & mask, (b >> 132 ) & mask, (b >> 198 ) & mask});
171
+ BOOST_AUTO_TEST_CASE(blueprint_non_native_addition_pallas) {
172
+ using non_native_field_type = typename crypto3::algebra::fields::curve25519_base_field;
173
+ using field_type = crypto3::algebra::curves::pallas::base_field_type;
174
+ test_field_add_all_cases<field_type, non_native_field_type>();
111
175
}
112
176
113
177
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments