/src/sentencepiece/third_party/absl/random/internal/randen.h
Line | Count | Source |
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_H_ |
16 | | #define ABSL_RANDOM_INTERNAL_RANDEN_H_ |
17 | | |
18 | | #include <cstddef> |
19 | | |
20 | | #include "absl/random/internal/platform.h" |
21 | | #include "absl/random/internal/randen_hwaes.h" |
22 | | #include "absl/random/internal/randen_slow.h" |
23 | | #include "absl/random/internal/randen_traits.h" |
24 | | |
25 | | namespace absl { |
26 | | ABSL_NAMESPACE_BEGIN |
27 | | namespace random_internal { |
28 | | |
29 | | // RANDen = RANDom generator or beetroots in Swiss High German. |
30 | | // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random |
31 | | // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32. |
32 | | // |
33 | | // Randen implements the basic state manipulation methods. |
34 | | class Randen { |
35 | | public: |
36 | | static constexpr size_t kStateBytes = RandenTraits::kStateBytes; |
37 | | static constexpr size_t kCapacityBytes = RandenTraits::kCapacityBytes; |
38 | | static constexpr size_t kSeedBytes = RandenTraits::kSeedBytes; |
39 | | |
40 | | ~Randen() = default; |
41 | | |
42 | | Randen(); |
43 | | |
44 | | // Generate updates the randen sponge. The outer portion of the sponge |
45 | | // (kCapacityBytes .. kStateBytes) may be consumed as PRNG state. |
46 | | // REQUIRES: state points to kStateBytes of state. |
47 | 1.14k | inline void Generate(void* state) const { |
48 | 1.14k | #if ABSL_RANDOM_INTERNAL_AES_DISPATCH |
49 | | // HW AES Dispatch. |
50 | 1.14k | if (has_crypto_) { |
51 | 0 | RandenHwAes::Generate(keys_, state); |
52 | 1.14k | } else { |
53 | 1.14k | RandenSlow::Generate(keys_, state); |
54 | 1.14k | } |
55 | | #elif ABSL_HAVE_ACCELERATED_AES |
56 | | // HW AES is enabled. |
57 | | RandenHwAes::Generate(keys_, state); |
58 | | #else |
59 | | // HW AES is disabled. |
60 | | RandenSlow::Generate(keys_, state); |
61 | | #endif |
62 | 1.14k | } |
63 | | |
64 | | // Absorb incorporates additional seed material into the randen sponge. After |
65 | | // absorb returns, Generate must be called before the state may be consumed. |
66 | | // REQUIRES: seed points to kSeedBytes of seed. |
67 | | // REQUIRES: state points to kStateBytes of state. |
68 | 9.79k | inline void Absorb(const void* seed, void* state) const { |
69 | 9.79k | #if ABSL_RANDOM_INTERNAL_AES_DISPATCH |
70 | | // HW AES Dispatch. |
71 | 9.79k | if (has_crypto_) { |
72 | 0 | RandenHwAes::Absorb(seed, state); |
73 | 9.79k | } else { |
74 | 9.79k | RandenSlow::Absorb(seed, state); |
75 | 9.79k | } |
76 | | #elif ABSL_HAVE_ACCELERATED_AES |
77 | | // HW AES is enabled. |
78 | | RandenHwAes::Absorb(seed, state); |
79 | | #else |
80 | | // HW AES is disabled. |
81 | | RandenSlow::Absorb(seed, state); |
82 | | #endif |
83 | 9.79k | } |
84 | | |
85 | | private: |
86 | | const void* keys_; |
87 | | #if ABSL_RANDOM_INTERNAL_AES_DISPATCH |
88 | | bool has_crypto_; |
89 | | #endif |
90 | | }; |
91 | | |
92 | | } // namespace random_internal |
93 | | ABSL_NAMESPACE_END |
94 | | } // namespace absl |
95 | | |
96 | | #endif // ABSL_RANDOM_INTERNAL_RANDEN_H_ |