keccak_functions.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Alexander Sokolov <asokolov@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_KECCAK_FUNCTIONS_HPP
27 #define CRYPTO3_KECCAK_FUNCTIONS_HPP
28 
30 
31 namespace nil {
32  namespace crypto3 {
33  namespace hashes {
34  namespace detail {
35  template<std::size_t DigestBits>
36  struct keccak_1600_functions : public keccak_1600_policy<DigestBits> {
38 
39  constexpr static const std::size_t word_bits = policy_type::word_bits;
41 
43 
44  constexpr static const std::size_t round_constants_size = policy_type::rounds;
45  typedef typename std::array<word_type, round_constants_size> round_constants_type;
46 
47  constexpr static const round_constants_type round_constants = {
48  UINT64_C(0x0000000000000001), UINT64_C(0x0000000000008082), UINT64_C(0x800000000000808a),
49  UINT64_C(0x8000000080008000), UINT64_C(0x000000000000808b), UINT64_C(0x0000000080000001),
50  UINT64_C(0x8000000080008081), UINT64_C(0x8000000000008009), UINT64_C(0x000000000000008a),
51  UINT64_C(0x0000000000000088), UINT64_C(0x0000000080008009), UINT64_C(0x000000008000000a),
52  UINT64_C(0x000000008000808b), UINT64_C(0x800000000000008b), UINT64_C(0x8000000000008089),
53  UINT64_C(0x8000000000008003), UINT64_C(0x8000000000008002), UINT64_C(0x8000000000000080),
54  UINT64_C(0x000000000000800a), UINT64_C(0x800000008000000a), UINT64_C(0x8000000080008081),
55  UINT64_C(0x8000000000008080), UINT64_C(0x0000000080000001), UINT64_C(0x8000000080008008)};
56 
57  static inline void permute(state_type &A) {
58  for (typename round_constants_type::value_type c : round_constants) {
59  const word_type C0 = A[0] ^ A[5] ^ A[10] ^ A[15] ^ A[20];
60  const word_type C1 = A[1] ^ A[6] ^ A[11] ^ A[16] ^ A[21];
61  const word_type C2 = A[2] ^ A[7] ^ A[12] ^ A[17] ^ A[22];
62  const word_type C3 = A[3] ^ A[8] ^ A[13] ^ A[18] ^ A[23];
63  const word_type C4 = A[4] ^ A[9] ^ A[14] ^ A[19] ^ A[24];
64 
65  const word_type D0 = policy_type::template rotl<1>(C0) ^ C3;
66  const word_type D1 = policy_type::template rotl<1>(C1) ^ C4;
67  const word_type D2 = policy_type::template rotl<1>(C2) ^ C0;
68  const word_type D3 = policy_type::template rotl<1>(C3) ^ C1;
69  const word_type D4 = policy_type::template rotl<1>(C4) ^ C2;
70 
71  const word_type B00 = A[0] ^ D1;
72  const word_type B10 = policy_type::template rotl<1>(A[1] ^ D2);
73  const word_type B20 = policy_type::template rotl<62>(A[2] ^ D3);
74  const word_type B05 = policy_type::template rotl<28>(A[3] ^ D4);
75  const word_type B15 = policy_type::template rotl<27>(A[4] ^ D0);
76  const word_type B16 = policy_type::template rotl<36>(A[5] ^ D1);
77  const word_type B01 = policy_type::template rotl<44>(A[6] ^ D2);
78  const word_type B11 = policy_type::template rotl<6>(A[7] ^ D3);
79  const word_type B21 = policy_type::template rotl<55>(A[8] ^ D4);
80  const word_type B06 = policy_type::template rotl<20>(A[9] ^ D0);
81  const word_type B07 = policy_type::template rotl<3>(A[10] ^ D1);
82  const word_type B17 = policy_type::template rotl<10>(A[11] ^ D2);
83  const word_type B02 = policy_type::template rotl<43>(A[12] ^ D3);
84  const word_type B12 = policy_type::template rotl<25>(A[13] ^ D4);
85  const word_type B22 = policy_type::template rotl<39>(A[14] ^ D0);
86  const word_type B23 = policy_type::template rotl<41>(A[15] ^ D1);
87  const word_type B08 = policy_type::template rotl<45>(A[16] ^ D2);
88  const word_type B18 = policy_type::template rotl<15>(A[17] ^ D3);
89  const word_type B03 = policy_type::template rotl<21>(A[18] ^ D4);
90  const word_type B13 = policy_type::template rotl<8>(A[19] ^ D0);
91  const word_type B14 = policy_type::template rotl<18>(A[20] ^ D1);
92  const word_type B24 = policy_type::template rotl<2>(A[21] ^ D2);
93  const word_type B09 = policy_type::template rotl<61>(A[22] ^ D3);
94  const word_type B19 = policy_type::template rotl<56>(A[23] ^ D4);
95  const word_type B04 = policy_type::template rotl<14>(A[24] ^ D0);
96 
97  A[0] = B00 ^ (~B01 & B02);
98  A[1] = B01 ^ (~B02 & B03);
99  A[2] = B02 ^ (~B03 & B04);
100  A[3] = B03 ^ (~B04 & B00);
101  A[4] = B04 ^ (~B00 & B01);
102  A[5] = B05 ^ (~B06 & B07);
103  A[6] = B06 ^ (~B07 & B08);
104  A[7] = B07 ^ (~B08 & B09);
105  A[8] = B08 ^ (~B09 & B05);
106  A[9] = B09 ^ (~B05 & B06);
107  A[10] = B10 ^ (~B11 & B12);
108  A[11] = B11 ^ (~B12 & B13);
109  A[12] = B12 ^ (~B13 & B14);
110  A[13] = B13 ^ (~B14 & B10);
111  A[14] = B14 ^ (~B10 & B11);
112  A[15] = B15 ^ (~B16 & B17);
113  A[16] = B16 ^ (~B17 & B18);
114  A[17] = B17 ^ (~B18 & B19);
115  A[18] = B18 ^ (~B19 & B15);
116  A[19] = B19 ^ (~B15 & B16);
117  A[20] = B20 ^ (~B21 & B22);
118  A[21] = B21 ^ (~B22 & B23);
119  A[22] = B22 ^ (~B23 & B24);
120  A[23] = B23 ^ (~B24 & B20);
121  A[24] = B24 ^ (~B20 & B21);
122 
123  A[0] ^= c;
124  }
125  }
126  };
127 
128  template<std::size_t DigestBits>
131  } // namespace detail
132  } // namespace hashes
133  } // namespace crypto3
134 } // namespace nil
135 
136 #endif // CRYPTO3_KECCAK_FUNCTIONS_HPP
Definition: pair.hpp:31
constexpr static const std::size_t word_bits
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:41
boost::uint_t< word_bits >::exact word_type
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:42
Definition: keccak_functions.hpp:36
std::array< word_type, round_constants_size > round_constants_type
Definition: keccak_functions.hpp:45
static void permute(state_type &A)
Definition: keccak_functions.hpp:57
constexpr static const round_constants_type round_constants
Definition: keccak_functions.hpp:47
policy_type::word_type word_type
Definition: keccak_functions.hpp:40
constexpr static const std::size_t round_constants_size
Definition: keccak_functions.hpp:44
constexpr static const std::size_t word_bits
Definition: keccak_functions.hpp:39
keccak_1600_policy< DigestBits > policy_type
Definition: keccak_functions.hpp:37
policy_type::state_type state_type
Definition: keccak_functions.hpp:42
Definition: keccak_policy.hpp:36
constexpr static const std::size_t rounds
Definition: keccak_policy.hpp:53
std::array< word_type, state_words > state_type
Definition: keccak_policy.hpp:43