Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

random.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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 <chrono> // For std::chrono::microseconds
14 #include <cstdint>
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 std::chrono::microseconds GetRandMicros(std::chrono::microseconds duration_max) noexcept;
25 int GetRandInt(int nMax);
27 
28 bool GetRandBool(double rate);
29 
35 void RandAddSeedSleep();
36 
41 void GetStrongRandBytes(unsigned char* buf, int num);
42 
49 private:
52 
53  unsigned char bytebuf[64];
55 
56  uint64_t bitbuf;
58 
59  void RandomSeed();
60 
62  {
63  if (requires_seed) {
64  RandomSeed();
65  }
66  rng.Keystream(bytebuf, sizeof(bytebuf));
67  bytebuf_size = sizeof(bytebuf);
68  }
69 
71  {
72  bitbuf = rand64();
73  bitbuf_size = 64;
74  }
75 
76 public:
77  explicit FastRandomContext(bool fDeterministic = false);
78 
80  explicit FastRandomContext(const uint256& seed);
81 
83  uint64_t rand64()
84  {
85  if (bytebuf_size < 8) FillByteBuffer();
86  uint64_t ret = ReadLE64(bytebuf + 64 - bytebuf_size);
87  bytebuf_size -= 8;
88  return ret;
89  }
90 
92  uint64_t randbits(int bits) {
93  if (bits == 0) {
94  return 0;
95  } else if (bits > 32) {
96  return rand64() >> (64 - bits);
97  } else {
99  uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits));
100  bitbuf >>= bits;
101  bitbuf_size -= bits;
102  return ret;
103  }
104  }
105 
107  uint64_t randrange(uint64_t range)
108  {
109  --range;
110  int bits = CountBits(range);
111  while (true) {
112  uint64_t ret = randbits(bits);
113  if (ret <= range) return ret;
114  }
115  }
116 
117  uint32_t rand32(uint32_t nMax) {
118  return rand32() % nMax;
119  }
120 
121  uint32_t operator()(uint32_t nMax) {
122  return rand32(nMax);
123  }
124 
126  std::vector<unsigned char> randbytes(size_t len);
127 
129  uint32_t rand32() { return randbits(32); }
130 
132  uint256 rand256();
133 
135  bool randbool() { return randbits(1); }
136 };
137 
138 /* Number of random bytes returned by GetOSRand.
139  * When changing this constant make sure to change all call sites, and make
140  * sure that the underlying OS APIs for all platforms support the number.
141  * (many cap out at 256 bytes).
142  */
143 static const int NUM_OS_RANDOM_BYTES = 32;
144 
148 void GetOSRand(unsigned char *ent32);
149 
153 bool Random_SanityCheck();
154 
156 void RandomInit();
157 
158 #endif // BITCOIN_RANDOM_H
static const int NUM_OS_RANDOM_BYTES
Definition: random.h:143
uint64_t randbits(int bits)
Generate a random (bits)-bit integer.
Definition: random.h:92
uint64_t rand64()
Generate a random 64-bit integer.
Definition: random.h:83
FastRandomContext(bool fDeterministic=false)
Definition: random.cpp:478
bool GetRandBool(double rate)
Definition: random.cpp:391
uint32_t rand32(uint32_t nMax)
Definition: random.h:117
unsigned char bytebuf[64]
Definition: random.h:53
constexpr auto bits
Definition: position.hpp:23
uint64_t bitbuf
Definition: random.h:56
uint64_t randrange(uint64_t range)
Generate a random integer in the range [0..range).
Definition: random.h:107
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
Definition: random.cpp:434
void RandAddSeedSleep()
Add a little bit of randomness to the output of GetStrongRangBytes.
Definition: random.cpp:282
void RandAddSeed()
Definition: random.cpp:130
void FillBitBuffer()
Definition: random.h:70
void FillByteBuffer()
Definition: random.h:61
int GetRandInt(int nMax)
Definition: random.cpp:379
A class for ChaCha20 256-bit stream cipher developed by Daniel J.
Definition: chacha20.h:13
void GetStrongRandBytes(unsigned char *buf, int num)
Function to gather random data from multiple sources, failing whenever any of those source fail to pr...
Definition: random.cpp:317
void RandomInit()
Initialize the RNG.
Definition: random.cpp:487
Fast randomness source.
Definition: random.h:48
uint256 rand256()
generate a random uint256.
Definition: random.cpp:409
void RandomSeed()
Definition: random.cpp:402
std::chrono::microseconds GetRandMicros(std::chrono::microseconds duration_max) noexcept
Definition: random.cpp:374
uint32_t rand32()
Generate a random 32-bit integer.
Definition: random.h:129
int bytebuf_size
Definition: random.h:54
static uint64_t CountBits(uint64_t x)
Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set...
Definition: common.h:83
bool requires_seed
Definition: random.h:50
256-bit opaque blob.
Definition: uint256.h:123
uint64_t GetRand(uint64_t nMax)
Definition: random.cpp:354
void Keystream(unsigned char *c, size_t bytes)
outputs the keystream of size <bytes> into
Definition: chacha20.cpp:74
bool randbool()
Generate a random boolean.
Definition: random.h:135
uint256 GetRandHash()
Definition: random.cpp:384
uint32_t operator()(uint32_t nMax)
Definition: random.h:121
void GetOSRand(unsigned char *ent32)
Get 32 bytes of system entropy.
Definition: random.cpp:202
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
ChaCha20 rng
Definition: random.h:51
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
Definition: random.cpp:273
std::vector< unsigned char > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:420
Released under the MIT license