Fabcoin Core  0.16.2
P2P Digital Currency
random.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_RANDOM_H
7 #define FABCOIN_RANDOM_H
8 
9 #include <crypto/chacha20.h>
10 #include <crypto/common.h>
11 #include <uint256.h>
12 
13 #include <stdint.h>
14 
15 /* Seed OpenSSL PRNG with additional entropy data */
16 void RandAddSeed();
17 
21 void GetRandBytes(unsigned char* buf, int num);
22 uint64_t GetRand(uint64_t nMax);
23 int GetRandInt(int nMax);
25 
31 void RandAddSeedSleep();
32 
37 void GetStrongRandBytes(unsigned char* buf, int num);
38 
45 private:
48 
49  unsigned char bytebuf[64];
51 
52  uint64_t bitbuf;
54 
55  void RandomSeed();
56 
58  {
59  if (requires_seed) {
60  RandomSeed();
61  }
62  rng.Output(bytebuf, sizeof(bytebuf));
63  bytebuf_size = sizeof(bytebuf);
64  }
65 
67  {
68  bitbuf = rand64();
69  bitbuf_size = 64;
70  }
71 
72 public:
73  explicit FastRandomContext(bool fDeterministic = false);
74 
76  explicit FastRandomContext(const uint256& seed);
77 
79  uint64_t rand64()
80  {
81  if (bytebuf_size < 8) FillByteBuffer();
82  uint64_t ret = ReadLE64(bytebuf + 64 - bytebuf_size);
83  bytebuf_size -= 8;
84  return ret;
85  }
86 
88  uint64_t randbits(int bits) {
89  if (bits == 0) {
90  return 0;
91  } else if (bits > 32) {
92  return rand64() >> (64 - bits);
93  } else {
94  if (bitbuf_size < bits) FillBitBuffer();
95  uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits));
96  bitbuf >>= bits;
97  bitbuf_size -= bits;
98  return ret;
99  }
100  }
101 
103  uint64_t randrange(uint64_t range)
104  {
105  --range;
106  int bits = CountBits(range);
107  while (true) {
108  uint64_t ret = randbits(bits);
109  if (ret <= range) return ret;
110  }
111  }
112 
114  std::vector<unsigned char> randbytes(size_t len);
115 
117  uint32_t rand32() { return randbits(32); }
118 
120  uint256 rand256();
121 
123  bool randbool() { return randbits(1); }
124 };
125 
126 /* Number of random bytes returned by GetOSRand.
127  * When changing this constant make sure to change all call sites, and make
128  * sure that the underlying OS APIs for all platforms support the number.
129  * (many cap out at 256 bytes).
130  */
131 static const ssize_t NUM_OS_RANDOM_BYTES = 32;
132 
136 void GetOSRand(unsigned char *ent32);
137 
141 bool Random_SanityCheck();
142 
144 void RandomInit();
145 
146 #endif // FABCOIN_RANDOM_H
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:88
uint64_t rand64()
Generate a random 64-bit integer.
Definition: random.h:79
FastRandomContext(bool fDeterministic=false)
Definition: random.cpp:455
unsigned char bytebuf[64]
Definition: random.h:49
uint64_t bitbuf
Definition: random.h:52
uint64_t randrange(uint64_t range)
Generate a random integer in the range [0..range).
Definition: random.h:103
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
Definition: random.cpp:411
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:66
void FillByteBuffer()
Definition: random.h:57
int GetRandInt(int nMax)
Definition: random.cpp:367
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 source fail to pr...
Definition: random.cpp:317
void RandomInit()
Initialize the RNG.
Definition: random.cpp:464
Fast randomness source.
Definition: random.h:44
uint256 rand256()
generate a random uint256.
Definition: random.cpp:386
void RandomSeed()
Definition: random.cpp:379
uint32_t rand32()
Generate a random 32-bit integer.
Definition: random.h:117
int bytebuf_size
Definition: random.h:50
bool requires_seed
Definition: random.h:46
256-bit opaque blob.
Definition: uint256.h:132
uint64_t GetRand(uint64_t nMax)
Definition: random.cpp:352
bool randbool()
Generate a random boolean.
Definition: random.h:123
uint256 GetRandHash()
Definition: random.cpp:372
void GetOSRand(unsigned char *ent32)
Get 32 bytes of system entropy.
Definition: random.cpp:202
ChaCha20 rng
Definition: random.h:47
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:397