1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_RANDOM_H
7 #define BITCOIN_RANDOM_H
9 #include "crypto/chacha20.h"
10 #include "crypto/common.h"
15 /* Seed OpenSSL PRNG with additional entropy data */
19 * Functions to gather random data via the OpenSSL PRNG
21 void GetRandBytes(unsigned char* buf
, int num
);
22 uint64_t GetRand(uint64_t nMax
);
23 int GetRandInt(int nMax
);
24 uint256
GetRandHash();
27 * Add a little bit of randomness to the output of GetStrongRangBytes.
28 * This sleeps for a millisecond, so should only be called when there is
29 * no other work to be done.
31 void RandAddSeedSleep();
34 * Function to gather random data from multiple sources, failing whenever any
35 * of those source fail to provide a result.
37 void GetStrongRandBytes(unsigned char* buf
, int num
);
40 * Fast randomness source. This is seeded once with secure random data, but
41 * is completely deterministic and insecure after that.
42 * This class is not thread-safe.
44 class FastRandomContext
{
49 unsigned char bytebuf
[64];
62 rng
.Output(bytebuf
, sizeof(bytebuf
));
63 bytebuf_size
= sizeof(bytebuf
);
73 explicit FastRandomContext(bool fDeterministic
= false);
75 /** Initialize with explicit seed (only for testing) */
76 explicit FastRandomContext(const uint256
& seed
);
78 /** Generate a random 64-bit integer. */
81 if (bytebuf_size
< 8) FillByteBuffer();
82 uint64_t ret
= ReadLE64(bytebuf
+ 64 - bytebuf_size
);
87 /** Generate a random (bits)-bit integer. */
88 uint64_t randbits(int bits
) {
91 } else if (bits
> 32) {
92 return rand64() >> (64 - bits
);
94 if (bitbuf_size
< bits
) FillBitBuffer();
95 uint64_t ret
= bitbuf
& (~(uint64_t)0 >> (64 - bits
));
102 /** Generate a random integer in the range [0..range). */
103 uint64_t randrange(uint64_t range
)
106 int bits
= CountBits(range
);
108 uint64_t ret
= randbits(bits
);
109 if (ret
<= range
) return ret
;
113 /** Generate random bytes. */
114 std::vector
<unsigned char> randbytes(size_t len
);
116 /** Generate a random 32-bit integer. */
117 uint32_t rand32() { return randbits(32); }
119 /** generate a random uint256. */
122 /** Generate a random boolean. */
123 bool randbool() { return randbits(1); }
126 /* Number of random bytes returned by GetOSRand.
127 * When changing this constant make sure to change all call sites, and make
128 * sure that the underlying OS APIs for all platforms support the number.
129 * (many cap out at 256 bytes).
131 static const ssize_t NUM_OS_RANDOM_BYTES
= 32;
133 /** Get 32 bytes of system entropy. Do not use this in application code: use
134 * GetStrongRandBytes instead.
136 void GetOSRand(unsigned char *ent32
);
138 /** Check that OS randomness is available and returning the requested number
141 bool Random_SanityCheck();
143 /** Initialize the RNG. */
146 #endif // BITCOIN_RANDOM_H