randen_engine.h 9.6 KB

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