diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -158,7 +158,7 @@ if (InsecureRandRange(5) == 0 || coin.IsSpent()) { CTxOut txout; - txout.nValue = int64_t(insecure_rand()) * SATOSHI; + txout.nValue = int64_t(InsecureRand32()) * SATOSHI; if (InsecureRandRange(16) == 0 && coin.IsSpent()) { txout.scriptPubKey.assign(1 + InsecureRandBits(6), OP_RETURN); @@ -166,14 +166,14 @@ added_an_unspendable_entry = true; } else { // Random sizes so we can test memory usage accounting - txout.scriptPubKey.assign(InsecureRandBits(6), 0); + txout.scriptPubKey.assign(InsecureRand32() & 0x3F, 0); (coin.IsSpent() ? added_an_entry : updated_an_entry) = true; coin = Coin(txout, 1, false); } Coin newcoin(txout, 1, false); stack.back()->AddCoin(COutPoint(txid, 0), newcoin, - !coin.IsSpent() || insecure_rand() & 1); + !coin.IsSpent() || InsecureRand32() & 1); } else { removed_an_entry = true; coin.Clear(); @@ -183,8 +183,8 @@ // One every 10 iterations, remove a random entry from the cache if (InsecureRandRange(10) == 0) { - COutPoint out(txids[insecure_rand() % txids.size()], 0); - int cacheid = insecure_rand() % stack.size(); + COutPoint out(txids[InsecureRand32() % txids.size()], 0); + int cacheid = InsecureRand32() % stack.size(); stack[cacheid]->Uncache(out); uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out); } @@ -298,7 +298,7 @@ std::set utxoset; for (int64_t i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { - uint32_t randiter = insecure_rand(); + uint32_t randiter = InsecureRand32(); // 19/20 txs add a new transaction if (randiter % 20 < 19) { @@ -308,8 +308,8 @@ // Keep txs unique unless intended to duplicate. tx.vout[0].nValue = i * SATOSHI; // Random sizes so we can test memory usage accounting - tx.vout[0].scriptPubKey.assign(insecure_rand() & 0x3F, 0); - unsigned int height = insecure_rand(); + tx.vout[0].scriptPubKey.assign(InsecureRandBits(6), 0); + unsigned int height = InsecureRand32(); Coin old_coin; // 2/20 times create a new coinbase @@ -448,15 +448,15 @@ // One every 10 iterations, remove a random entry from the cache if (utxoset.size() > 1 && InsecureRandRange(30) == 0) { - stack[insecure_rand() % stack.size()]->Uncache( + stack[InsecureRand32() % stack.size()]->Uncache( FindRandomFrom(utxoset)->first); } if (disconnected_coins.size() > 1 && InsecureRandRange(30) == 0) { - stack[insecure_rand() % stack.size()]->Uncache( + stack[InsecureRand32() % stack.size()]->Uncache( FindRandomFrom(disconnected_coins)->first); } if (duplicate_coins.size() > 1 && InsecureRandRange(30) == 0) { - stack[insecure_rand() % stack.size()]->Uncache( + stack[InsecureRand32() % stack.size()]->Uncache( FindRandomFrom(duplicate_coins)->first); } diff --git a/src/test/prevector_tests.cpp b/src/test/prevector_tests.cpp --- a/src/test/prevector_tests.cpp +++ b/src/test/prevector_tests.cpp @@ -193,7 +193,7 @@ for (int i = 0; i < 2048; i++) { if (InsecureRandBits(2) == 0) { test.insert(InsecureRandRange(test.size() + 1), - insecure_rand()); + InsecureRand32()); } if (test.size() > 0 && InsecureRandBits(2) == 1) { test.erase(InsecureRandRange(test.size())); @@ -206,7 +206,7 @@ } if (InsecureRandBits(3) == 3) { test.insert(InsecureRandRange(test.size() + 1), - 1 + InsecureRandBool(), insecure_rand()); + 1 + InsecureRandBool(), InsecureRand32()); } if (InsecureRandBits(3) == 4) { int del = std::min(test.size(), 1 + (InsecureRandBool())); @@ -214,7 +214,7 @@ test.erase(beg, beg + del); } if (InsecureRandBits(4) == 5) { - test.push_back(insecure_rand()); + test.push_back(InsecureRand32()); } if (test.size() > 0 && InsecureRandBits(4) == 6) { test.pop_back(); @@ -223,7 +223,7 @@ int values[4]; int num = 1 + (InsecureRandBits(2)); for (int k = 0; k < num; k++) { - values[k] = insecure_rand(); + values[k] = InsecureRand32(); } test.insert_range(InsecureRandRange(test.size() + 1), values, values + num); @@ -240,13 +240,13 @@ test.shrink_to_fit(); } if (test.size() > 0) { - test.update(InsecureRandRange(test.size()), insecure_rand()); + test.update(InsecureRandRange(test.size()), InsecureRand32()); } if (InsecureRandBits(10) == 11) { test.clear(); } if (InsecureRandBits(9) == 12) { - test.assign(InsecureRandBits(5), insecure_rand()); + test.assign(InsecureRandBits(5), InsecureRand32()); } if (InsecureRandBits(3) == 3) { test.swap(); diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp --- a/src/test/sighash_tests.cpp +++ b/src/test/sighash_tests.cpp @@ -98,10 +98,10 @@ } static void RandomTransaction(CMutableTransaction &tx, bool fSingle) { - tx.nVersion = insecure_rand(); + tx.nVersion = InsecureRand32(); tx.vin.clear(); tx.vout.clear(); - tx.nLockTime = (InsecureRandBool()) ? insecure_rand() : 0; + tx.nLockTime = (InsecureRandBool()) ? InsecureRand32() : 0; int ins = (InsecureRandBits(2)) + 1; int outs = fSingle ? ins : (InsecureRandBits(2)) + 1; for (int in = 0; in < ins; in++) { @@ -109,13 +109,14 @@ CTxIn &txin = tx.vin.back(); txin.prevout = COutPoint(InsecureRand256(), InsecureRandBits(2)); RandomScript(txin.scriptSig); - txin.nSequence = - (InsecureRandBool()) ? insecure_rand() : (unsigned int)-1; + txin.nSequence = InsecureRandBool() + ? InsecureRand32() + : std::numeric_limits::max(); } for (int out = 0; out < outs; out++) { tx.vout.push_back(CTxOut()); CTxOut &txout = tx.vout.back(); - txout.nValue = (int64_t(insecure_rand()) % 100000000) * SATOSHI; + txout.nValue = int64_t(InsecureRandRange(100000000)) * SATOSHI; RandomScript(txout.scriptPubKey); } } @@ -134,7 +135,7 @@ int nRandomTests = 1000; for (int i = 0; i < nRandomTests; i++) { - uint32_t nHashType = insecure_rand(); + uint32_t nHashType = InsecureRand32(); SigHashType sigHashType(nHashType); CMutableTransaction txTo; diff --git a/src/test/test_bitcoin.h b/src/test/test_bitcoin.h --- a/src/test/test_bitcoin.h +++ b/src/test/test_bitcoin.h @@ -28,7 +28,7 @@ insecure_rand_ctx = FastRandomContext(deterministic); } -static inline uint32_t insecure_rand() { +static inline uint32_t InsecureRand32() { return insecure_rand_ctx.rand32(); } static inline uint256 InsecureRand256() { diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -817,16 +817,17 @@ int err = 30 * 10000. / mod * sqrt((1. / mod * (1 - 1. / mod)) / 10000.); // mask is 2^ceil(log2(mod))-1 - while (mask < mod - 1) + while (mask < mod - 1) { mask = (mask << 1) + 1; + } int count = 0; // How often does it get a zero from the uniform range [0,mod)? for (int i = 0; i < 10000; i++) { uint32_t rval; do { - rval = insecure_rand() & mask; - } while (rval >= (uint32_t)mod); + rval = InsecureRand32() & mask; + } while (rval >= uint32_t(mod)); count += rval == 0; } BOOST_CHECK(count <= 10000 / mod + err); diff --git a/src/wallet/test/wallet_crypto_tests.cpp b/src/wallet/test/wallet_crypto_tests.cpp --- a/src/wallet/test/wallet_crypto_tests.cpp +++ b/src/wallet/test/wallet_crypto_tests.cpp @@ -238,7 +238,7 @@ std::string hash(GetRandHash().ToString()); std::vector vchSalt(8); GetRandBytes(&vchSalt[0], vchSalt.size()); - uint32_t rounds = insecure_rand(); + uint32_t rounds = InsecureRand32(); if (rounds > 30000) rounds = 30000; TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);