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

Commit c63077b

Browse files
committed
rewrite tests for non native field operations #107
1 parent 2ff2d19 commit c63077b

File tree

4 files changed

+337
-48
lines changed

4 files changed

+337
-48
lines changed

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

Lines changed: 90 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,32 @@
2828

2929
#include <boost/test/unit_test.hpp>
3030

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>
3134
#include <nil/crypto3/algebra/curves/pallas.hpp>
3235
#include <nil/crypto3/algebra/fields/arithmetic_params/pallas.hpp>
3336

3437
#include <nil/crypto3/algebra/curves/ed25519.hpp>
3538
#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>
3740

3841
#include <nil/crypto3/hash/keccak.hpp>
3942

4043
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
4144
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
4245
#include <nil/blueprint/components/algebra/fields/plonk/non_native/addition.hpp>
4346

47+
#include <../test/algebra/fields/plonk/non_native/chop_and_glue_non_native.hpp>
48+
4449
#include "../../../../test_plonk_component.hpp"
4550

4651
using namespace nil;
4752

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

51-
using ed25519_type = crypto3::algebra::curves::ed25519;
5257
constexpr std::size_t WitnessColumns = 9;
5358
constexpr std::size_t PublicInputColumns = 1;
5459
constexpr std::size_t ConstantColumns = 0;
@@ -63,7 +68,7 @@ void test_field_add(std::vector<typename BlueprintFieldType::value_type> public_
6368
using var = crypto3::zk::snark::plonk_variable<BlueprintFieldType>;
6469

6570
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>>;
6772

6873
std::array<var, 4> input_var_a = {
6974
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_
7479

7580
typename component_type::input_type instance_input = {input_var_a, input_var_b};
7681

77-
auto result_check = [](AssignmentType &assignment,
82+
auto result_check = [&expected_res, public_input](AssignmentType &assignment,
7883
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+
}
79116
};
80117

81118
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_
84121
component_instance, public_input, result_check, instance_input);
85122
}
86123

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);
92132
}
93133

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+
}
97167

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

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>();
111175
}
112176

113177
BOOST_AUTO_TEST_SUITE_END()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
template<typename FieldType, typename NonNativeFieldType>
2+
std::array<typename FieldType::value_type, 4> chop_non_native(typename NonNativeFieldType::value_type input) {
3+
typename NonNativeFieldType::integral_type input_integral =
4+
typename NonNativeFieldType::integral_type(
5+
input.data);
6+
7+
std::array<typename FieldType::value_type, 4> output;
8+
9+
typename NonNativeFieldType::integral_type base = 1;
10+
typename NonNativeFieldType::integral_type mask = (base << 66) - 1;
11+
12+
output[0] = input_integral & mask;
13+
output[1] = (input_integral >> 66) & mask;
14+
output[2] = (input_integral >> 132) & mask;
15+
output[3] = (input_integral >> 198) & mask;
16+
17+
return output;
18+
}
19+
20+
template<typename FieldType, typename NonNativeFieldType>
21+
typename NonNativeFieldType::value_type glue_non_native(std::array<typename FieldType::value_type, 4> input) {
22+
typename NonNativeFieldType::integral_type base = 1;
23+
typename NonNativeFieldType::integral_type chunk_size = (base << 66);
24+
25+
std::array<typename FieldType::integral_type, 4> input_integral;
26+
27+
for (std::size_t i = 0; i < input.size(); i++) {
28+
assert(input[i] < chunk_size);
29+
input_integral[i] = typename FieldType::integral_type(input[i].data);
30+
}
31+
32+
typename NonNativeFieldType::integral_type output_integral
33+
= input_integral[0] + (input_integral[1] << 66) + (input_integral[2] << 132) + (input_integral[3] << 198);
34+
35+
typename NonNativeFieldType::value_type output = typename NonNativeFieldType::value_type (output_integral);
36+
37+
return output;
38+
}
39+
40+
template <typename FieldType, typename NonNativeFieldType>
41+
std::vector<typename FieldType::value_type> create_public_input(std::array<typename FieldType::value_type, 4> a, std::array<typename FieldType::value_type, 4> b) {
42+
std::vector<typename FieldType::value_type> public_input;
43+
for (std::size_t i = 0; i < a.size(); i++){
44+
public_input.push_back(a[i]);
45+
}
46+
for (std::size_t i = 0; i < b.size(); i++){
47+
public_input.push_back(b[i]);
48+
}
49+
return public_input;
50+
}

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

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@
4242
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
4343
#include <nil/blueprint/components/algebra/fields/plonk/non_native/multiplication.hpp>
4444

