diff --git a/src/random.h b/src/random.h --- a/src/random.h +++ b/src/random.h @@ -198,6 +198,9 @@ /** Generate a random 32-bit integer. */ uint32_t rand32() noexcept { return randbits(32); } + /** generate a random uint160. */ + uint160 rand160() noexcept; + /** generate a random uint256. */ uint256 rand256() noexcept; diff --git a/src/random.cpp b/src/random.cpp --- a/src/random.cpp +++ b/src/random.cpp @@ -665,6 +665,16 @@ requires_seed = false; } +uint160 FastRandomContext::rand160() noexcept { + if (bytebuf_size < 20) { + FillByteBuffer(); + } + uint160 ret; + memcpy(ret.begin(), bytebuf + 64 - bytebuf_size, 20); + bytebuf_size -= 20; + return ret; +} + uint256 FastRandomContext::rand256() noexcept { if (bytebuf_size < 32) { FillByteBuffer(); @@ -674,7 +684,6 @@ bytebuf_size -= 32; return ret; } - std::vector FastRandomContext::randbytes(size_t len) { if (requires_seed) { RandomSeed(); diff --git a/src/test/cashaddrenc_tests.cpp b/src/test/cashaddrenc_tests.cpp --- a/src/test/cashaddrenc_tests.cpp +++ b/src/test/cashaddrenc_tests.cpp @@ -23,14 +23,6 @@ CBaseChainParams::REGTEST}; } -uint160 insecure_GetRandUInt160(FastRandomContext &rand) { - uint160 n; - for (uint8_t *c = n.begin(); c != n.end(); ++c) { - *c = static_cast(rand.rand32()); - } - return n; -} - std::vector insecure_GetRandomByteArray(FastRandomContext &rand, size_t n) { std::vector out; @@ -146,13 +138,11 @@ } BOOST_AUTO_TEST_CASE(random_dst) { - FastRandomContext rand(true); - const size_t NUM_TESTS = 5000; const auto params = CreateChainParams(CBaseChainParams::MAIN); for (size_t i = 0; i < NUM_TESTS; ++i) { - uint160 hash = insecure_GetRandUInt160(rand); + uint160 hash = InsecureRand160(); const CTxDestination dst_key = PKHash(hash); const CTxDestination dst_scr = ScriptHash(hash); diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -73,6 +73,9 @@ static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); } +static inline uint160 InsecureRand160() { + return g_insecure_rand_ctx.rand160(); +} static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }