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