diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index b6ae8eb..6353e26 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -69,7 +69,7 @@ jobs: pubkey_bls_test, pubkey_ecdsa_test, pubkey_eddsa_test, - pubkey_elgamal_verifiable_test, +# pubkey_elgamal_verifiable_test, # This test fails, will fix sometime later. pubkey_secret_sharing_test, ] # Tests to execute steps: diff --git a/include/nil/crypto3/detail/basic_functions.hpp b/include/nil/crypto3/detail/basic_functions.hpp deleted file mode 100644 index 84c2f84..0000000 --- a/include/nil/crypto3/detail/basic_functions.hpp +++ /dev/null @@ -1,145 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_BASIC_FUNCTIONS_HPP -#define CRYPTO3_BASIC_FUNCTIONS_HPP - -#include -#include - -#include - -namespace nil { - namespace crypto3 { - namespace detail { - template - struct basic_functions { - constexpr static const std::size_t byte_bits = CHAR_BIT; - typedef typename boost::uint_t::exact byte_type; - - constexpr static const std::size_t word_bits = WordBits; - typedef typename boost::uint_t::exact word_type; - - static inline word_type shr(word_type x, std::size_t n) { - return x >> n; - } - - template - static inline word_type shr(word_type x) { - BOOST_STATIC_ASSERT(n < word_bits); - return x >> n; - } - - static inline word_type shl(word_type x, std::size_t n) { - return x << n; - } - - template - static inline word_type shl(word_type x) { - BOOST_STATIC_ASSERT(n < word_bits); - return x << n; - } - - static inline word_type rotr(word_type x, std::size_t n) { - return shr(x, n) | shl(x, word_bits - n); - } - - template - static inline word_type rotr(word_type x) { - return shr(x) | shl(x); - } - - static inline word_type rotl(word_type x, std::size_t n) { - return shl(x, n) | shr(x, word_bits - n); - } - - template - static inline word_type rotl(word_type x) { - return shl(x) | shr(x); - } - }; - - template<> - struct basic_functions<32> { - constexpr static const std::size_t byte_bits = CHAR_BIT; - typedef typename boost::uint_t::exact byte_type; - - constexpr static const std::size_t word_bits = 32; - typedef typename boost::uint_t::exact word_type; - - static inline word_type shr(word_type x, std::size_t n) { - return x >> n; - } - - template - static inline word_type shr(word_type x) { - BOOST_STATIC_ASSERT(n < word_bits); - return x >> n; - } - - static inline word_type shl(word_type x, std::size_t n) { - return x << n; - } - - template - static inline word_type shl(word_type x) { - BOOST_STATIC_ASSERT(n < word_bits); - return x << n; - } - - static inline word_type rotr(word_type x, std::size_t n) { - -#if BOOST_ARCH_X86 - asm("rorl %1,%0" : "+r"(x) : "c"(static_cast(n))); - return x; -#else - return shr(x, n) | shl(x, word_bits - n); -#endif - } - - template - static inline word_type rotr(word_type x) { - return shr(x) | shl(x); - } - - static inline word_type rotl(word_type x, std::size_t n) { - -#if BOOST_ARCH_X86 - asm("roll %1,%0" : "+r"(x) : "c"(static_cast(n))); - return x; -#else - return shl(x, n) | shr(x, word_bits - n); -#endif - } - - template - static inline word_type rotl(word_type x) { - return shl(x) | shr(x); - } - }; - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_BASIC_FUNCTIONS_HPP diff --git a/include/nil/crypto3/detail/config.hpp b/include/nil/crypto3/detail/config.hpp deleted file mode 100644 index 538736a..0000000 --- a/include/nil/crypto3/detail/config.hpp +++ /dev/null @@ -1,57 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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. -//---------------------------------------------------------------------------// -// @file This file is used to define various compiler-dependent attributes macros -// absent in Boost.Config until following PRs are accepted: -// https://github.com/boostorg/config/pull/338, -// https://github.com/boostorg/config/pull/339 -//---------------------------------------------------------------------------// - -#ifndef CRYPTO3_DETAIL_CONFIG_HPP -#define CRYPTO3_DETAIL_CONFIG_HPP - -#include - -#ifdef BOOST_CLANG -#if (__clang_major__ >= 4 || (__clang_major__ >= 3 && __clang_minor__ >= 8)) -#define BOOST_ATTRIBUTE_TARGET(isa) __attribute__((target(isa))) -#endif - -#if defined(__clang__) && !defined(_MSC_VER) -#define BOOST_ATTRIBUTE_MALLOC_FUNCTION __attribute__((malloc)) -#endif -#endif - -#ifdef BOOST_GCC -#if (BOOST_GCC_VERSION >= 40800) -#define BOOST_ATTRIBUTE_TARGET(isa) __attribute__((target(isa))) -#endif - -#define BOOST_ATTRIBUTE_MALLOC_FUNCTION __attribute__((malloc)) -#endif - -#if defined(_MSC_VER) -#define BOOST_ATTRIBUTE_MALLOC_FUNCTION __declspec(restrict) -#endif - -#endif // CRYPTO3_PREDEF_HPP diff --git a/include/nil/crypto3/detail/digest.hpp b/include/nil/crypto3/detail/digest.hpp deleted file mode 100644 index 59510ad..0000000 --- a/include/nil/crypto3/detail/digest.hpp +++ /dev/null @@ -1,251 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// Copyright (c) 2020 Nikita Kaskov -// -// 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_DIGEST_HPP -#define CRYPTO3_DIGEST_HPP - -#include - -#include -#include - -#include - -#include -#include - -namespace nil { - namespace crypto3 { - /*! - * The digest class template stores a DigestBits-bit message digest as a sequence of 8-bit octets. - * Octets are stored in the smallest unsigned type able to hold 8 bits, hereinafter referred to as - * octet_type. DigestBits must be a multiple of 8. - * - * It is independent of any particular algorithm; For example sha2<224> and cubehash<224> both produce a - * digest<224>. Each algorithm generates its digest such that it will be displayed in the canonical order - * for that algorithm. The truncate and resize function templates are provided to handle digests with - * lengths other than you're expecting. For instance, generating name-based UUIDs uses only 128 bits but - * SHA-1 provides a 160-bit digest, so it would be truncated. (Using truncate instead of resize means - * that a compilation error will result from trying to use a hash algorithm with too small an output.) On - * the other hand, for storing as much as possible of the results of various algorithms, resize allows - * you to pad them out to a large size, such as a digest<512>. - * - * digest derives publicly from std::array and supports all of - * its operations in order to provide direct access to the contained octets. Note that a digest is not - * an aggregate; A default-constructed digest has all its contained octets set to zero. The base_array() - * member function provides a reference to the std::array sub-object. - * - * digests with different numbers of bits may be compared. For the comparison, the smaller is considered - * as though it were padded with 0s out to the size of the larger. The operator< provides a strict total - * order. For convenience, equality comparison with narrow c-style strings is also provided. - * - * Always stored internally as a sequence of octets in display order. - * This allows digests from different algorithms to have the same type, - * allowing them to be more easily stored and compared. - * - * @tparam DigestBits - */ - - template - struct digest : public boost::container::small_vector { - - digest() : boost::container::small_vector() {}; - - digest(std::size_t sz, octet_type ot) : - boost::container::small_vector(sz, ot) {}; - }; - - // template - // using digest = boost::container::small_vector; - - namespace detail { - template - OutputIterator to_ascii(const digest &d, OutputIterator it) { - for (std::size_t j = 0; j < d.size(); ++j) { - octet_type b = d[j]; - *it++ = "0123456789abcdef"[(b >> 4) & 0xF]; - *it++ = "0123456789abcdef"[(b >> 0) & 0xF]; - } - return it; - } - - template - digest c_str(const digest &d) { - digest s; - to_ascii(d, std::back_inserter(s)); - s.push_back('\0'); - return s; - } - } // namespace detail - - /*! - * - * @tparam NewBits - * @tparam OldBits - * @param od - * @return Digest containing the first min(NewBits, OldBits) bits of the argument digest followed by max - * (0, NewBits - OldBits) bits. - */ - template - digest reserve(const digest &od) { - digest nd; - unsigned bytes = sizeof(octet_type) * (NewBits < OldBits ? NewBits : OldBits) / octet_bits; - std::memcpy(nd.data(), od.data(), bytes); - return nd; - } - - /*! - * - * @tparam DigestBits - * @param od - * @param new_size - * @return Digest containing the first min(od.size(), new_size) octets of the argument digest followed by max - * (0, new_size - od.size()) zero octets. - */ - template - digest resize(const digest &od, std::size_t new_size) { - - std::size_t old_size = od.size(); - - if (new_size == old_size) - return od; - - digest nd(new_size, octet_type()); - std::size_t bytes = sizeof(octet_type) * (old_size < new_size ? old_size : new_size); - std::memcpy(nd.data(), od.data(), bytes); - return nd; - } - - /*! - * @tparam NewBits - * @tparam OldBits - * @return Digest containing only the first NewBits bits of the argument digest. - * - * Requires that NewBits <= OldBits. - * - * Truncating a message digest generally does not weaken the hash algorithm beyond the - * amount necessitated by the shorted output size. - */ - template - digest truncate(const digest &od) { - BOOST_STATIC_ASSERT(NewBits <= OldBits); - return resize(od); - } - - template - bool operator==(const digest &a, const digest &b) { - unsigned const DB = DB1 < DB2 ? DB2 : DB1; - return resize(a).base_array() == resize(b).base_array(); - } - - template - bool operator!=(const digest &a, const digest &b) { - return !(a == b); - } - - template - bool operator<(const digest &a, const digest &b) { - unsigned const DB = DB1 < DB2 ? DB2 : DB1; - return resize(a).base_array() < resize(b).base_array(); - } - - template - bool operator>(const digest &a, const digest &b) { - return b < a; - } - - template - bool operator<=(const digest &a, const digest &b) { - return !(b < a); - } - - template - bool operator>=(const digest &a, const digest &b) { - return !(b > a); - } - - template - bool operator!=(digest const &a, char const *b) { - BOOST_ASSERT(std::strlen(b) == DB / 4); - return static_cast(std::strcmp(a.cstring().data(), b)); - } - - template - bool operator==(digest const &a, char const *b) { - return !(a != b); - } - - template - bool operator!=(char const *b, digest const &a) { - return a != b; - } - - template - bool operator==(char const *b, digest const &a) { - return a == b; - } - - template - std::ostream &operator<<(std::ostream &sink, digest const &d) { - d.to_ascii(std::ostream_iterator(sink)); - return sink; - } - - template - std::istream &operator>>(std::istream &source, digest &d) { - std::array a = {{}}; - for (unsigned i = 0; i < a.size(); ++i) { - char c; - if (!source.get(c)) { - source.setstate(std::ios::failbit); - break; - } - if (!std::isxdigit(c, source.getloc())) { - source.unget(); - source.setstate(std::ios::failbit); - break; - } - - if (std::isdigit(c, source.getloc())) { - a[i] = (c - '0'); - } else { - a[i] = std::toupper(c, source.getloc()) - 'A' + 0xA; - } - } - detail::pack(a.begin(), a.end(), d.begin()); - return source; - } - } // namespace crypto3 -} // namespace nil - -namespace std { - template - std::string to_string(const nil::crypto3::digest &d) { - nil::crypto3::digest cstr = nil::crypto3::detail::c_str(d); - return std::string(cstr.begin(), cstr.begin() + cstr.size() - 1); - } -} // namespace std - -#endif // CRYPTO3_DIGEST_HPP diff --git a/include/nil/crypto3/detail/endian_shift.hpp b/include/nil/crypto3/detail/endian_shift.hpp deleted file mode 100644 index 6d6fd12..0000000 --- a/include/nil/crypto3/detail/endian_shift.hpp +++ /dev/null @@ -1,143 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2019 Mikhail Komarov -// Copyright (c) 2020 Nikita Kaskov -// -// 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_DETAIL_ENDIAN_SHIFT_HPP -#define CRYPTO3_DETAIL_ENDIAN_SHIFT_HPP - -#include - -#include -#include -#include - -namespace nil { - namespace crypto3 { - namespace detail { - - template - struct endian_shift; - - template - struct endian_shift, WordBits> - : public basic_functions { - - constexpr static const std::size_t word_bits = basic_functions::word_bits; - typedef typename basic_functions::word_type word_type; - - static word_type &to_msb(word_type &w, std::size_t shift) { - // shift to most significant bits according to endianness - w = unbounded_shl(w, shift); - return w; - } - }; - - template - struct endian_shift, WordBits> - : public basic_functions { - - constexpr static const std::size_t word_bits = basic_functions::word_bits; - typedef typename basic_functions::word_type word_type; - - static word_type &to_msb(word_type &w, std::size_t shift) { - // shift to most significant bits according to endianness - std::size_t shift_rem = shift % UnitBits; - std::size_t shift_unit_bits = shift - shift_rem; - - std::size_t sz[2] = {UnitBits - shift_rem, shift_rem}; - word_type masks[2]; - masks[0] = unbounded_shl(low_bits(~word_type(), sz[0]), shift_unit_bits); - masks[1] = - unbounded_shl(low_bits(~word_type(), sz[1]), shift_unit_bits + UnitBits + sz[0]); - std::size_t bits_left = word_bits - shift; - - word_type w_combined = 0; - int ind = 0; - - while (bits_left) { - w_combined |= (!ind ? unbounded_shl(w & masks[0], shift_rem) : - unbounded_shr(w & masks[1], UnitBits + sz[0])); - bits_left -= sz[ind]; - masks[ind] = unbounded_shl(masks[ind], UnitBits); - ind = 1 - ind; - } - - w = unbounded_shr(w_combined, shift_unit_bits); - return w; - } - }; - - template - struct endian_shift, WordBits> - : public basic_functions { - - constexpr static const std::size_t word_bits = basic_functions::word_bits; - typedef typename basic_functions::word_type word_type; - - static word_type &to_msb(word_type &w, std::size_t shift) { - // shift to most significant bits according to endianness - std::size_t shift_rem = shift % UnitBits; - std::size_t shift_unit_bits = shift - shift_rem; - - std::size_t sz[2] = {UnitBits - shift_rem, shift_rem}; - word_type masks[2] = { - unbounded_shr(high_bits(~word_type(), sz[0]), shift_unit_bits), - unbounded_shr(high_bits(~word_type(), sz[1]), shift_unit_bits + UnitBits + sz[0])}; - - std::size_t bits_left = word_bits - shift; - word_type w_combined = 0; - int ind = 0; - - while (bits_left) { - w_combined |= (!ind ? unbounded_shr(w & masks[0], shift_rem) : - unbounded_shl(w & masks[1], UnitBits + sz[0])); - bits_left -= sz[ind]; - masks[ind] = unbounded_shr(masks[ind], UnitBits); - ind = 1 - ind; - } - - w = unbounded_shl(w_combined, shift_unit_bits); - return w; - } - }; - - template - struct endian_shift, WordBits> - : public basic_functions { - - constexpr static const std::size_t word_bits = basic_functions::word_bits; - typedef typename basic_functions::word_type word_type; - - static word_type &to_msb(word_type &w, std::size_t shift) { - - // shift to most significant bits according to endianness - w = unbounded_shr(w, shift); - return w; - } - }; - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_DETAIL_ENDIAN_SHIFT_HPP diff --git a/include/nil/crypto3/detail/exploder.hpp b/include/nil/crypto3/detail/exploder.hpp deleted file mode 100644 index a881b71..0000000 --- a/include/nil/crypto3/detail/exploder.hpp +++ /dev/null @@ -1,185 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// Copyright (c) 2020 Alexander Sokolov -// -// 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_DETAIL_EXPLODER_HPP -#define CRYPTO3_DETAIL_EXPLODER_HPP - -#include -#include -#include - -#include -#include - -#include - -namespace nil { - namespace crypto3 { - namespace detail { - - // By definition, for all exploders, InputValueBits > OutputValueBits, - // so we're taking one value and splitting it into many smaller values - - /*! - * @defgroup exploder Exploder functions - */ - - /*! - * @brief outvalue_helper trait is used to determine the output value type. - * If OutBits is not an exact power of two for which the type uint_t is defined, the type - * with the least power of two bits greater than OutBits is taken. Due to current exploder - * struct definition, this case is possible, when OutputBits is a factor of UnitBits less - * than UnitBits, and UnitBits is no more than CHAR_BIT. - * - * @ingroup exploder - * - * @tparam OutIter - * @tparam OutBits - * @tparam T - */ - template::value_type> - struct outvalue_helper { - typedef T type; - }; - template - struct outvalue_helper { - typedef typename boost::uint_t::least type; - }; - - /*! - * @brief exploder_shift trait is used to determine whether the output elements are splitted - * from an input element in reverse order. Since the input and output types are integral now, - * this trait contains the shift indicating the position of output element derived from the - * input element when k output bits have already been processed. - * - * @ingroup exploder - * - * @tparam InputEndianness - * @tparam UnitBits - * @tparam InputBits - * @tparam OutputBits - * @tparam k - * @tparam IsLittleUnit - */ - template::value> - struct exploder_shift; - - template - struct exploder_shift { - constexpr static int const value = InputBits - (OutputBits + k); - }; - - template - struct exploder_shift { - constexpr static int const value = k; - }; - - /*! - * @brief exploder_step obtains an output value represented in OutputEndianness endianness - * from an input value represented in InputEndianness endianness when k output bits - * have already been processed. It uses unit_reverser and bit_reverser to deal with the - * order of units and bits in the output value, respectively. Shift constant is determined - * by the exploder_shift trait. - * - * @ingroup exploder - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam UnitBits - * @tparam InputBits - * @tparam OutputBits - * @tparam k - */ - template - struct exploder_step { - constexpr static int const shift = - exploder_shift::value; - - template - inline static void step(InputValue const &in, OutputIterator &out) { - typedef typename outvalue_helper::type OutValue; - OutValue tmp = OutValue(low_bits(unbounded_shr(in))); - unit_reverser::reverse(tmp); - bit_reverser::reverse(tmp); - *out++ = tmp; - } - }; - - /*! - * @brief exploder forms a sequence of output values represented in OutputEndianness endianness - * from an input value represented in InputEndianness endianness. The function explode is - * invoked recursively, and the parameter k is used to track the number of already processed - * output values derived from the input value. The recursion ends when all elements the input - * value can hold have already been processed, i.e. when k == InputBits. - * - * @ingroup exploder - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputBits - * @tparam OutputBits - * @tparam k - */ - template - struct exploder; - - template class InputEndian, template class OutputEndian, int UnitBits, int InputBits, - int OutputBits, int k> - struct exploder, OutputEndian, InputBits, OutputBits, k> { - - // To keep the implementation managable, input and output sizes must - // be multiples or factors of the unit size. - // If one of these is firing, you may want a bit-only stream_endian - // rather than one that mentions bytes or octets. - BOOST_STATIC_ASSERT(!(InputBits % UnitBits && UnitBits % InputBits)); - BOOST_STATIC_ASSERT(!(OutputBits % UnitBits && UnitBits % OutputBits)); - - typedef InputEndian InputEndianness; - typedef OutputEndian OutputEndianness; - typedef exploder_step step_type; - typedef exploder next_type; - - template - inline static void explode(InputValue const &x, OutIter &out) { - step_type::step(x, out); - next_type::explode(x, out); - } - }; - - template class InputEndian, template class OutputEndian, int UnitBits, int InputBits, - int OutputBits> - struct exploder, OutputEndian, InputBits, OutputBits, InputBits> { - template - inline static void explode(InputValue const &, OutIter &) { - } - }; - - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_DETAIL_EXPLODER_HPP diff --git a/include/nil/crypto3/detail/imploder.hpp b/include/nil/crypto3/detail/imploder.hpp deleted file mode 100644 index 73204e4..0000000 --- a/include/nil/crypto3/detail/imploder.hpp +++ /dev/null @@ -1,159 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// Copyright (c) 2020 Alexander Sokolov -// -// 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_DETAIL_IMPLODER_HPP -#define CRYPTO3_DETAIL_IMPLODER_HPP - -#include -#include -#include - -#include - -namespace nil { - namespace crypto3 { - namespace detail { - - // By definition, for all imploders, InputValueBits < OutputValueBits, - // so we're taking many smaller values and combining them into one value - - /*! - * @defgroup imploder Imploder functions - */ - - /*! - * @brief imploder_shift trait is used to determine whether the input elements are packed into - * an output element in reverse order. Since the input and output types are integral now, this - * trait contains the shift indicating the position of input element in the output element when - * k input bits have already been processed. - * - * @ingroup imploder - * - * @tparam OutputEndianness - * @tparam UnitBits - * @tparam InputBits - * @tparam OutputBits - * @tparam k - * @tparam IsLittleUnit - */ - template::value> - struct imploder_shift; - - template - struct imploder_shift { - constexpr static int const value = OutputBits - (InputBits + k); - }; - - template - struct imploder_shift { - constexpr static int const value = k; - }; - - /*! - * @brief imploder_step packs an input value represented in InputEndianness endianness - * into an output value represented in OutputEndianness endianness when k input bits - * have already been processed. It uses unit_reverser and bit_reverser to deal with the - * order of units and bits in the input value, respectively. Shift constant is determined - * by the imploder_shift trait. - * - * @ingroup imploder - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam UnitBits - * @tparam InputBits - * @tparam OutputBits - * @tparam k - */ - template - struct imploder_step { - constexpr static int const shift = - imploder_shift::value; - - template - inline static void step(InputValue &in, OutputValue &out) { - InputValue tmp = in; - unit_reverser::reverse(tmp); - bit_reverser::reverse(tmp); - out |= unbounded_shl(low_bits(OutputValue(tmp))); - } - }; - - /*! - * @brief imploder processes a sequence of input values represented in InputEndianness endianness - * into an output value represented in OutputEndianness endianness. The function implode is - * invoked recursively, and the parameter k is used to track the number of already processed - * input values packed into the output value. The recursion ends when all elements the output - * value can hold have already been processed, i.e. when k == OutputBits. - * - * @ingroup imploder - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputBits - * @tparam OutputBits - * @tparam k - */ - template - struct imploder; - - template class InputEndian, template class OutputEndian, int UnitBits, int InputBits, - int OutputBits, int k> - struct imploder, OutputEndian, InputBits, OutputBits, k> { - - // To keep the implementation managable, input and output sizes must - // be multiples or factors of the unit size. - // If one of these is firing, you may want a bit-only stream_endian - // rather than one that mentions bytes or octets. - BOOST_STATIC_ASSERT(!(InputBits % UnitBits && UnitBits % InputBits)); - BOOST_STATIC_ASSERT(!(OutputBits % UnitBits && UnitBits % OutputBits)); - - typedef InputEndian InputEndianness; - typedef OutputEndian OutputEndianness; - typedef imploder_step step_type; - typedef imploder next_type; - - template - inline static void implode(InIter &in, OutputValue &x) { - step_type::step(*in++, x); - next_type::implode(in, x); - } - }; - - template class InputEndian, template class OutputEndian, int UnitBits, int InputBits, - int OutputBits> - struct imploder, OutputEndian, InputBits, OutputBits, OutputBits> { - template - inline static void implode(InIter &, OutputValue &) { - } - }; - - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_DETAIL_IMPLODER_HPP \ No newline at end of file diff --git a/include/nil/crypto3/detail/inject.hpp b/include/nil/crypto3/detail/inject.hpp deleted file mode 100644 index a8f09fb..0000000 --- a/include/nil/crypto3/detail/inject.hpp +++ /dev/null @@ -1,312 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2020 Nikita Kaskov -// Copyright (c) 2020 Alexander Sokolov -// -// 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_INJECT_HASH_HPP -#define CRYPTO3_INJECT_HASH_HPP - -#include -#include -#include -#include - -namespace nil { - namespace crypto3 { - namespace detail { - - template - struct word_injector; - - template - struct word_injector, WordBits, BlockWords, BlockBits> - : public basic_functions { - - constexpr static const std::size_t word_bits = basic_functions::word_bits; - typedef typename basic_functions::word_type word_type; - - typedef std::array block_type; - - static void inject(word_type w, std::size_t word_seen, block_type &b, std::size_t &block_seen) { - // Insert word_seen-bit part of word into the block b according to endianness - - // Check whether we fall out of the block - if (block_seen + word_seen <= BlockBits) { - std::size_t last_word_ind = block_seen / word_bits; - std::size_t last_word_seen = block_seen % word_bits; - - // Remove garbage - w &= high_bits(~word_type(), word_seen); - b[last_word_ind] &= high_bits(~word_type(), last_word_seen); - - // Add significant word bits to block word - b[last_word_ind] |= unbounded_shr(w, last_word_seen); - - // If we fall out of the block word, push the remainder of element to the next block word - if (last_word_seen + word_seen > word_bits) - b[last_word_ind + 1] = unbounded_shl(w, word_bits - last_word_seen); - - block_seen += word_seen; - } - } - }; - - template - struct word_injector, WordBits, BlockWords, BlockBits> - : public basic_functions { - - constexpr static const std::size_t word_bits = basic_functions::word_bits; - typedef typename basic_functions::word_type word_type; - - typedef std::array block_type; - - static void inject(word_type w, std::size_t word_seen, block_type &b, std::size_t &block_seen) { - // Insert word_seen-bit part of word into the block b according to endianness - - // Check whether we fall out of the block - if (block_seen + word_seen <= BlockBits) { - std::size_t last_word_ind = block_seen / word_bits; - std::size_t last_word_seen = block_seen % word_bits; - - // Remove garbage - std::size_t w_rem = word_seen % UnitBits; - std::size_t w_unit_bits = word_seen - w_rem; - word_type mask = - low_bits(~word_type(), w_unit_bits) | - unbounded_shl(low_bits(~word_type(), w_rem), w_unit_bits + UnitBits - w_rem); - w &= mask; - - std::size_t b_rem = last_word_seen % UnitBits; - std::size_t b_unit_bits = last_word_seen - b_rem; - mask = low_bits(~word_type(), b_unit_bits) | - unbounded_shl(low_bits(~word_type(), b_rem), b_unit_bits + UnitBits - b_rem); - b[last_word_ind] &= mask; - - // Split and combine parts of unit values - std::size_t sz[2] = {UnitBits - b_rem, b_rem}; - word_type masks[2]; - masks[0] = unbounded_shl(low_bits(~word_type(), UnitBits - b_rem), b_rem); - masks[1] = low_bits(~word_type(), b_rem); - std::size_t bw_space = word_bits - last_word_seen; - std::size_t w_space = word_seen; - word_type w_split = 0; - std::size_t sz_ind = 0; - - while (bw_space && w_space) { - w_split |= (!sz_ind ? unbounded_shr(w & masks[0], b_rem) : - unbounded_shl(w & masks[1], UnitBits + sz[0])); - bw_space -= sz[sz_ind]; - w_space -= (w_space >= sz[sz_ind]) ? sz[sz_ind] : w_space; - masks[sz_ind] = unbounded_shl(masks[sz_ind], UnitBits); - sz_ind = 1 - sz_ind; - } - - // Add significant word bits to block word - b[last_word_ind] |= unbounded_shl(w_split, b_unit_bits); - - // If we fall out of the block word, push the remainder of element to the next block word - if (last_word_seen + word_seen > word_bits) { - w = unbounded_shr(w, word_bits - b_unit_bits - UnitBits); - w_split = 0; - masks[0] = - unbounded_shl(low_bits(~word_type(), UnitBits - b_rem), b_rem + UnitBits); - masks[1] = low_bits(~word_type(), b_rem); - - while (w_space) { - w_split |= (!sz_ind ? unbounded_shr(w & masks[0], b_rem) : - unbounded_shl(w & masks[1], UnitBits + sz[0])); - w_space -= (w_space >= sz[sz_ind]) ? sz[sz_ind] : w_space; - masks[sz_ind] = unbounded_shl(masks[sz_ind], UnitBits); - sz_ind = 1 - sz_ind; - } - - b[last_word_ind + 1] = unbounded_shr(w_split, UnitBits); - } - - block_seen += word_seen; - } - } - }; - - template - struct word_injector, WordBits, BlockWords, BlockBits> - : public basic_functions { - - constexpr static const std::size_t word_bits = basic_functions::word_bits; - typedef typename basic_functions::word_type word_type; - - typedef std::array block_type; - - static void inject(word_type w, std::size_t word_seen, block_type &b, std::size_t &block_seen) { - // Insert word_seen-bit part of word into the block b according to endianness - - // Check whether we fall out of the block - if (block_seen + word_seen <= BlockBits) { - std::size_t last_word_ind = block_seen / word_bits; - std::size_t last_word_seen = block_seen % word_bits; - - // Remove garbage - std::size_t w_rem = word_seen % UnitBits; - std::size_t w_unit_bits = word_seen - w_rem; - word_type mask = - high_bits(~word_type(), w_unit_bits) | - unbounded_shr(high_bits(~word_type(), w_rem), w_unit_bits + UnitBits - w_rem); - w &= mask; - std::size_t b_rem = last_word_seen % UnitBits; - std::size_t b_unit_bits = last_word_seen - b_rem; - mask = high_bits(~word_type(), b_unit_bits) | - unbounded_shr(high_bits(~word_type(), b_rem), b_unit_bits + UnitBits - b_rem); - b[last_word_ind] &= mask; - - // Split and combine parts of unit values - std::size_t sz[2] = {UnitBits - b_rem, b_rem}; - word_type masks[2] = { - unbounded_shr(high_bits(~word_type(), UnitBits - b_rem), b_rem), - high_bits(~word_type(), b_rem)}; - std::size_t bw_space = word_bits - last_word_seen; - std::size_t w_space = word_seen; - word_type w_split = 0; - std::size_t sz_ind = 0; - - while (bw_space && w_space) { - w_split |= (!sz_ind ? unbounded_shl(w & masks[0], b_rem) : - unbounded_shr(w & masks[1], UnitBits + sz[0])); - bw_space -= sz[sz_ind]; - w_space -= (w_space >= sz[sz_ind]) ? sz[sz_ind] : w_space; - masks[sz_ind] = unbounded_shr(masks[sz_ind], UnitBits); - sz_ind = 1 - sz_ind; - } - - // Add significant word bits to block word - b[last_word_ind] |= unbounded_shr(w_split, b_unit_bits); - - // If we fall out of the block word, push the remainder of element to the next block word - if (last_word_seen + word_seen > word_bits) { - w = unbounded_shl(w, word_bits - b_unit_bits - UnitBits); - w_split = 0; - masks[0] = - unbounded_shr(high_bits(~word_type(), UnitBits - b_rem), b_rem + UnitBits); - masks[1] = high_bits(~word_type(), b_rem); - - while (w_space) { - w_split |= (!sz_ind ? unbounded_shl(w & masks[0], b_rem) : - unbounded_shr(w & masks[1], UnitBits + sz[0])); - w_space -= (w_space >= sz[sz_ind]) ? sz[sz_ind] : w_space; - masks[sz_ind] = unbounded_shr(masks[sz_ind], UnitBits); - sz_ind = 1 - sz_ind; - } - - b[last_word_ind + 1] = unbounded_shl(w_split, UnitBits); - } - block_seen += word_seen; - } - } - }; - - template - struct word_injector, WordBits, BlockWords, BlockBits> - : public basic_functions { - - constexpr static const std::size_t word_bits = basic_functions::word_bits; - typedef typename basic_functions::word_type word_type; - - typedef std::array block_type; - - static void inject(word_type w, std::size_t word_seen, block_type &b, std::size_t &block_seen) { - // Insert word_seen-bit part of word into the block b according to endianness - - // Check whether we fall out of the block - if (block_seen + word_seen <= BlockBits) { - std::size_t last_word_ind = block_seen / word_bits; - std::size_t last_word_seen = block_seen % word_bits; - - // Remove garbage - w &= low_bits(~word_type(), word_seen); - b[last_word_ind] &= low_bits(~word_type(), last_word_seen); - - // Add significant word bits to block word - b[last_word_ind] |= unbounded_shl(w, last_word_seen); - - // If we fall out of the block word, push the remainder of element to the next block word - if (last_word_seen + word_seen > word_bits) - b[last_word_ind + 1] = unbounded_shr(w, word_bits - last_word_seen); - - block_seen += word_seen; - } - } - }; - - template - struct injector : word_injector { - - constexpr static const std::size_t word_bits = basic_functions::word_bits; - typedef typename basic_functions::word_type word_type; - - typedef std::array block_type; - - static void inject(const block_type &b_src, std::size_t b_src_seen, block_type &b_dst, - std::size_t &b_dst_seen, std::size_t block_shift = 0) { - // Insert word_seen-bit part of word into the block b according to endianness - - // Check whether we fall out of the block - if (b_src_seen + b_dst_seen <= BlockBits) { - - std::size_t first_word_ind = block_shift / word_bits; - std::size_t word_shift = block_shift % word_bits; - - std::size_t first_word_seen = - (word_bits - word_shift) > b_src_seen ? b_src_seen : (word_bits - word_shift); - - inject(b_src[first_word_ind], first_word_seen, b_dst, b_dst_seen, word_shift); - - b_src_seen -= first_word_seen; - - for (std::size_t i = 0; i < (b_src_seen / word_bits); i++) { - inject(b_src[first_word_ind + 1 + i], word_bits, b_dst, b_dst_seen); - } - - if (b_src_seen % word_bits) { - inject(b_src[first_word_ind + 1 + b_src_seen / word_bits], b_src_seen % word_bits, b_dst, - b_dst_seen); - } - } - } - - static void inject(word_type w, std::size_t word_seen, block_type &b, std::size_t &block_seen, - std::size_t word_shift = 0) { - - word_type word_shifted = w; - - if (word_shift > 0) { - endian_shift::to_msb(word_shifted, word_shift); - } - - word_injector::inject(word_shifted, word_seen, b, - block_seen); - } - }; - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_INJECT_HASH_HPP diff --git a/include/nil/crypto3/detail/inline_variable.hpp b/include/nil/crypto3/detail/inline_variable.hpp deleted file mode 100644 index 1c36127..0000000 --- a/include/nil/crypto3/detail/inline_variable.hpp +++ /dev/null @@ -1,60 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_INLINE_VARIABLE_HPP -#define CRYPTO3_INLINE_VARIABLE_HPP - -#define CRYPTO3_CXX_STD_14 201402L -#define CRYPTO3_CXX_STD_17 201703L - -#if defined(_MSVC_LANG) && _MSVC_LANG > __cplusplus // Older clangs define _MSVC_LANG < __cplusplus -#define CRYPTO3_CXX_VER _MSVC_LANG -#else -#define CRYPTO3_CXX_VER __cplusplus -#endif - -#ifndef CRYPTO3_CXX17_INLINE_VARIABLES -#ifdef __cpp_inline_variables -#define CRYPTO3_CXX17_INLINE_VARIABLES __cpp_inline_variables -#else -#define CRYPTO3_CXX17_INLINE_VARIABLES (CRYPTO3_CXX_VER >= CRYPTO3_CXX_STD_17) -#endif -#endif - -#ifdef CRYPTO3_CXX17_INLINE_VARIABLES -#define CRYPTO3_INLINE_VARIABLE(TYPE, NAME, VALUE) \ - constexpr static inline TYPE NAME() { \ - return TYPE VALUE; \ - } -#else -#define CRYPTO3_INLINE_VARIABLE(TYPE, NAME, VALUE) \ - struct NAME { \ - inline TYPE const &operator()() const { \ - static TYPE const v VALUE; \ - return v; \ - } \ - }; -#endif - -#endif // CRYPTO3_INLINE_VARIABLE_HPP diff --git a/include/nil/crypto3/detail/make_array.hpp b/include/nil/crypto3/detail/make_array.hpp deleted file mode 100644 index cd01123..0000000 --- a/include/nil/crypto3/detail/make_array.hpp +++ /dev/null @@ -1,75 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_MAKE_ARRAY_HPP -#define CRYPTO3_MAKE_ARRAY_HPP - -#include -#include - -namespace nil { - namespace crypto3 { - namespace detail { - template - struct indices { - using next = indices; - }; - template - struct build_indices { - using type = typename build_indices::type::next; - }; - template<> - struct build_indices<0> { - using type = indices<>; - }; - template - using BuildIndices = typename build_indices::type; - - template - using ValueType = typename std::iterator_traits::value_type; - - // internal overload with indices tag - - template, sizeof...(I)>> - - Array make_array(InputIterator first, indices) { - return Array {{(void(I), *first++)...}}; - } - } // namespace detail - - // externally visible interface - template - std::array, N> make_array(RandomAccessIterator first, - RandomAccessIterator last) { - // last is not relevant if we're assuming the size is N - // I'll assert it is correct anyway - assert(last - first == N); - return make_array(first, detail::BuildIndices {}); - } - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_MAKE_ARRAY_HPP diff --git a/include/nil/crypto3/detail/make_uint_t.hpp b/include/nil/crypto3/detail/make_uint_t.hpp deleted file mode 100644 index 8bebc31..0000000 --- a/include/nil/crypto3/detail/make_uint_t.hpp +++ /dev/null @@ -1,63 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_MAKE_UINT_T_HPP -#define CRYPTO3_MAKE_UINT_T_HPP - -#include - -#include - -namespace nil { - namespace crypto3 { - namespace detail { - template - static inline typename boost::uint_t::exact extract_uint_t(Integer v, std::size_t position) { - return static_cast::exact>(v >> - (((~position) & (sizeof(Integer) - 1)) << 3)); - } - - template - static inline typename boost::uint_t::exact make_uint_t(const std::initializer_list &args) { - typedef typename std::initializer_list::value_type value_type; - typename boost::uint_t::exact result = 0; - - - for (const value_type &itr : args) { - result = static_cast::exact>( - (result << std::numeric_limits::digits) | itr); - } - - return result; - } - - template - static inline typename boost::uint_t::exact make_uint_t(Args... args) { - return make_uint_t>::type>({args...}); - } - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_MAKE_UINT_T_HPP diff --git a/include/nil/crypto3/detail/octet.hpp b/include/nil/crypto3/detail/octet.hpp deleted file mode 100644 index 8c209e0..0000000 --- a/include/nil/crypto3/detail/octet.hpp +++ /dev/null @@ -1,37 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_OCTET_HPP -#define CRYPTO3_OCTET_HPP - -#include - -namespace nil { - namespace crypto3 { - constexpr static const std::size_t octet_bits = CHAR_BIT; - typedef boost::uint_t::least octet_type; - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_OCTET_HPP diff --git a/include/nil/crypto3/detail/pack.hpp b/include/nil/crypto3/detail/pack.hpp deleted file mode 100644 index 658c7cf..0000000 --- a/include/nil/crypto3/detail/pack.hpp +++ /dev/null @@ -1,954 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// Copyright (c) 2020 Alexander Sokolov -// -// 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_DETAIL_PACK_HPP -#define CRYPTO3_DETAIL_PACK_HPP - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -namespace nil { - namespace crypto3 { - namespace detail { - - /*! - * @defgroup pack Pack functions - */ - - /*! - * @brief The group of traits below is used to determine the possibility of fast data copy. - * By fast data copy we mean that the data is stored contiguously in the memory, so it can be - * copied faster byte-by-byte. Currently, fast data copy is implemented by memcpy function call. - */ - - /*! - * @brief host_can_memcpy trait checks whether the data to be copied and the container to be copied to - * are byte-aligned. Parameter types InT and OutT may refer to pointed data types or to iterator types. - * - * @ingroup pack - * - * @tparam UnitBits - * @tparam ValueBits - * @tparam InT - * @tparam OutT - */ - template - struct host_can_memcpy { - constexpr static const bool value = !(UnitBits % CHAR_BIT) && InputBits >= UnitBits && - OutputBits >= UnitBits && sizeof(InT) * CHAR_BIT == InputBits && - sizeof(OutT) * CHAR_BIT == OutputBits; - }; - - /*! - * @brief can_memcpy trait is derived from host_can_memcpy trait and is invoked depending on - * data endianness. Note that there is a single endianness template parameter since otherwise - * we have to transform data in accordance with endianness conversion rules. - * - * @ingroup pack - * - * @tparam Endianness - * @tparam ValueBits - * @tparam InT - * @tparam OutT - */ - template - struct can_memcpy { - constexpr static const bool value = InputBits == OutputBits && sizeof(InT) == sizeof(OutT); - }; - - template - struct can_memcpy, InputBits, OutputBits, InT, OutT> - : host_can_memcpy { }; - -#ifdef BOOST_ENDIAN_LITTLE_BYTE_AVAILABLE - template - struct can_memcpy, InputBits, OutputBits, InT, OutT> - : host_can_memcpy { }; - - template - struct can_memcpy, InputBits, OutputBits, InT, OutT> - : host_can_memcpy { }; - -#elif defined(BOOST_ENDIAN_BIG_BYTE_AVAILABLE) - template - struct can_memcpy, ValueBits, InT, OutT> - : host_can_memcpy { }; - template - struct can_memcpy, ValueBits, InT, OutT> - : host_can_memcpy { }; -#endif - - /*! - * @brief Real_packer is used to transform input data divided into chunks of the bit size InputValueBits - * represented in input endianness (InputEndianness) - * into output data (of the same bit length) divided into chunks of the bit size OutputValueBits - * represented in output endianness (OutputEndianness). - * - * The choice of packer depends on the following conditions: - * 1. input and output chunk size relation (equal, less, or greater); - * 2. input and output endianness relation (same or different); - * 3. the possibility of fast data copy using memcpy. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputType - * @tparam OutputType - * @tparam SameEndianness - * @tparam Implode - * @tparam Explode - */ - template::value, - bool Implode = (InputValueBits < OutputValueBits), - bool Explode = (InputValueBits > OutputValueBits)> - struct real_packer { }; - - /*! - * @brief This real_packer deals with the case of equal sizes (i.e. InputValueBits == OutputValueBits) - * and same endianness representations (i.e., speaking informally, - * InputEndianness == OutputEndianness). It packs input elements with ValueBits size represented - * in Endianness endianness into output elements with the same ValueBits size represented in the - * same Endianness endianness. - * - * @ingroup pack - * - * @tparam Endianness - * @tparam ValueBits - * @tparam InputType - * @tparam OutputType - */ - template - struct real_packer { - /*! - * @brief Packs n InputType elements pointed by constant pointer in - * (which, hence, cannot be iterated) into OutType elements pointed by out. - * This function is invoked only if memcpy call is possible. - * - * @ingroup pack - * - * @param in - * @param n - * @param out - * - * @return - */ - template - inline static typename std::enable_if< - can_memcpy::value>::type - pack_n(InputType const *in, std::size_t n, OutputType *out) { - std::memcpy(out, in, n * sizeof(InputType)); - } - - /*! - * @brief Packs n InputType elements pointed by pointer in into OutType elements pointed by out. - * This function is invoked only if memcpy call is possible. - * - * @ingroup pack - * - * @param in - * @param n - * @param out - * - * @return - */ - template - inline static typename std::enable_if< - can_memcpy::value>::type - pack_n(InputType *in, std::size_t n, OutputType *out) { - std::memcpy(out, in, n * sizeof(InputType)); - } - - /*! - * @brief Packs in_n elements iterated by in into elements iterated by out. - * - * @ingroup pack - * - * @tparam InputIterator - * @tparam OutputIterator - * - * @param in - * @param in_n - * @param out - * - * @return - */ - template - inline static void pack_n(InputIterator in, std::size_t in_n, OutputIterator out) { - std::copy(in, in + in_n, out); - } - - /*! - * @brief Packs elements in range [first, last) into elements iterated by out. - * This function is invoked only if input and output iterators meet RandomAccessIterator - * requirements. However, the restriction can be weakened to ContiguousIterator usage. - * - * @ingroup pack - * - * @tparam InputIterator - * @tparam OutputIterator - * - * @param first - * @param last - * @param random_access_iterator_tag - * @param out - * @param random_access_iterator_tag - * - * @return - */ - template - inline static void pack(InputIterator first, InputIterator last, std::random_access_iterator_tag, - OutputIterator out, std::random_access_iterator_tag) { - pack_n(first, std::distance(first, last), out); - } - - /*! - * @brief Packs elements in range [first, last) into elements iterated by out. - * This function is invoked only if input or output iterator doesn't meet RandomAccessIterator - * requirements. - * - * @ingroup pack - * - * @tparam InputIterator - * @tparam InCatT - * @tparam OutputIterator - * @tparam OutCatT - * - * @param first - * @param last - * @param InCatT - * @param out - * @param OutCatT - * - * @return - */ - template - inline static void pack(InputIterator first, InputIterator last, InCatT, OutputIterator out, OutCatT) { - std::copy(first, last, out); - } - - /*! - * @brief Generic function that chooses pack function depending on input and output iterator category. - * - * @ingroup pack - * - * @tparam InputIterator - * @tparam OutputIterator - * - * @param first - * @param last - * @param out - * - * @return - */ - template - inline static void pack(InputIterator first, InputIterator last, OutputIterator out) { - - typedef typename std::iterator_traits::iterator_category in_cat; - typedef typename std::iterator_traits::iterator_category out_cat; - - pack(first, last, in_cat(), out, out_cat()); - } - }; - - /*! - * @brief This real_packer deals with the case of equal sizes (i.e. InputValueBits == OutputValueBits) - * and different endianness representations (or, speaking informally, - * InputEndianness != OutputEndianness). It invokes functions which pack input elements - * with ValueBits size represented in InputEndianness endianness into output elements - * with the same ValueBits size represented in another OutputEndianness endianness. - * - * @ingroup pack - * - * @tparam UnitBits - * @tparam InputEndian - * @tparam OutputEndian - * @tparam ValueBits - * @tparam InputType - * @tparam OutputType - */ - template class InputEndian, template class OutputEndian, - std::size_t ValueBits, typename InputType, typename OutputType> - struct real_packer, OutputEndian, ValueBits, ValueBits, InputType, - OutputType, false, false, false> { - - typedef InputEndian InputEndianness; - typedef OutputEndian OutputEndianness; - - typedef unit_reverser units_reverser; - typedef bit_reverser bits_reverser; - - template - inline static void pack_n(InputIterator in, std::size_t in_n, OutputIterator out) { - - std::transform(in, in + in_n, out, [](InputType const &elem) { - return units_reverser::reverse(bits_reverser::reverse(elem)); - }); - } - - template - inline static void pack(InputIterator first, InputIterator last, OutputIterator out) { - - std::transform(first, last, out, [](InputType const &elem) { - return units_reverser::reverse(bits_reverser::reverse(elem)); - }); - } - }; - - /*! - * @brief This real_packer deals with case InputValueBits < OutputValueBits and invokes implode function, - * which, in its turn, packs input elements with InputValueBits size represented in InputEndianness - * endianness into output elements with OutputValueBits size represented in OutputEndianness - * endianness. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputType - * @tparam OutputType - * @tparam SameEndianness - */ - template - struct real_packer { - - BOOST_STATIC_ASSERT(!(OutputValueBits % InputValueBits)); - - typedef nil::crypto3::detail::imploder - imploder; - - template - inline static void pack_n(InputIterator in, std::size_t in_n, OutputIterator out) { - std::size_t out_n = in_n / (OutputValueBits / InputValueBits); - - while (out_n--) { - OutputType value = OutputType(); - imploder::implode(in, value); - *out++ = value; - } - } - - template - inline static void pack(InputIterator first, InputIterator last, OutputIterator out) { - while (first != last) { - OutputType value = OutputType(); - imploder::implode(first, value); - *out++ = value; - } - } - }; - - /*! - * @brief This real_packer deals with case InputValueBits > OutputValueBits and invokes explode function, - * which, in its turn, packs input elements with InputValueBits size represented in InputEndianness - * endianness into output elements with OutputValueBits size represented in OutputEndianness - * endianness. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputType - * @tparam OutputType - * @tparam SameEndianness - */ - template - struct real_packer { - - BOOST_STATIC_ASSERT(!(InputValueBits % OutputValueBits)); - - typedef nil::crypto3::detail::exploder - exploder; - - template - inline static void pack_n(InputIterator in, std::size_t in_n, OutputIterator out) { - while (in_n--) { - InputType const value = *in++; - exploder::explode(value, out); - } - } - - template - inline static void pack(InputIterator first, InputIterator last, OutputIterator out) { - while (first != last) { - InputType const value = *first++; - exploder::explode(value, out); - } - } - }; - - /*! - * @brief This packer deals with arbitrary input and output (but not bool) data elements. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputType - * @tparam OutputType - */ - template - struct packer { - - template - inline static void pack_n(InputIterator in, std::size_t n, OutputIterator out) { - typedef real_packer - packer_type; - - packer_type::pack_n(in, n, out); - } - - template - inline static void pack(InputIterator first, InputIterator last, OutputIterator out) { - typedef real_packer - packer_type; - - packer_type::pack(first, last, out); - } - }; - - /*! - * @brief This packer deals with bool input and output data elements. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - */ - template - struct packer { - - template - inline static void pack_n(InputIterator in, std::size_t n, OutputIterator out) { - typedef real_packer - packer_type; - - packer_type::pack_n(in, n, out); - } - - template - inline static void pack(InputIterator first, InputIterator last, OutputIterator out) { - typedef real_packer - packer_type; - - packer_type::pack(first, last, out); - } - }; - - /*! - * @brief This packer deals with bool input data and arbitrary (but not bool) output data elements. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam OutputType - */ - template - struct packer { - - template - inline static void pack_n(InputIterator in, std::size_t n, OutputIterator out) { - typedef real_packer - packer_type; - - packer_type::pack_n(in, n, out); - } - - template - inline static void pack(InputIterator first, InputIterator last, OutputIterator out) { - typedef real_packer - packer_type; - - packer_type::pack(first, last, out); - } - }; - - /*! - * @brief This packer deals with arbitrary (but not bool) input data and bool output data elements. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputType - */ - template - struct packer { - - template - inline static void pack_n(InputIterator in, std::size_t n, OutputIterator out) { - typedef real_packer - packer_type; - - packer_type::pack_n(in, n, out); - } - - template - inline static void pack(InputIterator first, InputIterator last, OutputIterator out) { - typedef real_packer - packer_type; - - packer_type::pack(first, last, out); - } - }; - - /*! - * @brief Packs elements from range [first, last) represented in machine-dependent endianness - * into elements starting from out represented in OutputEndianness endianness. - * - * @ingroup pack - * - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam OutputIterator - * - * @param first - * @param last - * @param out - * - * @return - */ - template - inline void pack_to(InputIterator first, InputIterator last, OutputIterator out) { - - typedef typename std::iterator_traits::value_type InputType; - typedef typename std::iterator_traits::value_type OutputType; - -#ifdef BOOST_ENDIAN_BIG_BYTE_AVAILABLE - typedef packer - packer_type; -#elif defined(BOOST_ENDIAN_LITTLE_BYTE_AVAILABLE) - typedef packer - packer_type; -#elif defined(BOOST_ENDIAN_BIG_WORD_AVAILABLE) - typedef packer, OutputEndianness, - InputValueBits, OutputValueBits, InputType, OutputType> - packer_type; -#elif defined(BOOST_ENDIAN_LITTLE_WORD_AVAILABLE) - typedef packer, OutputEndianness, - InputValueBits, OutputValueBits, InputType, OutputType> - packer_type; -#else -#error "Unknown endianness" -#endif - - packer_type::pack(first, last, out); - } - - /*! - * @brief Packs elements from range [first, last) represented in InputEndianness endianness - * into elements starting from out represented in machine-dependent endianness. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam OutputIterator - * - * @param first - * @param last - * @param out - * - * @return - */ - template - inline void pack_from(InputIterator first, InputIterator last, OutputIterator out) { - - typedef typename std::iterator_traits::value_type InputType; - typedef typename std::iterator_traits::value_type OutputType; - -#ifdef BOOST_ENDIAN_BIG_BYTE_AVAILABLE - typedef packer - packer_type; -#elif defined(BOOST_ENDIAN_LITTLE_BYTE_AVAILABLE) - typedef packer - packer_type; -#elif defined(BOOST_ENDIAN_BIG_WORD_AVAILABLE) - typedef packer, - InputValueBits, OutputValueBits, InputType, OutputType> - packer_type; -#elif defined(BOOST_ENDIAN_LITTLE_WORD_AVAILABLE) - typedef packer, - InputValueBits, OutputValueBits, InputType, OutputType> - packer_type; -#else -#error "Unknown endianness" -#endif - - packer_type::pack(first, last, out); - } - - /*! - * @brief Packs in_n input elements starting from in into output elements beginning from out. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam OutputIterator - * - * @param in - * @param in_n - * @param out - * - * @return - */ - template - inline void pack_n(InputIterator in, std::size_t in_n, OutputIterator out) { - typedef typename std::iterator_traits::value_type InputType; - typedef typename std::iterator_traits::value_type OutputType; - typedef packer - packer_type; - - packer_type::pack_n(in, in_n, out); - } - - /*! - * @brief Packs in_n input elements starting from in into in_out elements beginning from out. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam OutputIterator - * - * @param in - * @param in_n - * @param out - * @param out_n - * - * @return - */ - template - inline void pack_n(InputIterator in, std::size_t in_n, OutputIterator out, std::size_t out_n) { - BOOST_ASSERT(in_n * InputValueBits == out_n * OutputValueBits); - - pack_n(in, in_n, out); - } - - /*! - * @brief Packs elements from the range [first, last) into elements starting from out. - * Works for input containers meeting RandomAccessIterator requirements. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam OutputIterator - * - * @param first - * @param last - * @param random_access_iterator_tag - * @param out - * - * @return - */ - template - inline void pack(InputIterator first, InputIterator last, std::random_access_iterator_tag, - OutputIterator out) { - pack_n(first, last - first, out); - } - - /*! - * @brief Packs elements from the range [first, last) into elements starting from out. - * Works for input containers meeting InCatT category requirements and output containers - * meeting OutputIterator requirements. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam InCatT - * @tparam OutputIterator - * - * @param first - * @param last - * @param InCatT - * @param out - * - * @return - */ - template::value>::type, - typename = typename std::enable_if::value>::type> - inline void pack(InputIterator first, InputIterator last, InCatT, OutputIterator out) { - typedef typename std::iterator_traits::value_type InputType; - typedef typename std::iterator_traits::value_type OutputType; - typedef packer - packer_type; - - packer_type::pack(first, last, out); - } - - /*! - * @brief Generic function that chooses pack function depending on input iterator category. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam OutputIterator - * - * @param first - * @param last - * @param out - * - * @return - */ - template::value>::type> - inline void pack(InputIterator first, InputIterator last, OutputIterator out) { - typedef typename std::iterator_traits::iterator_category in_cat; - - pack(first, last, in_cat(), out); - } - - /*! - * @brief Packs elements from the range [first, last) into elements starting from out. - * Works for input and output containers meeting RandomAccessIterator requirements. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam OutputIterator - * - * @param in_first - * @param in_last - * @param random_access_iterator_tag - * @param out_first - * @param out_last - * @param random_access_iterator_tag - * - * @return - */ - template - inline void pack(InputIterator in_first, InputIterator in_last, std::random_access_iterator_tag, - OutputIterator out_first, OutputIterator out_last, std::random_access_iterator_tag) { - pack_n( - in_first, in_last - in_first, out_first, out_last - out_first); - } - - /*! - * @brief Packs elements from the range [first, last) into elements starting from out. - * Works for input containers meeting InCatT category requirements and output containers - * meeting OutCatT category requirements. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam InCatT - * @tparam OutputIterator - * @tparam OutCatT - * - * @param in_first - * @param in_last - * @param InCatT - * @param out - * @param OutputIterator - * @param OutCatT - * - * @return - */ - template - inline void pack(InputIterator in_first, InputIterator in_last, InCatT, OutputIterator out, OutputIterator, - OutCatT) { - pack(in_first, in_last, out); - } - - /*! - * @brief Generic function that chooses pack function depending on input and output iterator category. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputIterator - * @tparam OutputIterator - * - * @param in_first - * @param in_last - * @param out_first - * @param out_last - * - * @return - */ - template - inline void pack(InputIterator in_first, InputIterator in_last, OutputIterator out_first, - OutputIterator out_last) { - typedef typename std::iterator_traits::iterator_category in_cat; - typedef typename std::iterator_traits::iterator_category out_cat; - - pack( - in_first, in_last, in_cat(), out_first, out_last, out_cat()); - } - - /*! - * @brief Packs immutable data referenced by in into data referenced by out. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputType - * @tparam OutputType - * - * @param in - * @param out - * - * @return - */ - template - inline void pack(const InputType &in, OutputType &out) { - pack_n(in.begin(), in.size(), - out.begin(), out.size()); - } - - /*! - * @brief Packs elements from range [first, last) into data referenced by out with - * non-arithmetic value type. - * - * @ingroup pack - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam InputValueBits - * @tparam OutputValueBits - * @tparam InputType - * @tparam OutputType - * - * @param in - * @param out - * - * @return - */ - template::value>::type> - inline void pack(InputIterator first, InputIterator last, OutputType &out) { - pack_n( - first, std::distance(first, last), out.begin(), out.size()); - } - - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_DETAIL_PACK_HPP diff --git a/include/nil/crypto3/detail/pack_numeric.hpp b/include/nil/crypto3/detail/pack_numeric.hpp deleted file mode 100644 index 7e0ebe0..0000000 --- a/include/nil/crypto3/detail/pack_numeric.hpp +++ /dev/null @@ -1,62 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_PACK_NUMERIC_HPP -#define CRYPTO3_PACK_NUMERIC_HPP - -#include -#include - -#include - -namespace nil { - namespace crypto3 { - namespace detail { - using namespace nil::crypto3::multiprecision; - - template - inline void pack(InputIterator first, InputIterator last, number &out) { - import_bits(out, first, last); - BOOST_ASSERT(msb(out) == OutValueBits); - } - - template - inline void pack(const InputType &in, number &out) { - import_bits(out, in.begin(), in.end()); - BOOST_ASSERT(msb(out) == OutValueBits); - } - - template - inline void pack(const number &in, OutputType &out) { - export_bits(in, out); - BOOST_ASSERT(msb(out) == OutValueBits); - } - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_BLOCK_PACK_HPP diff --git a/include/nil/crypto3/detail/predef.hpp b/include/nil/crypto3/detail/predef.hpp deleted file mode 100644 index 0bc921e..0000000 --- a/include/nil/crypto3/detail/predef.hpp +++ /dev/null @@ -1,198 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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. -//---------------------------------------------------------------------------// -// @file This particular header is responsible for defining current architecture's -// (the architecture the library being compiled for) properties such as amount of -// bits target machine CPU's machine word is being represented with. -// This particular header is a pretty temporary one, and is present in Crypto3 library -// until following PR's are not accepted: https://github.com/boostorg/predef/pull/108, -// https://github.com/boostorg/predef/pull/107. -//---------------------------------------------------------------------------// - -#ifndef CRYPTO3_DETAIL_PREDEF_HPP -#define CRYPTO3_DETAIL_PREDEF_HPP - -#include - -#if defined(BOOST_ARCH_ALPHA_AVAILABLE) && defined(BOOST_ARCH_ALPHA_NAME) -#define BOOST_ARCH_ALPHA_WORD_BITS 64 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_ALPHA_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_ALPHA_WORD_BITS -#elif defined(BOOST_ARCH_ARM_AVAILABLE) && defined(BOOST_ARCH_ARM_NAME) -#if defined(__ARM_ARCH) || defined(__TARGET_ARCH_ARM) || defined(__TARGET_ARCH_THUMB) || defined(_M_ARM) || \ - defined(__arm__) || defined(__arm64) || defined(__thumb__) || defined(_M_ARM64) || defined(__aarch64__) || \ - defined(__AARCH64EL__) || defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || \ - defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6KZ__) || \ - defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__) || \ - defined(__ARM_ARCH_4T__) || defined(__ARM_ARCH_4__) -#if (defined(__arm64) || defined(_M_ARM64) || defined(__aarch64__) || defined(__AARCH64EL__)) -#define BOOST_ARCH_ARM_WORD_BITS 64 -#endif -#if (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__)) -#define BOOST_ARCH_ARM_WORD_BITS 32 -#endif -#if (defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6KZ__) || defined(__ARM_ARCH_6T2__)) -#define BOOST_ARCH_ARM_WORD_BITS 32 -#endif -#if (defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__)) -#define BOOST_ARCH_ARM_WORD_BITS 32 -#endif -#if (defined(__ARM_ARCH_4T__) || defined(__ARM_ARCH_4__)) -#define BOOST_ARCH_ARM_WORD_BITS 32 -#endif -#endif -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_ARM_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_ARM_WORD_BITS -#elif defined(BOOST_ARCH_BLACKFIN_AVAILABLE) && defined(BOOST_ARCH_BLACKFIN_NAME) -#define BOOST_ARCH_BLACKFIN_WORD_BITS 16 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_BLACKFIN_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_BLACKFIN_WORD_BITS -#elif defined(BOOST_ARCH_CONVEX_AVAILABLE) && defined(BOOST_ARCH_CONVEX_NAME) -#define BOOST_ARCH_CONVEX_WORD_BITS 32 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_CONVEX_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_CONVEX_WORD_BITS -#elif defined(BOOST_ARCH_IA64_AVAILABLE) && defined(BOOST_ARCH_IA64_NAME) -#define BOOST_ARCH_IA64_WORD_BITS 64 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_IA64_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_IA64_WORD_BITS -#elif defined(BOOST_ARCH_M68K_AVAILABLE) && defined(BOOST_ARCH_M68K_NAME) -#define BOOST_ARCH_M68K_WORD_BITS 32 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_M68K_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_M68K_WORD_BITS -#elif defined(BOOST_ARCH_MIPS_AVAILABLE) && defined(BOOST_ARCH_MIPS_NAME) -#if defined(__mips__) || defined(__mips) || defined(__MIPS__) -#if !defined(BOOST_ARCH_MIPS) && (defined(_MIPS_ISA_MIPS1) || defined(_R3000)) -#define BOOST_ARCH_MIPS_WORD_BITS 32 -#endif -#if !defined(BOOST_ARCH_MIPS) && (defined(_MIPS_ISA_MIPS2) || defined(__MIPS_ISA2__) || defined(_R4000)) -#define BOOST_ARCH_MIPS_WORD_BITS 32 -#endif -#if !defined(BOOST_ARCH_MIPS) && (defined(_MIPS_ISA_MIPS3) || defined(__MIPS_ISA3__)) -#define BOOST_ARCH_MIPS_WORD_BITS 64 -#endif -#if !defined(BOOST_ARCH_MIPS) && (defined(_MIPS_ISA_MIPS4) || defined(__MIPS_ISA4__)) -#define BOOST_ARCH_MIPS_WORD_BITS 64 -#endif -#endif -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_MIPS_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_MIPS_WORD_BITS -#elif defined(BOOST_ARCH_PARISC_AVAILABLE) && defined(BOOST_ARCH_PARISC_NAME) -#define BOOST_ARCH_PARISC_WORD_BITS 32 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_PARISC_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_PARISC_WORD_BITS -#elif defined(BOOST_ARCH_PPC_AVAILABLE) && defined(BOOST_ARCH_PPC_NAME) -#if defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) || defined(__ppc__) || defined(_M_PPC) || \ - defined(_ARCH_PPC) || defined(__PPCGECKO__) || defined(__PPCBROADWAY__) || defined(_XENON) -#if !defined(BOOST_ARCH_PPC) && (defined(__ppc601__) || defined(_ARCH_601)) -#define BOOST_ARCH_PPC_WORD_BITS 32 -#endif -#if !defined(BOOST_ARCH_PPC) && (defined(__ppc603__) || defined(_ARCH_603)) -#define BOOST_ARCH_PPC_WORD_BITS 32 -#endif -#if !defined(BOOST_ARCH_PPC) && (defined(__ppc604__) || defined(__ppc604__)) -#define BOOST_ARCH_PPC_WORD_BITS 32 -#endif -#endif -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_PPC_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_PPC_WORD_BITS -#elif defined(BOOST_ARCH_PTX_AVAILABLE) && defined(BOOST_ARCH_PTX_NAME) -#define BOOST_ARCH_PTX_WORD_BITS 64 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_PTX_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_PTX_WORD_BITS -#elif defined(BOOST_ARCH_PYRAMID_AVAILABLE) && defined(BOOST_ARCH_PYRAMID_NAME) -#define BOOST_ARCH_PYRAMID_WORD_BITS 32 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_PYRAMID_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_PYRAMID_WORD_BITS -#elif defined(BOOST_ARCH_RISCV_AVAILABLE) && defined(BOOST_ARCH_RISCV_NAME) -#define BOOST_ARCH_RISCV_WORD_BITS 32 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_RISCV_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_RISCV_WORD_BITS -#elif defined(BOOST_ARCH_RS6K_AVAILABLE) && defined(BOOST_ARCH_RS6K_NAME) -#define BOOST_ARCH_PWR BOOST_ARCH_RS6000 -#define BOOST_ARCH_PWR_NAME BOOST_ARCH_RS6000_NAME -#define BOOST_ARCH_PWR_WORD_BITS 32 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_RS6K_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_RS6K_WORD_BITS -#elif defined(BOOST_ARCH_SPARC_AVAILABLE) && defined(BOOST_ARCH_SPARC_NAME) -#if defined(__sparc__) || defined(__sparc) -#undef BOOST_ARCH_SPARC -#if !defined(BOOST_ARCH_SPARC) && defined(__sparcv9) -#define BOOST_ARCH_SPARC_WORD_BITS 64 -#endif -#if !defined(BOOST_ARCH_SPARC) && defined(__sparcv8) -#define BOOST_ARCH_SPARC_WORD_BITS 32 -#endif -#endif -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_SPARC_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_SPARC_WORD_BITS -#elif defined(BOOST_ARCH_SUPERH_AVAILABLE) && defined(BOOST_ARCH_SUPERH_NAME) -#if defined(__sh__) -#undef BOOST_ARCH_SH -#if !defined(BOOST_ARCH_SH) && (defined(__SH5__)) -#define BOOST_ARCH_SH_WORD_BITS 64 -#endif -#if !defined(BOOST_ARCH_SH) && (defined(__SH4__)) -#define BOOST_ARCH_SH_WORD_BITS 32 -#endif -#if !defined(BOOST_ARCH_SH) && (defined(__sh3__) || defined(__SH3__)) -#define BOOST_ARCH_SH_WORD_BITS 32 -#endif -#if !defined(BOOST_ARCH_SH) && (defined(__sh2__)) -#define BOOST_ARCH_SH_WORD_BITS 16 -#endif -#if !defined(BOOST_ARCH_SH) && (defined(__sh1__)) -#define BOOST_ARCH_SH_WORD_BITS 16 -#endif -#endif -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_SUPERH_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_SUPERH_WORD_BITS -#elif defined(BOOST_ARCH_SYS370_AVAILABLE) && defined(BOOST_ARCH_SYS370_NAME) -#define BOOST_ARCH_SYS370_WORD_BITS 32 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_SYS370_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_SYS370_WORD_BITS -#elif defined(BOOST_ARCH_SYS390_AVAILABLE) && defined(BOOST_ARCH_SYS390_NAME) -#define BOOST_ARCH_SYS390_WORD_BITS 32 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_SYS390_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_SYS390_WORD_BITS -#elif defined(BOOST_ARCH_X86_AVAILABLE) && defined(BOOST_ARCH_X86_NAME) -#define BOOST_ARCH_X86_32_WORD_BITS 32 -#define BOOST_ARCH_X86_64_WORD_BITS 64 -#if defined(BOOST_ARCH_x86_32) -#define BOOST_ARCH_X86_WORD_BITS BOOST_ARCH_X86_32_WORD_BITS -#define BOOST_ARCH_X86_NAME BOOST_ARCH_X86_32_NAME -#elif defined(BOOST_ARCH_X86_64) -#define BOOST_ARCH_X86_WORD_BITS BOOST_ARCH_X86_64_WORD_BITS -#endif -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_X86_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_X86_WORD_BITS -#elif defined(BOOST_ARCH_Z_AVAILABLE) && defined(BOOST_ARCH_Z_NAME) -#define BOOST_ARCH_Z_WORD_BITS 64 -#define BOOST_ARCH_CURRENT_NAME BOOST_ARCH_Z_NAME -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_Z_WORD_BITS -#elif defined(__EMSCRIPTEN__) -#define BOOST_ARCH_EMSCRIPTEN_WORD_BITS 32 -#define BOOST_ARCH_CURRENT_NAME "Emscripten" -#define BOOST_ARCH_CURRENT_WORD_BITS BOOST_ARCH_EMSCRIPTEN_WORD_BITS -#endif - -#endif // CRYPTO3_PREDEF_HPP diff --git a/include/nil/crypto3/detail/primes.hpp b/include/nil/crypto3/detail/primes.hpp deleted file mode 100644 index f234031..0000000 --- a/include/nil/crypto3/detail/primes.hpp +++ /dev/null @@ -1,94 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_HASH_DETAIL_PRIMES_HPP -#define CRYPTO3_HASH_DETAIL_PRIMES_HPP - -#include - -namespace nil { - namespace crypto3 { - namespace detail { - - template - struct all_ones { - typedef typename boost::uint_t::least type; - static type const value = (all_ones::value << 1) | 1; - }; - template<> - struct all_ones<0> { - typedef boost::uint_t<0>::least type; - static type const value = 0; - }; - - template - struct largest_prime; - -#define CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(B, D) \ - template<> \ - struct largest_prime { \ - constexpr static boost::uint_t::least const value = all_ones::value - D; \ - }; \ - constexpr boost::uint_t::least const largest_prime::value; - - // http://primes.utm.edu/lists/2small/0bit.html or - // http://www.research.att.com/~njas/sequences/A013603 - // Though those offets are from 2**b; This code is offsets from 2**b-1 - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(2, 0); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(3, 0); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(4, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(5, 0); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(6, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(7, 0); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(8, 4); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(9, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(10, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(11, 8); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(12, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(13, 0); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(14, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(15, 18); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(16, 14); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(17, 0); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(18, 4); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(19, 0); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(20, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(21, 8); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(22, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(23, 14); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(24, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(25, 38); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(26, 4); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(27, 38); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(28, 56); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(29, 2); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(30, 34); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(31, 0); - CRYPTO3_HASH_DEFINE_LARGEST_PRIME_BY_OFFSET(32, 4); - - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_HASH_DETAIL_PRIMES_HPP diff --git a/include/nil/crypto3/detail/reverser.hpp b/include/nil/crypto3/detail/reverser.hpp deleted file mode 100644 index 9eb61e1..0000000 --- a/include/nil/crypto3/detail/reverser.hpp +++ /dev/null @@ -1,519 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// Copyright (c) 2020 Alexander Sokolov -// Copyright (c) 2020 Nikita Kaskov -// -// 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_DETAIL_REVERSER_HPP -#define CRYPTO3_DETAIL_REVERSER_HPP - -#include -#include - -#include -#include -#include - -#include -#include -#include - -namespace nil { - namespace crypto3 { - namespace detail { - - /*! - * @defgroup reverser Reverser functions - */ - - typedef typename boost::uint_t::exact byte_type; - - /*! - * @brief This function reverses bit order in the byte b depending on the machine word size. - * The underlying algorithms used in this function are described in - * http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits and in - * http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64BitsDiv . - * - * @ingroup reverser - * - * @param b - * - * @return - */ - inline void reverse_byte(byte_type &b) { - - - -#if (BOOST_ARCH_CURRENT_WORD_BITS == 32) - b = unbounded_shr<16>(((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU); -#elif (BOOST_ARCH_CURRENT_WORD_BITS == 64) - b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023; -#else -#error "BOOST_ARCH_CURRENT_WORD_BITS not set" -#endif - } - - /*! - * @brief bit_in_unit_byte_reverser transforms the sequence of bits in each byte of - * the input unit into reversed sequence of bits in each byte of the output unit. - * The function reverse is recursively invoked and the parameter k is used to track - * the number of already processed input bytes. The recursion ends, when all input - * bytes have been processed, i.e. when k == UnitBits. - * - * @ingroup reverser - * - * @tparam UnitBits - * @tparam k - */ - template - struct bit_in_unit_byte_reverser { - - BOOST_STATIC_ASSERT(!(UnitBits % CHAR_BIT)); - - typedef bit_in_unit_byte_reverser next_type; - typedef typename boost::uint_t::exact UnitType; - - inline static void reverse(UnitType &in, UnitType &out) { - int const shift = UnitBits - (CHAR_BIT + k); - byte_type byte = byte_type(low_bits(unbounded_shr(in, shift))); - reverse_byte(byte); - out |= unbounded_shl(low_bits(UnitType(byte)), shift); - - next_type::reverse(in, out); - } - }; - - template - struct bit_in_unit_byte_reverser { - inline static void reverse(typename boost::uint_t::exact &, - typename boost::uint_t::exact &) { - } - }; - - /*! - * @brief The functions listed below deal with bit reversal in a unit. - */ - - /*! - * @brief This function deals with the case of UnitBits > CHAR_BIT. To reverse - * the order of bits, first, it reverses the byte order of unit, and then, it - * invokes bit_in_unit_byte_reverser to reverse bits in each byte of unit. - * - * @ingroup reverser - * - * @tparam UnitType - * @tparam UnitBits - * - * @param unit - * - * @return - */ - template CHAR_BIT), int>::type = 0> - inline void reverse_bits(UnitType &unit) { - boost::endian::endian_reverse_inplace(unit); - UnitType out = UnitType(); - bit_in_unit_byte_reverser::reverse(unit, out); - unit = out; - } - /*! - * @brief This function deals with the special case of UnitBits == CHAR_BIT, - * it just reverses the bit order in the byte. - * @ingroup reverser - * - * @tparam UnitType - * @tparam UnitBits - * - * @param unit - * - * @return - */ - template::type = 0> - inline void reverse_bits(UnitType &unit) { - reverse_byte(unit); - } - - /*! - * @brief bit_in_unit_reverser transforms the sequence of bits in each unit of - * the input value into reversed sequence of bytes in each unit of the output value. - * The function reverse is recursively invoked and the parameter k is used to track - * the number of already processed input units. The recursion ends, when all input - * units have been processed, i.e. when k == InputBits. - * - * @ingroup reverser - * - * @tparam InputBits - * @tparam UnitBits - * @tparam k - */ - template - struct bit_in_unit_reverser { - - BOOST_STATIC_ASSERT(!(InputBits % UnitBits) && !(UnitBits % CHAR_BIT)); - - typedef bit_in_unit_reverser next_type; - typedef typename boost::uint_t::exact UnitType; - - template - inline static void reverse(ValueType &in, ValueType &out) { - int const shift = InputBits - (UnitBits + k); - UnitType unit = UnitType(low_bits(unbounded_shr(in, shift))); - reverse_bits(unit); - out |= unbounded_shl(low_bits(ValueType(unit)), shift); - - next_type::reverse(in, out); - } - }; - - template - struct bit_in_unit_reverser { - template - inline static void reverse(ValueType &, ValueType &) { - } - }; - - /*! - * @brief The group of traits below is used to determine the order of bits defined - * by the endianness. - */ - - /*! - * @brief Trait to determine whether the order of bits defined by Endianness endianness - * is big. - * - * @ingroup reverser - * - * @tparam Endianness - * @tparam UnitBits - */ - template - struct is_big_bit { - constexpr static const bool value = - std::is_same>::value || - std::is_same>::value; - }; - - /*! - * @brief Trait to determine whether the order of bits defined by Endianness endianness - * is little. - * - * @ingroup reverser - * - * @tparam Endianness - * @tparam UnitBits - */ - template - struct is_little_bit { - constexpr static const bool value = - std::is_same>::value || - std::is_same>::value; - }; - - /*! - * @brief Trait to determine whether the orders of bits defined by Endianness1 endianness - * and Endianness2 endianness are the same. - * - * @ingroup reverser - * - * @tparam Endianness1 - * @tparam Endianness2 - * @tparam UnitBits - */ - template - struct is_same_bit { - constexpr static const bool value = - (is_big_bit::value && is_big_bit::value) || - (is_little_bit::value && is_little_bit::value); - }; - - /*! - * @brief bit_reverser reverses the sequence of bits in each unit of the given value, - * if InputEndianness and OutputEndianness endiannesses have different bit orders, and - * does nothing, otherwise. - * - * @ingroup reverser - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam UnitBits - * @tparam IsSameBit - */ - template::value> - struct bit_reverser; - - /*! - * @brief This bit_reverser is a dummy and deals with the case of the endiannesses with - * the same order of bits. - * - * @ingroup reverser - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam UnitBits - */ - template - struct bit_reverser { - template - inline static void reverse(ValueType &) { - } - - template - inline static ValueType reverse(ValueType const &val) { - return val; - } - }; - - /*! - * @brief This bit_reverser deals with the case of the endiannesses with different order of - * bits and invokes bit_in_unit_reverser which reverses bits in each unit of the input value. - * - * @ingroup reverser - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam UnitBits - */ - template - struct bit_reverser { - template - inline static void reverse(ValueType &val) { - ValueType out = ValueType(); - bit_in_unit_reverser::reverse(val, out); - val = out; - } - - template - inline static ValueType reverse(ValueType const &val) { - ValueType tmp = val; - ValueType out = ValueType(); - bit_in_unit_reverser::reverse(tmp, out); - return out; - } - }; - - /*! - * @brief byte_in_unit_reverser transforms the sequence of bytes in each unit of - * the input value into reversed sequence of bytes in each unit of the output value. - * The function reverse is recursively invoked and the parameter k is used to track - * the number of already processed input units. The recursion ends, when all input - * units have been processed, i.e. when k == InputBits. - * - * @ingroup reverser - * - * @tparam InputBits - * @tparam UnitBits - * @tparam k - */ - template - struct byte_in_unit_reverser { - - BOOST_STATIC_ASSERT(!(InputBits % UnitBits) && !(UnitBits % CHAR_BIT)); - - typedef byte_in_unit_reverser next_type; - typedef typename boost::uint_t::exact UnitType; - - template - inline static void reverse(ValueType &in, ValueType &out) { - int const shift = InputBits - (UnitBits + k); - UnitType unit = UnitType(low_bits(unbounded_shr(in, shift))); - boost::endian::endian_reverse_inplace(unit); - out |= unbounded_shl(low_bits(ValueType(unit)), shift); - - next_type::reverse(in, out); - } - }; - - template - struct byte_in_unit_reverser { - template - inline static void reverse(ValueType &, ValueType &) { - } - }; - - /*! - * @brief The group of traits below is used to determine the order of units defined - * by the endianness. - */ - - /*! - * @brief Trait to determine whether the order of units defined by Endianness endianness - * is big. - * - * @ingroup reverser - * - * @tparam Endianness - * @tparam UnitBits - */ - template - struct is_big_unit { - constexpr static const bool value = - std::is_same>::value || - std::is_same>::value; - }; - - /*! - * @brief Trait to determine whether the order of units defined by Endianness endianness - * is little. - * - * @ingroup reverser - * - * @tparam Endianness - * @tparam UnitBits - */ - template - struct is_little_unit { - constexpr static const bool value = - std::is_same>::value || - std::is_same>::value; - }; - - /*! - * @brief Trait to determine whether the orders of units defined by Endianness1 endianness - * and Endianness2 endianness are the same. - * - * @ingroup reverser - * - * @tparam Endianness1 - * @tparam Endianness2 - * @tparam UnitBits - */ - template - struct is_same_unit { - constexpr static const bool value = - (is_big_unit::value && is_big_unit::value) || - (is_little_unit::value && is_little_unit::value); - }; - - /*! - * @brief unit_reverser reverses the sequence of units in the given value, if InputEndianness - * and OutputEndianness endiannesses have different unit orders, and does nothing, otherwise. - * - * @ingroup reverser - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam UnitBits - * @tparam Enable - */ - template - struct unit_reverser; - - /*! - * @brief This unit_reverser is a dummy and deals with the case of the endiannesses with - * the same order of units. - * - * @ingroup reverser - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam UnitBits - */ - template - struct unit_reverser< - InputEndianness, - OutputEndianness, - UnitBits, - typename std::enable_if::value>::type> { - template - inline static void reverse(ValueType &) { - } - - template - inline static ValueType reverse(ValueType const &val) { - return val; - } - }; - - /*! - * @brief This unit_reverser deals with the case of UnitBits == CHAR_BIT. This case is - * special since it is sufficient to reverse the order of bytes in an input value. - * - * @ingroup reverser - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam UnitBits - */ - template - struct unit_reverser< - InputEndianness, - OutputEndianness, - UnitBits, - typename std::enable_if::value && - UnitBits == CHAR_BIT>::type> { - template - inline static void reverse(ValueType &val) { - boost::endian::endian_reverse_inplace(val); - } - - template - inline static ValueType reverse(ValueType const &val) { - return boost::endian::endian_reverse(val); - } - }; - - /*! - * @brief This unit_reverser deals with the case of UnitBits > CHAR_BIT. To reverse the - * order of units, first, it reverses the byte order in an input value, and then, it - * invokes byte_in_unit_reverser to reverse the byte order in each unit of the input value. - * - * @ingroup reverser - * - * @tparam InputEndianness - * @tparam OutputEndianness - * @tparam UnitBits - */ - template - struct unit_reverser< - InputEndianness, - OutputEndianness, - UnitBits, - typename std::enable_if::value && - (UnitBits > CHAR_BIT)>::type> { - template - inline static void reverse(ValueType &val) { - boost::endian::endian_reverse_inplace(val); - ValueType out = ValueType(); - byte_in_unit_reverser::reverse(val, out); - val = out; - } - - template - inline static ValueType reverse(ValueType const &val) { - ValueType tmp = boost::endian::endian_reverse(val); - ValueType out = ValueType(); - byte_in_unit_reverser::reverse(tmp, out); - return out; - } - }; - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_DETAIL_REVERSER_HPP diff --git a/include/nil/crypto3/detail/state_adder.hpp b/include/nil/crypto3/detail/state_adder.hpp deleted file mode 100644 index bb69c58..0000000 --- a/include/nil/crypto3/detail/state_adder.hpp +++ /dev/null @@ -1,46 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_DETAIL_STATE_ADDER_HPP -#define CRYPTO3_DETAIL_STATE_ADDER_HPP - -namespace nil { - namespace crypto3 { - namespace detail { - struct state_adder { - template - void operator()(T &s1, T const &s2) { - typedef typename T::size_type size_type; - size_type n = (s2.size() < s1.size() ? s2.size() : s1.size()); - for (typename T::size_type i = 0; i < n; ++i) { - s1[i] += s2[i]; - } - } - }; - - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_BLOCK_DETAIL_STATE_ADDER_HPP diff --git a/include/nil/crypto3/detail/static_digest.hpp b/include/nil/crypto3/detail/static_digest.hpp deleted file mode 100644 index 951c114..0000000 --- a/include/nil/crypto3/detail/static_digest.hpp +++ /dev/null @@ -1,228 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_STATIC_DIGEST_HPP -#define CRYPTO3_STATIC_DIGEST_HPP - -#include -#include - -#include -#include -#include -#include - -#include -#include - -namespace nil { - namespace crypto3 { - /*! - * The digest class template stores a DigestBits-bit message digest as a sequence of 8-bit octets. - * Octets are stored in the smallest std::size_t type able to hold 8 bits, hereinafter referred to as - * octet_type. DigestBits must be a multiple of 8. - * - * It is independent of any particular algorithm; For example sha2<224> and cubehash<224> both produce a - * digest<224>. Each algorithm generates its digest such that it will be displayed in the canonical order - * for that algorithm. The truncate and resize function templates are provided to handle digests with - * lengths other than you're expecting. For instance, generating name-based UUIDs uses only 128 bits but - * SHA-1 provides a 160-bit digest, so it would be truncated. (Using truncate instead of resize means - * that a compilation error will result from trying to use a hash algorithm with too small an output.) On - * the other hand, for storing as much as possible of the results of various algorithms, resize allows - * you to pad them out to a large size, such as a digest<512>. - * - * digest derives publicly from std::array and supports all of - * its operations in order to provide direct access to the contained octets. Note that a digest is not - * an aggregate; A default-constructed digest has all its contained octets set to zero. The base_array() - * member function provides a reference to the std::array sub-object. - * - * digests with different numbers of bits may be compared. For the comparison, the smaller is considered - * as though it were padded with 0s out to the size of the larger. The operator< provides a strict total - * order. For convenience, equality comparison with narrow c-style strings is also provided. - * - * Always stored internally as a sequence of octets in display order. - * This allows digests from different algorithms to have the same type, - * allowing them to be more easily stored and compared. - * - * @tparam DigestBits - */ - - template - class static_digest : public std::array { }; - - namespace detail { - template - OutputIterator to_ascii(const static_digest &d, OutputIterator it) { - for (std::size_t j = 0; j < DigestBits / octet_bits; ++j) { - octet_type b = d[j]; - *it++ = "0123456789abcdef"[(b >> 4) & 0xF]; - *it++ = "0123456789abcdef"[(b >> 0) & 0xF]; - } - return it; - } - - template - std::array c_str(const static_digest &d) { - std::array s; - char *p = to_ascii(d, s.data()); - *p++ = '\0'; - return s; - } - } // namespace detail - } // namespace crypto3 -} // namespace nil - -namespace std { - template - std::string to_string(const nil::crypto3::static_digest &d) { - std::array cstr = nil::crypto3::detail::c_str(d); - return std::string(cstr.data(), cstr.size() - 1); - } -} // namespace std - -namespace nil { - namespace crypto3 { - - /*! - * - * @tparam NewBits - * @tparam OldBits - * @param od - * @return Digest containing the first min(NewBits, OldBits) bits of the argument digest followed by max - * (0, NewBits - OldBits) bits. - */ - template - static_digest resize(const static_digest &od) { - static_digest nd; - nd.fill(0); - std::size_t bytes = sizeof(octet_type) * (NewBits < OldBits ? NewBits : OldBits) / octet_bits; - std::memcpy(nd.data(), od.data(), bytes); - return nd; - } - - /*! - * @tparam NewBits - * @tparam OldBits - * @return Digest containing only the first NewBits bits of the argument digest. - * - * Requires that NewBits <= OldBits. - * - * Truncating a message digest generally does not weaken the hash algorithm beyond the - * amount necessitated by the shorted output size. - */ - template - static_digest truncate(const static_digest &od) { - BOOST_STATIC_ASSERT(NewBits <= OldBits); - return resize(od); - } - - template - bool operator==(const static_digest &a, const static_digest &b) { - // TODO: Think about size of static_digest. We can't use resize here because - // it's change element which we compare - return static_cast(DB1 == DB2 ? std::equal(std::begin(a), std::end(a), std::begin(b), std::end(b)) : - 0); - } - - template - bool operator!=(const static_digest &a, const static_digest &b) { - return !(a == b); - } - - template - bool operator<(const static_digest &a, const static_digest &b) { - // #TODO: Implement this right - return DB1 < DB2; - } - - template - bool operator>(const static_digest &a, const static_digest &b) { - return b < a; - } - - template - bool operator<=(const static_digest &a, const static_digest &b) { - return !(b < a); - } - - template - bool operator>=(const static_digest &a, const static_digest &b) { - return !(b > a); - } - - template - bool operator!=(const static_digest &a, char const *b) { - BOOST_ASSERT(std::strlen(b) == DB / 4); - return std::to_string(a) != b; - } - - template - bool operator==(const static_digest &a, char const *b) { - return !(a != b); - } - - template - bool operator!=(char const *b, const static_digest &a) { - return a != b; - } - - template - bool operator==(char const *b, const static_digest &a) { - return a == b; - } - - template - std::ostream &operator<<(std::ostream &sink, const static_digest &d) { - detail::to_ascii(d, std::ostream_iterator(sink)); - return sink; - } - - template - std::istream &operator>>(std::istream &source, static_digest &d) { - std::array a = {{}}; - for (unsigned i = 0; i < a.size(); ++i) { - char c; - if (!source.get(c)) { - source.setstate(std::ios::failbit); - break; - } - if (!std::isxdigit(c, source.getloc())) { - source.unget(); - source.setstate(std::ios::failbit); - break; - } - - if (std::isdigit(c, source.getloc())) { - a[i] = (c - '0'); - } else { - a[i] = std::toupper(c, source.getloc()) - 'A' + 0xA; - } - } - detail::pack(a.begin(), a.end(), d.begin()); - return source; - } - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_HASH_DIGEST_HPP diff --git a/include/nil/crypto3/detail/stream_endian.hpp b/include/nil/crypto3/detail/stream_endian.hpp deleted file mode 100644 index 6498d5a..0000000 --- a/include/nil/crypto3/detail/stream_endian.hpp +++ /dev/null @@ -1,72 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// -// 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_STREAM_ENDIAN_HPP -#define CRYPTO3_STREAM_ENDIAN_HPP - -#include - -#include - -namespace nil { - namespace crypto3 { - namespace stream_endian { - // General versions; There should be no need to use these directly - - template - struct big_unit_big_bit { }; - template - struct little_unit_little_bit { }; - template - struct big_unit_little_bit { }; - template - struct little_unit_big_bit { }; - template - struct host_unit { - BOOST_STATIC_ASSERT(UnitBits % CHAR_BIT == 0); - }; - - // Typical, useful instantiations - - typedef big_unit_big_bit<1> big_bit; - typedef big_unit_big_bit big_byte_big_bit; - typedef big_unit_big_bit<8> big_octet_big_bit; - - typedef little_unit_little_bit<1> little_bit; - typedef little_unit_little_bit little_byte_little_bit; - typedef little_unit_little_bit<8> little_octet_little_bit; - - typedef big_unit_little_bit big_byte_little_bit; - typedef big_unit_little_bit<8> big_octet_little_bit; - - typedef little_unit_big_bit little_byte_big_bit; - typedef little_unit_big_bit<8> little_octet_big_bit; - - typedef host_unit host_byte; - - } // namespace stream_endian - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_STREAM_ENDIAN_HPP diff --git a/include/nil/crypto3/detail/unbounded_shift.hpp b/include/nil/crypto3/detail/unbounded_shift.hpp deleted file mode 100644 index 230cce2..0000000 --- a/include/nil/crypto3/detail/unbounded_shift.hpp +++ /dev/null @@ -1,108 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2020 Mikhail Komarov -// Copyright (c) 2020 Alexander Sokolov -// Copyright (c) 2020 Nikita Kaskov -// -// 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_DETAIL_UNBOUNDED_SHIFT_HPP -#define CRYPTO3_DETAIL_UNBOUNDED_SHIFT_HPP - -#include - -#include - -namespace nil { - namespace crypto3 { - namespace detail { - - template - struct unbounded_shifter { - static T shl(T x) { - return unbounded_shifter::shl(T(x << 1)); - } - - static T shr(T x) { - return unbounded_shifter::shr(T(x >> 1)); - } - }; - - template - struct unbounded_shifter<0, T> { - static T shl(T x) { - return x; - } - - static T shr(T x) { - return x; - } - }; - - template - T unbounded_shl(T x) { - return unbounded_shifter::shl(x); - } - - template - T unbounded_shr(T x) { - return unbounded_shifter::shr(x); - } - - template - T unbounded_shl(T x, std::size_t n) { - return x << n; - } - - template - T unbounded_shr(T x, std::size_t n) { - return x >> n; - } - // FIXME: it wouldn't work when Shift == sizeof(T) * CHAR_BIT - template - T low_bits(T x) { - T highmask = unbounded_shl(~T()); - return T(x & ~highmask); - } - - template - T low_bits(T x) { - constexpr size_t real_shift = TypeBits - Shift; - T lowmask = ((bool)Shift) * unbounded_shr(~T()); - return x & lowmask; - } - - template - T low_bits(T x, size_t shift) { - T lowmask = ((bool)shift) * unbounded_shr(~T(), type_bits - shift); - return x & lowmask; - } - - template - T high_bits(T x, size_t shift) { - T highmask = ((bool)shift) * unbounded_shl(~T(), type_bits - shift); - return x & highmask; - } - } // namespace detail - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_HASH_DETAIL_UNBOUNDED_SHIFT_HPP