2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/thread_local.h"
|
|
|
|
|
|
|
|
#if TD_HAVE_OPENSSL
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#endif
|
|
|
|
|
2018-12-11 21:18:58 +01:00
|
|
|
#include <atomic>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <limits>
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
#if TD_HAVE_OPENSSL
|
2018-12-11 21:18:58 +01:00
|
|
|
|
2018-03-26 16:01:27 +02:00
|
|
|
namespace {
|
2018-12-11 21:18:58 +01:00
|
|
|
std::atomic<int64> random_seed_generation{0};
|
|
|
|
} // namespace
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void Random::secure_bytes(MutableSlice dest) {
|
|
|
|
Random::secure_bytes(dest.ubegin(), dest.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Random::secure_bytes(unsigned char *ptr, size_t size) {
|
2020-06-22 01:40:05 +02:00
|
|
|
constexpr size_t BUF_SIZE = 512;
|
2018-12-31 20:04:05 +01:00
|
|
|
static TD_THREAD_LOCAL unsigned char *buf; // static zero-initialized
|
|
|
|
static TD_THREAD_LOCAL size_t buf_pos;
|
2018-11-16 13:41:53 +01:00
|
|
|
static TD_THREAD_LOCAL int64 generation;
|
2020-06-22 01:40:05 +02:00
|
|
|
if (init_thread_local<unsigned char[]>(buf, BUF_SIZE)) {
|
|
|
|
buf_pos = BUF_SIZE;
|
2018-11-16 13:41:53 +01:00
|
|
|
generation = 0;
|
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
if (ptr == nullptr) {
|
2020-06-26 01:24:13 +02:00
|
|
|
MutableSlice(buf, BUF_SIZE).fill_zero_secure();
|
2020-06-24 13:47:36 +02:00
|
|
|
buf_pos = BUF_SIZE;
|
|
|
|
return;
|
|
|
|
}
|
2018-11-16 13:41:53 +01:00
|
|
|
if (generation != random_seed_generation.load(std::memory_order_relaxed)) {
|
|
|
|
generation = random_seed_generation.load(std::memory_order_acquire);
|
2020-06-22 01:40:05 +02:00
|
|
|
buf_pos = BUF_SIZE;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 01:40:05 +02:00
|
|
|
auto ready = min(size, BUF_SIZE - buf_pos);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (ready != 0) {
|
|
|
|
std::memcpy(ptr, buf + buf_pos, ready);
|
|
|
|
buf_pos += ready;
|
|
|
|
ptr += ready;
|
|
|
|
size -= ready;
|
|
|
|
if (size == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 01:40:05 +02:00
|
|
|
if (size < BUF_SIZE) {
|
|
|
|
int err = RAND_bytes(buf, static_cast<int>(BUF_SIZE));
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO: it CAN fail
|
|
|
|
LOG_IF(FATAL, err != 1);
|
|
|
|
buf_pos = size;
|
|
|
|
std::memcpy(ptr, buf, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(size <= static_cast<size_t>(std::numeric_limits<int>::max()));
|
|
|
|
int err = RAND_bytes(ptr, static_cast<int>(size));
|
|
|
|
// TODO: it CAN fail
|
|
|
|
LOG_IF(FATAL, err != 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 Random::secure_int32() {
|
|
|
|
int32 res = 0;
|
|
|
|
secure_bytes(reinterpret_cast<unsigned char *>(&res), sizeof(int32));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 Random::secure_int64() {
|
|
|
|
int64 res = 0;
|
|
|
|
secure_bytes(reinterpret_cast<unsigned char *>(&res), sizeof(int64));
|
|
|
|
return res;
|
|
|
|
}
|
2018-03-26 16:01:27 +02:00
|
|
|
|
2019-07-04 12:55:17 +02:00
|
|
|
uint32 Random::secure_uint32() {
|
|
|
|
uint32 res = 0;
|
|
|
|
secure_bytes(reinterpret_cast<unsigned char *>(&res), sizeof(uint32));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 Random::secure_uint64() {
|
|
|
|
uint64 res = 0;
|
|
|
|
secure_bytes(reinterpret_cast<unsigned char *>(&res), sizeof(uint64));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:01:27 +02:00
|
|
|
void Random::add_seed(Slice bytes, double entropy) {
|
|
|
|
RAND_add(bytes.data(), static_cast<int>(bytes.size()), entropy);
|
2018-11-16 13:41:53 +01:00
|
|
|
random_seed_generation++;
|
2018-03-26 16:01:27 +02:00
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
|
|
|
|
void Random::secure_cleanup() {
|
|
|
|
Random::secure_bytes(nullptr, 0);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static unsigned int rand_device_helper() {
|
|
|
|
static TD_THREAD_LOCAL std::random_device *rd;
|
|
|
|
init_thread_local<std::random_device>(rd);
|
|
|
|
return (*rd)();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 Random::fast_uint32() {
|
|
|
|
static TD_THREAD_LOCAL std::mt19937 *gen;
|
|
|
|
if (!gen) {
|
|
|
|
auto &rg = rand_device_helper;
|
|
|
|
std::seed_seq seq{rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg()};
|
2018-09-07 02:41:21 +02:00
|
|
|
init_thread_local<std::mt19937>(gen, seq);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return static_cast<uint32>((*gen)());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 Random::fast_uint64() {
|
|
|
|
static TD_THREAD_LOCAL std::mt19937_64 *gen;
|
|
|
|
if (!gen) {
|
|
|
|
auto &rg = rand_device_helper;
|
|
|
|
std::seed_seq seq{rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg()};
|
2018-09-07 02:41:21 +02:00
|
|
|
init_thread_local<std::mt19937_64>(gen, seq);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return static_cast<uint64>((*gen)());
|
|
|
|
}
|
|
|
|
|
2021-12-15 18:55:03 +01:00
|
|
|
int Random::fast(int min_value, int max_value) {
|
|
|
|
if (min_value == std::numeric_limits<int>::min() && max_value == std::numeric_limits<int>::max()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// to prevent integer overflow and division by zero
|
2021-12-15 18:55:03 +01:00
|
|
|
min_value++;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-12-15 18:55:03 +01:00
|
|
|
DCHECK(min_value <= max_value);
|
|
|
|
return static_cast<int>(min_value + fast_uint32() % (max_value - min_value + 1)); // TODO signed_cast
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 18:55:03 +01:00
|
|
|
double Random::fast(double min_value, double max_value) {
|
|
|
|
DCHECK(min_value <= max_value);
|
|
|
|
return min_value + fast_uint32() * 1.0 / std::numeric_limits<uint32>::max() * (max_value - min_value);
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 11:47:03 +02:00
|
|
|
bool Random::fast_bool() {
|
|
|
|
return (fast_uint32() & 1) != 0;
|
|
|
|
}
|
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
Random::Xorshift128plus::Xorshift128plus(uint64 seed) {
|
2020-02-26 16:15:19 +01:00
|
|
|
auto next = [&] {
|
2018-08-13 19:15:09 +02:00
|
|
|
// splitmix64
|
2020-06-24 13:47:36 +02:00
|
|
|
seed += static_cast<uint64>(0x9E3779B97F4A7C15ull);
|
2018-09-07 02:41:21 +02:00
|
|
|
uint64 z = seed;
|
2020-06-24 13:47:36 +02:00
|
|
|
z = (z ^ (z >> 30)) * static_cast<uint64>(0xBF58476D1CE4E5B9ull);
|
|
|
|
z = (z ^ (z >> 27)) * static_cast<uint64>(0x94D049BB133111EBull);
|
2018-08-13 19:15:09 +02:00
|
|
|
return z ^ (z >> 31);
|
|
|
|
};
|
|
|
|
seed_[0] = next();
|
|
|
|
seed_[1] = next();
|
|
|
|
}
|
|
|
|
|
|
|
|
Random::Xorshift128plus::Xorshift128plus(uint64 seed_a, uint64 seed_b) {
|
|
|
|
seed_[0] = seed_a;
|
|
|
|
seed_[1] = seed_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 Random::Xorshift128plus::operator()() {
|
2018-09-07 02:41:21 +02:00
|
|
|
uint64 x = seed_[0];
|
|
|
|
const uint64 y = seed_[1];
|
2018-08-13 19:15:09 +02:00
|
|
|
seed_[0] = y;
|
2018-09-07 02:41:21 +02:00
|
|
|
x ^= x << 23;
|
|
|
|
seed_[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
|
2018-08-13 19:15:09 +02:00
|
|
|
return seed_[1] + y;
|
|
|
|
}
|
2020-06-12 18:43:58 +02:00
|
|
|
|
2021-12-15 18:55:03 +01:00
|
|
|
int Random::Xorshift128plus::fast(int min_value, int max_value) {
|
|
|
|
return static_cast<int>((*this)() % (max_value - min_value + 1) + min_value);
|
2018-12-19 15:48:39 +01:00
|
|
|
}
|
2021-12-15 18:55:03 +01:00
|
|
|
int64 Random::Xorshift128plus::fast64(int64 min_value, int64 max_value) {
|
|
|
|
return static_cast<int64>((*this)() % (max_value - min_value + 1) + min_value);
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2020-06-12 17:06:40 +02:00
|
|
|
void Random::Xorshift128plus::bytes(MutableSlice dest) {
|
|
|
|
int cnt = 0;
|
2020-06-13 04:13:11 +02:00
|
|
|
uint64 buf = 0;
|
2020-06-12 17:06:40 +02:00
|
|
|
for (auto &c : dest) {
|
|
|
|
if (cnt == 0) {
|
|
|
|
buf = operator()();
|
|
|
|
cnt = 8;
|
|
|
|
}
|
|
|
|
cnt--;
|
2020-06-12 18:43:58 +02:00
|
|
|
c = static_cast<char>(buf & 255);
|
2020-06-12 17:06:40 +02:00
|
|
|
buf >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|