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

Commit 6e52a42

Browse files
author
Ilias Khairullin
committed
Serialization into bits added. #3
1 parent 6484a44 commit 6e52a42

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ namespace nil {
125125

126126
template<typename Endianness, typename Coordinates>
127127
struct curve_element_writer<
128-
129128
Endianness,
130129
typename algebra::curves::bls12_381::template g2_type<Coordinates,
131130
algebra::curves::forms::short_weierstrass>> {
@@ -175,7 +174,6 @@ namespace nil {
175174

176175
template<typename Coordinates>
177176
struct curve_element_writer<
178-
179177
nil::marshalling::endian::little_endian,
180178
typename algebra::curves::curve25519::template g1_type<Coordinates,
181179
algebra::curves::forms::twisted_edwards>> {
@@ -240,12 +238,26 @@ namespace nil {
240238
/// https://zips.z.cash/protocol/protocol.pdf#concreteextractorjubjub
241239
template<typename TIter>
242240
static nil::marshalling::status_type process(const group_value_type &point, TIter &iter) {
243-
write_data<params_type::bit_length(), endianness>(
241+
write_data<params_type::bit_length(), endianness>(
244242
static_cast<typename group_value_type::field_type::integral_type>(point.to_affine().X.data),
245243
iter);
246244

247245
return nil::marshalling::status_type::success;
248246
}
247+
248+
// TODO: refactor
249+
template<>
250+
static nil::marshalling::status_type
251+
process<typename std::vector<bool>::iterator>(const group_value_type &point,
252+
typename std::vector<bool>::iterator &iter) {
253+
auto X_affine = static_cast<typename group_value_type::field_type::integral_type>(point.to_affine().X.data);
254+
for (std::size_t i = 0; i < params_type::bit_length(); ++i) {
255+
*iter++ = bit_test(X_affine, 0);
256+
X_affine >>= 1;
257+
}
258+
259+
return nil::marshalling::status_type::success;
260+
}
249261
};
250262

251263
template<typename Endianness, typename Coordinates>

test/curve_element.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ BOOST_AUTO_TEST_CASE(curve_element_jubjub_g1) {
187187
for (auto i = 0; i < expected_bits.size(); ++i) {
188188
BOOST_CHECK(expected_bits[i] == ((cv[i / 8] >> (i % 8)) & 1));
189189
}
190+
191+
/// serialization into bits
192+
std::vector<bool> cv_bits = nil::marshalling::unpack_bits<nil::marshalling::option::little_endian>(point, status);
193+
auto it1 = expected_bits.begin();
194+
auto it2 = cv_bits.begin();
195+
while (it1 != expected_bits.end() && it2 != cv_bits.end()) {
196+
BOOST_CHECK(*it1++ == *it2++);
197+
}
198+
BOOST_CHECK(cv_bits.size() == field_type::value_bits);
190199
}
191200

192201
BOOST_AUTO_TEST_CASE(curve_element_curve25519_g1) {

0 commit comments

Comments
 (0)