Skip to content

Commit

Permalink
Pack, unpack algorithms work for non-containerized integral type. #3
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaskov committed Nov 9, 2021
1 parent 2b68625 commit 549e668
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 80 deletions.
60 changes: 60 additions & 0 deletions include/nil/crypto3/marshalling/inference.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2021 Mikhail Komarov <nemo@nil.foundation>
// Copyright (c) 2021 Nikita Kaskov <nbering@nil.foundation>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_MARSHALLING_MULTIPRECISION_INFERENCE_TYPE_TRAITS_HPP
#define CRYPTO3_MARSHALLING_MULTIPRECISION_INFERENCE_TYPE_TRAITS_HPP

// #include <nil/crypto3/marshalling/types/integral.hpp>

namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {
template<typename TTypeBase, typename IntegralContainer, typename... TOptions>
class integral;
} // namespace types
} // namespace marshalling
} // namespace crypto3
namespace marshalling {

template<typename T, typename Enabled>
class is_compatible;

template<typename Backend,
nil::crypto3::multiprecision::expression_template_option ExpressionTemplates>
class is_compatible <nil::crypto3::multiprecision::number<Backend, ExpressionTemplates>, void> {
using default_endianness = option::big_endian;
public:
template <typename TEndian = default_endianness, typename... TOptions>
using type = typename nil::crypto3::marshalling::types::integral<field_type<TEndian>,
nil::crypto3::multiprecision::number<Backend, ExpressionTemplates>, TOptions...>;
static const bool value = true;
static const bool fixed_size = true;
};

} // namespace marshalling
} // namespace nil

#endif // CRYPTO3_MARSHALLING_MULTIPRECISION_INFERENCE_TYPE_TRAITS_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ namespace nil {

