mirror of
https://github.com/dogecoin/dogecoin.git
synced 2026-01-31 02:20:53 +00:00
Add FastRandomContext::rand256() and ::randbytes()
FastRandomContext now provides all functionality that the real Rand* functions provide. Cherry-picked from: 37e864eb9fee4b592bd61c5ec3555b00a2de2cf7
This commit is contained in:
parent
3f511675be
commit
a8cad7ed4c
@ -247,6 +247,26 @@ void FastRandomContext::RandomSeed()
|
||||
requires_seed = false;
|
||||
}
|
||||
|
||||
uint256 FastRandomContext::rand256()
|
||||
{
|
||||
if (bytebuf_size < 32) {
|
||||
FillByteBuffer();
|
||||
}
|
||||
uint256 ret;
|
||||
memcpy(ret.begin(), bytebuf + 64 - bytebuf_size, 32);
|
||||
bytebuf_size -= 32;
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
|
||||
{
|
||||
std::vector<unsigned char> ret(len);
|
||||
if (len > 0) {
|
||||
rng.Output(&ret[0], len);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
FastRandomContext::FastRandomContext(const uint256& seed) : requires_seed(false), bytebuf_size(0), bitbuf_size(0)
|
||||
{
|
||||
rng.SetKey(seed.begin(), 32);
|
||||
|
||||
@ -103,9 +103,15 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/** Generate random bytes. */
|
||||
std::vector<unsigned char> randbytes(size_t len);
|
||||
|
||||
/** Generate a random 32-bit integer. */
|
||||
uint32_t rand32() { return randbits(32); }
|
||||
|
||||
/** generate a random uint256. */
|
||||
uint256 rand256();
|
||||
|
||||
/** Generate a random boolean. */
|
||||
bool randbool() { return randbits(1); }
|
||||
};
|
||||
|
||||
@ -24,18 +24,18 @@
|
||||
* using BOOST_CHECK_CLOSE to fail.
|
||||
*
|
||||
*/
|
||||
FastRandomContext insecure_rand(true);
|
||||
FastRandomContext local_rand_ctx(true);
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
|
||||
|
||||
|
||||
/** insecure_GetRandHash fills in a uint256 from insecure_rand
|
||||
/** insecure_GetRandHash fills in a uint256 from local_rand_ctx
|
||||
*/
|
||||
void insecure_GetRandHash(uint256& t)
|
||||
{
|
||||
uint32_t* ptr = (uint32_t*)t.begin();
|
||||
for (uint8_t j = 0; j < 8; ++j)
|
||||
*(ptr++) = insecure_rand.rand32();
|
||||
*(ptr++) = local_rand_ctx.rand32();
|
||||
}
|
||||
|
||||
/** Definition copied from /src/script/sigcache.cpp
|
||||
@ -60,7 +60,7 @@ public:
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
||||
{
|
||||
insecure_rand = FastRandomContext(true);
|
||||
local_rand_ctx = FastRandomContext(true);
|
||||
CuckooCache::cache<uint256, uint256Hasher> cc{};
|
||||
cc.setup_bytes(32 << 20);
|
||||
uint256 v;
|
||||
@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
||||
template <typename Cache>
|
||||
double test_cache(size_t megabytes, double load)
|
||||
{
|
||||
insecure_rand = FastRandomContext(true);
|
||||
local_rand_ctx = FastRandomContext(true);
|
||||
std::vector<uint256> hashes;
|
||||
Cache set{};
|
||||
size_t bytes = megabytes * (1 << 20);
|
||||
@ -90,7 +90,7 @@ double test_cache(size_t megabytes, double load)
|
||||
for (uint32_t i = 0; i < n_insert; ++i) {
|
||||
uint32_t* ptr = (uint32_t*)hashes[i].begin();
|
||||
for (uint8_t j = 0; j < 8; ++j)
|
||||
*(ptr++) = insecure_rand.rand32();
|
||||
*(ptr++) = local_rand_ctx.rand32();
|
||||
}
|
||||
/** We make a copy of the hashes because future optimizations of the
|
||||
* cuckoocache may overwrite the inserted element, so the test is
|
||||
@ -151,7 +151,7 @@ template <typename Cache>
|
||||
void test_cache_erase(size_t megabytes)
|
||||
{
|
||||
double load = 1;
|
||||
insecure_rand = FastRandomContext(true);
|
||||
local_rand_ctx = FastRandomContext(true);
|
||||
std::vector<uint256> hashes;
|
||||
Cache set{};
|
||||
size_t bytes = megabytes * (1 << 20);
|
||||
@ -161,7 +161,7 @@ void test_cache_erase(size_t megabytes)
|
||||
for (uint32_t i = 0; i < n_insert; ++i) {
|
||||
uint32_t* ptr = (uint32_t*)hashes[i].begin();
|
||||
for (uint8_t j = 0; j < 8; ++j)
|
||||
*(ptr++) = insecure_rand.rand32();
|
||||
*(ptr++) = local_rand_ctx.rand32();
|
||||
}
|
||||
/** We make a copy of the hashes because future optimizations of the
|
||||
* cuckoocache may overwrite the inserted element, so the test is
|
||||
@ -214,7 +214,7 @@ template <typename Cache>
|
||||
void test_cache_erase_parallel(size_t megabytes)
|
||||
{
|
||||
double load = 1;
|
||||
insecure_rand = FastRandomContext(true);
|
||||
local_rand_ctx = FastRandomContext(true);
|
||||
std::vector<uint256> hashes;
|
||||
Cache set{};
|
||||
size_t bytes = megabytes * (1 << 20);
|
||||
@ -224,7 +224,7 @@ void test_cache_erase_parallel(size_t megabytes)
|
||||
for (uint32_t i = 0; i < n_insert; ++i) {
|
||||
uint32_t* ptr = (uint32_t*)hashes[i].begin();
|
||||
for (uint8_t j = 0; j < 8; ++j)
|
||||
*(ptr++) = insecure_rand.rand32();
|
||||
*(ptr++) = local_rand_ctx.rand32();
|
||||
}
|
||||
/** We make a copy of the hashes because future optimizations of the
|
||||
* cuckoocache may overwrite the inserted element, so the test is
|
||||
@ -316,7 +316,7 @@ void test_cache_generations()
|
||||
// iterations with non-deterministic values, so it isn't "overfit" to the
|
||||
// specific entropy in FastRandomContext(true) and implementation of the
|
||||
// cache.
|
||||
insecure_rand = FastRandomContext(true);
|
||||
local_rand_ctx = FastRandomContext(true);
|
||||
|
||||
// block_activity models a chunk of network activity. n_insert elements are
|
||||
// adde to the cache. The first and last n/4 are stored for removal later
|
||||
@ -333,7 +333,7 @@ void test_cache_generations()
|
||||
for (uint32_t i = 0; i < n_insert; ++i) {
|
||||
uint32_t* ptr = (uint32_t*)inserts[i].begin();
|
||||
for (uint8_t j = 0; j < 8; ++j)
|
||||
*(ptr++) = insecure_rand.rand32();
|
||||
*(ptr++) = local_rand_ctx.rand32();
|
||||
}
|
||||
for (uint32_t i = 0; i < n_insert / 4; ++i)
|
||||
reads.push_back(inserts[i]);
|
||||
|
||||
@ -25,14 +25,21 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
|
||||
BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
|
||||
BOOST_CHECK_EQUAL(ctx1.rand64(), ctx2.rand64());
|
||||
BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));
|
||||
BOOST_CHECK(ctx1.randbytes(17) == ctx2.randbytes(17));
|
||||
BOOST_CHECK(ctx1.rand256() == ctx2.rand256());
|
||||
BOOST_CHECK_EQUAL(ctx1.randbits(7), ctx2.randbits(7));
|
||||
BOOST_CHECK(ctx1.randbytes(128) == ctx2.randbytes(128));
|
||||
BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
|
||||
BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));
|
||||
BOOST_CHECK(ctx1.rand256() == ctx2.rand256());
|
||||
BOOST_CHECK(ctx1.randbytes(50) == ctx2.randbytes(50));
|
||||
|
||||
// Check that a nondeterministic ones are not
|
||||
FastRandomContext ctx3;
|
||||
FastRandomContext ctx4;
|
||||
BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); // extremely unlikely to be equal
|
||||
BOOST_CHECK(ctx3.rand256() != ctx4.rand256());
|
||||
BOOST_CHECK(ctx3.randbytes(7) != ctx4.randbytes(7));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(fastrandom_randbits)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user