hash/include/nil/crypto3/detail/basic_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_BASIC_FUNCTIONS_HPP
26 #define CRYPTO3_BASIC_FUNCTIONS_HPP
27 
28 #include <boost/integer.hpp>
29 #include <boost/static_assert.hpp>
30 
31 #include <nil/crypto3/detail/make_uint_t.hpp>
32 
33 namespace nil {
34  namespace crypto3 {
35  namespace detail {
36  template<std::size_t WordBits>
37  struct basic_functions {
38  typedef typename boost::uint_t<CHAR_BIT>::exact byte_type;
39 
40  constexpr static const std::size_t word_bits = WordBits;
41  typedef typename boost::uint_t<word_bits>::exact word_type;
42 
43  static inline word_type shr(word_type x, std::size_t n) {
44  return x >> n;
45  }
46 
47  template<std::size_t n>
48  static inline word_type shr(word_type x) {
49  BOOST_STATIC_ASSERT(n < word_bits);
50  return x >> n;
51  }
52 
53  static inline word_type shl(word_type x, std::size_t n) {
54  return x << n;
55  }
56 
57  template<std::size_t n>
58  static inline word_type shl(word_type x) {
59  BOOST_STATIC_ASSERT(n < word_bits);
60  return x << n;
61  }
62 
63  static inline word_type rotr(word_type x, std::size_t n) {
64  return shr(x, n) | shl(x, word_bits - n);
65  }
66 
67  template<std::size_t n>
68  static inline word_type rotr(word_type x) {
69  return shr<n>(x) | shl<word_bits - n>(x);
70  }
71 
72  static inline word_type rotl(word_type x, std::size_t n) {
73  return shl(x, n) | shr(x, word_bits - n);
74  }
75 
76  template<std::size_t n>
77  static inline word_type rotl(word_type x) {
78  return shl<n>(x) | shr<word_bits - n>(x);
79  }
80  };
81  } // namespace detail
82  } // namespace crypto3
83 } // namespace nil
84 
85 #endif // CRYPTO3_BASIC_FUNCTIONS_HPP
Definition: pair.hpp:31
constexpr static const std::size_t word_bits
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:41
static word_type rotl(word_type x)
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:77
boost::uint_t< CHAR_BIT >::exact byte_type
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:38
static word_type rotr(word_type x)
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:68
boost::uint_t< word_bits >::exact word_type
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:41
static word_type rotl(word_type x, std::size_t n)
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:72
static word_type shl(word_type x, std::size_t n)
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:53
static word_type rotr(word_type x, std::size_t n)
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:63
static word_type shr(word_type x)
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:48
static word_type shl(word_type x)
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:58
static word_type shr(word_type x, std::size_t n)
Definition: hash/include/nil/crypto3/detail/basic_functions.hpp:43