Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2017-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <random.h>7#include <test/util/random.h>8#include <test/util/setup_common.h>9#include <util/time.h>11#include <boost/test/unit_test.hpp>13#include <algorithm>14#include <random>16BOOST_FIXTURE_TEST_SUITE(random_tests, BasicTestingSetup)18BOOST_AUTO_TEST_CASE(osrandom_tests)19{20BOOST_CHECK(Random_SanityCheck());21}23BOOST_AUTO_TEST_CASE(fastrandom_tests_deterministic)24{25// Check that deterministic FastRandomContexts are deterministic26SeedRandomForTest(SeedRand::ZEROS);27FastRandomContext ctx1{true};28FastRandomContext ctx2{true};30{31BOOST_CHECK_EQUAL(FastRandomContext().rand<uint64_t>(), uint64_t{9330418229102544152u});32BOOST_CHECK_EQUAL(FastRandomContext().rand<int>(), int{618925161});33BOOST_CHECK_EQUAL(FastRandomContext().randrange<std::chrono::microseconds>(1h).count(), 1271170921);34BOOST_CHECK_EQUAL(FastRandomContext().randrange<std::chrono::milliseconds>(1h).count(), 2803534);36BOOST_CHECK_EQUAL(FastRandomContext().rand<uint64_t>(), uint64_t{10170981140880778086u});37BOOST_CHECK_EQUAL(FastRandomContext().rand<int>(), int{1689082725});38BOOST_CHECK_EQUAL(FastRandomContext().randrange<std::chrono::microseconds>(1h).count(), 2464643716);39BOOST_CHECK_EQUAL(FastRandomContext().randrange<std::chrono::milliseconds>(1h).count(), 2312205);41BOOST_CHECK_EQUAL(FastRandomContext().rand<uint64_t>(), uint64_t{5689404004456455543u});42BOOST_CHECK_EQUAL(FastRandomContext().rand<int>(), int{785839937});43BOOST_CHECK_EQUAL(FastRandomContext().randrange<std::chrono::microseconds>(1h).count(), 93558804);44BOOST_CHECK_EQUAL(FastRandomContext().randrange<std::chrono::milliseconds>(1h).count(), 507022);45}47{48constexpr SteadySeconds time_point{1s};49FastRandomContext ctx{true};50BOOST_CHECK_EQUAL(7, ctx.rand_uniform_delay(time_point, 9s).time_since_epoch().count());51BOOST_CHECK_EQUAL(-6, ctx.rand_uniform_delay(time_point, -9s).time_since_epoch().count());52BOOST_CHECK_EQUAL(1, ctx.rand_uniform_delay(time_point, 0s).time_since_epoch().count());53BOOST_CHECK_EQUAL(4652286523065884857, ctx.rand_uniform_delay(time_point, 9223372036854775807s).time_since_epoch().count());54BOOST_CHECK_EQUAL(-8813961240025683129, ctx.rand_uniform_delay(time_point, -9223372036854775807s).time_since_epoch().count());55BOOST_CHECK_EQUAL(26443, ctx.rand_uniform_delay(time_point, 9h).time_since_epoch().count());56}57BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());58BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());59BOOST_CHECK_EQUAL(ctx1.rand64(), ctx2.rand64());60BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));61BOOST_CHECK(std::ranges::equal(ctx1.randbytes<std::byte>(17), ctx2.randbytes<17>())); // check vector/array behavior symmetry62BOOST_CHECK(ctx1.rand256() == ctx2.rand256());63BOOST_CHECK_EQUAL(ctx1.randbits(7), ctx2.randbits(7));64BOOST_CHECK(ctx1.randbytes(128) == ctx2.randbytes(128));65BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());66BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));67BOOST_CHECK(ctx1.rand256() == ctx2.rand256());68BOOST_CHECK(ctx1.randbytes(50) == ctx2.randbytes(50));69{70struct MicroClock {71using duration = std::chrono::microseconds;72};73FastRandomContext ctx{true};74// Check with clock type75BOOST_CHECK_EQUAL(47222, ctx.rand_uniform_duration<MicroClock>(1s).count());76// Check with time-point type77BOOST_CHECK_EQUAL(2782, ctx.rand_uniform_duration<SteadySeconds>(9h).count());78}79}81BOOST_AUTO_TEST_CASE(fastrandom_tests_nondeterministic)82{83// Check that a nondeterministic ones are not84{85BOOST_CHECK(FastRandomContext().rand<uint64_t>() != uint64_t{9330418229102544152u});86BOOST_CHECK(FastRandomContext().rand<int>() != int{618925161});87BOOST_CHECK(FastRandomContext().randrange<std::chrono::microseconds>(1h).count() != 1271170921);88BOOST_CHECK(FastRandomContext().randrange<std::chrono::milliseconds>(1h).count() != 2803534);90BOOST_CHECK(FastRandomContext().rand<uint64_t>() != uint64_t{10170981140880778086u});91BOOST_CHECK(FastRandomContext().rand<int>() != int{1689082725});92BOOST_CHECK(FastRandomContext().randrange<std::chrono::microseconds>(1h).count() != 2464643716);93BOOST_CHECK(FastRandomContext().randrange<std::chrono::milliseconds>(1h).count() != 2312205);95BOOST_CHECK(FastRandomContext().rand<uint64_t>() != uint64_t{5689404004456455543u});96BOOST_CHECK(FastRandomContext().rand<int>() != int{785839937});97BOOST_CHECK(FastRandomContext().randrange<std::chrono::microseconds>(1h).count() != 93558804);98BOOST_CHECK(FastRandomContext().randrange<std::chrono::milliseconds>(1h).count() != 507022);99}101{102FastRandomContext ctx3, ctx4;103BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); // extremely unlikely to be equal104}105{106FastRandomContext ctx3, ctx4;107BOOST_CHECK(ctx3.rand256() != ctx4.rand256());108}109{110FastRandomContext ctx3, ctx4;111BOOST_CHECK(ctx3.randbytes(7) != ctx4.randbytes(7));112}113}115BOOST_AUTO_TEST_CASE(fastrandom_randbits)116{117FastRandomContext ctx1;118FastRandomContext ctx2;119for (int bits = 0; bits < 63; ++bits) {120for (int j = 0; j < 1000; ++j) {121uint64_t rangebits = ctx1.randbits(bits);122BOOST_CHECK_EQUAL(rangebits >> bits, 0U);123uint64_t range = (uint64_t{1}) << bits | rangebits;124uint64_t rand = ctx2.randrange(range);125BOOST_CHECK(rand < range);126}127}128}130/** Verify that RandomMixin::randbits returns 0 and 1 for every requested bit. */131BOOST_AUTO_TEST_CASE(randbits_test)132{133FastRandomContext ctx_lens; //!< RNG for producing the lengths requested from ctx_test.134FastRandomContext ctx_test1(true), ctx_test2(true); //!< The RNGs being tested.135int ctx_test_bitsleft{0}; //!< (Assumed value of) ctx_test::bitbuf_len137// Run the entire test 5 times.138for (int i = 0; i < 5; ++i) {139// count (first) how often it has occurred, and (second) how often it was true:140// - for every bit position, in every requested bits count (0 + 1 + 2 + ... + 64 = 2080)141// - for every value of ctx_test_bitsleft (0..63 = 64)142std::vector<std::pair<uint64_t, uint64_t>> seen(2080 * 64);143while (true) {144// Loop 1000 times, just to not continuously check std::all_of.145for (int j = 0; j < 1000; ++j) {146// Decide on a number of bits to request (0 through 64, inclusive; don't use randbits/randrange).147int bits = ctx_lens.rand64() % 65;148// Generate that many bits.149uint64_t gen = ctx_test1.randbits(bits);150// For certain bits counts, also test randbits<Bits> and compare.151uint64_t gen2;152if (bits == 0) {153gen2 = ctx_test2.randbits<0>();154} else if (bits == 1) {155gen2 = ctx_test2.randbits<1>();156} else if (bits == 7) {157gen2 = ctx_test2.randbits<7>();158} else if (bits == 32) {159gen2 = ctx_test2.randbits<32>();160} else if (bits == 51) {161gen2 = ctx_test2.randbits<51>();162} else if (bits == 64) {163gen2 = ctx_test2.randbits<64>();164} else {165gen2 = ctx_test2.randbits(bits);166}167BOOST_CHECK_EQUAL(gen, gen2);168// Make sure the result is in range.169if (bits < 64) BOOST_CHECK_EQUAL(gen >> bits, 0);170// Mark all the seen bits in the output.171for (int bit = 0; bit < bits; ++bit) {172int idx = bit + (bits * (bits - 1)) / 2 + 2080 * ctx_test_bitsleft;173seen[idx].first += 1;174seen[idx].second += (gen >> bit) & 1;175}176// Update ctx_test_bitself.177if (bits > ctx_test_bitsleft) {178ctx_test_bitsleft = ctx_test_bitsleft + 64 - bits;179} else {180ctx_test_bitsleft -= bits;181}182}183// Loop until every bit position/combination is seen 242 times.184if (std::all_of(seen.begin(), seen.end(), [](const auto& x) { return x.first >= 242; })) break;185}186// Check that each bit appears within 7.78 standard deviations of 50%187// (each will fail with P < 1/(2080 * 64 * 10^9)).188for (const auto& val : seen) {189assert(fabs(val.first * 0.5 - val.second) < sqrt(val.first * 0.25) * 7.78);190}191}192}194/** Does-it-compile test for compatibility with standard library RNG interface. */195BOOST_AUTO_TEST_CASE(stdrandom_test)196{197FastRandomContext ctx;198std::uniform_int_distribution<int> distribution(3, 9);199for (int i = 0; i < 100; ++i) {200int x = distribution(ctx);201BOOST_CHECK(x >= 3);202BOOST_CHECK(x <= 9);204std::vector<int> test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};205std::shuffle(test.begin(), test.end(), ctx);206for (int j = 1; j <= 10; ++j) {207BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());208}209}210}212/** Test that Shuffle reaches every permutation with equal probability. */213BOOST_AUTO_TEST_CASE(shuffle_stat_test)214{215FastRandomContext ctx(true);216uint32_t counts[5 * 5 * 5 * 5 * 5] = {0};217for (int i = 0; i < 12000; ++i) {218int data[5] = {0, 1, 2, 3, 4};219std::shuffle(std::begin(data), std::end(data), ctx);220int pos = data[0] + data[1] * 5 + data[2] * 25 + data[3] * 125 + data[4] * 625;221++counts[pos];222}223unsigned int sum = 0;224double chi_score = 0.0;225for (int i = 0; i < 5 * 5 * 5 * 5 * 5; ++i) {226int i1 = i % 5, i2 = (i / 5) % 5, i3 = (i / 25) % 5, i4 = (i / 125) % 5, i5 = i / 625;227uint32_t count = counts[i];228if (i1 == i2 || i1 == i3 || i1 == i4 || i1 == i5 || i2 == i3 || i2 == i4 || i2 == i5 || i3 == i4 || i3 == i5 || i4 == i5) {229BOOST_CHECK(count == 0);230} else {231chi_score += ((count - 100.0) * (count - 100.0)) / 100.0;232BOOST_CHECK(count > 50);233BOOST_CHECK(count < 150);234sum += count;235}236}237BOOST_CHECK(chi_score > 58.1411); // 99.9999% confidence interval238BOOST_CHECK(chi_score < 210.275);239BOOST_CHECK_EQUAL(sum, 12000U);240}242BOOST_AUTO_TEST_CASE(xoroshiro128plusplus_reference_values)243{244// numbers generated from reference implementation245InsecureRandomContext rng(0);246BOOST_TEST(0x6f68e1e7e2646ee1 == rng());247BOOST_TEST(0xbf971b7f454094ad == rng());248BOOST_TEST(0x48f2de556f30de38 == rng());249BOOST_TEST(0x6ea7c59f89bbfc75 == rng());
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.