Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13711165
D8496.id25957.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
D8496.id25957.diff
View Options
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<uint8_t> 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<uint8_t>(rand.rand32());
- }
- return n;
-}
-
std::vector<uint8_t> insecure_GetRandomByteArray(FastRandomContext &rand,
size_t n) {
std::vector<uint8_t> 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();
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Apr 26, 10:38 (5 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5573310
Default Alt Text
D8496.id25957.diff (2 KB)
Attached To
D8496: Add primitive to generate random 160 bits numbers for tests
Event Timeline
Log In to Comment