block/include/nil/crypto3/detail/endian_shift.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2019 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Nikita Kaskov <nbering@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_DETAIL_ENDIAN_SHIFT_HPP
27 #define CRYPTO3_DETAIL_ENDIAN_SHIFT_HPP
28 
29 #include <boost/assert.hpp>
30 
31 #include <nil/crypto3/detail/stream_endian.hpp>
32 #include <nil/crypto3/detail/basic_functions.hpp>
33 #include <nil/crypto3/detail/unbounded_shift.hpp>
34 
35 namespace nil {
36  namespace crypto3 {
37  namespace detail {
38 
39  template<typename Endianness, std::size_t WordBits>
40  struct endian_shift;
41 
42  template<int UnitBits, std::size_t WordBits>
43  struct endian_shift<stream_endian::big_unit_big_bit<UnitBits>, WordBits>
44  : public basic_functions<WordBits> {
45 
46  constexpr static const std::size_t word_bits = basic_functions<WordBits>::word_bits;
48 
49  static word_type &to_msb(word_type &w, std::size_t shift) {
50  // shift to most significant bits according to endianness
51  w = unbounded_shl(w, shift);
52  return w;
53  }
54  };
55 
56  template<int UnitBits, std::size_t WordBits>
57  struct endian_shift<stream_endian::little_unit_big_bit<UnitBits>, WordBits>
58  : public basic_functions<WordBits> {
59 
60  constexpr static const std::size_t word_bits = basic_functions<WordBits>::word_bits;
62 
63  static word_type &to_msb(word_type &w, std::size_t shift) {
64  // shift to most significant bits according to endianness
65  std::size_t shift_rem = shift % UnitBits;
66  std::size_t shift_unit_bits = shift - shift_rem;
67 
68  std::size_t sz[2] = {UnitBits - shift_rem, shift_rem};
69  word_type masks[2];
70  masks[0] = unbounded_shl(low_bits<word_bits>(~word_type(), sz[0]), shift_unit_bits);
71  masks[1] =
72  unbounded_shl(low_bits<word_bits>(~word_type(), sz[1]), shift_unit_bits + UnitBits + sz[0]);
73  std::size_t bits_left = word_bits - shift;
74 
75  word_type w_combined = 0;
76  int ind = 0;
77 
78  while (bits_left) {
79  w_combined |= (!ind ? unbounded_shl(w & masks[0], shift_rem) :
80  unbounded_shr(w & masks[1], UnitBits + sz[0]));
81  bits_left -= sz[ind];
82  masks[ind] = unbounded_shl(masks[ind], UnitBits);
83  ind = 1 - ind;
84  }
85 
86  w = unbounded_shr(w_combined, shift_unit_bits);
87  return w;
88  }
89  };
90 
91  template<int UnitBits, std::size_t WordBits>
92  struct endian_shift<stream_endian::big_unit_little_bit<UnitBits>, WordBits>
93  : public basic_functions<WordBits> {
94 
95  constexpr static const std::size_t word_bits = basic_functions<WordBits>::word_bits;
97 
98  static word_type &to_msb(word_type &w, std::size_t shift) {
99  // shift to most significant bits according to endianness
100  std::size_t shift_rem = shift % UnitBits;
101  std::size_t shift_unit_bits = shift - shift_rem;
102 
103  std::size_t sz[2] = {UnitBits - shift_rem, shift_rem};
104  word_type masks[2] = {
105  unbounded_shr(high_bits<word_bits>(~word_type(), sz[0]), shift_unit_bits),
106  unbounded_shr(high_bits<word_bits>(~word_type(), sz[1]), shift_unit_bits + UnitBits + sz[0])};
107 
108  std::size_t bits_left = word_bits - shift;
109  word_type w_combined = 0;
110  int ind = 0;
111 
112  while (bits_left) {
113  w_combined |= (!ind ? unbounded_shr(w & masks[0], shift_rem) :
114  unbounded_shl(w & masks[1], UnitBits + sz[0]));
115  bits_left -= sz[ind];
116  masks[ind] = unbounded_shr(masks[ind], UnitBits);
117  ind = 1 - ind;
118  }
119 
120  w = unbounded_shl(w_combined, shift_unit_bits);
121  return w;
122  }
123  };
124 
125  template<int UnitBits, std::size_t WordBits>
126  struct endian_shift<stream_endian::little_unit_little_bit<UnitBits>, WordBits>
127  : public basic_functions<WordBits> {
128 
129  constexpr static const std::size_t word_bits = basic_functions<WordBits>::word_bits;
131 
132  static word_type &to_msb(word_type &w, std::size_t shift) {
133 
134  // shift to most significant bits according to endianness
135  w = unbounded_shr(w, shift);
136  return w;
137  }
138  };
139  } // namespace detail
140  } // namespace crypto3
141 } // namespace nil
142 
143 #endif // CRYPTO3_DETAIL_ENDIAN_SHIFT_HPP
T unbounded_shr(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:64
T unbounded_shl(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:59
Definition: pair.hpp:31
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:37
boost::uint_t< word_bits >::exact word_type
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:42
basic_functions< WordBits >::word_type word_type
Definition: block/include/nil/crypto3/detail/endian_shift.hpp:47
static word_type & to_msb(word_type &w, std::size_t shift)
Definition: block/include/nil/crypto3/detail/endian_shift.hpp:49
static word_type & to_msb(word_type &w, std::size_t shift)
Definition: block/include/nil/crypto3/detail/endian_shift.hpp:98
basic_functions< WordBits >::word_type word_type
Definition: block/include/nil/crypto3/detail/endian_shift.hpp:96
basic_functions< WordBits >::word_type word_type
Definition: block/include/nil/crypto3/detail/endian_shift.hpp:61
static word_type & to_msb(word_type &w, std::size_t shift)
Definition: block/include/nil/crypto3/detail/endian_shift.hpp:63
static word_type & to_msb(word_type &w, std::size_t shift)
Definition: block/include/nil/crypto3/detail/endian_shift.hpp:132
basic_functions< WordBits >::word_type word_type
Definition: block/include/nil/crypto3/detail/endian_shift.hpp:130
Definition: block/include/nil/crypto3/detail/endian_shift.hpp:40