Raven Core  3.0.0
P2P Digital Currency
random.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_RANDOM_H
8 #define RAVEN_RANDOM_H
9 
10 #include "crypto/chacha20.h"
11 #include "crypto/common.h"
12 #include "uint256.h"
13 
14 #include <stdint.h>
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 
127 /* Number of random bytes returned by GetOSRand.
128  * When changing this constant make sure to change all call sites, and make
129  * sure that the underlying OS APIs for all platforms support the number.
130  * (many cap out at 256 bytes).
131  */
132 static const ssize_t NUM_OS_RANDOM_BYTES = 32;
133 
137 void GetOSRand(unsigned char *ent32);
138 
142 bool Random_SanityCheck();
143 
145 void RandomInit();
146 
147 #endif // RAVEN_RANDOM_H
void Output(unsigned char *output, size_t bytes)
Definition: chacha20.cpp:75
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:456
unsigned char bytebuf[64]
Definition: random.h:50
uint64_t bitbuf
Definition: random.h:53
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:412
void RandAddSeedSleep()
Add a little bit of randomness to the output of GetStrongRangBytes.
Definition: random.cpp:283
void RandAddSeed()
Definition: random.cpp:131
void FillBitBuffer()
Definition: random.h:67
void FillByteBuffer()
Definition: random.h:58
int GetRandInt(int nMax)
Definition: random.cpp:368
A PRNG class for ChaCha20.
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:318
void RandomInit()
Initialize the RNG.
Definition: random.cpp:465
Fast randomness source.
Definition: random.h:45
uint256 rand256()
generate a random uint256.
Definition: random.cpp:387
void RandomSeed()
Definition: random.cpp:380
uint32_t rand32()
Generate a random 32-bit integer.
Definition: random.h:118
int bytebuf_size
Definition: random.h:51
bool requires_seed
Definition: random.h:47
256-bit opaque blob.
Definition: uint256.h:123
uint64_t GetRand(uint64_t nMax)
Definition: random.cpp:353
bool randbool()
Generate a random boolean.
Definition: random.h:124
uint256 GetRandHash()
Definition: random.cpp:373
void GetOSRand(unsigned char *ent32)
Get 32 bytes of system entropy.
Definition: random.cpp:203
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:274
std::vector< unsigned char > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:398