accumulators/passhash.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_ACCUMULATORS_PASSHASH_HPP
26 #define CRYPTO3_ACCUMULATORS_PASSHASH_HPP
27 
28 #include <boost/parameter/value_type.hpp>
29 
30 #include <boost/accumulators/framework/accumulator_base.hpp>
31 #include <boost/accumulators/framework/extractor.hpp>
32 #include <boost/accumulators/framework/depends_on.hpp>
33 #include <boost/accumulators/framework/parameters/sample.hpp>
34 
35 #include <boost/container/static_vector.hpp>
36 
37 #include <nil/crypto3/detail/make_array.hpp>
38 #include <nil/crypto3/detail/static_digest.hpp>
39 
42 
43 namespace nil {
44  namespace crypto3 {
45  namespace accumulators {
46  namespace impl {
47  template<typename CodecMode>
48  struct passhash_impl<CodecMode> : boost::accumulators::accumulator_base {
49  protected:
50  typedef CodecMode codec_mode_type;
51 
52  typedef typename codec_mode_type::finalizer_type finalizer_type;
53  typedef typename codec_mode_type::preprocessor_type preprocessor_type;
54 
55  constexpr static const std::size_t input_block_bits = codec_mode_type::input_block_bits;
56  constexpr static const std::size_t input_block_values = codec_mode_type::input_block_values;
57  typedef typename codec_mode_type::input_block_type input_block_type;
58 
59  constexpr static const std::size_t input_value_bits = codec_mode_type::input_value_bits;
60  typedef typename input_block_type::value_type input_value_type;
61 
62  constexpr static const std::size_t output_block_bits = codec_mode_type::output_block_bits;
63  constexpr static const std::size_t output_block_values = codec_mode_type::output_block_values;
64  typedef typename codec_mode_type::output_block_type output_block_type;
65 
66  constexpr static const std::size_t output_value_bits = codec_mode_type::output_value_bits;
67  typedef typename output_block_type::value_type output_value_type;
68 
69  public:
71 
72  passhash_impl(boost::accumulators::dont_care) : leading_zeros(0) {
73  }
74 
75  template<typename ArgumentPack>
76  inline void operator()(const ArgumentPack &args) {
77  preprocessor_type preprocessor;
79  = args[boost::accumulators::sample]; // TODO: I think it must be user type block like
80  // dgst
81  if (input.empty()) {
82  preprocessor(block);
83  leading_zeros = preprocessor.leading_zeros;
84  }
85  std::move(block.begin(), block.end(), std::back_inserter(input));
86  }
87 
88  inline result_type result(boost::accumulators::dont_care) const {
89  result_type res;
90  output_block_type ob = codec_mode_type::process_block(input);
91  std::move(ob.begin(), ob.end(), std::inserter(res, res.end()));
92  if (leading_zeros) {
93  finalizer_type fin(leading_zeros);
94  fin(res);
95  }
96  std::reverse(res.begin(), res.end());
97  return res;
98  }
99 
100  protected:
101  std::size_t leading_zeros;
103  };
104  } // namespace impl
105 
106  namespace tag {
107  template<typename Hash>
108  struct passhash : boost::accumulators::depends_on<> {
109  typedef Hash hash_type;
110 
113 
114  typedef boost::mpl::always<accumulators::impl::passhash_impl<Hash>> impl;
115  };
116  } // namespace tag
117 
118  namespace extract {
119  template<typename Hash, typename AccumulatorSet>
120  typename boost::mpl::apply<AccumulatorSet, tag::passhash<Hash>>::type::result_type
121  passhash(const AccumulatorSet &acc) {
122  return boost::accumulators::extract_result<tag::passhash<Hash>>(acc);
123  }
124  } // namespace extract
125  } // namespace accumulators
126  } // namespace crypto3
127 } // namespace nil
128 
129 #endif // CRYPTO3_ACCUMULATORS_BLOCK_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::passhash< Hash > >::type::result_type passhash(const AccumulatorSet &acc)
Definition: accumulators/passhash.hpp:121
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
void reverse(Range &a, std::size_t n)
Definition: basic_operations.hpp:54
Definition: pair.hpp:31
CodecMode codec_mode_type
Definition: accumulators/passhash.hpp:50
codec_mode_type::input_block_type input_block_type
Definition: accumulators/passhash.hpp:57
std::size_t leading_zeros
Definition: accumulators/passhash.hpp:101
result_type result(boost::accumulators::dont_care) const
Definition: accumulators/passhash.hpp:88
void operator()(const ArgumentPack &args)
Definition: accumulators/passhash.hpp:76
input_block_type input
Definition: accumulators/passhash.hpp:102
codec_mode_type::finalizer_type finalizer_type
Definition: accumulators/passhash.hpp:52
digest< output_block_bits > result_type
Definition: accumulators/passhash.hpp:70
passhash_impl(boost::accumulators::dont_care)
Definition: accumulators/passhash.hpp:72
input_block_type::value_type input_value_type
Definition: accumulators/passhash.hpp:60
output_block_type::value_type output_value_type
Definition: accumulators/passhash.hpp:67
codec_mode_type::preprocessor_type preprocessor_type
Definition: accumulators/passhash.hpp:53
codec_mode_type::output_block_type output_block_type
Definition: accumulators/passhash.hpp:64
Definition: accumulators/passhash.hpp:108
boost::mpl::always< accumulators::impl::passhash_impl< Hash > > impl
Definition: accumulators/passhash.hpp:114
Hash hash_type
Definition: accumulators/passhash.hpp:109