blake2b_functions.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 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_BLAKE2B_FUNCTIONS_HPP
27 #define CRYPTO3_BLAKE2B_FUNCTIONS_HPP
28 
29 #include <array>
30 
32 
33 #include <boost/static_assert.hpp>
34 
35 namespace nil {
36  namespace crypto3 {
37  namespace hashes {
38  namespace detail {
39  template<std::size_t DigestBits>
40  struct blake2b_functions : public blake2b_policy<DigestBits> {
42 
44 
45  constexpr static const std::size_t state_words = policy_type::state_words;
46 
47  inline static void g(word_type &a, word_type &b, word_type &c, word_type &d, word_type M0,
48  word_type M1) {
49  a = a + b + M0;
50  d = policy_type::template rotr<32>(d ^ a);
51  c = c + d;
52  b = policy_type::template rotr<24>(b ^ c);
53  a = a + b + M1;
54  d = policy_type::template rotr<16>(d ^ a);
55  c = c + d;
56  b = policy_type::template rotr<63>(b ^ c);
57  }
58 
59  template<size_t i0, size_t i1, size_t i2, size_t i3, size_t i4, size_t i5, size_t i6, size_t i7,
60  size_t i8, size_t i9, size_t iA, size_t iB, size_t iC, size_t iD, size_t iE, size_t iF>
61  inline static void round(std::array<word_type, state_words * 2> &v,
62  const std::array<word_type, state_words * 2> &M) {
63  g(v[0], v[4], v[8], v[12], M[i0], M[i1]);
64  g(v[1], v[5], v[9], v[13], M[i2], M[i3]);
65  g(v[2], v[6], v[10], v[14], M[i4], M[i5]);
66  g(v[3], v[7], v[11], v[15], M[i6], M[i7]);
67  g(v[0], v[5], v[10], v[15], M[i8], M[i9]);
68  g(v[1], v[6], v[11], v[12], M[iA], M[iB]);
69  g(v[2], v[7], v[8], v[13], M[iC], M[iD]);
70  g(v[3], v[4], v[9], v[14], M[iE], M[iF]);
71  }
72  };
73  } // namespace detail
74  } // namespace hashes
75  } // namespace crypto3
76 } // namespace nil
77 
78 #endif // CRYPTO3_BLAKE2B_FUNCTIONS_HPP
Definition: pair.hpp:31
boost::uint_t< word_bits >::exact word_type
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:42
Definition: blake2b_functions.hpp:40
static void g(word_type &a, word_type &b, word_type &c, word_type &d, word_type M0, word_type M1)
Definition: blake2b_functions.hpp:47
static void round(std::array< word_type, state_words *2 > &v, const std::array< word_type, state_words *2 > &M)
Definition: blake2b_functions.hpp:61
blake2b_policy< DigestBits > policy_type
Definition: blake2b_functions.hpp:41
policy_type::word_type word_type
Definition: blake2b_functions.hpp:43
constexpr static const std::size_t state_words
Definition: blake2b_functions.hpp:45
Definition: blake2b_policy.hpp:38
constexpr static const std::size_t state_words
Definition: blake2b_policy.hpp:41