template<typename TIter>
nil::marshalling::status_type read(TIter &iter, std::size_t size) {
// if (size < length()) {
// return nil::marshalling::status_type::not_enough_data;
// }
if (size < length()) {
return nil::marshalling::status_type::not_enough_data;
}

read_no_status(iter);
iter += max_length();
Expand All @@ -138,9 +138,9 @@ namespace nil {

template<typename TIter>
nil::marshalling::status_type write(TIter &iter, std::size_t size) const {
// if (size < length()) {
// return nil::marshalling::status_type::buffer_overflow;
// }
if (size < length()) {
return nil::marshalling::status_type::buffer_overflow;
}

write_no_status(iter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ namespace nil {

using base_impl_type = TTypeBase;

std::size_t cur_length = 0;
public:
using value_type = T;
using serialized_type = value_type;

basic_integral() = default;

explicit basic_integral(value_type val) : value_(val) {

std::size_t bits_count = multiprecision::msb(val) + 1;

cur_length = bits_count / 8 + (bits_count%8?1:0);
}

basic_integral(const basic_integral &) = default;
Expand All @@ -78,8 +83,8 @@ namespace nil {
return value_;
}

static constexpr std::size_t length() {
return sizeof(serialized_type);
std::size_t length() {
return cur_length;
}

static constexpr std::size_t min_length() {
Expand All @@ -105,20 +110,22 @@ namespace nil {
// }

read_no_status(iter, size);
iter += size;
cur_length += size;
return nil::marshalling::status_type::success;
}

template<typename TIter>
void read_no_status(TIter &iter) {
read_no_status(length());
}
// template<typename TIter>
// void read_no_status(TIter &iter) {
// read_no_status(length());
// }

private:
template<typename TIter>
void read_no_status(TIter &iter, std::size_t size) {
value_ =
crypto3::marshalling::processing::read_data<T, typename base_impl_type::endian_type>(
iter, size);
iter, size * 8);
}

public:
Expand All @@ -129,6 +136,7 @@ namespace nil {
// }

write_no_status(iter);
iter += size;
return nil::marshalling::status_type::success;
}

Expand Down
3 changes: 2 additions & 1 deletion include/nil/crypto3/marshalling/types/integral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <nil/crypto3/marshalling/types/detail/integral/basic_fixed_precision_type.hpp>
#include <nil/crypto3/marshalling/types/detail/integral/basic_non_fixed_precision_type.hpp>
#include <nil/crypto3/marshalling/inference.hpp>

namespace nil {
namespace crypto3 {
Expand Down Expand Up @@ -154,7 +155,7 @@ namespace nil {

/// @brief Get length required to serialise the current field value.
/// @return Number of bytes it will take to serialise the field value.
static constexpr std::size_t length() {
std::size_t length() {
return base_impl_type::length();
}

Expand Down
98 changes: 32 additions & 66 deletions test/integral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include <nil/crypto3/multiprecision/cpp_int.hpp>
#include <nil/crypto3/multiprecision/number.hpp>

// #include <nil/crypto3/marshalling/algorithms/pack.hpp>
#include <nil/marshalling/algorithms/pack.hpp>
#include <nil/marshalling/algorithms/unpack.hpp>
#include <nil/crypto3/marshalling/types/integral.hpp>

template<class T>
Expand Down Expand Up @@ -102,20 +105,16 @@ void test_round_trip_fixed_precision_big_endian(T val) {

export_bits(val, cv.begin() + begin_index, units_bits, true);

auto read_iter = cv.begin();
nil::marshalling::status_type status = test_val.read(read_iter, cv.size() * units_bits);
BOOST_CHECK(status == nil::marshalling::status_type::success);
nil::marshalling::status_type status;
T test_val1 = nil::marshalling::pack<nil::marshalling::option::big_endian, T>(cv, status);

BOOST_CHECK(val == test_val.value());
BOOST_CHECK(val == test_val1);
BOOST_CHECK(status == nil::marshalling::status_type::success);

std::vector<unit_type> test_val_byteblob;
test_val_byteblob.resize(cv.size());
auto write_iter = test_val_byteblob.begin();
std::vector<unit_type> test_cv = nil::marshalling::unpack<nil::marshalling::option::big_endian, unit_type>(val, status);

status = test_val.write(write_iter, test_val_byteblob.size() * units_bits);
BOOST_CHECK(std::equal(test_cv.begin(), test_cv.end(), cv.begin()));
BOOST_CHECK(status == nil::marshalling::status_type::success);

BOOST_CHECK(cv == test_val_byteblob);
}

template<class T>
Expand All @@ -133,24 +132,23 @@ void test_round_trip_fixed_precision_little_endian(T val) {
export_bits(val, std::back_inserter(cv), units_bits, false);
cv.resize(unitblob_size, 0x00);

auto read_iter = cv.begin();
nil::marshalling::status_type status = test_val.read(read_iter, cv.size() * units_bits);
BOOST_CHECK(status == nil::marshalling::status_type::success);
nil::marshalling::status_type status;
T test_val1 = nil::marshalling::pack<nil::marshalling::option::little_endian, T>(cv, status);

BOOST_CHECK(val == test_val.value());
BOOST_CHECK(val == test_val1);
BOOST_CHECK(status == nil::marshalling::status_type::success);

std::vector<unit_type> test_val_byteblob;
test_val_byteblob.resize(cv.size());
auto write_iter = test_val_byteblob.begin();
std::vector<unit_type> test_cv = nil::marshalling::unpack<nil::marshalling::option::little_endian, unit_type>(val, status);

status = test_val.write(write_iter, test_val_byteblob.size() * units_bits);
BOOST_CHECK(std::equal(test_cv.begin(), test_cv.end(), cv.begin()));
BOOST_CHECK(status == nil::marshalling::status_type::success);

BOOST_CHECK(cv == test_val_byteblob);
}

template<class T>
void test_round_trip_fixed_precision() {

static_assert(nil::marshalling::is_compatible<T>::value);

std::cout << std::hex;
std::cerr << std::hex;
for (unsigned i = 0; i < 1000; ++i) {
Expand All @@ -160,72 +158,40 @@ void test_round_trip_fixed_precision() {
}
}

template<class T>
void test_round_trip_non_fixed_precision_big_endian(T val) {
template<typename TEndianness, class T>
void test_round_trip_non_fixed_precision(T val) {
using namespace nil::crypto3::marshalling;

std::size_t units_bits = 8;
using unit_type = unsigned char;
using integral_type = types::integral<nil::marshalling::field_type<nil::marshalling::option::big_endian>, T>;

integral_type test_val;

std::vector<unit_type> cv;
export_bits(val, std::back_inserter(cv), units_bits, true);

auto read_iter = cv.begin();
nil::marshalling::status_type status = test_val.read(read_iter, cv.size() * units_bits);
BOOST_CHECK(status == nil::marshalling::status_type::success);

BOOST_CHECK(val == test_val.value());

std::vector<unit_type> test_val_byteblob;
test_val_byteblob.resize(cv.size());
auto write_iter = test_val_byteblob.begin();

status = test_val.write(write_iter, test_val_byteblob.size() * units_bits);
BOOST_CHECK(status == nil::marshalling::status_type::success);

BOOST_CHECK(cv == test_val_byteblob);
}
export_bits(val, std::back_inserter(cv), units_bits,
std::is_same<TEndianness, nil::marshalling::option::big_endian>::value?true:false);

template<class T>
void test_round_trip_non_fixed_precision_little_endian(T val) {
using namespace nil::crypto3::marshalling;

std::size_t units_bits = 8;
using unit_type = unsigned char;
using integral_type = types::integral<nil::marshalling::field_type<nil::marshalling::option::little_endian>, T>;
nil::marshalling::status_type status;
T test_val1 = nil::marshalling::pack<TEndianness, T>(cv, status);

integral_type test_val;

std::vector<unsigned char> cv;
export_bits(val, std::back_inserter(cv), units_bits, false);

auto read_iter = cv.begin();
nil::marshalling::status_type status = test_val.read(read_iter, cv.size() * units_bits);
BOOST_CHECK(val == test_val1);
BOOST_CHECK(status == nil::marshalling::status_type::success);

BOOST_CHECK(val == test_val.value());
std::vector<unit_type> test_cv = nil::marshalling::unpack<TEndianness, unit_type>(val, status);

std::vector<unsigned char> test_val_byteblob;
test_val_byteblob.resize(cv.size());
auto write_iter = test_val_byteblob.begin();

status = test_val.write(write_iter, test_val_byteblob.size() * units_bits);
BOOST_CHECK(std::equal(test_cv.begin(), test_cv.end(), cv.begin()));
BOOST_CHECK(status == nil::marshalling::status_type::success);

BOOST_CHECK(cv == test_val_byteblob);
}

template<class T>
void test_round_trip_non_fixed_precision() {

static_assert(nil::marshalling::is_compatible<T>::value);

std::cout << std::hex;
std::cerr << std::hex;
for (unsigned i = 0; i < 1000; ++i) {
T val = generate_random<T>();
test_round_trip_non_fixed_precision_big_endian(val);
test_round_trip_non_fixed_precision_little_endian(val);
test_round_trip_non_fixed_precision<nil::marshalling::option::big_endian>(val);
test_round_trip_non_fixed_precision<nil::marshalling::option::little_endian>(val);
}
}

Expand Down

0 comments on commit 549e668

Please sign in to comment.