MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / Random_SanityCheck

Function Random_SanityCheck

src/random.cpp:412–454  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

410}
411
412bool Random_SanityCheck()
413{
414 uint64_t start = GetPerformanceCounter();
415
416 /* This does not measure the quality of randomness, but it does test that
417 * OSRandom() overwrites all 32 bytes of the output given a maximum
418 * number of tries.
419 */
420 static const ssize_t MAX_TRIES = 1024;
421 uint8_t data[NUM_OS_RANDOM_BYTES];
422 bool overwritten[NUM_OS_RANDOM_BYTES] = {}; /* Tracks which bytes have been overwritten at least once */
423 int num_overwritten;
424 int tries = 0;
425 /* Loop until all bytes have been overwritten at least once, or max number tries reached */
426 do {
427 memset(data, 0, NUM_OS_RANDOM_BYTES);
428 GetOSRand(data);
429 for (int x=0; x < NUM_OS_RANDOM_BYTES; ++x) {
430 overwritten[x] |= (data[x] != 0);
431 }
432
433 num_overwritten = 0;
434 for (int x=0; x < NUM_OS_RANDOM_BYTES; ++x) {
435 if (overwritten[x]) {
436 num_overwritten += 1;
437 }
438 }
439
440 tries += 1;
441 } while (num_overwritten < NUM_OS_RANDOM_BYTES && tries < MAX_TRIES);
442 if (num_overwritten != NUM_OS_RANDOM_BYTES) return false; /* If this failed, bailed out after too many tries */
443
444 // Check that GetPerformanceCounter increases at least during a GetOSRand() call + 1ms sleep.
445 std::this_thread::sleep_for(std::chrono::milliseconds(1));
446 uint64_t stop = GetPerformanceCounter();
447 if (stop == start) return false;
448
449 // We called GetPerformanceCounter. Use it as entropy.
450 RAND_add((const unsigned char*)&start, sizeof(start), 1);
451 RAND_add((const unsigned char*)&stop, sizeof(stop), 1);
452
453 return true;
454}
455
456FastRandomContext::FastRandomContext(bool fDeterministic) : requires_seed(!fDeterministic), bytebuf_size(0), bitbuf_size(0)
457{

Callers 2

InitSanityCheckFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 2

GetPerformanceCounterFunction · 0.85
GetOSRandFunction · 0.85

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68