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
|
|
|
|
|
|
|
|
#include "uint256.h"
|
|
|
|
|
|
|
|
#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);
|
|
|
|
|
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 {
|
|
|
|
public:
|
|
|
|
explicit FastRandomContext(bool fDeterministic=false);
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2016-10-18 15:38:44 +02:00
|
|
|
uint32_t rand32() {
|
|
|
|
Rz = 36969 * (Rz & 65535) + (Rz >> 16);
|
|
|
|
Rw = 18000 * (Rw & 65535) + (Rw >> 16);
|
|
|
|
return (Rw << 16) + Rz;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
uint32_t Rz;
|
|
|
|
uint32_t Rw;
|
2016-11-20 07:52:45 +01:00
|
|
|
};
|
2016-10-09 13:46:46 +02:00
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
#endif // BITCOIN_RANDOM_H
|