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

Commit 2ff2d19

Browse files
committed
rewrite tests, add bls12 field, update random elements generation #107
1 parent fc390d1 commit 2ff2d19

File tree

8 files changed

+701
-232
lines changed

8 files changed

+701
-232
lines changed

include/nil/blueprint/components/algebra/fields/plonk/exponentiation.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ namespace nil {
124124

125125
};
126126

127-
template<typename BlueprintFieldType, typename ArithmetizationParams, std::size_t ExponentSize, std::int32_t WitnessesAmount>
127+
template<typename BlueprintFieldType, typename ArithmetizationParams, std::size_t ExponentSize, std::uint32_t WitnessesAmount>
128128
using plonk_exponentiation =
129129
exponentiation<
130130
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,

test/algebra/curves/plonk/endo_scalar.cpp

Lines changed: 140 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <nil/crypto3/algebra/fields/arithmetic_params/pallas.hpp>
3434
#include <nil/crypto3/algebra/curves/vesta.hpp>
3535
#include <nil/crypto3/algebra/fields/arithmetic_params/vesta.hpp>
36-
#include <nil/crypto3/algebra/random_element.hpp>
36+
#include <nil/crypto3/random/algebraic_engine.hpp>
3737

3838
#include <nil/crypto3/hash/algorithm/hash.hpp>
3939
#include <nil/crypto3/hash/sha2.hpp>
@@ -47,9 +47,94 @@
4747

4848
#include "test_plonk_component.hpp"
4949

