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

Commit ee56d1b

Browse files
authored
Merge pull request #2 from NilFoundation/1-pack-unpack
Pack and unpack for curve element added
2 parents 8fb2cfa + f2bedf6 commit ee56d1b

12 files changed

+288
-154
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2021 Mikhail Komarov <[email protected]>
3+
// Copyright (c) 2021 Nikita Kaskov <[email protected]>
4+
//
5+
// MIT License
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
//---------------------------------------------------------------------------//
25+
26+
#ifndef CRYPTO3_MARSHALLING_ALGEBRA_INFERENCE_TYPE_TRAITS_HPP
27+
#define CRYPTO3_MARSHALLING_ALGEBRA_INFERENCE_TYPE_TRAITS_HPP
28+
29+
#include <boost/type_traits.hpp>
30+
#include <boost/type_traits/is_same.hpp>
31+
32+
#include <nil/crypto3/algebra/type_traits.hpp>
33+
#include <nil/crypto3/marshalling/algebra/types/field_element.hpp>
34+
35+
namespace nil {
36+
namespace crypto3 {
37+
namespace marshalling {
38+
namespace types {
39+
template<typename TTypeBase, typename CurveGroupType, typename... TOptions>
40+
class curve_element;
41+
} // namespace types
42+
} // namespace marshalling
43+
} // namespace crypto3
44+
namespace marshalling {
45+
46+
template<typename T, typename Enabled>
47+
class is_compatible;
48+
49+
template<typename T>
50+
class is_compatible <T, typename std::enable_if<nil::crypto3::algebra::is_group_element<T>::value>::type> {
51+
using default_endianness = option::big_endian;
52+
public:
53+
template <typename TEndian = default_endianness>
54+
using type = typename nil::crypto3::marshalling::types::curve_element<field_type<TEndian>, typename T::group_type>;
55+
static const bool value = true;
56+
static const bool fixed_size = true;
57+
};
58+
59+
template<typename T>
60+
class is_compatible <T, typename std::enable_if<nil::crypto3::algebra::is_field<T>::value>::type> {
61+
using default_endianness = option::big_endian;
62+
public:
63+
template <typename TEndian = default_endianness>
64+
using type = nil::crypto3::marshalling::types::field_element<
65+
nil::marshalling::field_type<TEndian>,
66+
T>;
67+
static const bool value = true;
68+
static const bool fixed_size = true;
69+
};
70+
71+
} // namespace marshalling
72+
} // namespace nil
73+
74+
#endif // CRYPTO3_MARSHALLING_ALGEBRA_INFERENCE_TYPE_TRAITS_HPP

include/nil/crypto3/marshalling/processing/curve_element.hpp include/nil/crypto3/marshalling/algebra/processing/curve_element.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
#include <nil/crypto3/algebra/curves/bls12.hpp>
4040
#include <nil/crypto3/algebra/curves/curve25519.hpp>
4141

42-
#include <nil/crypto3/marshalling/processing/integral.hpp>
43-
#include <nil/crypto3/marshalling/processing/detail/curve_element.hpp>
42+
#include <nil/crypto3/marshalling/multiprecision/processing/integral.hpp>
43+
44+
#include <nil/crypto3/marshalling/algebra/processing/detail/curve_element.hpp>
4445

