Skip to content

Commit

Permalink
Bugfix: randbytes should seed when needed (non reachable issue)
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Dec 12, 2018
1 parent 273d025 commit 8d98d42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ uint256 FastRandomContext::rand256()

std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
{
if (requires_seed) RandomSeed();
std::vector<unsigned char> ret(len);
if (len > 0) {
rng.Output(&ret[0], len);
Expand Down
17 changes: 12 additions & 5 deletions src/test/random_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
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));
{
FastRandomContext ctx3, ctx4;
BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); // extremely unlikely to be equal
}
{
FastRandomContext ctx3, ctx4;
BOOST_CHECK(ctx3.rand256() != ctx4.rand256());
}
{
FastRandomContext ctx3, ctx4;
BOOST_CHECK(ctx3.randbytes(7) != ctx4.randbytes(7));
}
}

BOOST_AUTO_TEST_CASE(fastrandom_randbits)
Expand Down

0 comments on commit 8d98d42

Please sign in to comment.