siphash_functions.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2019 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_MAC_SIPHASH_FUNCTIONS_HPP
26 #define CRYPTO3_MAC_SIPHASH_FUNCTIONS_HPP
27 
28 #include <boost/integer.hpp>
29 #include <boost/container/static_vector.hpp>
30 
32 
33 namespace nil {
34  namespace crypto3 {
35  namespace mac {
36  namespace detail {
37  template<std::size_t Rounds, std::size_t FinalRounds>
38  struct siphash_functions : public siphash_policy<Rounds, FinalRounds> {
40 
41  constexpr static const std::size_t rounds = policy_type::rounds;
42  constexpr static const std::size_t final_rounds = policy_type::final_rounds;
43 
44  constexpr static const std::size_t word_bits = policy_type::word_bits;
46 
47  constexpr static const std::size_t key_bits = policy_type::key_bits;
48  constexpr static const std::size_t key_words = policy_type::key_words;
49  typedef typename policy_type::key_type key_type;
50 
51  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
52  constexpr static const std::size_t key_schedule_words = policy_type::key_schedule_words;
54 
55  template<std::size_t InternalRounds>
57  word_type V0 = V[0], V1 = V[1], V2 = V[2], V3 = V[3];
58 
59  V3 ^= M;
60 
61  for (size_t i = 0; i != InternalRounds; ++i) {
62  V0 += V1;
63  V2 += V3;
64  V1 = policy_type::template rotl<13>(V1);
65  V3 = policy_type::template rotl<16>(V3);
66  V1 ^= V0;
67  V3 ^= V2;
68  V0 = policy_type::template rotl<32>(V0);
69 
70  V2 += V1;
71  V0 += V3;
72  V1 = policy_type::template rotl<17>(V1);
73  V3 = policy_type::template rotl<21>(V3);
74  V1 ^= V2;
75  V3 ^= V0;
76  V2 = policy_type::template rotl<32>(V2);
77  }
78  V0 ^= M;
79 
80  V[0] = V0;
81  V[1] = V1;
82  V[2] = V2;
83  V[3] = V3;
84  }
85  };
86  } // namespace detail
87  } // namespace mac
88  } // namespace crypto3
89 } // namespace nil
90 
91 #endif // CRYPTO3_SIPHASH_POLICY_HPP
boost::mpl::apply< AccumulatorSet, tag::mac< ProcessingPolicy > >::type::result_type mac(const AccumulatorSet &acc)
Definition: accumulators/mac.hpp:99
Definition: pair.hpp:31
boost::uint_t< word_bits >::exact word_type
Definition: mac/include/nil/crypto3/mac/detail/basic_functions.hpp:40
Definition: siphash_functions.hpp:38
constexpr static const std::size_t final_rounds
Definition: siphash_functions.hpp:42
constexpr static const std::size_t key_bits
Definition: siphash_functions.hpp:47
siphash_policy< Rounds, FinalRounds > policy_type
Definition: siphash_functions.hpp:39
constexpr static const std::size_t word_bits
Definition: siphash_functions.hpp:44
constexpr static const std::size_t rounds
Definition: siphash_functions.hpp:41
policy_type::key_schedule_type key_schedule_type
Definition: siphash_functions.hpp:53
void sip_rounds(key_schedule_type &V, word_type M)
Definition: siphash_functions.hpp:56
constexpr static const std::size_t key_words
Definition: siphash_functions.hpp:48
constexpr static const std::size_t key_schedule_words
Definition: siphash_functions.hpp:52
policy_type::word_type word_type
Definition: siphash_functions.hpp:45
policy_type::key_type key_type
Definition: siphash_functions.hpp:49
constexpr static const std::size_t key_schedule_bits
Definition: siphash_functions.hpp:51
Definition: siphash_policy.hpp:39
constexpr static const std::size_t rounds
Definition: siphash_policy.hpp:45
std::array< word_type, key_words > key_type
Definition: siphash_policy.hpp:50
std::array< word_type, key_schedule_size > key_schedule_type
Definition: siphash_policy.hpp:53
constexpr static const std::size_t key_words
Definition: siphash_policy.hpp:48
constexpr static const std::size_t word_bits
Definition: siphash_policy.hpp:42
basic_functions< 64 >::word_type word_type
Definition: siphash_policy.hpp:43
constexpr static const std::size_t key_bits
Definition: siphash_policy.hpp:49
constexpr static const std::size_t final_rounds
Definition: siphash_policy.hpp:46