4546
namespace nil {
4647
namespace crypto3 {

include/nil/crypto3/marshalling/processing/detail/curve_element.hpp include/nil/crypto3/marshalling/algebra/processing/detail/curve_element.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include <nil/crypto3/algebra/type_traits.hpp>
3838
#include <nil/crypto3/algebra/curves/curve25519.hpp>
3939

40-
#include <nil/crypto3/marshalling/processing/integral.hpp>
40+
#include <nil/crypto3/marshalling/multiprecision/processing/integral.hpp>
4141

4242
namespace nil {
4343
namespace crypto3 {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2018-2021 Mikhail Komarov <[email protected]>
3+
//
4+
// MIT License
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//---------------------------------------------------------------------------//
24+
25+
#ifndef CRYPTO3_MARSHALLING_ALGEBRA_TYPE_TRAITS_HPP
26+
#define CRYPTO3_MARSHALLING_ALGEBRA_TYPE_TRAITS_HPP
27+
28+
#include <boost/type_traits.hpp>
29+
#include <boost/type_traits/is_same.hpp>
30+
31+
#include <nil/marshalling/type_traits.hpp>
32+
#include <nil/crypto3/marshalling/algebra/types/field_element.hpp>
33+
34+
namespace nil {
35+
namespace crypto3 {
36+
namespace marshalling {
37+
namespace types {
38+
template<typename TTypeBase, typename CurveGroupType, typename... TOptions>
39+
class curve_element;
40+
} // namespace types
41+
} // namespace marshalling
42+
} // namespace crypto3
43+
namespace marshalling {
44+
45+
/// @brief Compile time check function of whether a provided type is any
46+
/// variant of nil::crypto3::marshalling::types::curve_element.
47+
/// @tparam T Any type.
48+
/// @return true in case provided type is any variant of @ref curve_element
49+
/// @related nil::crypto3::marshalling::types::curve_element
50+
template<typename T>
51+
struct is_curve_element {
52+
53+
static const bool value = false;
54+
};
55+
56+
template<typename TTypeBase, typename CurveGroupType, typename... TOptions>
57+
struct is_curve_element<nil::crypto3::marshalling::types::curve_element<TTypeBase,
58+
CurveGroupType, TOptions...>> {
59+
60+
static const bool value = true;
61+
};
62+
63+
template<typename T, typename Enabled>
64+
struct is_container;
65+
66+
template<typename T>
67+
struct is_container <T, typename std::enable_if<is_curve_element<T>::value>::type> {
68+
static const bool value = false;
69+
};
70+
71+
} // namespace marshalling
72+
} // namespace nil
73+
74+
#endif // CRYPTO3_MARSHALLING_ALGEBRA_TYPE_TRAITS_HPP

include/nil/crypto3/marshalling/types/algebra/curve_element.hpp include/nil/crypto3/marshalling/algebra/types/curve_element.hpp

+49-47
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
#include <nil/marshalling/types/tag.hpp>
3838
#include <nil/marshalling/types/detail/adapt_basic_field.hpp>
3939

40-
#include <nil/crypto3/marshalling/types/algebra/detail/curve_element/basic_type.hpp>
40+
#include <nil/crypto3/marshalling/algebra/types/detail/curve_element/basic_type.hpp>
41+
#include <nil/crypto3/marshalling/algebra/inference.hpp>
42+
#include <nil/crypto3/marshalling/algebra/type_traits.hpp>
4143

4244
namespace nil {
4345
namespace crypto3 {
@@ -352,52 +354,52 @@ namespace nil {
352354
return field;
353355
}
354356

355-
template<typename CurveGroupType, typename Endianness>
356-
nil::marshalling::types::array_list<
357-
nil::marshalling::field_type<Endianness>,
358-
curve_element<nil::marshalling::field_type<Endianness>, CurveGroupType>,
359-
nil::marshalling::option::sequence_size_field_prefix<
360-
nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>
361-
fill_curve_element_vector(std::vector<typename CurveGroupType::value_type> curve_elem_vector) {
362-
363-
using TTypeBase = nil::marshalling::field_type<Endianness>;
364-
365-
using curve_element_type = curve_element<TTypeBase, CurveGroupType>;
366-
367-
using curve_element_vector_type = nil::marshalling::types::array_list<
368-
TTypeBase,
369-
curve_element_type,
370-
nil::marshalling::option::sequence_size_field_prefix<
371-
nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>;
372-
373-
curve_element_vector_type result;
374-
375-
std::vector<curve_element_type> &val = result.value();
376-
for (std::size_t i = 0; i < curve_elem_vector.size(); i++) {
377-
val.push_back(curve_element_type(curve_elem_vector[i]));
378-
}
379-
return result;
380-
}
381-
382-
template<typename CurveGroupType, typename Endianness>
383-
std::vector<typename CurveGroupType::value_type> make_curve_element_vector(
384-
nil::marshalling::types::array_list<
385-
nil::marshalling::field_type<Endianness>,
386-
curve_element<nil::marshalling::field_type<Endianness>, CurveGroupType>,
387-
nil::marshalling::option::sequence_size_field_prefix<
388-
nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>
389-
curve_elem_vector) {
390-
391-
std::vector<typename CurveGroupType::value_type> result;
392-
std::vector<curve_element<nil::marshalling::field_type<Endianness>, CurveGroupType>> &values =
393-
curve_elem_vector.value();
394-
std::size_t size = values.size();
395-
396-
for (std::size_t i = 0; i < size; i++) {
397-
result.push_back(values[i].value());
398-
}
399-
return result;
400-
}
357+
// template<typename CurveGroupType, typename Endianness>
358+
// nil::marshalling::types::array_list<
359+
// nil::marshalling::field_type<Endianness>,
360+
// curve_element<nil::marshalling::field_type<Endianness>, CurveGroupType>,
361+
// nil::marshalling::option::sequence_size_field_prefix<
362+
// nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>
363+
// fill_curve_element_vector(std::vector<typename CurveGroupType::value_type> curve_elem_vector) {
364+
365+
// using TTypeBase = nil::marshalling::field_type<Endianness>;
366+
367+
// using curve_element_type = curve_element<TTypeBase, CurveGroupType>;
368+
369+
// using curve_element_vector_type = nil::marshalling::types::array_list<
370+
// TTypeBase,
371+
// curve_element_type,
372+
// nil::marshalling::option::sequence_size_field_prefix<
373+
// nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>;
374+
375+
// curve_element_vector_type result;
376+
377+
// std::vector<curve_element_type> &val = result.value();
378+
// for (std::size_t i = 0; i < curve_elem_vector.size(); i++) {
379+
// val.push_back(curve_element_type(curve_elem_vector[i]));
380+
// }
381+
// return result;
382+
// }
383+
384+
// template<typename CurveGroupType, typename Endianness>
385+
// std::vector<typename CurveGroupType::value_type> make_curve_element_vector(
386+
// nil::marshalling::types::array_list<
387+
// nil::marshalling::field_type<Endianness>,
388+
// curve_element<nil::marshalling::field_type<Endianness>, CurveGroupType>,
389+
// nil::marshalling::option::sequence_size_field_prefix<
390+
// nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>
391+
// curve_elem_vector) {
392+
393+
// std::vector<typename CurveGroupType::value_type> result;
394+
// std::vector<curve_element<nil::marshalling::field_type<Endianness>, CurveGroupType>> &values =
395+
// curve_elem_vector.value();
396+
// std::size_t size = values.size();
397+
398+
// for (std::size_t i = 0; i < size; i++) {
399+
// result.push_back(values[i].value());
400+
// }
401+
// return result;
402+
// }
401403
} // namespace types
402404
} // namespace marshalling
403405
} // namespace crypto3

include/nil/crypto3/marshalling/types/algebra/detail/curve_element/basic_type.hpp include/nil/crypto3/marshalling/algebra/types/detail/curve_element/basic_type.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#include <nil/marshalling/status_type.hpp>
3232

33-
#include <nil/crypto3/marshalling/processing/curve_element.hpp>
33+
#include <nil/crypto3/marshalling/algebra/processing/curve_element.hpp>
3434

3535
namespace nil {
3636
namespace crypto3 {

include/nil/crypto3/marshalling/types/algebra/field_element.hpp include/nil/crypto3/marshalling/algebra/types/field_element.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
#include <nil/crypto3/algebra/type_traits.hpp>
4141

42-
#include <nil/crypto3/marshalling/types/integral.hpp>
42+
#include <nil/crypto3/marshalling/multiprecision/types/integral.hpp>
4343

4444
namespace nil {
4545
namespace crypto3 {

test/curve_element.cpp

+19-29
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@
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

47-
#include <nil/crypto3/marshalling/types/algebra/curve_element.hpp>
43+
#include <nil/marshalling/algorithms/pack.hpp>
44+
#include <nil/marshalling/algorithms/unpack.hpp>
45+
46+
#include <nil/crypto3/marshalling/algebra/types/curve_element.hpp>
4847

4948
template<typename TIter>
5049
void print_byteblob(TIter iter_begin, TIter iter_end) {
@@ -53,39 +52,30 @@ void print_byteblob(TIter iter_begin, TIter iter_end) {
5352
}
5453
}
5554

56-
template<typename CurveGroupElement>
57-
void test_curve_element_big_endian(CurveGroupElement val) {
55+
template<typename T>
56+
void test_curve_element_big_endian(T val) {
5857
using namespace nil::crypto3::marshalling;
5958

60-
std::size_t units_bits = 8;
61-
using unit_type = unsigned char;
62-
63-
using curve_element_type = types::curve_element<nil::marshalling::field_type<nil::marshalling::option::big_endian>,
64-
typename CurveGroupElement::group_type>;
65-
using curve_type = typename CurveGroupElement::group_type::curve_type;
66-
67-
auto compressed_curve_group_element =
68-
nil::marshalling::curve_element_serializer<curve_type>::point_to_octets_compress(val);
59+
using Endianness = nil::marshalling::option::big_endian;
6960

70-
std::size_t unitblob_size =
71-
curve_element_type::bit_length() / units_bits + ((curve_element_type::bit_length() % units_bits) ? 1 : 0);
72-
curve_element_type test_val = curve_element_type(val);
73-
74-
std::vector<unit_type> cv;
75-
cv.resize(unitblob_size);
61+
using unit_type = unsigned char;
7662

77-
auto write_iter = cv.begin();
63+
using curve_element_type = types::curve_element<nil::marshalling::field_type<Endianness>,
64+
typename T::group_type>;
7865

79-
nil::marshalling::status_type status = test_val.write(write_iter, unitblob_size * units_bits);
66+
static_assert(nil::marshalling::is_curve_element<curve_element_type>::value);
67+
static_assert(nil::marshalling::is_compatible<T>::value);
8068

81-
BOOST_CHECK(std::equal(compressed_curve_group_element.begin(), compressed_curve_group_element.end(), cv.begin()));
69+
nil::marshalling::status_type status;
70+
std::vector<unit_type> cv =
71+
nil::marshalling::unpack<Endianness, unit_type>(val, status);
8272

83-
curve_element_type test_val_read;
73+
BOOST_CHECK(status == nil::marshalling::status_type::success);
8474

85-
auto read_iter = cv.begin();
86-
status = test_val_read.read(read_iter, curve_element_type::bit_length());
75+
T test_val = nil::marshalling::pack<Endianness, T>(cv, status);
8776

88-
BOOST_CHECK(test_val == test_val_read);
77+
BOOST_CHECK(val == test_val);
78+
BOOST_CHECK(status == nil::marshalling::status_type::success);
8979
}
9080

9181
template<typename CurveGroup>

0 commit comments

Comments
 (0)