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-04-16 12:25:12 +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();
|
|
|
|
|
2016-04-16 12:25:12 +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
|
|
|
/**
|
|
|
|
* Seed insecure_rand using the random pool.
|
2014-11-25 08:18:33 +01:00
|
|
|
* @param Deterministic Use a deterministic seed
|
2014-06-26 14:41:53 +02:00
|
|
|
*/
|
|
|
|
void seed_insecure_rand(bool fDeterministic = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MWC RNG of George Marsaglia
|
|
|
|
* This is intended to be fast. It has a period of 2^59.3, though the
|
|
|
|
* least significant 16 bits only have a period of about 2^30.1.
|
|
|
|
*
|
|
|
|
* @return random value
|
|
|
|
*/
|
|
|
|
extern uint32_t insecure_rand_Rz;
|
|
|
|
extern uint32_t insecure_rand_Rw;
|
|
|
|
static inline uint32_t insecure_rand(void)
|
|
|
|
{
|
|
|
|
insecure_rand_Rz = 36969 * (insecure_rand_Rz & 65535) + (insecure_rand_Rz >> 16);
|
|
|
|
insecure_rand_Rw = 18000 * (insecure_rand_Rw & 65535) + (insecure_rand_Rw >> 16);
|
|
|
|
return (insecure_rand_Rw << 16) + insecure_rand_Rz;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // BITCOIN_RANDOM_H
|