blake2b_padding.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020 Alexander Sokolov <asokolov@nil.foundation>
3 // Copyright (c) 2020 Nikita Kaskov <nbering@nil.foundation>
4 // Copyright (c) 2020 Mikhail Komarov <nemo@nil.foundation>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------//
26 
27 #ifndef CRYPTO3_BLAKE2B_PADDING_HPP
28 #define CRYPTO3_BLAKE2B_PADDING_HPP
29 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace hashes {
35  namespace detail {
36  template<typename Hash>
38  typedef Hash policy_type;
39 
40  typedef typename policy_type::digest_endian endian_type;
41 
42  constexpr static const std::size_t word_bits = policy_type::word_bits;
43  typedef typename policy_type::word_type word_type;
44 
45  constexpr static const std::size_t block_bits = policy_type::block_bits;
46  constexpr static const std::size_t block_words = policy_type::block_words;
47  typedef typename policy_type::block_type block_type;
48 
49  typedef ::nil::crypto3::detail::injector<endian_type, word_bits, block_words, block_bits>
51 
52  public:
53  void operator()(block_type &block, word_type total_seen) {
54  // Pad block with zero bits if it is empty or incomplete
55  if (!total_seen || total_seen % block_bits) {
56  word_type seen_words =
57  ((total_seen / word_bits) % block_words) + ((total_seen % word_bits) ? 1 : 0);
58  std::fill(block.begin() + seen_words, block.end(), 0);
59  // Pad with zeros last significant word if it is incomplete
60  if (total_seen % word_bits) {
61  word_type block_seen = total_seen % block_bits;
62  injector_type::inject(word_type(), word_bits - block_seen % word_bits, block,
63  block_seen);
64  }
65  }
66  }
67  };
68  } // namespace detail
69  } // namespace hashes
70  } // namespace crypto3
71 } // namespace nil
72 
73 #endif // CRYPTO3_BLAKE2B_PADDING_HPP
Definition: blake2b_padding.hpp:37
void operator()(block_type &block, word_type total_seen)
Definition: blake2b_padding.hpp:53
constexpr matrix< T, N, M > fill(T value)
generates a matrix containing a single value
Definition: matrix/utility.hpp:102
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
Definition: pair.hpp:31
Definition: block/include/nil/crypto3/detail/inject.hpp:260
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)
Definition: block/include/nil/crypto3/detail/inject.hpp:267