MCKL
Monte Carlo Kernel Library
aes_key_seq.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/internal/aes_key_seq.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_RANDOM_INTERNAL_AES_KEY_SEQ_HPP
33 #define MCKL_RANDOM_INTERNAL_AES_KEY_SEQ_HPP
34 
36 
37 MCKL_PUSH_GCC_WARNING("-Wignored-attributes")
38 
39 namespace mckl {
40 
41 namespace internal {
42 
43 template <std::size_t Rounds, typename KeySeqGenerator>
45 {
46  public:
47  using key_type = typename KeySeqGenerator::key_type;
48  using rk_type = typename KeySeqGenerator::rk_type;
49 
50  static constexpr std::size_t rounds() { return Rounds; }
51 
52  key_type key() const { return KeySeqGenerator::key(rk_); }
53 
54  void set(const key_type &key)
55  {
56  KeySeqGenerator generator;
57  generator(key, rk_);
58  }
59 
60  const std::array<rk_type, rounds() + 1> &get() const { return rk_; }
61 
62  template <std::size_t N>
63  const rk_type &get() const
64  {
65  return std::get<N>(rk_);
66  }
67 
70  {
71  std::array<std::uint64_t, 2 * (rounds() + 1)> ks1;
72  std::array<std::uint64_t, 2 * (rounds() + 1)> ks2;
73  std::memcpy(ks1.data(), seq1.rk_.data(),
74  sizeof(std::uint64_t) * 2 * (rounds() + 1));
75  std::memcpy(ks2.data(), seq2.rk_.data(),
76  sizeof(std::uint64_t) * 2 * (rounds() + 1));
77 
78  return ks1 == ks2;
79  }
80 
83  {
84  return !(seq1 == seq2);
85  }
86 
87  template <typename CharT, typename Traits>
88  friend std::basic_ostream<CharT, Traits> &operator<<(
89  std::basic_ostream<CharT, Traits> &os,
91  {
92  if (!os) {
93  return os;
94  }
95 
96  std::array<std::uint64_t, 2 * (rounds() + 1)> ks;
97  std::memcpy(ks.data(), seq.rk_.data(),
98  sizeof(std::uint64_t) * 2 * (rounds() + 1));
99  ostream(os, ks);
100 
101  return os;
102  }
103 
104  template <typename CharT, typename Traits>
105  friend std::basic_istream<CharT, Traits> &operator>>(
106  std::basic_istream<CharT, Traits> &is,
108  {
109  if (!is) {
110  return is;
111  }
112 
113  std::array<std::uint64_t, 2 * (rounds() + 1)> ks;
114  istream(is, ks);
115  if (is) {
116  std::memcpy(seq.rk_.data(), ks.data(),
117  sizeof(std::uint64_t) * 2 * (rounds() + 1));
118  }
119 
120  return is;
121  }
122 
123  private:
124  std::array<rk_type, rounds() + 1> rk_;
125 }; // class AESKeySeqImpl
126 
127 template <std::size_t Rounds, typename KeySeqGenerator>
129 {
130  public:
131  using key_type = typename KeySeqGenerator::key_type;
132  using rk_type = typename KeySeqGenerator::rk_type;
133 
134  static constexpr std::size_t rounds() { return Rounds; }
135 
136  const key_type &key() const { return key_; }
137 
138  void set(const key_type &key) { key_ = key; }
139 
140  std::array<rk_type, rounds() + 1> get() const
141  {
142  KeySeqGenerator generator;
143  std::array<rk_type, rounds() + 1> rk;
144  generator(key_, rk);
145 
146  return rk;
147  }
148 
151  {
152  return seq1.key_ == seq2.key_;
153  }
154 
157  {
158  return !(seq1 == seq2);
159  }
160 
161  template <typename CharT, typename Traits>
162  friend std::basic_ostream<CharT, Traits> &operator<<(
163  std::basic_ostream<CharT, Traits> &os,
165  {
166  if (!os) {
167  return os;
168  }
169 
170  ostream(os, seq.key_);
171 
172  return os;
173  }
174 
175  template <typename CharT, typename Traits>
176  friend std::basic_istream<CharT, Traits> &operator>>(
177  std::basic_istream<CharT, Traits> &is,
179  {
180  if (!is) {
181  return is;
182  }
183 
184  key_type k = {{0}};
185  istream(is, k);
186  if (is) {
187  seq.key_ = k;
188  }
189 
190  return is;
191  }
192 
193  private:
194  key_type key_;
195 }; // class ARSKeySeqImpl
196 
197 } // namespace internal
198 
199 } // namespace mckl
200 
202 
203 #endif // MCKL_RANDOM_INTERNAL_AES_KEY_SEQ_HPP
#define MCKL_PUSH_GCC_WARNING(warning)
Definition: compiler.h:78
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, ARSKeySeqImpl< Rounds, KeySeqGenerator > &seq)
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, AESKeySeqImpl< Rounds, KeySeqGenerator > &seq)
friend bool operator==(const ARSKeySeqImpl< Rounds, KeySeqGenerator > &seq1, const ARSKeySeqImpl< Rounds, KeySeqGenerator > &seq2)
ulong uint64_t
Definition: opencl.h:42
static constexpr std::size_t rounds()
friend bool operator!=(const ARSKeySeqImpl< Rounds, KeySeqGenerator > &seq1, const ARSKeySeqImpl< Rounds, KeySeqGenerator > &seq2)
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const AESKeySeqImpl< Rounds, KeySeqGenerator > &seq)
Definition: aes_key_seq.hpp:88
std::basic_ostream< CharT, Traits > & ostream(std::basic_ostream< CharT, Traits > &os, const std::array< T, N > &ary)
Definition: iostream.hpp:46
typename KeySeqGenerator::rk_type rk_type
static constexpr std::size_t rounds()
Definition: aes_key_seq.hpp:50
const key_type & key() const
friend bool operator!=(const AESKeySeqImpl< Rounds, KeySeqGenerator > &seq1, const AESKeySeqImpl< Rounds, KeySeqGenerator > &seq2)
Definition: aes_key_seq.hpp:81
typename KeySeqGenerator::rk_type rk_type
Definition: aes_key_seq.hpp:48
typename KeySeqGenerator::key_type key_type
Definition: aes_key_seq.hpp:47
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const ARSKeySeqImpl< Rounds, KeySeqGenerator > &seq)
Definition: mcmc.hpp:40
friend bool operator==(const AESKeySeqImpl< Rounds, KeySeqGenerator > &seq1, const AESKeySeqImpl< Rounds, KeySeqGenerator > &seq2)
Definition: aes_key_seq.hpp:68
std::basic_istream< CharT, Traits > & istream(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
Definition: iostream.hpp:66
#define MCKL_POP_GCC_WARNING
Definition: compiler.h:79
typename KeySeqGenerator::key_type key_type