Skip to content

Commit

Permalink
Introduce a Shuffle for FastRandomContext and use it in wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzbawls committed Apr 14, 2021
1 parent 1cdf124 commit 746d466
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ class FastRandomContext {
inline uint64_t operator()() { return rand64(); }
};

/** More efficient than using std::shuffle on a FastRandomContext.
*
* This is more efficient as std::shuffle will consume entropy in groups of
* 64 bits at the time and throw away most.
*
* This also works around a bug in libstdc++ std::shuffle that may cause
* type::operator=(type&&) to be invoked on itself, which the library's
* debug mode detects and panics on. This is a known issue, see
* https://stackoverflow.com/questions/22915325/avoiding-self-assignment-in-stdshuffle
*/
template<typename I, typename R>
void Shuffle(I first, I last, R&& rng)
{
while (first != last) {
size_t j = rng.randrange(last - first);
if (j) {
using std::swap;
swap(*first, *(first + j));
}
++first;
}
}

/* Number of random bytes returned by GetOSRand.
* When changing this constant make sure to change all call sites, and make
* sure that the underlying OS APIs for all platforms support the number.
Expand Down
34 changes: 34 additions & 0 deletions src/test/random_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,42 @@ BOOST_AUTO_TEST_CASE(stdrandom_test)
for (int j = 1; j <= 10; ++j) {
BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());
}
Shuffle(test.begin(), test.end(), ctx);
for (int j = 1; j <= 10; ++j) {
BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());
}
}

}

/** Test that Shuffle reaches every permutation with equal probability. */
BOOST_AUTO_TEST_CASE(shuffle_stat_test)
{
FastRandomContext ctx(true);
uint32_t counts[5 * 5 * 5 * 5 * 5] = {0};
for (int i = 0; i < 12000; ++i) {
int data[5] = {0, 1, 2, 3, 4};
Shuffle(std::begin(data), std::end(data), ctx);
int pos = data[0] + data[1] * 5 + data[2] * 25 + data[3] * 125 + data[4] * 625;
++counts[pos];
}
unsigned int sum = 0;
double chi_score = 0.0;
for (int i = 0; i < 5 * 5 * 5 * 5 * 5; ++i) {
int i1 = i % 5, i2 = (i / 5) % 5, i3 = (i / 25) % 5, i4 = (i / 125) % 5, i5 = i / 625;
uint32_t count = counts[i];
if (i1 == i2 || i1 == i3 || i1 == i4 || i1 == i5 || i2 == i3 || i2 == i4 || i2 == i5 || i3 == i4 || i3 == i5 || i4 == i5) {
BOOST_CHECK(count == 0);
} else {
chi_score += ((count - 100.0) * (count - 100.0)) / 100.0;
BOOST_CHECK(count > 50);
BOOST_CHECK(count < 150);
sum += count;
}
}
BOOST_CHECK(chi_score > 58.1411); // 99.9999% confidence interval
BOOST_CHECK(chi_score < 210.275);
BOOST_CHECK_EQUAL(sum, 12000);
}

BOOST_AUTO_TEST_SUITE_END()
2 changes: 1 addition & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2731,7 +2731,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int
std::vector<std::pair<CAmount, std::pair<const CWalletTx*, unsigned int> > > vValue;
CAmount nTotalLower = 0;

random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
Shuffle(vCoins.begin(), vCoins.end(), FastRandomContext());

for (const COutput& output : vCoins) {
if (!output.fSpendable)
Expand Down

0 comments on commit 746d466

Please sign in to comment.