block/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  constexpr static const std::size_t byte_bits = CHAR_BIT;
39  typedef typename boost::uint_t<byte_bits>::exact byte_type;
40 
41  constexpr static const std::size_t word_bits = WordBits;
42  typedef typename boost::uint_t<word_bits>::exact word_type;
43 
44  static inline word_type shr(word_type x, std::size_t n) {
45  return x >> n;
46  }
47 
48  template<std::size_t n>
49  static inline word_type shr(word_type x) {
50  BOOST_STATIC_ASSERT(n < word_bits);
51  return x >> n;
52  }
53 
54  static inline word_type shl(word_type x, std::size_t n) {
55  return x << n;
56  }
57 
58  template<std::size_t n>
59  static inline word_type shl(word_type x) {
60  BOOST_STATIC_ASSERT(n < word_bits);
61  return x << n;
62  }
63 
64  static inline word_type rotr(word_type x, std::size_t n) {
65  return shr(x, n) | shl(x, word_bits - n);
66  }
67 
68  template<std::size_t n>
69  static inline word_type rotr(word_type x) {
70  return shr<n>(x) | shl<word_bits - n>(x);
71  }
72 
73  static inline word_type rotl(word_type x, std::size_t n) {
74  return shl(x, n) | shr(x, word_bits - n);
75  }
76 
77  template<std::size_t n>
78  static inline word_type rotl(word_type x) {
79  return shl<n>(x) | shr<word_bits - n>(x);
80  }
81  };
82 
83  template<>
84  struct basic_functions<32> {
85  constexpr static const std::size_t byte_bits = CHAR_BIT;
86  typedef typename boost::uint_t<byte_bits>::exact byte_type;
87 
88  constexpr static const std::size_t word_bits = 32;
89  typedef typename boost::uint_t<word_bits>::exact word_type;
90 
91  static inline word_type shr(word_type x, std::size_t n) {
92  return x >> n;
93  }
94 
95  template<std::size_t n>
96  static inline word_type shr(word_type x) {
97  BOOST_STATIC_ASSERT(n < word_bits);
98  return x >> n;
99  }
100 
101  static inline word_type shl(word_type x, std::size_t n) {
102  return x << n;
103  }
104 
105  template<std::size_t n>
106  static inline word_type shl(word_type x) {
107  BOOST_STATIC_ASSERT(n < word_bits);
108  return x << n;
109  }
110 
111  static inline word_type rotr(word_type x, std::size_t n) {
112 #if defined(BOOST_ARCH_X86)
113  asm("rorl %1,%0" : "+r"(x) : "c"(static_cast<uint8_t>(n)));
114  return x;
115 #else
116  return shr(x, n) | shl(x, word_bits - n);
117 #endif
118  }
119 
120  template<std::size_t n>
121  static inline word_type rotr(word_type x) {
122  return shr<n>(x) | shl<word_bits - n>(x);
123  }
124 
125  static inline word_type rotl(word_type x, std::size_t n) {
126 #if defined(BOOST_ARCH_X86)
127  asm("roll %1,%0" : "+r"(x) : "c"(static_cast<uint8_t>(n)));
128  return x;
129 #else
130  return shl(x, n) | shr(x, word_bits - n);
131 #endif
132  }
133 
134  template<std::size_t n>
135  static inline word_type rotl(word_type x) {
136  return shl<n>(x) | shr<word_bits - n>(x);
137  }
138  };
139  } // namespace detail
140  } // namespace crypto3
141 } // namespace nil
142 
143 #endif // CRYPTO3_BASIC_FUNCTIONS_HPP
Definition: pair.hpp:31
static word_type shl(word_type x, std::size_t n)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:101
static word_type shr(word_type x)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:96
static word_type shr(word_type x, std::size_t n)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:91
static word_type rotr(word_type x)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:121
static word_type shl(word_type x)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:106
boost::uint_t< word_bits >::exact word_type
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:89
static word_type rotr(word_type x, std::size_t n)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:111
static word_type rotl(word_type x)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:135
static word_type rotl(word_type x, std::size_t n)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:125
boost::uint_t< byte_bits >::exact byte_type
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:86
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:37
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: block/include/nil/crypto3/detail/basic_functions.hpp:78
constexpr static const std::size_t byte_bits
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:38
static word_type rotr(word_type x)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:69
boost::uint_t< word_bits >::exact word_type
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:42
static word_type rotl(word_type x, std::size_t n)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:73
static word_type shl(word_type x, std::size_t n)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:54
static word_type rotr(word_type x, std::size_t n)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:64
boost::uint_t< byte_bits >::exact byte_type
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:39
static word_type shr(word_type x)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:49
static word_type shl(word_type x)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:59
static word_type shr(word_type x, std::size_t n)
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:44