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

Commit f2bedf6

Browse files
committed
Pack and unpack work for curve element containers. #1
1 parent 4598171 commit f2bedf6

3 files changed

+60
-73
lines changed

test/curve_element.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@
3737
#include <nil/marshalling/field_type.hpp>
3838
#include <nil/marshalling/endianness.hpp>
3939

40-
#include <nil/crypto3/multiprecision/cpp_int.hpp>
41-
#include <nil/crypto3/multiprecision/number.hpp>
42-
4340
#include <nil/crypto3/algebra/random_element.hpp>
4441
#include <nil/crypto3/algebra/curves/bls12.hpp>
45-
#include <nil/crypto3/algebra/curves/detail/marshalling.hpp>
4642

4743
#include <nil/marshalling/algorithms/pack.hpp>
4844
#include <nil/marshalling/algorithms/unpack.hpp>

test/curve_element_fixed_size_container.cpp

+16-36
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@
3333
#include <iomanip>
3434

3535
#include <nil/marshalling/status_type.hpp>
36-
#include <nil/marshalling/types/array_list.hpp>
37-
#include <nil/marshalling/container/static_vector.hpp>
38-
#include <nil/marshalling/field_type.hpp>
3936
#include <nil/marshalling/endianness.hpp>
4037

41-
#include <nil/crypto3/multiprecision/cpp_int.hpp>
42-
#include <nil/crypto3/multiprecision/number.hpp>
43-
4438
#include <nil/crypto3/algebra/random_element.hpp>
4539
#include <nil/crypto3/algebra/curves/bls12.hpp>
46-
#include <nil/crypto3/algebra/curves/detail/marshalling.hpp>
40+
41+
#include <nil/marshalling/algorithms/pack.hpp>
42+
#include <nil/marshalling/algorithms/unpack.hpp>
4743

4844
#include <nil/crypto3/marshalling/algebra/types/curve_element.hpp>
4945

@@ -65,42 +61,26 @@ void print_fp2_curve_group_element(Fp2CurveGroupElement e) {
6561
<< e.Y.data[1].data << ") (" << e.Z.data[0].data << " " << e.Z.data[1].data << ")" << std::endl;
6662
}
6763

68-
template<class CurveGroupElement, std::size_t TSize>
69-
void test_curve_element_fixed_size_container_big_endian(std::array<CurveGroupElement, TSize> val_container) {
64+
template<class T, std::size_t TSize>
65+
void test_curve_element_fixed_size_container_big_endian(std::array<T, TSize> val_container) {
7066
using namespace nil::crypto3::marshalling;
71-
std::size_t units_bits = 8;
72-
using unit_type = unsigned char;
73-
using curve_element_type = types::curve_element<nil::marshalling::field_type<nil::marshalling::option::big_endian>,
74-
typename CurveGroupElement::group_type>;
75-
using curve_type = typename CurveGroupElement::group_type::curve_type;
76-
77-
using container_type =
78-
nil::marshalling::types::array_list<nil::marshalling::field_type<nil::marshalling::option::little_endian>,
79-
curve_element_type,
80-
nil::marshalling::option::fixed_size_storage<TSize>>;
81-
82-
std::size_t unitblob_size =
83-
curve_element_type::bit_length() / units_bits + ((curve_element_type::bit_length() % units_bits) ? 1 : 0);
84-
std::vector<unit_type> cv;
85-
cv.resize(unitblob_size * TSize, 0x00);
86-
87-
nil::marshalling::container::static_vector<curve_element_type, TSize> container_data;
88-
for (std::size_t i = 0; i < TSize; i++) {
89-
container_data.push_back(curve_element_type(val_container[i]));
90-
}
9167

92-
container_type test_val = container_type(container_data);
68+
using Endianness = nil::marshalling::option::big_endian;
69+
70+
using unit_type = unsigned char;
9371

94-
auto write_iter = cv.begin();
72+
static_assert(nil::marshalling::is_compatible<T>::value);
9573

96-
nil::marshalling::status_type status = test_val.write(write_iter, cv.size());
74+
nil::marshalling::status_type status;
75+
std::vector<unit_type> cv =
76+
nil::marshalling::unpack<Endianness, unit_type>(val_container, status);
9777

98-
container_type test_val_read;
78+
BOOST_CHECK(status == nil::marshalling::status_type::success);
9979

100-
auto read_iter = cv.begin();
101-
status = test_val_read.read(read_iter, cv.size());
80+
std::array<T, TSize> test_val = nil::marshalling::pack<Endianness, std::array<T, TSize>>(cv, status);
10281

103-
BOOST_CHECK(std::equal(test_val.value().begin(), test_val.value().end(), test_val_read.value().begin()));
82+
BOOST_CHECK(std::equal(val_container.begin(), val_container.end(), test_val.begin()));
83+
BOOST_CHECK(status == nil::marshalling::status_type::success);
10484
}
10585

10686
template<class CurveGroup, std::size_t TSize>

test/curve_element_non_fixed_size_container.cpp

+44-33
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@
3939
#include <nil/marshalling/field_type.hpp>
4040
#include <nil/marshalling/endianness.hpp>
4141

42-
#include <nil/crypto3/multiprecision/cpp_int.hpp>
43-
#include <nil/crypto3/multiprecision/number.hpp>
44-
4542
#include <nil/crypto3/algebra/random_element.hpp>
4643
#include <nil/crypto3/algebra/curves/bls12.hpp>
47-
#include <nil/crypto3/algebra/curves/detail/marshalling.hpp>
44+
45+
#include <nil/marshalling/algorithms/pack.hpp>
46+
#include <nil/marshalling/algorithms/unpack.hpp>
4847

4948
#include <nil/crypto3/marshalling/algebra/types/curve_element.hpp>
5049

@@ -55,51 +54,63 @@ void print_byteblob(TIter iter_begin, TIter iter_end) {
5554
}
5655
}
5756