45+
#include <nil/crypto3/random/algebraic_engine.hpp>
46+
#include <../test/algebra/fields/plonk/non_native/chop_and_glue_non_native.hpp>
47+
4548
#include "../../../../test_plonk_component.hpp"
4649

4750
using namespace nil;
4851

49-
template <typename BlueprintFieldType>
50-
void test_field_mul(std::vector<typename BlueprintFieldType::value_type> public_input){
51-
52-
using ed25519_type = crypto3::algebra::curves::ed25519;
52+
template <typename BlueprintFieldType, typename NonNativeFieldType>
53+
void test_field_mul(std::vector<typename BlueprintFieldType::value_type> public_input,
54+
std::array<typename BlueprintFieldType::value_type, 4> expected_res){
55+
5356
constexpr std::size_t WitnessColumns = 9;
5457
constexpr std::size_t PublicInputColumns = 1;
5558
constexpr std::size_t ConstantColumns = 0;
@@ -64,7 +67,7 @@ void test_field_mul(std::vector<typename BlueprintFieldType::value_type> public_
6467
using var = crypto3::zk::snark::plonk_variable<BlueprintFieldType>;
6568

6669
using component_type = blueprint::components::multiplication<ArithmetizationType,
67-
typename ed25519_type::base_field_type, 9, blueprint::basic_non_native_policy<BlueprintFieldType>>;
70+
NonNativeFieldType, 9, blueprint::basic_non_native_policy<BlueprintFieldType>>;
6871

6972
std::array<var, 4> input_var_a = {
7073
var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input),
@@ -75,8 +78,41 @@ void test_field_mul(std::vector<typename BlueprintFieldType::value_type> public_
7578

7679
typename component_type::input_type instance_input = {input_var_a, input_var_b};
7780

78-
auto result_check = [](AssignmentType &assignment,
81+
auto result_check = [&expected_res, public_input](AssignmentType &assignment,
7982
typename component_type::result_type &real_res) {
83+
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+
}
80116
};
81117

82118
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{});
@@ -87,10 +123,65 @@ void test_field_mul(std::vector<typename BlueprintFieldType::value_type> public_
87123

88124
BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
89125

90-
BOOST_AUTO_TEST_CASE(blueprint_non_native_multiplication_test0) {
91-
test_field_mul<typename crypto3::algebra::curves::pallas::base_field_type>(
92-
{0xc801afd_cppui255, 0xc801afd_cppui255, 0xc801afd_cppui255, 0xc801afd_cppui255,
93-
0xc801afd_cppui255, 0xc801afd_cppui255, 0xc801afd_cppui255, 0xc801afd_cppui255});
126+
template <typename FieldType, typename NonNativeFieldType>
127+
void test_field_mul_useable(typename NonNativeFieldType::value_type a, typename NonNativeFieldType::value_type b){
128+
using chunked_non_native_type = std::array<typename FieldType::value_type, 4>;
129+
chunked_non_native_type first = chop_non_native<FieldType, NonNativeFieldType>(a);
130+
chunked_non_native_type second = chop_non_native<FieldType, NonNativeFieldType>(b);
131+
chunked_non_native_type expected_result = chop_non_native<FieldType, NonNativeFieldType>(a * b);
132+
std::vector<typename FieldType::value_type> public_input = create_public_input<FieldType, NonNativeFieldType>(first, second);
133+
test_field_mul<FieldType, NonNativeFieldType>(public_input, expected_result);
134+
}
135+
136+
template <typename FieldType, typename NonNativeFieldType>
137+
void test_field_mul_all_cases(){
138+
nil::crypto3::random::algebraic_engine<NonNativeFieldType> rand;
139+
boost::random::mt19937 seed_seq;
140+
rand.seed(seed_seq);
141+
142+
typename NonNativeFieldType::value_type f = 0xf;
143+
typename NonNativeFieldType::integral_type f_integral;
144+
for (std::size_t i = 0; i < 63; i++) {
145+
f_integral = typename NonNativeFieldType::integral_type(f.data);
146+
f_integral = (f_integral << 4) + 0xf;
147+
f = typename NonNativeFieldType::value_type(f_integral);
148+
test_field_mul_useable<FieldType, NonNativeFieldType>(f, f);
149+
}
150+
151+
test_field_mul_useable<FieldType, NonNativeFieldType>(0, 0);
152+
test_field_mul_useable<FieldType, NonNativeFieldType>(1, 1);
153+
test_field_mul_useable<FieldType, NonNativeFieldType>(-1, -1);
154+
test_field_mul_useable<FieldType, NonNativeFieldType>(1, -1);
155+
test_field_mul_useable<FieldType, NonNativeFieldType>(-1, 0);
156+
test_field_mul_useable<FieldType, NonNativeFieldType>(1000, -1000);
157+
test_field_mul_useable<FieldType, NonNativeFieldType>(
158+
glue_non_native<FieldType, NonNativeFieldType>({45524, 52353, 68769, 5431}),
159+
glue_non_native<FieldType, NonNativeFieldType>({3724, 342453, 5425, 54222}));
160+
161+
test_field_mul_useable<FieldType, NonNativeFieldType>(
162+
glue_non_native<FieldType, NonNativeFieldType>({1,1,1,1}),
163+
glue_non_native<FieldType, NonNativeFieldType>({1,1,1,1}));
164+
165+
test_field_mul_useable<FieldType, NonNativeFieldType>(
166+
glue_non_native<FieldType, NonNativeFieldType>({1,0,0,0}),
167+
glue_non_native<FieldType, NonNativeFieldType>({1,0,0,0}));
168+
169+
test_field_mul_useable<FieldType, NonNativeFieldType>(
170+
glue_non_native<FieldType, NonNativeFieldType>({0x2BCA8C5A0FDF3D53E_cppui253, 0x39840DDF4C421B2D5_cppui253, 0x24FCE5728D26931CA_cppui253, 0xFBD6153B4CE63_cppui253}),
171+
glue_non_native<FieldType, NonNativeFieldType>({0x3CD7BA9506A76AA1C_cppui253, 0x15C58810F101DDB2F_cppui253, 0x1AA5750251F6DA658_cppui253, 0x1323F61B67242F_cppui253}));
172+
173+
test_field_mul_useable<FieldType, NonNativeFieldType>(
174+
glue_non_native<FieldType, NonNativeFieldType>({0xc801afd_cppui255, 0xc801afd_cppui255, 0xc801afd_cppui255, 0xc801afd_cppui255}),
175+
glue_non_native<FieldType, NonNativeFieldType>({0xc801afd_cppui255, 0xc801afd_cppui255, 0xc801afd_cppui255, 0xc801afd_cppui255}));
176+
for (std::size_t i = 0; i < 10; i++) {
177+
test_field_mul_useable<FieldType, NonNativeFieldType>(rand(), rand());
178+
}
179+
}
180+
181+
BOOST_AUTO_TEST_CASE(blueprint_non_native_multiplication_pallas) {
182+
using non_native_field_type = typename crypto3::algebra::fields::curve25519_base_field;
183+
using field_type = crypto3::algebra::curves::pallas::base_field_type;
184+
test_field_mul_all_cases<field_type, non_native_field_type>();
94185
}
95186

96187
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)