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 https://www.opensource.org/licenses/mit-license.php .
8 #include "support/cleanse.h"
10 #include "compat.h" // for Windows API
12 #include "serialize.h" // for begin_ptr(vec)
13 #include "util.h" // for LogPrint()
14 #include "utilstrencodings.h" // for GetTime()
24 static inline int64_t GetPerformanceCounter()
28 QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
31 gettimeofday(&t, NULL);
32 nCounter = (int64_t)(t.tv_sec * 1000000 + t.tv_usec);
37 void GetRandBytes(unsigned char* buf, size_t num)
39 randombytes_buf(buf, num);
42 uint64_t GetRand(uint64_t nMax)
47 // The range of the random source must be a multiple of the modulus
48 // to give every possible output value an equal possibility
49 uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
52 GetRandBytes((unsigned char*)&nRand, sizeof(nRand));
53 } while (nRand >= nRange);
54 return (nRand % nMax);
57 int GetRandInt(int nMax)
65 GetRandBytes((unsigned char*)&hash, sizeof(hash));
69 uint32_t insecure_rand_Rz = 11;
70 uint32_t insecure_rand_Rw = 11;
71 void seed_insecure_rand(bool fDeterministic)
73 // The seed values have some unlikely fixed points which we avoid.
75 insecure_rand_Rz = insecure_rand_Rw = 11;
79 GetRandBytes((unsigned char*)&tmp, 4);
80 } while (tmp == 0 || tmp == 0x9068ffffU);
81 insecure_rand_Rz = tmp;
83 GetRandBytes((unsigned char*)&tmp, 4);
84 } while (tmp == 0 || tmp == 0x464fffffU);
85 insecure_rand_Rw = tmp;
89 int GenIdentity(int n)