58-
template<typename Endianness, class CurveGroupElement, std::size_t TSize>
59-
void test_curve_element_non_fixed_size_container(std::vector<CurveGroupElement> val_container) {
57+
template<typename Endianness, class T, std::size_t TSize>
58+
void test_curve_element_non_fixed_size_container(std::vector<T> val_container) {
6059
using namespace nil::crypto3::marshalling;
6160
std::size_t units_bits = 8;
6261
using unit_type = unsigned char;
63-
using CurveGroup = typename CurveGroupElement::group_type;
6462

65-
using curve_element_type = types::curve_element<nil::marshalling::field_type<Endianness>, CurveGroup>;
66-
using curve_type = typename CurveGroup::curve_type;
63+
nil::marshalling::status_type status;
64+
std::vector<unit_type> cv =
65+
nil::marshalling::unpack<Endianness, unit_type>(val_container, status);
6766

68-
using container_type = nil::marshalling::types::array_list<
69-
nil::marshalling::field_type<Endianness>,
70-
curve_element_type,
71-
nil::marshalling::option::sequence_size_field_prefix<
72-
nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>;
67+
BOOST_CHECK(status == nil::marshalling::status_type::success);
7368

74-
std::size_t unitblob_size =
75-
curve_element_type::bit_length() / units_bits + ((curve_element_type::bit_length() % units_bits) ? 1 : 0);
76-
std::vector<unit_type> cv;
77-
cv.resize(unitblob_size * TSize + sizeof(std::size_t), 0x00);
69+
std::vector<T> test_val = nil::marshalling::pack<Endianness, std::vector<T>>(cv, status);
7870

79-
std::vector<curve_element_type> container_data;
71+
BOOST_CHECK(std::equal(val_container.begin(), val_container.end(), test_val.begin()));
72+
BOOST_CHECK(status == nil::marshalling::status_type::success);
8073

81-
for (std::size_t i = 0; i < TSize; i++) {
82-
container_data.push_back(curve_element_type(val_container[i]));
83-
}
74+
// using CurveGroup = typename T::group_type;
75+
76+
// using curve_element_type = types::curve_element<nil::marshalling::field_type<Endianness>, CurveGroup>;
77+
// using curve_type = typename CurveGroup::curve_type;
78+
79+
// using container_type = nil::marshalling::types::array_list<
80+
// nil::marshalling::field_type<Endianness>,
81+
// curve_element_type,
82+
// nil::marshalling::option::sequence_size_field_prefix<
83+
// nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>;
84+
85+
// std::size_t unitblob_size =
86+
// curve_element_type::bit_length() / units_bits + ((curve_element_type::bit_length() % units_bits) ? 1 : 0);
87+
// std::vector<unit_type> cv;
88+
// cv.resize(unitblob_size * TSize + sizeof(std::size_t), 0x00);
89+
90+
// std::vector<curve_element_type> container_data;
91+
92+
// for (std::size_t i = 0; i < TSize; i++) {
93+
// container_data.push_back(curve_element_type(val_container[i]));
94+
// }
8495

85-
container_type test_val = container_type(container_data);
96+
// container_type test_val = container_type(container_data);
8697

87-
container_type filled_val = types::fill_curve_element_vector<CurveGroup, Endianness>(val_container);
98+
// container_type filled_val = types::fill_curve_element_vector<CurveGroup, Endianness>(val_container);
8899

89-
std::vector<typename CurveGroup::value_type> constructed_val =
90-
types::make_curve_element_vector<CurveGroup, Endianness>(filled_val);
91-
BOOST_CHECK(std::equal(val_container.begin(), val_container.end(), constructed_val.begin()));
100+
// std::vector<typename CurveGroup::value_type> constructed_val =
101+
// types::make_curve_element_vector<CurveGroup, Endianness>(filled_val);
102+
// BOOST_CHECK(std::equal(val_container.begin(), val_container.end(), constructed_val.begin()));
92103

93-
auto write_iter = cv.begin();
104+
// auto write_iter = cv.begin();
94105

95-
nil::marshalling::status_type status = test_val.write(write_iter, cv.size());
106+
// nil::marshalling::status_type status = test_val.write(write_iter, cv.size());
96107

97-
container_type test_val_read;
108+
// container_type test_val_read;
98109

99-
auto read_iter = cv.begin();
100-
status = test_val_read.read(read_iter, cv.size());
110+
// auto read_iter = cv.begin();
111+
// status = test_val_read.read(read_iter, cv.size());
101112

102-
BOOST_CHECK(std::equal(test_val.value().begin(), test_val.value().end(), test_val_read.value().begin()));
113+
// BOOST_CHECK(std::equal(test_val.value().begin(), test_val.value().end(), test_val_read.value().begin()));
103114
}
104115

105116
template<typename Endianness, class CurveGroup, std::size_t TSize>

0 commit comments

Comments
 (0)