You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

randen_engine.h 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_RANDOM_INTERNAL_RANDEN_ENGINE_H_
  15. #define ABSL_RANDOM_INTERNAL_RANDEN_ENGINE_H_
  16. #include <algorithm>
  17. #include <cinttypes>
  18. #include <cstdlib>
  19. #include <iostream>
  20. #include <iterator>
  21. #include <limits>
  22. #include <type_traits>
  23. #include "absl/base/internal/endian.h"
  24. #include "absl/meta/type_traits.h"
  25. #include "absl/random/internal/iostream_state_saver.h"
  26. #include "absl/random/internal/randen.h"
  27. namespace absl
  28. {
  29. ABSL_NAMESPACE_BEGIN
  30. namespace random_internal
  31. {
  32. // Deterministic pseudorandom byte generator with backtracking resistance
  33. // (leaking the state does not compromise prior outputs). Based on Reverie
  34. // (see "A Robust and Sponge-Like PRNG with Improved Efficiency") instantiated
  35. // with an improved Simpira-like permutation.
  36. // Returns values of type "T" (must be a built-in unsigned integer type).
  37. //
  38. // RANDen = RANDom generator or beetroots in Swiss High German.
  39. // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random
  40. // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32.
  41. template<typename T>
  42. class alignas(8) randen_engine
  43. {
  44. public:
  45. // C++11 URBG interface:
  46. using result_type = T;
  47. static_assert(std::is_unsigned<result_type>::value, "randen_engine template argument must be a built-in unsigned "
  48. "integer type");
  49. static constexpr result_type(min)()
  50. {
  51. return (std::numeric_limits<result_type>::min)();
  52. }
  53. static constexpr result_type(max)()
  54. {
  55. return (std::numeric_limits<result_type>::max)();
  56. }
  57. randen_engine() :
  58. randen_engine(0)
  59. {
  60. }
  61. explicit randen_engine(result_type seed_value)
  62. {
  63. seed(seed_value);
  64. }
  65. template<class SeedSequence, typename = typename absl::enable_if_t<!std::is_same<SeedSequence, randen_engine>::value>>
  66. explicit randen_engine(SeedSequence&& seq)
  67. {
  68. seed(seq);
  69. }
  70. // alignment requirements dictate custom copy and move constructors.
  71. randen_engine(const randen_engine& other) :
  72. next_(other.next_),
  73. impl_(other.impl_)
  74. {
  75. std::memcpy(state(), other.state(), kStateSizeT * sizeof(result_type));
  76. }
  77. randen_engine& operator=(const randen_engine& other)
  78. {
  79. next_ = other.next_;
  80. impl_ = other.impl_;
  81. std::memcpy(state(), other.state(), kStateSizeT * sizeof(result_type));
  82. return *this;
  83. }
  84. // Returns random bits from the buffer in units of result_type.
  85. result_type operator()()
  86. {
  87. // Refill the buffer if needed (unlikely).
  88. auto* begin = state();
  89. if (next_ >= kStateSizeT)
  90. {
  91. next_ = kCapacityT;
  92. impl_.Generate(begin);
  93. }
  94. return little_endian::ToHost(begin[next_++]);
  95. }
  96. template<class SeedSequence>
  97. typename absl::enable_if_t<
  98. !std::is_convertible<SeedSequence, result_type>::value>
  99. seed(SeedSequence&& seq)
  100. {
  101. // Zeroes the state.
  102. seed();
  103. reseed(seq);
  104. }
  105. void seed(result_type seed_value = 0)
  106. {
  107. next_ = kStateSizeT;
  108. // Zeroes the inner state and fills the outer state with seed_value to
  109. // mimic the behaviour of reseed
  110. auto* begin = state();
  111. std::fill(begin, begin + kCapacityT, 0);
  112. std::fill(begin + kCapacityT, begin + kStateSizeT, seed_value);
  113. }
  114. // Inserts entropy into (part of) the state. Calling this periodically with
  115. // sufficient entropy ensures prediction resistance (attackers cannot predict
  116. // future outputs even if state is compromised).
  117. template<class SeedSequence>
  118. void reseed(SeedSequence& seq)
  119. {
  120. using sequence_result_type = typename SeedSequence::result_type;
  121. static_assert(sizeof(sequence_result_type) == 4, "SeedSequence::result_type must be 32-bit");
  122. constexpr size_t kBufferSize =
  123. Randen::kSeedBytes / sizeof(sequence_result_type);
  124. alignas(16) sequence_result_type buffer[kBufferSize];
  125. // Randen::Absorb XORs the seed into state, which is then mixed by a call
  126. // to Randen::Generate. Seeding with only the provided entropy is preferred
  127. // to using an arbitrary generate() call, so use [rand.req.seed_seq]
  128. // size as a proxy for the number of entropy units that can be generated
  129. // without relying on seed sequence mixing...
  130. const size_t entropy_size = seq.size();
  131. if (entropy_size < kBufferSize)
  132. {
  133. // ... and only request that many values, or 256-bits, when unspecified.
  134. const size_t requested_entropy = (entropy_size == 0) ? 8u : entropy_size;
  135. std::fill(buffer + requested_entropy, buffer + kBufferSize, 0);
  136. seq.generate(buffer, buffer + requested_entropy);
  137. #ifdef ABSL_IS_BIG_ENDIAN
  138. // Randen expects the seed buffer to be in Little Endian; reverse it on
  139. // Big Endian platforms.
  140. for (sequence_result_type& e : buffer)
  141. {
  142. e = absl::little_endian::FromHost(e);
  143. }
  144. #endif
  145. // The Randen paper suggests preferentially initializing even-numbered
  146. // 128-bit vectors of the randen state (there are 16 such vectors).
  147. // The seed data is merged into the state offset by 128-bits, which
  148. // implies prefering seed bytes [16..31, ..., 208..223]. Since the
  149. // buffer is 32-bit values, we swap the corresponding buffer positions in
  150. // 128-bit chunks.
  151. size_t dst = kBufferSize;
  152. while (dst > 7)
  153. {
  154. // leave the odd bucket as-is.
  155. dst -= 4;
  156. size_t src = dst >> 1;
  157. // swap 128-bits into the even bucket
  158. std::swap(buffer[--dst], buffer[--src]);
  159. std::swap(buffer[--dst], buffer[--src]);
  160. std::swap(buffer[--dst], buffer[--src]);
  161. std::swap(buffer[--dst], buffer[--src]);
  162. }
  163. }
  164. else
  165. {
  166. seq.generate(buffer, buffer + kBufferSize);
  167. }
  168. impl_.Absorb(buffer, state());
  169. // Generate will be called when operator() is called
  170. next_ = kStateSizeT;
  171. }
  172. void discard(uint64_t count)
  173. {
  174. uint64_t step = std::min<uint64_t>(kStateSizeT - next_, count);
  175. count -= step;
  176. constexpr uint64_t kRateT = kStateSizeT - kCapacityT;
  177. auto* begin = state();
  178. while (count > 0)
  179. {
  180. next_ = kCapacityT;
  181. impl_.Generate(*reinterpret_cast<result_type(*)[kStateSizeT]>(begin));
  182. step = std::min<uint64_t>(kRateT, count);
  183. count -= step;
  184. }
  185. next_ += step;
  186. }
  187. bool operator==(const randen_engine& other) const
  188. {
  189. const auto* begin = state();
  190. return next_ == other.next_ &&
  191. std::equal(begin, begin + kStateSizeT, other.state());
  192. }
  193. bool operator!=(const randen_engine& other) const
  194. {
  195. return !(*this == other);
  196. }
  197. template<class CharT, class Traits>
  198. friend std::basic_ostream<CharT, Traits>& operator<<(
  199. std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
  200. const randen_engine<T>& engine
  201. )
  202. { // NOLINT(runtime/references)
  203. using numeric_type =
  204. typename random_internal::stream_format_type<result_type>::type;
  205. auto saver = random_internal::make_ostream_state_saver(os);
  206. auto* it = engine.state();
  207. for (auto* end = it + kStateSizeT; it < end; ++it)
  208. {
  209. // In the case that `elem` is `uint8_t`, it must be cast to something
  210. // larger so that it prints as an integer rather than a character. For
  211. // simplicity, apply the cast all circumstances.
  212. os << static_cast<numeric_type>(little_endian::FromHost(*it))
  213. << os.fill();
  214. }
  215. os << engine.next_;
  216. return os;
  217. }
  218. template<class CharT, class Traits>
  219. friend std::basic_istream<CharT, Traits>& operator>>(
  220. std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
  221. randen_engine<T>& engine
  222. )
  223. { // NOLINT(runtime/references)
  224. using numeric_type =
  225. typename random_internal::stream_format_type<result_type>::type;
  226. result_type state[kStateSizeT];
  227. size_t next;
  228. for (auto& elem : state)
  229. {
  230. // It is not possible to read uint8_t from wide streams, so it is
  231. // necessary to read a wider type and then cast it to uint8_t.
  232. numeric_type value;
  233. is >> value;
  234. elem = little_endian::ToHost(static_cast<result_type>(value));
  235. }
  236. is >> next;
  237. if (is.fail())
  238. {
  239. return is;
  240. }
  241. std::memcpy(engine.state(), state, sizeof(state));
  242. engine.next_ = next;
  243. return is;
  244. }
  245. private:
  246. static constexpr size_t kStateSizeT =
  247. Randen::kStateBytes / sizeof(result_type);
  248. static constexpr size_t kCapacityT =
  249. Randen::kCapacityBytes / sizeof(result_type);
  250. // Returns the state array pointer, which is aligned to 16 bytes.
  251. // The first kCapacityT are the `inner' sponge; the remainder are available.
  252. result_type* state()
  253. {
  254. return reinterpret_cast<result_type*>(
  255. (reinterpret_cast<uintptr_t>(&raw_state_) & 0xf) ? (raw_state_ + 8) : raw_state_
  256. );
  257. }
  258. const result_type* state() const
  259. {
  260. return const_cast<randen_engine*>(this)->state();
  261. }
  262. // raw state array, manually aligned in state(). This overallocates
  263. // by 8 bytes since C++ does not guarantee extended heap alignment.
  264. alignas(8) char raw_state_[Randen::kStateBytes + 8];
  265. size_t next_; // index within state()
  266. Randen impl_;
  267. };
  268. } // namespace random_internal
  269. ABSL_NAMESPACE_END
  270. } // namespace absl
  271. #endif // ABSL_RANDOM_INTERNAL_RANDEN_ENGINE_H_