BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
random.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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.
5 
6 #ifndef BITCOIN_RANDOM_H
7 #define BITCOIN_RANDOM_H
8 
9 #include <crypto/chacha20.h>
10 #include <crypto/common.h>
11 #include <uint256.h>
12 
13 #include <stdint.h>
14 #include <limits>
15 
16 /* Seed OpenSSL PRNG with additional entropy data */
17 void RandAddSeed();
18 
22 void GetRandBytes(unsigned char* buf, int num);
23 uint64_t GetRand(uint64_t nMax);
24 int GetRandInt(int nMax);
26 
32 void RandAddSeedSleep();
33 
38 void GetStrongRandBytes(unsigned char* buf, int num);
39 
46 private:
49 
50  unsigned char bytebuf[64];
52 
53  uint64_t bitbuf;
55 
56  void RandomSeed();
57 
59  {
60  if (requires_seed) {
61  RandomSeed();
62  }
63  rng.Output(bytebuf, sizeof(bytebuf));
64  bytebuf_size = sizeof(bytebuf);
65  }
66 
68  {
69  bitbuf = rand64();
70  bitbuf_size = 64;
71  }
72 
73 public:
74  explicit FastRandomContext(bool fDeterministic = false);
75 
77  explicit FastRandomContext(const uint256& seed);
78 
80  uint64_t rand64()
81  {
82  if (bytebuf_size < 8) FillByteBuffer();
83  uint64_t ret = ReadLE64(bytebuf + 64 - bytebuf_size);
84  bytebuf_size -= 8;
85  return ret;
86  }
87 
89  uint64_t randbits(int bits) {
90  if (bits == 0) {
91  return 0;
92  } else if (bits > 32) {
93  return rand64() >> (64 - bits);
94  } else {
95  if (bitbuf_size < bits) FillBitBuffer();
96  uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits));
97  bitbuf >>= bits;
98  bitbuf_size -= bits;
99  return ret;
100  }
101  }
102 
104  uint64_t randrange(uint64_t range)
105  {
106  --range;
107  int bits = CountBits(range);
108  while (true) {
109  uint64_t ret = randbits(bits);
110  if (ret <= range) return ret;
111  }
112  }
113 
115  std::vector<unsigned char> randbytes(size_t len);
116 
118  uint32_t rand32() { return randbits(32); }
119 
121  uint256 rand256();
122 
124  bool randbool() { return randbits(1); }
125 
126  // Compatibility with the C++11 UniformRandomBitGenerator concept
127  typedef uint64_t result_type;
128  static constexpr uint64_t min() { return 0; }
129  static constexpr uint64_t max() { return std::numeric_limits<uint64_t>::max(); }
130  inline uint64_t operator()() { return rand64(); }
131 };
132 
133 /* Number of random bytes returned by GetOSRand.
134  * When changing this constant make sure to change all call sites, and make
135  * sure that the underlying OS APIs for all platforms support the number.
136  * (many cap out at 256 bytes).
137  */
138 static const int NUM_OS_RANDOM_BYTES = 32;
139 
143 void GetOSRand(unsigned char *ent32);
144 
148 bool Random_SanityCheck();
149 
151 void RandomInit();
152 
153 #endif // BITCOIN_RANDOM_H
uint64_t operator()()
Definition: random.h:130
void Output(unsigned char *output, size_t bytes)
Definition: chacha20.cpp:74
uint64_t randbits(int bits)
Generate a random (bits)-bit integer.
Definition: random.h:89
uint64_t rand64()
Generate a random 64-bit integer.
Definition: random.h:80
FastRandomContext(bool fDeterministic=false)
Definition: random.cpp:457
unsigned char bytebuf[64]
Definition: random.h:50
uint64_t bitbuf
Definition: random.h:53
UniValue ret(UniValue::VARR)
Definition: rpcwallet.cpp:1140
uint64_t randrange(uint64_t range)
Generate a random integer in the range [0..range).
Definition: random.h:104
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
Definition: random.cpp:413
void RandAddSeedSleep()
Add a little bit of randomness to the output of GetStrongRangBytes.
Definition: random.cpp:284
void RandAddSeed()
Definition: random.cpp:132
void FillBitBuffer()
Definition: random.h:67
void FillByteBuffer()
Definition: random.h:58
int GetRandInt(int nMax)
Definition: random.cpp:369
A PRNG class for ChaCha20.
Definition: chacha20.h:12
void GetStrongRandBytes(unsigned char *buf, int num)
Function to gather random data from multiple sources, failing whenever any of those sources fail to p...
Definition: random.cpp:319
void RandomInit()
Initialize the RNG.
Definition: random.cpp:466
Fast randomness source.
Definition: random.h:45
uint256 rand256()
generate a random uint256.
Definition: random.cpp:388
static constexpr uint64_t max()
Definition: random.h:129
void RandomSeed()
Definition: random.cpp:381
uint32_t rand32()
Generate a random 32-bit integer.
Definition: random.h:118
int bytebuf_size
Definition: random.h:51
uint64_t result_type
Definition: random.h:127
bool requires_seed
Definition: random.h:47
256-bit opaque blob.
Definition: uint256.h:122
uint64_t GetRand(uint64_t nMax)
Definition: random.cpp:354
bool randbool()
Generate a random boolean.
Definition: random.h:124
uint256 GetRandHash()
Definition: random.cpp:374
void GetOSRand(unsigned char *ent32)
Get 32 bytes of system entropy.
Definition: random.cpp:204
static constexpr uint64_t min()
Definition: random.h:128
ChaCha20 rng
Definition: random.h:48
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
Definition: random.cpp:275
std::vector< unsigned char > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:399