2014-06-26 14:41:53 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-06-26 14:41:53 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_RANDOM_H
|
|
|
|
#define BITCOIN_RANDOM_H
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <crypto/chacha20.h>
|
|
|
|
#include <crypto/common.h>
|
|
|
|
#include <uint256.h>
|
2014-06-26 14:41:53 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-05-30 15:46:16 +02:00
|
|
|
/* Seed OpenSSL PRNG with additional entropy data */
|
2014-06-26 14:41:53 +02:00
|
|
|
void RandAddSeed();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions to gather random data via the OpenSSL PRNG
|
|
|
|
*/
|
2014-11-07 13:42:52 +01:00
|
|
|
void GetRandBytes(unsigned char* buf, int num);
|
2014-06-26 14:41:53 +02:00
|
|
|
uint64_t GetRand(uint64_t nMax);
|
|
|
|
int GetRandInt(int nMax);
|
|
|
|
uint256 GetRandHash();
|
|
|
|
|
2019-01-09 07:30:19 +01:00
|
|
|
bool GetRandBool(double rate);
|
|
|
|
|
2017-05-23 17:53:00 +02:00
|
|
|
/**
|
|
|
|
* Add a little bit of randomness to the output of GetStrongRangBytes.
|
|
|
|
* This sleeps for a millisecond, so should only be called when there is
|
|
|
|
* no other work to be done.
|
|
|
|
*/
|
|
|
|
void RandAddSeedSleep();
|
|
|
|
|
2016-05-30 15:46:16 +02:00
|
|
|
/**
|
|
|
|
* Function to gather random data from multiple sources, failing whenever any
|
|
|
|
* of those source fail to provide a result.
|
|
|
|
*/
|
|
|
|
void GetStrongRandBytes(unsigned char* buf, int num);
|
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
/**
|
2016-10-18 15:38:44 +02:00
|
|
|
* Fast randomness source. This is seeded once with secure random data, but
|
|
|
|
* is completely deterministic and insecure after that.
|
|
|
|
* This class is not thread-safe.
|
2014-06-26 14:41:53 +02:00
|
|
|
*/
|
2016-10-18 15:38:44 +02:00
|
|
|
class FastRandomContext {
|
2017-04-24 14:02:12 +02:00
|
|
|
private:
|
|
|
|
bool requires_seed;
|
|
|
|
ChaCha20 rng;
|
|
|
|
|
|
|
|
unsigned char bytebuf[64];
|
|
|
|
int bytebuf_size;
|
|
|
|
|
|
|
|
uint64_t bitbuf;
|
|
|
|
int bitbuf_size;
|
|
|
|
|
|
|
|
void RandomSeed();
|
|
|
|
|
|
|
|
void FillByteBuffer()
|
|
|
|
{
|
|
|
|
if (requires_seed) {
|
|
|
|
RandomSeed();
|
|
|
|
}
|
2019-05-10 09:26:02 +02:00
|
|
|
rng.Keystream(bytebuf, sizeof(bytebuf));
|
2017-04-24 14:02:12 +02:00
|
|
|
bytebuf_size = sizeof(bytebuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FillBitBuffer()
|
|
|
|
{
|
|
|
|
bitbuf = rand64();
|
|
|
|
bitbuf_size = 64;
|
|
|
|
}
|
|
|
|
|
2016-10-18 15:38:44 +02:00
|
|
|
public:
|
2017-04-24 14:02:12 +02:00
|
|
|
explicit FastRandomContext(bool fDeterministic = false);
|
|
|
|
|
|
|
|
/** Initialize with explicit seed (only for testing) */
|
|
|
|
explicit FastRandomContext(const uint256& seed);
|
|
|
|
|
|
|
|
/** Generate a random 64-bit integer. */
|
|
|
|
uint64_t rand64()
|
|
|
|
{
|
|
|
|
if (bytebuf_size < 8) FillByteBuffer();
|
|
|
|
uint64_t ret = ReadLE64(bytebuf + 64 - bytebuf_size);
|
|
|
|
bytebuf_size -= 8;
|
|
|
|
return ret;
|
|
|
|
}
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2017-04-24 14:02:12 +02:00
|
|
|
/** Generate a random (bits)-bit integer. */
|
|
|
|
uint64_t randbits(int bits) {
|
|
|
|
if (bits == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (bits > 32) {
|
|
|
|
return rand64() >> (64 - bits);
|
|
|
|
} else {
|
|
|
|
if (bitbuf_size < bits) FillBitBuffer();
|
|
|
|
uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits));
|
|
|
|
bitbuf >>= bits;
|
|
|
|
bitbuf_size -= bits;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Generate a random integer in the range [0..range). */
|
|
|
|
uint64_t randrange(uint64_t range)
|
|
|
|
{
|
|
|
|
--range;
|
|
|
|
int bits = CountBits(range);
|
|
|
|
while (true) {
|
|
|
|
uint64_t ret = randbits(bits);
|
|
|
|
if (ret <= range) return ret;
|
|
|
|
}
|
2016-10-18 15:38:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:37:39 +01:00
|
|
|
uint32_t rand32(uint32_t nMax) {
|
|
|
|
return rand32() % nMax;
|
|
|
|
}
|
2016-11-20 07:52:45 +01:00
|
|
|
|
2018-01-12 16:37:39 +01:00
|
|
|
uint32_t operator()(uint32_t nMax) {
|
|
|
|
return rand32(nMax);
|
2016-11-20 07:52:45 +01:00
|
|
|
}
|
2018-01-12 16:37:39 +01:00
|
|
|
|
2017-06-07 23:59:41 +02:00
|
|
|
/** Generate random bytes. */
|
|
|
|
std::vector<unsigned char> randbytes(size_t len);
|
|
|
|
|
2017-04-24 14:02:12 +02:00
|
|
|
/** Generate a random 32-bit integer. */
|
|
|
|
uint32_t rand32() { return randbits(32); }
|
|
|
|
|
2017-06-07 23:59:41 +02:00
|
|
|
/** generate a random uint256. */
|
|
|
|
uint256 rand256();
|
|
|
|
|
2017-04-24 14:02:12 +02:00
|
|
|
/** Generate a random boolean. */
|
|
|
|
bool randbool() { return randbits(1); }
|
2016-11-20 07:52:45 +01:00
|
|
|
};
|
2016-10-09 13:46:46 +02:00
|
|
|
|
2017-03-01 12:40:06 +01:00
|
|
|
/* 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.
|
|
|
|
* (many cap out at 256 bytes).
|
|
|
|
*/
|
2017-12-13 13:32:00 +01:00
|
|
|
static const int NUM_OS_RANDOM_BYTES = 32;
|
2017-03-01 12:40:06 +01:00
|
|
|
|
|
|
|
/** Get 32 bytes of system entropy. Do not use this in application code: use
|
|
|
|
* GetStrongRandBytes instead.
|
|
|
|
*/
|
|
|
|
void GetOSRand(unsigned char *ent32);
|
|
|
|
|
|
|
|
/** Check that OS randomness is available and returning the requested number
|
|
|
|
* of bytes.
|
|
|
|
*/
|
|
|
|
bool Random_SanityCheck();
|
|
|
|
|
2017-06-14 15:22:08 +02:00
|
|
|
/** Initialize the RNG. */
|
|
|
|
void RandomInit();
|
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
#endif // BITCOIN_RANDOM_H
|