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
13 /* Seed OpenSSL PRNG with additional entropy data */
17 * Functions to gather random data via the OpenSSL PRNG
19 void GetRandBytes(unsigned char* buf
, int num
);
20 uint64_t GetRand(uint64_t nMax
);
21 int GetRandInt(int nMax
);
22 uint256
GetRandHash();
25 * Function to gather random data from multiple sources, failing whenever any
26 * of those source fail to provide a result.
28 void GetStrongRandBytes(unsigned char* buf
, int num
);
31 * Fast randomness source. This is seeded once with secure random data, but
32 * is completely deterministic and insecure after that.
33 * This class is not thread-safe.
35 class FastRandomContext
{
37 explicit FastRandomContext(bool fDeterministic
=false);
40 Rz
= 36969 * (Rz
& 65535) + (Rz
>> 16);
41 Rw
= 18000 * (Rw
& 65535) + (Rw
>> 16);
42 return (Rw
<< 16) + Rz
;
49 /* Number of random bytes returned by GetOSRand.
50 * When changing this constant make sure to change all call sites, and make
51 * sure that the underlying OS APIs for all platforms support the number.
52 * (many cap out at 256 bytes).
54 static const ssize_t NUM_OS_RANDOM_BYTES
= 32;
56 /** Get 32 bytes of system entropy. Do not use this in application code: use
57 * GetStrongRandBytes instead.
59 void GetOSRand(unsigned char *ent32
);
61 /** Check that OS randomness is available and returning the requested number
64 bool Random_SanityCheck();
66 #endif // BITCOIN_RANDOM_H