varlength_block_stream_processor.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2019 Aleksey Moskvin <zerg1996@yandex.ru>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_VARLENGTH_BLOCK_STREAM_PROCESSOR_HPP
27 #define CRYPTO3_VARLENGTH_BLOCK_STREAM_PROCESSOR_HPP
28 
29 #include <array>
30 #include <iterator>
31 
32 #include <nil/crypto3/detail/pack.hpp>
33 #include <nil/crypto3/detail/digest.hpp>
34 
35 #include <boost/integer.hpp>
36 #include <boost/static_assert.hpp>
37 #include <boost/utility/enable_if.hpp>
38 
39 #include <boost/range/algorithm/copy.hpp>
40 
41 namespace nil {
42  namespace crypto3 {
43  namespace codec {
44  template<typename Mode, typename StateAccumulator, typename Params>
46  private:
47  typedef Mode mode_type;
48  typedef StateAccumulator accumulator_type;
49  typedef Params params_type;
50 
51  constexpr static const std::size_t input_block_bits = mode_type::input_block_bits;
52  typedef typename mode_type::input_block_type input_block_type;
53 
54  constexpr static const std::size_t input_value_bits = mode_type::input_value_bits;
55  typedef typename input_block_type::value_type input_value_type;
56 
57  constexpr static const std::size_t output_block_bits = mode_type::output_block_bits;
58  typedef typename mode_type::output_block_type output_block_type;
59 
60  constexpr static const std::size_t output_value_bits = mode_type::output_value_bits;
61  typedef typename output_block_type::value_type output_value_type;
62 
63  public:
64  typedef typename params_type::endian_type endian_type;
65 
66  constexpr static const std::size_t value_bits = params_type::value_bits;
67  typedef typename boost::uint_t<value_bits>::least value_type;
68  BOOST_STATIC_ASSERT(input_block_bits % value_bits == 0);
69  constexpr static const std::size_t block_values = input_block_bits / value_bits;
70  typedef std::array<value_type, block_values> value_array_type;
71 
72  private:
73  constexpr static const std::size_t length_bits = params_type::length_bits;
74  // FIXME: do something more intelligent than capping at 64
75  constexpr static const std::size_t length_type_bits =
76  length_bits < input_block_bits ? input_block_bits : length_bits > 64 ? 64 : length_bits;
77  typedef typename boost::uint_t<length_type_bits>::least length_type;
78 
79  BOOST_STATIC_ASSERT(!length_bits || length_bits % input_block_bits == 0);
80  BOOST_STATIC_ASSERT(output_block_bits % value_bits == 0);
81 
82  BOOST_STATIC_ASSERT(!length_bits || value_bits <= length_bits);
83 
84  public:
85  varlength_block_stream_processor(StateAccumulator &s) : state(s) {
86  }
87 
88  template<typename InputIterator>
89  inline void operator()(InputIterator first, InputIterator last, std::random_access_iterator_tag) {
90  input_block_type block =
91  {}; // TODO: fill it with zero value for base32/64, and find true size for base58
92  ::nil::crypto3::detail::pack_to<endian_type, value_bits, input_value_bits>(
93  first, last, std::inserter(block, block.begin()));
94  state(block);
95  }
96 
97  template<typename InputIterator, typename Category>
98  inline void operator()(InputIterator first, InputIterator last, Category) {
99  input_block_type block = {0};
100  ::nil::crypto3::detail::pack_to<endian_type, value_bits, input_value_bits>(
101  first, last, block.begin());
102  state(block);
103  }
104 
105  template<typename ValueType,
106  typename = typename std::enable_if<std::is_same<ValueType, input_value_type>::value>::type>
107  inline void operator()(const ValueType &value) {
108  state(value);
109  }
110 
111  template<typename InputIterator>
112  inline void operator()(InputIterator first, InputIterator last) {
113  typedef typename std::iterator_traits<InputIterator>::iterator_category cat;
114  return operator()(first, last, cat());
115  }
116 
117  template<typename ValueType>
118  inline void operator()(const std::initializer_list<ValueType> &il) {
119  return operator()(il.begin(), il.end());
120  }
121 
122  void reset() {
123  }
124 
125  StateAccumulator &state;
126  };
127  } // namespace codec
128  } // namespace crypto3
129 } // namespace nil
130 
131 #endif // CRYPTO3_FIXED_BLOCK_STREAM_PROCESSOR_HPP
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
boost::mpl::apply< AccumulatorSet, tag::codec< Mode > >::type::result_type codec(const AccumulatorSet &acc)
Definition: accumulators/codec.hpp:261
typename std::iterator_traits< Iterator >::value_type ValueType
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:50
Definition: pair.hpp:31
Definition: varlength_block_stream_processor.hpp:45
void operator()(InputIterator first, InputIterator last)
Definition: varlength_block_stream_processor.hpp:112
void reset()
Definition: varlength_block_stream_processor.hpp:122
void operator()(InputIterator first, InputIterator last, Category)
Definition: varlength_block_stream_processor.hpp:98
constexpr static const std::size_t block_values
Definition: varlength_block_stream_processor.hpp:69
boost::uint_t< value_bits >::least value_type
Definition: varlength_block_stream_processor.hpp:67
std::array< value_type, block_values > value_array_type
Definition: varlength_block_stream_processor.hpp:70
void operator()(const ValueType &value)
Definition: varlength_block_stream_processor.hpp:107
constexpr static const std::size_t value_bits
Definition: varlength_block_stream_processor.hpp:66
void operator()(InputIterator first, InputIterator last, std::random_access_iterator_tag)
Definition: varlength_block_stream_processor.hpp:89
params_type::endian_type endian_type
Definition: varlength_block_stream_processor.hpp:64
BOOST_STATIC_ASSERT(input_block_bits % value_bits==0)
void operator()(const std::initializer_list< ValueType > &il)
Definition: varlength_block_stream_processor.hpp:118
varlength_block_stream_processor(StateAccumulator &s)
Definition: varlength_block_stream_processor.hpp:85
StateAccumulator & state
Definition: varlength_block_stream_processor.hpp:125