1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

pow-testgiver support

This commit is contained in:
ton 2020-07-06 17:07:20 +03:00
parent dbde9c1c40
commit f064b1047a
257 changed files with 6665 additions and 2608 deletions

View file

@ -43,25 +43,25 @@ void Random::secure_bytes(MutableSlice dest) {
}
void Random::secure_bytes(unsigned char *ptr, size_t size) {
constexpr size_t buf_size = 512;
constexpr size_t BUF_SIZE = 512;
static TD_THREAD_LOCAL unsigned char *buf; // static zero-initialized
static TD_THREAD_LOCAL size_t buf_pos;
static TD_THREAD_LOCAL int64 generation;
if (init_thread_local<unsigned char[]>(buf, buf_size)) {
buf_pos = buf_size;
if (init_thread_local<unsigned char[]>(buf, BUF_SIZE)) {
buf_pos = BUF_SIZE;
generation = 0;
}
if (ptr == nullptr) {
td::MutableSlice(buf, buf_size).fill_zero_secure();
buf_pos = buf_size;
td::MutableSlice(buf, BUF_SIZE).fill_zero_secure();
buf_pos = BUF_SIZE;
return;
}
if (generation != random_seed_generation.load(std::memory_order_relaxed)) {
generation = random_seed_generation.load(std::memory_order_acquire);
buf_pos = buf_size;
buf_pos = BUF_SIZE;
}
auto ready = min(size, buf_size - buf_pos);
auto ready = min(size, BUF_SIZE - buf_pos);
if (ready != 0) {
std::memcpy(ptr, buf + buf_pos, ready);
buf_pos += ready;
@ -71,8 +71,8 @@ void Random::secure_bytes(unsigned char *ptr, size_t size) {
return;
}
}
if (size < buf_size) {
int err = RAND_bytes(buf, static_cast<int>(buf_size));
if (size < BUF_SIZE) {
int err = RAND_bytes(buf, static_cast<int>(BUF_SIZE));
// TODO: it CAN fail
LOG_IF(FATAL, err != 1);
buf_pos = size;
@ -164,7 +164,7 @@ double Random::fast(double min, double max) {
}
Random::Xorshift128plus::Xorshift128plus(uint64 seed) {
auto next = [&]() {
auto next = [&] {
// splitmix64
seed += static_cast<uint64>(0x9E3779B97F4A7C15ull);
uint64 z = seed;
@ -189,6 +189,7 @@ uint64 Random::Xorshift128plus::operator()() {
seed_[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
return seed_[1] + y;
}
int Random::Xorshift128plus::fast(int min, int max) {
return static_cast<int>((*this)() % (max - min + 1) + min);
}
@ -196,4 +197,18 @@ int64 Random::Xorshift128plus::fast64(int64 min, int64 max) {
return static_cast<int64>((*this)() % (max - min + 1) + min);
}
void Random::Xorshift128plus::bytes(MutableSlice dest) {
int cnt = 0;
uint64 buf = 0;
for (auto &c : dest) {
if (cnt == 0) {
buf = operator()();
cnt = 8;
}
cnt--;
c = static_cast<char>(buf & 255);
buf >>= 8;
}
}
} // namespace td