hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2019 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Nikita Kaskov <nbering@nil.foundation>
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_HASH_BLOCK_STREAM_PROCESSOR_HPP
27 #define CRYPTO3_HASH_BLOCK_STREAM_PROCESSOR_HPP
28 
29 #include <array>
30 #include <iterator>
31 
32 #include <nil/crypto3/detail/pack.hpp>
33 
36 
37 #include <boost/integer.hpp>
38 #include <boost/static_assert.hpp>
39 #include <boost/utility/enable_if.hpp>
40 
41 namespace nil {
42  namespace crypto3 {
43  namespace hashes {
44 
54  template<typename Construction, typename StateAccumulator, typename Params>
56  protected:
57  typedef typename Construction::type construction_type;
58  typedef StateAccumulator accumulator_type;
59  typedef Params params_type;
60 
61  constexpr static const std::size_t word_bits = construction_type::word_bits;
62 
63  constexpr static const std::size_t block_bits = construction_type::block_bits;
64  typedef typename construction_type::block_type block_type;
65 
66  public:
67  typedef typename params_type::digest_endian endian_type;
68 
69  constexpr static const std::size_t value_bits = params_type::value_bits;
70  typedef typename boost::uint_t<value_bits>::least value_type;
72  constexpr static const std::size_t block_values = block_bits / value_bits;
73  typedef std::array<value_type, block_values> cache_type;
74 
75  protected:
77 
78  inline void process_block(std::size_t block_seen = block_bits) {
79  using namespace nil::crypto3::detail;
80  // Convert the input into words
82  pack_to<endian_type, value_bits, word_bits>(cache.begin(), cache.end(), block.begin());
83  // Process the block
84  acc(block, ::nil::crypto3::accumulators::bits = block_seen);
85  }
86 
87  public:
88  inline void update_one(value_type value) {
89  cache[cache_seen] = value;
90  ++cache_seen;
91  if (cache_seen == block_values) {
92  // Process the completed block
93  process_block();
94  cache_seen = 0;
95  }
96  }
97 
98  template<typename InputIterator>
99  inline void update_n(InputIterator p, size_t n) {
100  for (; n; --n) {
101  update_one(*p++);
102  }
103  }
104 
105  template<typename InputIterator>
106  inline void update_n(InputIterator first, InputIterator last) {
107  std::size_t n = std::distance(first, last);
108  update_n(first, n);
109  }
110 
111  template<typename InputIterator>
112  inline void operator()(InputIterator b, InputIterator e, std::random_access_iterator_tag) {
113  update_n(b, e);
114  }
115 
116  template<typename InputIterator, typename Category>
117  inline void operator()(InputIterator b, InputIterator e, Category) {
118  while (b != e) {
119  update_one(*b++);
120  }
121  }
122 
123  template<typename InputIterator>
124  inline void operator()(InputIterator b, InputIterator e) {
125  typedef typename std::iterator_traits<InputIterator>::iterator_category cat;
126 
127  operator()(b, e, cat());
128  }
129 
130  template<typename ContainerT>
131  inline void operator()(const ContainerT &c) {
132  update_n(c.data(), c.size());
133  }
134 
135  public:
136  block_stream_processor(accumulator_type &acc) : acc(acc), cache(), cache_seen(0) {
137  }
138 
140  if (cache_seen > 0) {
141  process_block(cache_seen * value_bits);
142  cache_seen = 0;
143  }
144  }
145 
146  private:
147  accumulator_type &acc;
148 
149  cache_type cache;
150  std::size_t cache_seen;
151  };
152  } // namespace hashes
153  } // namespace crypto3
154 } // namespace nil
155 
156 #endif
This will do the usual Merkle-Damgård-style strengthening, padding with a 1 bit, then 0 bits as neede...
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:55
std::array< value_type, block_values > cache_type
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:73
constexpr static const std::size_t word_bits
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:61
BOOST_STATIC_ASSERT(word_bits % value_bits==0)
block_stream_processor(accumulator_type &acc)
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:136
constexpr static const std::size_t block_bits
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:63
Construction::type construction_type
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:57
void update_n(InputIterator p, size_t n)
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:99
void update_one(value_type value)
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:88
void operator()(InputIterator b, InputIterator e)
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:124
StateAccumulator accumulator_type
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:58
BOOST_STATIC_ASSERT(block_bits % value_bits==0)
Params params_type
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:59
void operator()(const ContainerT &c)
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:131
constexpr static const std::size_t block_values
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:72
construction_type::block_type block_type
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:64
params_type::digest_endian endian_type
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:67
boost::uint_t< value_bits >::least value_type
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:70
constexpr static const std::size_t value_bits
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:69
void operator()(InputIterator b, InputIterator e, Category)
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:117
void update_n(InputIterator first, InputIterator last)
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:106
virtual ~block_stream_processor()
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:139
void process_block(std::size_t block_seen=block_bits)
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:78
void operator()(InputIterator b, InputIterator e, std::random_access_iterator_tag)
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:112
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:33
Definition: pair.hpp:31