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