1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
#ifndef MPT_RANDOM_DEVICE_HPP
#define MPT_RANDOM_DEVICE_HPP
#include "mpt/base/bit.hpp"
#include "mpt/base/detect.hpp"
#include "mpt/base/macros.hpp"
#include "mpt/base/math.hpp"
#include "mpt/base/integer.hpp"
#include "mpt/base/namespace.hpp"
#include "mpt/crc/crc.hpp"
#include "mpt/endian/integer.hpp"
#include "mpt/mutex/mutex.hpp"
#include "mpt/out_of_memory/out_of_memory.hpp"
#include "mpt/random/engine.hpp"
#include "mpt/random/engine_lcg.hpp"
#include "mpt/random/random.hpp"
#include <chrono>
#include <limits>
#include <memory>
#include <random>
#include <string>
#include <cmath>
#include <cstring>
namespace mpt {
inline namespace MPT_INLINE_NS {
inline constexpr uint32 DETERMINISTIC_RNG_SEED = 3141592653u; // pi
template <typename T>
struct default_radom_seed_hash {
};
template <>
struct default_radom_seed_hash<uint8> {
using type = mpt::crc16;
};
template <>
struct default_radom_seed_hash<uint16> {
using type = mpt::crc16;
};
template <>
struct default_radom_seed_hash<uint32> {
using type = mpt::crc32c;
};
template <>
struct default_radom_seed_hash<uint64> {
using type = mpt::crc64_jones;
};
class prng_random_device_time_seeder {
public:
template <typename T>
inline T generate_seed() {
// Note: CRC is actually not that good a choice here, but it is simple and we
// already have an implementaion available. Better choices for mixing entropy
// would be a hash function with proper avalanche characteristics or a block
// or stream cipher with any pre-choosen random key and IV. The only aspect we
// really need here is whitening of the bits.
typename mpt::default_radom_seed_hash<T>::type hash;
{
uint64be time;
time = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock().now().time_since_epoch()).count();
std::byte bytes[sizeof(time)];
std::memcpy(bytes, &time, sizeof(time));
hash(std::begin(bytes), std::end(bytes));
}
#if !defined(MPT_COMPILER_QUIRK_CHRONO_NO_HIGH_RESOLUTION_CLOCK)
// Avoid std::chrono::high_resolution_clock on Emscripten because availability is problematic in AudioWorklet context.
{
uint64be time;
time = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock().now().time_since_epoch()).count();
std::byte bytes[sizeof(time)];
std::memcpy(bytes, &time, sizeof(time));
hash(std::begin(bytes), std::end(bytes));
}
#endif // !MPT_COMPILER_QUIRK_CHRONO_NO_HIGH_RESOLUTION_CLOCK
return static_cast<T>(hash.result());
}
public:
prng_random_device_time_seeder() = default;
};
// C++11 std::random_device may be implemented as a deterministic PRNG.
// There is no way to seed this PRNG and it is allowed to be seeded with the
// same value on each program invocation. This makes std::random_device
// completely useless even as a non-cryptographic entropy pool.
// We fallback to time-seeded std::mt19937 if std::random_device::entropy() is
// 0 or less.
class sane_random_device {
private:
mpt::mutex m;
std::string token;
#if !defined(MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE)
std::unique_ptr<std::random_device> prd;
bool rd_reliable{false};
#endif // !MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE
std::unique_ptr<std::mt19937> rd_fallback;
public:
using result_type = unsigned int;
private:
void init_fallback() {
if (!rd_fallback) {
if (token.length() > 0) {
uint64 seed_val = mpt::prng_random_device_time_seeder().generate_seed<uint64>();
std::vector<unsigned int> seeds;
seeds.push_back(static_cast<uint32>(seed_val >> 32));
seeds.push_back(static_cast<uint32>(seed_val >> 0));
for (std::size_t i = 0; i < token.length(); ++i) {
seeds.push_back(static_cast<unsigned int>(static_cast<unsigned char>(token[i])));
}
std::seed_seq seed(seeds.begin(), seeds.end());
rd_fallback = std::make_unique<std::mt19937>(seed);
} else {
uint64 seed_val = mpt::prng_random_device_time_seeder().generate_seed<uint64>();
unsigned int seeds[2];
seeds[0] = static_cast<uint32>(seed_val >> 32);
seeds[1] = static_cast<uint32>(seed_val >> 0);
std::seed_seq seed(seeds + 0, seeds + 2);
rd_fallback = std::make_unique<std::mt19937>(seed);
}
}
}
public:
sane_random_device() {
#if !defined(MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE)
try {
prd = std::make_unique<std::random_device>();
rd_reliable = ((*prd).entropy() > 0.0);
} catch (mpt::out_of_memory e) {
mpt::rethrow_out_of_memory(e);
} catch (const std::exception &) {
rd_reliable = false;
}
if (!rd_reliable) {
init_fallback();
}
#else // MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE
init_fallback();
#endif // !MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE
}
sane_random_device(const std::string & token_)
: token(token_) {
#if !defined(MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE)
try {
prd = std::make_unique<std::random_device>(token);
rd_reliable = ((*prd).entropy() > 0.0);
} catch (mpt::out_of_memory e) {
mpt::rethrow_out_of_memory(e);
} catch (const std::exception &) {
rd_reliable = false;
}
if (!rd_reliable) {
init_fallback();
}
#else // MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE
init_fallback();
#endif // !MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE
}
static MPT_CONSTEXPRINLINE result_type min() {
return std::numeric_limits<result_type>::min();
}
static MPT_CONSTEXPRINLINE result_type max() {
return std::numeric_limits<result_type>::max();
}
static MPT_CONSTEXPRINLINE int result_bits() {
return sizeof(result_type) * 8;
}
result_type operator()() {
mpt::lock_guard<mpt::mutex> l(m);
result_type result = 0;
#if !defined(MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE)
if (prd) {
try {
if constexpr (std::random_device::min() != 0 || !mpt::is_mask(std::random_device::max())) {
// insane std::random_device
// This implementation is not exactly uniformly distributed but good enough
// for OpenMPT.
constexpr double rd_min = static_cast<double>(std::random_device::min());
constexpr double rd_max = static_cast<double>(std::random_device::max());
constexpr double rd_range = rd_max - rd_min;
constexpr double rd_size = rd_range + 1.0;
const double rd_entropy = mpt::log2(rd_size);
const int iterations = static_cast<int>(std::ceil(result_bits() / rd_entropy));
double tmp = 0.0;
for (int i = 0; i < iterations; ++i) {
tmp = (tmp * rd_size) + (static_cast<double>((*prd)()) - rd_min);
}
double result_01 = std::floor(tmp / std::pow(rd_size, iterations));
result = static_cast<result_type>(std::floor(result_01 * (static_cast<double>(max() - min()) + 1.0))) + min();
} else {
// sane std::random_device
result = 0;
std::size_t rd_bits = mpt::lower_bound_entropy_bits(std::random_device::max());
for (std::size_t entropy = 0; entropy < (sizeof(result_type) * 8); entropy += rd_bits) {
if (rd_bits < (sizeof(result_type) * 8)) {
result = (result << rd_bits) | static_cast<result_type>((*prd)());
} else {
result = result | static_cast<result_type>((*prd)());
}
}
}
} catch (const std::exception &) {
rd_reliable = false;
init_fallback();
}
} else {
rd_reliable = false;
}
if (!rd_reliable) {
// std::random_device is unreliable
// XOR the generated random number with more entropy from the time-seeded
// PRNG.
// Note: This is safe even if the std::random_device itself is implemented
// as a std::mt19937 PRNG because we are very likely using a different
// seed.
result ^= mpt::random<result_type>(*rd_fallback);
}
#else // MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE
result ^= mpt::random<result_type>(*rd_fallback);
#endif // !MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE
return result;
}
};
class prng_random_device_deterministic_seeder {
protected:
template <typename T>
constexpr T generate_seed() noexcept {
return static_cast<T>(mpt::DETERMINISTIC_RNG_SEED);
}
protected:
prng_random_device_deterministic_seeder() = default;
};
template <typename Trng = mpt::lcg_musl, typename seeder = mpt::prng_random_device_time_seeder>
class prng_random_device
: private seeder {
public:
using result_type = unsigned int;
private:
mpt::mutex m;
Trng rng;
public:
prng_random_device()
: rng(seeder::template generate_seed<typename Trng::state_type>()) {
return;
}
prng_random_device(const std::string &)
: rng(seeder::template generate_seed<typename Trng::state_type>()) {
return;
}
static MPT_CONSTEXPRINLINE result_type min() {
return std::numeric_limits<unsigned int>::min();
}
static MPT_CONSTEXPRINLINE result_type max() {
return std::numeric_limits<unsigned int>::max();
}
static MPT_CONSTEXPRINLINE int result_bits() {
return sizeof(unsigned int) * 8;
}
result_type operator()() {
mpt::lock_guard<mpt::mutex> l(m);
return mpt::random<unsigned int>(rng);
}
};
using deterministc_random_device = mpt::prng_random_device<mpt::lcg_musl, mpt::prng_random_device_deterministic_seeder>;
} // namespace MPT_INLINE_NS
} // namespace mpt
#endif // MPT_RANDOM_DEVICE_HPP
|