50+
template<typename CurveType>
51+
struct endo_scalar_params;
52+
53+
template<>
54+
struct endo_scalar_params<nil::crypto3::algebra::curves::vesta> {
55+
using curve_type = nil::crypto3::algebra::curves::vesta;
56+
using scalar_field_type = typename curve_type::scalar_field_type;
57+
using base_field_type = typename curve_type::base_field_type;
58+
constexpr static const typename scalar_field_type::value_type endo_r =
59+
0x12CCCA834ACDBA712CAAD5DC57AAB1B01D1F8BD237AD31491DAD5EBDFDFE4AB9_cppui255;
60+
constexpr static const typename base_field_type::value_type endo_q =
61+
0x2D33357CB532458ED3552A23A8554E5005270D29D19FC7D27B7FD22F0201B547_cppui255;
62+
};
63+
64+
template<>
65+
struct endo_scalar_params<nil::crypto3::algebra::curves::pallas> {
66+
using curve_type = nil::crypto3::algebra::curves::pallas;
67+
using scalar_field_type = typename curve_type::scalar_field_type;
68+
using base_field_type = typename curve_type::base_field_type;
69+
constexpr static const typename scalar_field_type::value_type endo_r =
70+
0x397E65A7D7C1AD71AEE24B27E308F0A61259527EC1D4752E619D1840AF55F1B1_cppui255;
71+
constexpr static const typename base_field_type::value_type endo_q =
72+
0x2D33357CB532458ED3552A23A8554E5005270D29D19FC7D27B7FD22F0201B547_cppui255;
73+
};
74+
75+
template<typename CurveType, std::size_t ScalarSize>
76+
typename CurveType::scalar_field_type::value_type calculate_endo_scalar(typename CurveType::scalar_field_type::value_type scalar) {
77+
78+
using endo_params = endo_scalar_params<CurveType>;
79+
using BlueprintFieldType = typename CurveType::scalar_field_type;
80+
81+
typename BlueprintFieldType::value_type endo_r = endo_params::endo_r;
82+
83+
const std::size_t crumbs_per_row = 8;
84+
const std::size_t bits_per_crumb = 2;
85+
const std::size_t bits_per_row =
86+
bits_per_crumb * crumbs_per_row; // we suppose that ScalarSize % bits_per_row = 0
87+
88+
typename BlueprintFieldType::integral_type integral_scalar =
89+
typename BlueprintFieldType::integral_type(scalar.data);
90+
std::array<bool, ScalarSize> bits_msb;
91+
{
92+
nil::marshalling::status_type status;
93+
assert(ScalarSize <= 255);
94+
std::array<bool, 255> bits_msb_all =
95+
nil::marshalling::pack<nil::marshalling::option::big_endian>(integral_scalar, status);
96+
assert(status == nil::marshalling::status_type::success);
97+
std::copy(bits_msb_all.end() - ScalarSize, bits_msb_all.end(), bits_msb.begin());
98+
99+
for(std::size_t i = 0; i < 255 - ScalarSize; ++i) {
100+
assert(bits_msb_all[i] == false);
101+
}
102+
}
103+
typename BlueprintFieldType::value_type a = 2;
104+
typename BlueprintFieldType::value_type b = 2;
105+
typename BlueprintFieldType::value_type n = 0;
106+
107+
assert (ScalarSize % bits_per_row == 0);
108+
for (std::size_t chunk_start = 0; chunk_start < bits_msb.size(); chunk_start += bits_per_row) {
109+
for (std::size_t j = 0; j < crumbs_per_row; j++) {
110+
std::size_t crumb = chunk_start + j * bits_per_crumb;
111+
typename BlueprintFieldType::value_type b0 = static_cast<int>(bits_msb[crumb + 1]);
112+
typename BlueprintFieldType::value_type b1 = static_cast<int>(bits_msb[crumb + 0]);
113+
114+
typename BlueprintFieldType::value_type crumb_value = b0 + b1.doubled();
115+
116+
a = a.doubled();
117+
b = b.doubled();
118+
119+
typename BlueprintFieldType::value_type s =
120+
(b0 == BlueprintFieldType::value_type::one()) ? 1 : -1;
121+
if (b1 == BlueprintFieldType::value_type::zero()) {
122+
b += s;
123+
} else {
124+
a += s;
125+
}
126+
127+
n = (n.doubled()).doubled();
128+
n += crumb_value;
129+
}
130+
}
131+
auto res = a * endo_r + b;
132+
return res;
133+
}
134+
50135
template <typename CurveType>
51136
void test_endo_scalar(std::vector<typename CurveType::scalar_field_type::value_type> public_input,
52-
typename CurveType::scalar_field_type::value_type expected_res){
137+
typename CurveType::scalar_field_type::value_type expected_res){
53138
using BlueprintFieldType = typename CurveType::scalar_field_type;
54139
constexpr std::size_t WitnessColumns = 15;
55140
constexpr std::size_t PublicInputColumns = 1;
@@ -70,8 +155,14 @@ void test_endo_scalar(std::vector<typename CurveType::scalar_field_type::value_t
70155
var challenge_var(0, 0, false, var::column_type::public_input);
71156
typename component_type::input_type instance_input = {challenge_var};
72157

73-
auto result_check = [&expected_res](AssignmentType &assignment,
158+
auto result_check = [&expected_res, public_input](AssignmentType &assignment,
74159
typename component_type::result_type &real_res) {
160+
#ifdef BLUEPRINT_PLONK_PROFILING_ENABLED
161+
std::cout << "endo_scalar input: " << std::hex << public_input[0].data << "\n";
162+
std::cout << "expected result : " << std::hex << expected_res.data << "\n";
163+
std::cout << "real result : " << std::hex << var_value(assignment, real_res.output).data << "\n\n";
164+
#endif
165+
75166
assert(expected_res == var_value(assignment, real_res.output));
76167
};
77168

@@ -80,18 +171,59 @@ void test_endo_scalar(std::vector<typename CurveType::scalar_field_type::value_t
80171
nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda> (component_instance, public_input, result_check, instance_input);
81172
}
82173

174+
constexpr static const std::size_t random_tests_amount = 10;
175+
83176
BOOST_AUTO_TEST_SUITE(blueprint_plonk_endo_scalar_test_suite)
84177

85-
BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_addition) {
178+
BOOST_AUTO_TEST_CASE(blueprint_plonk_endo_scalar_vesta) {
86179
using curve_type = nil::crypto3::algebra::curves::vesta;
87180
using BlueprintFieldType = typename curve_type::scalar_field_type;
88-
181+
89182
typename BlueprintFieldType::value_type challenge = 0x00000000000000000000000000000000FC93536CAE0C612C18FBE5F6D8E8EEF2_cppui255;
90183
typename BlueprintFieldType::value_type result = 0x004638173549A4C55A118327904B54E5F6F6314225C8C862F5AFA2506C77AC65_cppui255;
91184

92-
std::vector<typename BlueprintFieldType::value_type> public_input = {challenge};
93-
94-
test_endo_scalar<curve_type>(public_input, result);
185+
test_endo_scalar<curve_type>({challenge}, result);
186+
test_endo_scalar<curve_type>({1}, calculate_endo_scalar<curve_type, 128>(1));
187+
test_endo_scalar<curve_type>({0}, calculate_endo_scalar<curve_type, 128>(0));
188+
test_endo_scalar<curve_type>({0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255},
189+
calculate_endo_scalar<curve_type, 128>(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255));
190+
191+
nil::crypto3::random::algebraic_engine<curve_type::scalar_field_type> generate_random;
192+
boost::random::mt19937 seed_seq;
193+
generate_random.seed(seed_seq);
194+
195+
for (std::size_t i = 0; i < random_tests_amount; i++){
196+
typename curve_type::scalar_field_type::value_type input = generate_random();
197+
typename curve_type::scalar_field_type::integral_type input_integral = typename curve_type::scalar_field_type::integral_type(input.data);
198+
input_integral = input_integral & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255;
199+
typename curve_type::scalar_field_type::value_type input_scalar = input_integral;
200+
test_endo_scalar<curve_type>({input_scalar}, calculate_endo_scalar<curve_type, 128>(input_scalar));
201+
}
202+
}
203+
204+
BOOST_AUTO_TEST_CASE(blueprint_plonk_endo_scalar_pallas) {
205+
using curve_type = nil::crypto3::algebra::curves::pallas;
206+
using BlueprintFieldType = typename curve_type::scalar_field_type;
207+
208+
typename BlueprintFieldType::value_type challenge = 0x00000000000000000000000000000000FC93536CAE0C612C18FBE5F6D8E8EEF2_cppui255;
209+
210+
test_endo_scalar<curve_type>({challenge}, calculate_endo_scalar<curve_type, 128>(challenge));
211+
test_endo_scalar<curve_type>({1}, calculate_endo_scalar<curve_type, 128>(1));
212+
test_endo_scalar<curve_type>({0}, calculate_endo_scalar<curve_type, 128>(0));
213+
test_endo_scalar<curve_type>({0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255},
214+
calculate_endo_scalar<curve_type, 128>(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255));
215+
216+
nil::crypto3::random::algebraic_engine<curve_type::scalar_field_type> generate_random;
217+
boost::random::mt19937 seed_seq;
218+
generate_random.seed(seed_seq);
219+
220+
for (std::size_t i = 0; i < random_tests_amount; i++){
221+
typename curve_type::scalar_field_type::value_type input = generate_random();
222+
typename curve_type::scalar_field_type::integral_type input_integral = typename curve_type::scalar_field_type::integral_type(input.data);
223+
input_integral = input_integral & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255;
224+
typename curve_type::scalar_field_type::value_type input_scalar = input_integral;
225+
test_endo_scalar<curve_type>({input_scalar}, calculate_endo_scalar<curve_type, 128>(input_scalar));
226+
}
95227
}
96228

97229
BOOST_AUTO_TEST_SUITE_END()

test/algebra/curves/plonk/unified_addition.cpp

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

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

31+
#include <nil/crypto3/algebra/curves/vesta.hpp>
32+
#include <nil/crypto3/algebra/fields/arithmetic_params/vesta.hpp>
3133
#include <nil/crypto3/algebra/curves/pallas.hpp>
3234
#include <nil/crypto3/algebra/fields/arithmetic_params/pallas.hpp>
33-
#include <nil/crypto3/algebra/random_element.hpp>
35+
#include <nil/crypto3/random/algebraic_engine.hpp>
3436

3537
#include <nil/crypto3/hash/keccak.hpp>
3638

@@ -70,8 +72,15 @@ void test_unified_addition(std::vector<typename CurveType::base_field_type::valu
7072
{var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input)},
7173
{var(0, 2, false, var::column_type::public_input), var(0, 3, false, var::column_type::public_input)}};
7274

73-
auto result_check = [&expected_res](AssignmentType &assignment,
75+
auto result_check = [&expected_res, public_input](AssignmentType &assignment,
7476
typename component_type::result_type &real_res) {
77+
#ifdef BLUEPRINT_PLONK_PROFILING_ENABLED
78+
std::cout << "unified_addition test: " << "\n";
79+
std::cout << "input : " << public_input[0].data << " " << public_input[1].data << "\n";
80+
std::cout << "input : " << public_input[2].data << " " << public_input[3].data << "\n";
81+
std::cout << "expected: " << expected_res.X.data << " " << expected_res.Y.data << "\n";
82+
std::cout << "real : " << var_value(assignment, real_res.X).data << " " << var_value(assignment, real_res.Y).data << "\n\n";
83+
#endif
7584
assert(expected_res.X == var_value(assignment, real_res.X));
7685
assert(expected_res.Y == var_value(assignment, real_res.Y));
7786
};
@@ -82,48 +91,105 @@ void test_unified_addition(std::vector<typename CurveType::base_field_type::valu
8291
component_instance, public_input, result_check, instance_input);
8392
}
8493

85-
BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
94+
template<typename CurveType>
95+
void test_unified_addition_with_zeroes() {
96+
nil::crypto3::random::algebraic_engine<typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>> generate_random_point;
97+
boost::random::mt19937 seed_seq;
98+
generate_random_point.seed(seed_seq);
8699

87-
BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_double) {
100+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type zero_algebraic = {0, 1};
101+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type zero_circuits = {0, 0};
102+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type P = generate_random_point();
103+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type Q = -P;
88104

89-
using curve_type = crypto3::algebra::curves::pallas;
105+
std::vector<typename CurveType::base_field_type::value_type> public_input;
106+
107+
public_input = {zero_circuits.X, zero_circuits.Y, zero_circuits.X, zero_circuits.Y};
108+
test_unified_addition<CurveType>(public_input, zero_circuits);
109+
110+
public_input = {zero_circuits.X, zero_circuits.Y, P.X, P.Y};
111+
test_unified_addition<CurveType>(public_input, P);
90112

91-
auto P = crypto3::algebra::random_element<curve_type::template g1_type<>>().to_affine();
92-
auto Q(P);
113+
public_input = {P.X, P.Y, zero_circuits.X, zero_circuits.Y};
114+
test_unified_addition<CurveType>(public_input, P);
93115

94-
std::vector<typename curve_type::base_field_type::value_type> public_input = {P.X, P.Y, Q.X, Q.Y};
95-
typename curve_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type expected_res = P + Q;
116+
public_input = {P.X, P.Y, Q.X, Q.Y};
117+
test_unified_addition<CurveType>(public_input, zero_circuits);
96118

97-
test_unified_addition<curve_type>(public_input, expected_res);
119+
public_input = {Q.X, Q.Y, P.X, P.Y};
120+
test_unified_addition<CurveType>(public_input, zero_circuits);
98121
}
99122

100-
BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_addition) {
123+
template<typename CurveType>
124+
void test_unified_addition_doubling() {
125+
nil::crypto3::random::algebraic_engine<typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>> generate_random_point;
126+
boost::random::mt19937 seed_seq;
127+
generate_random_point.seed(seed_seq);
101128

102-
using curve_type = crypto3::algebra::curves::pallas;
129+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type P = generate_random_point();
130+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type Q(P);
131+
132+
std::vector<typename CurveType::base_field_type::value_type> public_input;
133+
134+
public_input = {P.X, P.Y, Q.X, Q.Y};
135+
test_unified_addition<CurveType>(public_input, P+Q);
136+
137+
public_input = {Q.X, Q.Y, P.X, P.Y};
138+
test_unified_addition<CurveType>(public_input, P+Q);
139+
}
103140

104-
auto P = crypto3::algebra::random_element<curve_type::template g1_type<>>().to_affine();
105-
auto Q = crypto3::algebra::random_element<curve_type::template g1_type<>>().to_affine();
106-
typename curve_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type zero = {0, 0};
107-
typename curve_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type expected_res;
108-
P.X = Q.X;
109-
P.Y = -Q.Y;
110-
if (Q.X == zero.X && Q.Y == zero.Y) {
111-
expected_res = P;
112-
} else {
113-
if (P.X == zero.X && P.Y == zero.Y) {
114-
expected_res = Q;
141+
template<typename CurveType, std::size_t RandomTestsAmount>
142+
void test_unified_addition_random_data() {
143+
nil::crypto3::random::algebraic_engine<typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>> generate_random_point;
144+
boost::random::mt19937 seed_seq;
145+
generate_random_point.seed(seed_seq);
146+
147+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type P = generate_random_point();
148+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type Q = generate_random_point();
149+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type zero = {0, 0};
150+
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type expected_res;
151+
152+
std::vector<typename CurveType::base_field_type::value_type> public_input;
153+
154+
for (std::size_t i = 0; i < RandomTestsAmount; i++){
155+
P = generate_random_point();
156+
Q = generate_random_point();
157+
158+
if (Q.X == zero.X && Q.Y == zero.Y) {
159+
expected_res = P;
115160
} else {
116-
if (P.X == Q.X && P.Y == -Q.Y) {
117-
expected_res = {0, 0};
161+
if (P.X == zero.X && P.Y == zero.Y) {
162+
expected_res = Q;
118163
} else {
119-
expected_res = P + Q;
164+
if (P.X == Q.X && P.Y == -Q.Y) {
165+
expected_res = {0, 0};
166+
} else {
167+
expected_res = P + Q;
168+
}
120169
}
121170
}
171+
172+
public_input = {P.X, P.Y, Q.X, Q.Y};
173+
test_unified_addition<CurveType>(public_input, expected_res);
122174
}
175+
}
176+
177+
constexpr static const std::size_t random_tests_amount = 10;
123178

124-
std::vector<typename curve_type::base_field_type::value_type> public_input = {P.X, P.Y, Q.X, Q.Y};
179+
BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)
180+
181+
BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_pallas) {
182+
using curve_type = crypto3::algebra::curves::pallas;
183+
test_unified_addition_with_zeroes<curve_type>();
184+
test_unified_addition_doubling<curve_type>();
185+
test_unified_addition_random_data<curve_type, random_tests_amount>();
186+
}
125187

126-
test_unified_addition<curve_type>(public_input, expected_res);
188+
BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_vesta) {
189+
using curve_type = crypto3::algebra::curves::vesta;
190+
test_unified_addition_with_zeroes<curve_type>();
191+
test_unified_addition_doubling<curve_type>();
192+
test_unified_addition_random_data<curve_type, random_tests_amount>();
127193
}
128194

129195
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)