PRCYCoin  2.0.0.7rc1
P2P Digital Currency
random.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 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 
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 
78  // Do not permit copying a FastRandomContext (move it, or create a new one to get reseeded).
79  FastRandomContext(const FastRandomContext&) = delete;
82 
85 
87  uint64_t rand64()
88  {
89  if (bytebuf_size < 8) FillByteBuffer();
90  uint64_t ret = ReadLE64(bytebuf + 64 - bytebuf_size);
91  bytebuf_size -= 8;
92  return ret;
93  }
94 
96  uint64_t randbits(int bits) {
97  if (bits == 0) {
98  return 0;
99  } else if (bits > 32) {
100  return rand64() >> (64 - bits);
101  } else {
102  if (bitbuf_size < bits) FillBitBuffer();
103  uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits));
104  bitbuf >>= bits;
105  bitbuf_size -= bits;
106  return ret;
107  }
108  }
109 
111  uint64_t randrange(uint64_t range)
112  {
113  --range;
114  int bits = CountBits(range);
115  while (true) {
116  uint64_t ret = randbits(bits);
117  if (ret <= range) return ret;
118  }
119  }
120 
122  std::vector<unsigned char> randbytes(size_t len);
123 
125  uint32_t rand32() { return randbits(32); }
126 
128  uint256 rand256();
129 
131  bool randbool() { return randbits(1); }
132 };
133 
134 /* Number of random bytes returned by GetOSRand.
135  * When changing this constant make sure to change all call sites, and make
136  * sure that the underlying OS APIs for all platforms support the number.
137  * (many cap out at 256 bytes).
138  */
139 static const ssize_t NUM_OS_RANDOM_BYTES = 32;
140 
144 void GetOSRand(unsigned char *ent32);
145 
149 bool Random_SanityCheck();
150 
152 void RandomInit();
153 
154 #endif // BITCOIN_RANDOM_H
FastRandomContext::FillBitBuffer
void FillBitBuffer()
Definition: random.h:66
GetRandBytes
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
Definition: random.cpp:273
FastRandomContext::FastRandomContext
FastRandomContext(bool fDeterministic=false)
Definition: random.cpp:455
uint256.h
ChaCha20::Output
void Output(unsigned char *output, size_t bytes)
Definition: chacha20.cpp:74
FastRandomContext::bytebuf
unsigned char bytebuf[64]
Definition: random.h:49
Random_SanityCheck
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
Definition: random.cpp:411
FastRandomContext::randbytes
std::vector< unsigned char > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:396
RandomInit
void RandomInit()
Initialize the RNG.
Definition: random.cpp:478
chacha20.h
FastRandomContext::operator=
FastRandomContext & operator=(const FastRandomContext &)=delete
FastRandomContext::bitbuf
uint64_t bitbuf
Definition: random.h:52
RandAddSeedSleep
void RandAddSeedSleep()
Add a little bit of randomness to the output of GetStrongRangBytes.
Definition: random.cpp:282
FastRandomContext::rand256
uint256 rand256()
generate a random uint256.
Definition: random.cpp:385
FastRandomContext::FillByteBuffer
void FillByteBuffer()
Definition: random.h:57
GetRand
uint64_t GetRand(uint64_t nMax)
Definition: random.cpp:351
GetStrongRandBytes
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:316
FastRandomContext::RandomSeed
void RandomSeed()
Definition: random.cpp:378
FastRandomContext::bytebuf_size
int bytebuf_size
Definition: random.h:50
GetOSRand
void GetOSRand(unsigned char *ent32)
Get 32 bytes of system entropy.
Definition: random.cpp:202
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
FastRandomContext::bitbuf_size
int bitbuf_size
Definition: random.h:53
FastRandomContext::rand32
uint32_t rand32()
Generate a random 32-bit integer.
Definition: random.h:125
GetRandHash
uint256 GetRandHash()
Definition: random.cpp:371
FastRandomContext::requires_seed
bool requires_seed
Definition: random.h:46
common.h
RandAddSeed
void RandAddSeed()
Definition: random.cpp:130
FastRandomContext::rng
ChaCha20 rng
Definition: random.h:47
FastRandomContext::randbool
bool randbool()
Generate a random boolean.
Definition: random.h:131
ChaCha20
A PRNG class for ChaCha20.
Definition: chacha20.h:12
FastRandomContext::randrange
uint64_t randrange(uint64_t range)
Generate a random integer in the range [0..range).
Definition: random.h:111
FastRandomContext::rand64
uint64_t rand64()
Generate a random 64-bit integer.
Definition: random.h:87
FastRandomContext
Fast randomness source.
Definition: random.h:44
FastRandomContext::randbits
uint64_t randbits(int bits)
Generate a random (bits)-bit integer.
Definition: random.h:96
GetRandInt
int GetRandInt(int nMax)
Definition: random.cpp:366