randen_engine.h 9.6 KB

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