keccak_finalizer.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020 Alexander Sokolov <asokolov@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_KECCAK_FINALIZER_HPP
26 #define CRYPTO3_KECCAK_FINALIZER_HPP
27 
29 
30 #include <boost/endian/conversion.hpp>
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace hashes {
35  namespace detail {
36  template<typename PolicyType>
38  typedef PolicyType policy_type;
39 
40  constexpr static const std::size_t word_bits = policy_type::word_bits;
41  typedef typename policy_type::word_type word_type;
42 
43  constexpr static const std::size_t state_bits = policy_type::state_bits;
44  constexpr static const std::size_t state_words = policy_type::state_words;
45  typedef typename policy_type::state_type state_type;
46 
47  constexpr static const std::size_t block_bits = policy_type::block_bits;
48  constexpr static const std::size_t block_words = policy_type::block_words;
49  typedef typename policy_type::block_type block_type;
50 
51  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
52  constexpr static const std::size_t digest_blocks = digest_bits / block_bits;
53  constexpr static const std::size_t last_digest_bits = digest_bits % block_bits;
54  constexpr static const std::size_t last_digest_words =
55  last_digest_bits / word_bits + ((last_digest_bits % word_bits) ? 1 : 0);
56 
57  typedef typename policy_type::digest_type digest_type;
59 
60  public:
61  void operator()(state_type &state) {
62  state_type temp_state;
63  std::fill(temp_state.begin(), temp_state.end(), 0);
64 
65  for (std::size_t i = 0; i != digest_blocks; ++i) {
66  for (std::size_t j = 0; j != block_words; ++j)
67  temp_state[i * block_words + j] = state[j];
68 
69  for (std::size_t i = 0; i != state_words; ++i)
70  boost::endian::endian_reverse_inplace(state[i]);
71 
73 
74  for (std::size_t i = 0; i != state_words; ++i)
75  boost::endian::endian_reverse_inplace(state[i]);
76  }
77 
78  if (last_digest_bits) {
79  for (std::size_t j = 0; j != last_digest_words; ++j)
80  temp_state[digest_blocks * block_words + j] = state[j];
81  }
82 
83  state = temp_state;
84  }
85  };
86  } // namespace detail
87  } // namespace hashes
88  } // namespace crypto3
89 } // namespace nil
90 
91 #endif // CRYPTO3_KECCAK_FINALIZER_HPP
Definition: keccak_finalizer.hpp:37
void operator()(state_type &state)
Definition: keccak_finalizer.hpp:61
constexpr matrix< T, N, M > fill(T value)
generates a matrix containing a single value
Definition: matrix/utility.hpp:102
Definition: pair.hpp:31
Definition: keccak_functions.hpp:36
static void permute(state_type &A)
Definition: keccak_functions.hpp:57