Skip to content

Commit

Permalink
Replace usage of array_list with standard_array_list #132
Browse files Browse the repository at this point in the history
fix sparse_vector

Removed print from tests #132
  • Loading branch information
vo-nil committed Dec 5, 2024
1 parent f0af64e commit 4f01c76
Show file tree
Hide file tree
Showing 30 changed files with 284 additions and 538 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2017-2021 Mikhail Komarov <[email protected]>
// Copyright (c) 2020-2021 Nikita Kaskov <[email protected]>
// Copyright (c) 2021 Ilias Khairullin <[email protected]>
// Copyright (c) 2024 Vasiliy Olekhov <[email protected]>
//
// MIT License
//
Expand Down Expand Up @@ -355,27 +356,21 @@ namespace nil {
return field;
}

template<typename CurveGroupType, typename TTypeBase>
using curve_element_vector = nil::marshalling::types::standard_array_list<
TTypeBase,
curve_element<TTypeBase, CurveGroupType>>;

template<typename CurveGroupType, typename Endianness>
nil::marshalling::types::array_list<
nil::marshalling::field_type<Endianness>,
curve_element<nil::marshalling::field_type<Endianness>, CurveGroupType>,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>
curve_element_vector<CurveGroupType, nil::marshalling::field_type<Endianness>>
fill_curve_element_vector(
const std::vector<typename CurveGroupType::value_type> &curve_elem_vector) {

using TTypeBase = nil::marshalling::field_type<Endianness>;

using curve_element_type = curve_element<TTypeBase, CurveGroupType>;

using curve_element_vector_type = nil::marshalling::types::array_list<
TTypeBase,
curve_element_type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>;

curve_element_vector_type result;

curve_element_vector<CurveGroupType, TTypeBase> result;
std::vector<curve_element_type> &val = result.value();
for (std::size_t i = 0; i < curve_elem_vector.size(); i++) {
val.push_back(curve_element_type(curve_elem_vector[i]));
Expand All @@ -385,11 +380,7 @@ namespace nil {

template<typename CurveGroupType, typename Endianness>
std::vector<typename CurveGroupType::value_type> make_curve_element_vector(
const nil::marshalling::types::array_list<
nil::marshalling::field_type<Endianness>,
curve_element<nil::marshalling::field_type<Endianness>, CurveGroupType>,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>>>
const curve_element_vector<CurveGroupType, nil::marshalling::field_type<Endianness>>
&curve_elem_vector) {

std::vector<typename CurveGroupType::value_type> result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2017-2021 Mikhail Komarov <[email protected]>
// Copyright (c) 2020-2021 Nikita Kaskov <[email protected]>
// Copyright (c) 2021 Ilias Khairullin <[email protected]>
// Copyright (c) 2024 Vasiliy Olekhov <[email protected]>
//
// MIT License
//
Expand Down Expand Up @@ -686,27 +687,20 @@ namespace nil {
// return field;
// }


template<typename FieldValueType, typename TTypeBase>
using field_element_vector = nil::marshalling::types::array_list<
using field_element_vector = nil::marshalling::types::standard_array_list<
TTypeBase,
field_element<TTypeBase, FieldValueType>,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>>;
field_element<TTypeBase, FieldValueType>>;

template<typename FieldValueType, typename Endianness>
field_element_vector<FieldValueType, nil::marshalling::field_type<Endianness>>
fill_field_element_vector(const std::vector<FieldValueType> &field_elem_vector) {

using TTypeBase = nil::marshalling::field_type<Endianness>;
using field_element_type = field_element<TTypeBase, FieldValueType>;
using field_element_vector_type = nil::marshalling::types::array_list<
TTypeBase,
field_element_type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>>;

field_element_vector_type result;
field_element_vector<FieldValueType, TTypeBase> result;
for (std::size_t i = 0; i < field_elem_vector.size(); i++) {
result.value().push_back(field_element_type(field_elem_vector[i]));
}
Expand Down
49 changes: 29 additions & 20 deletions crypto3/libs/marshalling/algebra/test/field_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@
#include <nil/marshalling/algorithms/pack.hpp>
#include <nil/crypto3/marshalling/algebra/types/field_element.hpp>

template<typename TIter>
void print_byteblob(TIter iter_begin, TIter iter_end) {
for (TIter it = iter_begin; it != iter_end; it++) {
std::cout << std::hex << int(*it) << std::endl;
}
}

template<typename T, typename Endianness>
void test_field_element(T val) {

Expand Down Expand Up @@ -80,47 +73,63 @@ void test_field_element(T val) {
BOOST_CHECK(status == nil::marshalling::status_type::success);
}

template<typename T, typename Endianness>
void test_field_element_vector() {

using namespace nil::crypto3::marshalling;

using unit_type = unsigned char;
using field_element_type = types::field_element<nil::marshalling::field_type<Endianness>,
T>;

std::vector<typename T::value_type> vec(16);

for(auto &v : vec) {
v = nil::crypto3::algebra::random_element<T>();
}

nil::marshalling::status_type status;
std::vector<unit_type> cv = nil::marshalling::pack<Endianness>(vec, status);

BOOST_CHECK(status == nil::marshalling::status_type::success);

std::vector<typename T::value_type> test_val = nil::marshalling::pack<Endianness>(cv, status);

BOOST_CHECK(vec == test_val);
BOOST_CHECK(status == nil::marshalling::status_type::success);
}


template<typename FieldType, typename Endianness>
void test_field_element() {
std::cout << std::hex;
std::cerr << std::hex;
for (unsigned i = 0; i < 128; ++i) {
if (!(i % 16) && i) {
std::cout << std::dec << i << " tested" << std::endl;
}
typename FieldType::value_type val = nil::crypto3::algebra::random_element<FieldType>();
test_field_element<typename FieldType::value_type, Endianness>(val);
}

test_field_element_vector<FieldType, Endianness>();
}

BOOST_AUTO_TEST_SUITE(field_element_test_suite)

BOOST_AUTO_TEST_CASE(field_element_bls12_381_g1_field_be) {
std::cout << "BLS12-381 g1 group field big-endian test started" << std::endl;
test_field_element<nil::crypto3::algebra::curves::bls12<381>::g1_type<>::field_type,
nil::marshalling::option::big_endian>();
std::cout << "BLS12-381 g1 group field big-endian test finished" << std::endl;
}

BOOST_AUTO_TEST_CASE(field_element_bls12_381_g1_field_le) {
std::cout << "BLS12-381 g1 group field little-endian test started" << std::endl;
test_field_element<nil::crypto3::algebra::curves::bls12<381>::g1_type<>::field_type,
nil::marshalling::option::little_endian>();
std::cout << "BLS12-381 g1 group field little-endian test finished" << std::endl;
}

BOOST_AUTO_TEST_CASE(field_element_bls12_381_g2_field_be) {
std::cout << "BLS12-381 g2 group field big-endian test started" << std::endl;
test_field_element<nil::crypto3::algebra::curves::bls12<381>::g2_type<>::field_type,
nil::marshalling::option::big_endian>();
std::cout << "BLS12-381 g2 group field big-endian test finished" << std::endl;
}

BOOST_AUTO_TEST_CASE(field_element_bls12_381_g2_field_le) {
std::cout << "BLS12-381 g2 group field little-endian test started" << std::endl;
test_field_element<nil::crypto3::algebra::curves::bls12<381>::g2_type<>::field_type,
nil::marshalling::option::little_endian>();
std::cout << "BLS12-381 g2 group field little-endian test finished" << std::endl;
}

BOOST_AUTO_TEST_SUITE_END()
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ namespace nil {
typename std::enable_if<std::is_same<
std::uint8_t,
typename std::iterator_traits<typename ValueType::iterator>::value_type>::value>::type> {
using type = nil::marshalling::types::array_list<
TTypeBase,
nil::marshalling::types::integral<TTypeBase, std::uint8_t>,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::uint64_t>>>;
using type = nil::marshalling::types::standard_array_list<
TTypeBase, nil::marshalling::types::integral<TTypeBase, uint8_t>>;
};

// For Poseidon, Merkle node will contain a Group Element, not a vector of bytes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,18 @@ namespace nil {

template<typename TTypeBase, typename MerkleProof, typename = void>
struct merkle_proof_layer {
using type = nil::marshalling::types::array_list<
using type = nil::marshalling::types::standard_array_list<
TTypeBase,
// path_element_t
typename merkle_proof_path_element<TTypeBase, MerkleProof>::type,
// layer
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::uint64_t>>>;
typename merkle_proof_path_element<TTypeBase, MerkleProof>::type>;
};

template<typename TTypeBase, typename MerkleProof, typename = void>
struct merkle_proof_path {
using type = nil::marshalling::types::array_list<
using type = nil::marshalling::types::standard_array_list<
TTypeBase,
// layer path
typename merkle_proof_layer<TTypeBase, MerkleProof>::type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::uint64_t>>>;
typename merkle_proof_layer<TTypeBase, MerkleProof>::type>;
};

template<typename TTypeBase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ namespace nil {
namespace marshalling {
namespace types {
template<typename TTypeBase, typename MerkleTree>
using merkle_tree = nil::marshalling::types::array_list<
using merkle_tree = nil::marshalling::types::standard_array_list<
TTypeBase,
typename merkle_node_value<TTypeBase, MerkleTree>::type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>>;
typename merkle_node_value<TTypeBase, MerkleTree>::type>;

template<typename MerkleTree, typename Endianness>
merkle_tree<nil::marshalling::field_type<Endianness>, MerkleTree>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,19 @@ namespace nil {
TTypeBase,
std::tuple<
// std::vector<math::term<VariableType>> terms
nil::marshalling::types::array_list<
nil::marshalling::types::standard_array_list<
TTypeBase,
typename term<TTypeBase, typename ExpressionType::term_type>::type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
typename term<TTypeBase, typename ExpressionType::term_type>::type
>,
// std::vector<flat_pow_operation> pow_operations
nil::marshalling::types::array_list<
nil::marshalling::types::standard_array_list<
TTypeBase,
typename flat_pow_operation<TTypeBase>::type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
typename flat_pow_operation<TTypeBase>::type
>,
// std::vector<flat_binary_arithmetic_operation> binary_operations
nil::marshalling::types::array_list<
nil::marshalling::types::standard_array_list<
TTypeBase,
typename flat_binary_arithmetic_operation<TTypeBase>::type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
typename flat_binary_arithmetic_operation<TTypeBase>::type
>,
// flat_node_type root_type;
nil::marshalling::types::integral<TTypeBase, std::uint8_t>,
Expand Down Expand Up @@ -152,45 +146,34 @@ namespace nil {
const auto& flat_expr = flattener.get_result();

using TTypeBase = nil::marshalling::field_type<Endianness>;
using size_t_marshalling_type = nil::marshalling::types::integral<TTypeBase, std::size_t>;
// Fill the terms.
using term_marshalling_type =
typename term<TTypeBase, typename ExpressionType::term_type>::type;
using term_vector_marshalling_type = nil::marshalling::types::array_list<
TTypeBase, term_marshalling_type,
nil::marshalling::option::sequence_size_field_prefix<size_t_marshalling_type>>;
term_vector_marshalling_type filled_terms;
nil::marshalling::types::standard_array_list<TTypeBase,
typename term<TTypeBase, typename ExpressionType::term_type>::type> filled_terms;
for (const auto &term : flat_expr.terms) {
filled_terms.value().push_back(
fill_term<Endianness, typename ExpressionType::term_type>(term));
}

// Fill the power operations.
using pow_operation_type = typename flat_pow_operation<TTypeBase>::type;
using pow_vector_marshalling_type = nil::marshalling::types::array_list<
TTypeBase, pow_operation_type, nil::marshalling::option::sequence_size_field_prefix<
size_t_marshalling_type>>;
pow_vector_marshalling_type filled_powers;
nil::marshalling::types::standard_array_list<TTypeBase,
typename flat_pow_operation<TTypeBase>::type> filled_powers;
for (const auto &power : flat_expr.pow_operations) {
filled_powers.value().push_back(fill_power_operation<Endianness>(power));
}

// Fill the binary operations.
using binary_operation_type = typename flat_binary_arithmetic_operation<TTypeBase>::type;
using binary_operation_vector_marshalling_type = nil::marshalling::types::array_list<
TTypeBase, binary_operation_type,
nil::marshalling::option::sequence_size_field_prefix<size_t_marshalling_type>>;
binary_operation_vector_marshalling_type filled_binary_opeations;
nil::marshalling::types::standard_array_list<TTypeBase,
typename flat_binary_arithmetic_operation<TTypeBase>::type> filled_binary_operations;
for (const auto &bin_op : flat_expr.binary_operations) {
filled_binary_opeations.value().push_back(
filled_binary_operations.value().push_back(
fill_binary_operation<Endianness>(bin_op));
}

return typename expression<nil::marshalling::field_type<Endianness>, ExpressionType>::type(
std::make_tuple(
filled_terms,
filled_powers,
filled_binary_opeations,
filled_binary_operations,
nil::marshalling::types::integral<TTypeBase, std::uint8_t>((std::uint8_t)flat_expr.root_type),
nil::marshalling::types::integral<TTypeBase, std::uint32_t>(flat_expr.root_index)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ namespace nil {
// assignment_type coeff
field_element<TTypeBase, typename NonLinearTerm::assignment_type>,
// std::vector<VariableType> vars
nil::marshalling::types::array_list<
TTypeBase, typename variable<TTypeBase, typename NonLinearTerm::variable_type>::type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
>
nil::marshalling::types::standard_array_list<
TTypeBase, typename variable<TTypeBase, typename NonLinearTerm::variable_type>::type>
>
>;
};
Expand All @@ -68,14 +65,12 @@ namespace nil {

using TTypeBase = nil::marshalling::field_type<Endianness>;
using result_type = typename term<TTypeBase, NonLinearTerm>::type;
using size_t_marshalling_type = nil::marshalling::types::integral<TTypeBase, std::size_t>;
using field_element_marhsalling_type =
field_element<TTypeBase, typename NonLinearTerm::assignment_type>;
using variable_marshalling_type =
typename variable<TTypeBase, typename NonLinearTerm::variable_type>::type;
using variable_vector_marshalling_type = nil::marshalling::types::array_list<
TTypeBase, variable_marshalling_type,
nil::marshalling::option::sequence_size_field_prefix<size_t_marshalling_type>>;
using variable_vector_marshalling_type = nil::marshalling::types::standard_array_list<
TTypeBase, variable_marshalling_type>;

variable_vector_marshalling_type filled_vars;
for (const auto &var : t.get_vars()) {
Expand Down
Loading

0 comments on commit 4f01c76

Please sign in to comment.