32 #ifndef MCKL_RANDOM_THREEFRY_HPP 33 #define MCKL_RANDOM_THREEFRY_HPP 51 #ifndef MCKL_THREEFRY_ROUNDS 52 #define MCKL_THREEFRY_ROUNDS 20 60 template <
typename T, std::
size_t K, std::
size_t Rounds,
typename Constants>
64 template <
typename T, std::
size_t K, std::
size_t Rounds,
typename Constants>
65 using ThreefryGeneratorImpl =
67 #else // MCKL_USE_AVX2 68 template <
typename T, std::
size_t K, std::
size_t Rounds,
typename Constants>
69 using ThreefryGeneratorImpl =
71 #endif // MCKL_USE_AVX2 86 static_assert(std::is_unsigned<T>::value,
87 "**ThreefryGenerator** used with T other than unsigned integer types");
89 static_assert(K != 0 && K % 2 == 0,
90 "**ThreefryGenerator** used with K other than multiples of 2");
93 Rounds != 0,
"**ThreefryGenerator** used with rounds equal to zero");
99 static constexpr std::size_t
size() {
return sizeof(T) * K; }
104 std::memcpy(key.data(), par_.data(),
sizeof(T) * K);
111 constexpr T p = Constants::parity::value;
113 std::memcpy(par_.data(), key.data(),
sizeof(T) * K);
114 std::get<K>(par_) = p;
115 for (std::size_t i = 0; i != key.size(); ++i) {
116 std::get<K>(par_) ^= par_[i];
118 std::get<K + 1>(par_) = 0;
119 std::get<K + 2>(par_) = 0;
120 std::get<K + 3>(par_) = 0;
125 return std::make_pair(std::get<K + 1>(par_), std::get<K + 2>(par_));
130 std::get<K + 1>(par_) = t0;
131 std::get<K + 2>(par_) = t1;
132 std::get<K + 3>(par_) = t0 ^ t1;
138 plain, cipher, par_);
141 template <
typename ResultType>
148 template <
typename ResultType>
159 return gen1.par_ == gen2.par_;
166 return !(gen1 == gen2);
169 template <
typename CharT,
typename Traits>
171 std::basic_ostream<CharT, Traits> &os,
183 template <
typename CharT,
typename Traits>
185 std::basic_istream<CharT, Traits> &is,
193 gen_tmp.par_.fill(0);
194 is >> std::ws >> gen_tmp.par_;
197 gen = std::move(gen_tmp);
204 std::array<T, K + 4> par_;
209 template <
typename ResultType,
typename T, std::size_t K,
217 template <
typename ResultType>
222 template <
typename ResultType>
227 template <
typename ResultType>
232 template <
typename ResultType>
237 template <
typename ResultType>
242 template <
typename ResultType>
246 template <
typename ResultType>
250 template <
typename ResultType>
254 template <
typename ResultType>
331 #endif // MCKL_RANDOM_THREEFRY_HPP
Counter based RNG engine.
void operator()(const void *plain, void *cipher) const
void operator()(ctr_type &ctr, std::size_t n, ResultType *r) const
static void eval(const void *plain, void *cipher, const std::array< T, K+4 > &par)
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, ThreefryGenerator< T, K, Rounds, Constants > &gen)
void reset(const key_type &key)
typename internal::CounterImpl< T, K >::type Counter
A counter type with the same width as std::array<T, K> but with possibly fewer elements.
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const ThreefryGenerator< T, K, Rounds, Constants > &gen)
void operator()(ctr_type &ctr, ResultType *r) const
friend bool operator==(const ThreefryGenerator< T, K, Rounds, Constants > &gen1, const ThreefryGenerator< T, K, Rounds, Constants > &gen2)
std::pair< T, T > tweak() const
Default Threefry constants.
ThreefryGeneratorAVX2Impl< T, K, Rounds, Constants > ThreefryGeneratorImpl
std::array< T, K > key_type
friend bool operator!=(const ThreefryGenerator< T, K, Rounds, Constants > &gen1, const ThreefryGenerator< T, K, Rounds, Constants > &gen2)
#define MCKL_THREEFRY_ROUNDS
ThreefryGenerator default rounds.
static constexpr std::size_t size()