aboutsummaryrefslogtreecommitdiff
path: root/src/random.cpp
diff options
context:
space:
mode:
authorMarcoFalke <6399679+MarcoFalke@users.noreply.github.com>2023-03-02 14:43:43 +0100
committerMarcoFalke <6399679+MarcoFalke@users.noreply.github.com>2023-03-02 14:48:28 +0100
commit1111e2f8b43cd9ed62dcf6b571a224b84fc421fd (patch)
treef68a00409a4359bfcb69d47222ddccf0465e3a07 /src/random.cpp
parent74981aa02d2b14ad1c0b82d1eb09cf3169eaa8ae (diff)
downloadbitcoin-1111e2f8b43cd9ed62dcf6b571a224b84fc421fd.tar.xz
Use steady clock in SeedStrengthen and FindBestImplementation
Diffstat (limited to 'src/random.cpp')
-rw-r--r--src/random.cpp14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/random.cpp b/src/random.cpp
index 432592589a..f4c51574cc 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -221,14 +221,14 @@ static void SeedHardwareSlow(CSHA512& hasher) noexcept {
}
/** 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;
inner_hasher.Write(seed, sizeof(seed));
// Hash loop
unsigned char buffer[64];
- int64_t stop = GetTimeMicros() + microseconds;
+ const auto stop{SteadyClock::now() + dur};
do {
for (int i = 0; i < 1000; ++i) {
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.
int64_t perf = GetPerformanceCounter();
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.
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. */
-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.
unsigned char strengthen_seed[32];
rng.MixExtract(strengthen_seed, sizeof(strengthen_seed), CSHA512(hasher), false);
// 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
@@ -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);
// Strengthen for 10 ms
- SeedStrengthen(hasher, rng, 10000);
+ SeedStrengthen(hasher, rng, 10ms);
}
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);
// Strengthen for 100 ms
- SeedStrengthen(hasher, rng, 100000);
+ SeedStrengthen(hasher, rng, 100ms);
}
enum class RNGLevel {