stream/include/nil/crypto3/stream/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_STREAM_BASIC_FUNCTIONS_HPP
26 #define CRYPTO3_STREAM_BASIC_FUNCTIONS_HPP
27 
28 #include <vector>
29 
30 #include <boost/integer.hpp>
31 #include <boost/static_assert.hpp>
32 
33 namespace nil {
34  namespace crypto3 {
35  namespace stream {
36  namespace detail {
37  template<std::size_t WordBits>
38  struct basic_functions {
39  typedef typename boost::uint_t<CHAR_BIT>::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  } // namespace detail
83  } // namespace stream
84  } // namespace crypto3
85 } // namespace nil
86 
87 #endif // CRYPTO3_BASIC_FUNCTIONS_HPP
boost::mpl::apply< AccumulatorSet, tag::stream< Mode > >::type::result_type stream(const AccumulatorSet &acc)
Definition: accumulators/stream.hpp:175
Definition: pair.hpp:31
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:38
static word_type shl(word_type x)
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:59
static word_type shr(word_type x)
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:49
static word_type rotr(word_type x, std::size_t n)
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:64
constexpr static const std::size_t word_bits
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:41
static word_type rotl(word_type x, std::size_t n)
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:73
static word_type rotr(word_type x)
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:69
static word_type shr(word_type x, std::size_t n)
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:44
static word_type rotl(word_type x)
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:78
boost::uint_t< word_bits >::exact word_type
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:42
static word_type shl(word_type x, std::size_t n)
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:54
boost::uint_t< CHAR_BIT >::exact byte_type
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:39