mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 23:09:44 -04:00
Use steady clock in SeedStrengthen and FindBestImplementation
This commit is contained in:
parent
74981aa02d
commit
1111e2f8b4
4 changed files with 14 additions and 12 deletions
|
@ -50,12 +50,14 @@ if [ "${RUN_TIDY}" = "true" ]; then
|
||||||
" src/node/chainstate.cpp"\
|
" src/node/chainstate.cpp"\
|
||||||
" src/node/chainstatemanager_args.cpp"\
|
" src/node/chainstatemanager_args.cpp"\
|
||||||
" src/node/mempool_args.cpp"\
|
" src/node/mempool_args.cpp"\
|
||||||
|
" src/node/minisketchwrapper.cpp"\
|
||||||
" src/node/utxo_snapshot.cpp"\
|
" src/node/utxo_snapshot.cpp"\
|
||||||
" src/node/validation_cache_args.cpp"\
|
" src/node/validation_cache_args.cpp"\
|
||||||
" src/policy/feerate.cpp"\
|
" src/policy/feerate.cpp"\
|
||||||
" src/policy/packages.cpp"\
|
" src/policy/packages.cpp"\
|
||||||
" src/policy/settings.cpp"\
|
" src/policy/settings.cpp"\
|
||||||
" src/primitives/transaction.cpp"\
|
" src/primitives/transaction.cpp"\
|
||||||
|
" src/random.cpp"\
|
||||||
" src/rpc/fees.cpp"\
|
" src/rpc/fees.cpp"\
|
||||||
" src/rpc/signmessage.cpp"\
|
" src/rpc/signmessage.cpp"\
|
||||||
" src/test/fuzz/txorphan.cpp"\
|
" src/test/fuzz/txorphan.cpp"\
|
||||||
|
|
|
@ -23,17 +23,17 @@ static constexpr uint32_t BITS = 32;
|
||||||
|
|
||||||
uint32_t FindBestImplementation()
|
uint32_t FindBestImplementation()
|
||||||
{
|
{
|
||||||
std::optional<std::pair<int64_t, uint32_t>> best;
|
std::optional<std::pair<SteadyClock::duration, uint32_t>> best;
|
||||||
|
|
||||||
uint32_t max_impl = Minisketch::MaxImplementation();
|
uint32_t max_impl = Minisketch::MaxImplementation();
|
||||||
for (uint32_t impl = 0; impl <= max_impl; ++impl) {
|
for (uint32_t impl = 0; impl <= max_impl; ++impl) {
|
||||||
std::vector<int64_t> benches;
|
std::vector<SteadyClock::duration> benches;
|
||||||
uint64_t offset = 0;
|
uint64_t offset = 0;
|
||||||
/* Run a little benchmark with capacity 32, adding 184 entries, and decoding 11 of them once. */
|
/* Run a little benchmark with capacity 32, adding 184 entries, and decoding 11 of them once. */
|
||||||
for (int b = 0; b < 11; ++b) {
|
for (int b = 0; b < 11; ++b) {
|
||||||
if (!Minisketch::ImplementationSupported(BITS, impl)) break;
|
if (!Minisketch::ImplementationSupported(BITS, impl)) break;
|
||||||
Minisketch sketch(BITS, impl, 32);
|
Minisketch sketch(BITS, impl, 32);
|
||||||
auto start = GetTimeMicros();
|
auto start = SteadyClock::now();
|
||||||
for (uint64_t e = 0; e < 100; ++e) {
|
for (uint64_t e = 0; e < 100; ++e) {
|
||||||
sketch.Add(e*1337 + b*13337 + offset);
|
sketch.Add(e*1337 + b*13337 + offset);
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ uint32_t FindBestImplementation()
|
||||||
sketch.Add(e*1337 + b*13337 + offset);
|
sketch.Add(e*1337 + b*13337 + offset);
|
||||||
}
|
}
|
||||||
offset += (*sketch.Decode(32))[0];
|
offset += (*sketch.Decode(32))[0];
|
||||||
auto stop = GetTimeMicros();
|
auto stop = SteadyClock::now();
|
||||||
benches.push_back(stop - start);
|
benches.push_back(stop - start);
|
||||||
}
|
}
|
||||||
/* Remember which implementation has the best median benchmark time. */
|
/* Remember which implementation has the best median benchmark time. */
|
||||||
|
|
|
@ -221,14 +221,14 @@ static void SeedHardwareSlow(CSHA512& hasher) noexcept {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Use repeated SHA512 to strengthen the randomness in seed32, and feed into hasher. */
|
/** Use repeated SHA512 to strengthen the randomness in seed32, and feed into hasher. */
|
||||||
static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA512& hasher) noexcept
|
static void Strengthen(const unsigned char (&seed)[32], SteadyClock::duration dur, CSHA512& hasher) noexcept
|
||||||
{
|
{
|
||||||
CSHA512 inner_hasher;
|
CSHA512 inner_hasher;
|
||||||
inner_hasher.Write(seed, sizeof(seed));
|
inner_hasher.Write(seed, sizeof(seed));
|
||||||
|
|
||||||
// Hash loop
|
// Hash loop
|
||||||
unsigned char buffer[64];
|
unsigned char buffer[64];
|
||||||
int64_t stop = GetTimeMicros() + microseconds;
|
const auto stop{SteadyClock::now() + dur};
|
||||||
do {
|
do {
|
||||||
for (int i = 0; i < 1000; ++i) {
|
for (int i = 0; i < 1000; ++i) {
|
||||||
inner_hasher.Finalize(buffer);
|
inner_hasher.Finalize(buffer);
|
||||||
|
@ -238,7 +238,7 @@ static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA51
|
||||||
// Benchmark operation and feed it into outer hasher.
|
// Benchmark operation and feed it into outer hasher.
|
||||||
int64_t perf = GetPerformanceCounter();
|
int64_t perf = GetPerformanceCounter();
|
||||||
hasher.Write((const unsigned char*)&perf, sizeof(perf));
|
hasher.Write((const unsigned char*)&perf, sizeof(perf));
|
||||||
} while (GetTimeMicros() < stop);
|
} while (SteadyClock::now() < stop);
|
||||||
|
|
||||||
// Produce output from inner state and feed it to outer hasher.
|
// Produce output from inner state and feed it to outer hasher.
|
||||||
inner_hasher.Finalize(buffer);
|
inner_hasher.Finalize(buffer);
|
||||||
|
@ -492,13 +492,13 @@ static void SeedSlow(CSHA512& hasher, RNGState& rng) noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Extract entropy from rng, strengthen it, and feed it into hasher. */
|
/** Extract entropy from rng, strengthen it, and feed it into hasher. */
|
||||||
static void SeedStrengthen(CSHA512& hasher, RNGState& rng, int microseconds) noexcept
|
static void SeedStrengthen(CSHA512& hasher, RNGState& rng, SteadyClock::duration dur) noexcept
|
||||||
{
|
{
|
||||||
// Generate 32 bytes of entropy from the RNG, and a copy of the entropy already in hasher.
|
// Generate 32 bytes of entropy from the RNG, and a copy of the entropy already in hasher.
|
||||||
unsigned char strengthen_seed[32];
|
unsigned char strengthen_seed[32];
|
||||||
rng.MixExtract(strengthen_seed, sizeof(strengthen_seed), CSHA512(hasher), false);
|
rng.MixExtract(strengthen_seed, sizeof(strengthen_seed), CSHA512(hasher), false);
|
||||||
// Strengthen the seed, and feed it into hasher.
|
// Strengthen the seed, and feed it into hasher.
|
||||||
Strengthen(strengthen_seed, microseconds, hasher);
|
Strengthen(strengthen_seed, dur, hasher);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SeedPeriodic(CSHA512& hasher, RNGState& rng) noexcept
|
static void SeedPeriodic(CSHA512& hasher, RNGState& rng) noexcept
|
||||||
|
@ -518,7 +518,7 @@ static void SeedPeriodic(CSHA512& hasher, RNGState& rng) noexcept
|
||||||
LogPrint(BCLog::RAND, "Feeding %i bytes of dynamic environment data into RNG\n", hasher.Size() - old_size);
|
LogPrint(BCLog::RAND, "Feeding %i bytes of dynamic environment data into RNG\n", hasher.Size() - old_size);
|
||||||
|
|
||||||
// Strengthen for 10 ms
|
// Strengthen for 10 ms
|
||||||
SeedStrengthen(hasher, rng, 10000);
|
SeedStrengthen(hasher, rng, 10ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
|
static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
|
||||||
|
@ -538,7 +538,7 @@ static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
|
||||||
LogPrint(BCLog::RAND, "Feeding %i bytes of environment data into RNG\n", hasher.Size() - old_size);
|
LogPrint(BCLog::RAND, "Feeding %i bytes of environment data into RNG\n", hasher.Size() - old_size);
|
||||||
|
|
||||||
// Strengthen for 100 ms
|
// Strengthen for 100 ms
|
||||||
SeedStrengthen(hasher, rng, 100000);
|
SeedStrengthen(hasher, rng, 100ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class RNGLevel {
|
enum class RNGLevel {
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
#include <compat/compat.h>
|
#include <compat/compat.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono> // IWYU pragma: export
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue