]>
Commit | Line | Data |
---|---|---|
6354935c | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
78253fcb | 3 | // Distributed under the MIT software license, see the accompanying |
bc909a7a | 4 | // file COPYING or https://www.opensource.org/licenses/mit-license.php . |
6354935c PK |
5 | |
6 | #include "random.h" | |
7 | ||
1630219d | 8 | #include "support/cleanse.h" |
9cb1ec9c | 9 | #ifdef _WIN32 |
6354935c PK |
10 | #include "compat.h" // for Windows API |
11 | #endif | |
20e01b1a PW |
12 | #include "serialize.h" // for begin_ptr(vec) |
13 | #include "util.h" // for LogPrint() | |
ad49c256 | 14 | #include "utilstrencodings.h" // for GetTime() |
6354935c | 15 | |
611116d4 PK |
16 | #include <limits> |
17 | ||
9cb1ec9c | 18 | #ifndef _WIN32 |
6354935c PK |
19 | #include <sys/time.h> |
20 | #endif | |
611116d4 | 21 | |
31062675 | 22 | #include "sodium.h" |
6354935c PK |
23 | |
24 | static inline int64_t GetPerformanceCounter() | |
25 | { | |
26 | int64_t nCounter = 0; | |
9cb1ec9c | 27 | #ifdef _WIN32 |
6354935c PK |
28 | QueryPerformanceCounter((LARGE_INTEGER*)&nCounter); |
29 | #else | |
30 | timeval t; | |
31 | gettimeofday(&t, NULL); | |
32 | nCounter = (int64_t)(t.tv_sec * 1000000 + t.tv_usec); | |
33 | #endif | |
34 | return nCounter; | |
35 | } | |
36 | ||
31062675 | 37 | void GetRandBytes(unsigned char* buf, size_t num) |
6354935c | 38 | { |
bf6a1383 | 39 | randombytes_buf(buf, num); |
6354935c PK |
40 | } |
41 | ||
42 | uint64_t GetRand(uint64_t nMax) | |
43 | { | |
44 | if (nMax == 0) | |
45 | return 0; | |
46 | ||
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; | |
50 | uint64_t nRand = 0; | |
51 | do { | |
52 | GetRandBytes((unsigned char*)&nRand, sizeof(nRand)); | |
53 | } while (nRand >= nRange); | |
54 | return (nRand % nMax); | |
55 | } | |
56 | ||
57 | int GetRandInt(int nMax) | |
58 | { | |
59 | return GetRand(nMax); | |
60 | } | |
61 | ||
62 | uint256 GetRandHash() | |
63 | { | |
64 | uint256 hash; | |
65 | GetRandBytes((unsigned char*)&hash, sizeof(hash)); | |
66 | return hash; | |
67 | } | |
68 | ||
69 | uint32_t insecure_rand_Rz = 11; | |
70 | uint32_t insecure_rand_Rw = 11; | |
71 | void seed_insecure_rand(bool fDeterministic) | |
72 | { | |
73 | // The seed values have some unlikely fixed points which we avoid. | |
20e01b1a | 74 | if (fDeterministic) { |
6354935c PK |
75 | insecure_rand_Rz = insecure_rand_Rw = 11; |
76 | } else { | |
77 | uint32_t tmp; | |
78 | do { | |
79 | GetRandBytes((unsigned char*)&tmp, 4); | |
20e01b1a | 80 | } while (tmp == 0 || tmp == 0x9068ffffU); |
6354935c PK |
81 | insecure_rand_Rz = tmp; |
82 | do { | |
83 | GetRandBytes((unsigned char*)&tmp, 4); | |
20e01b1a | 84 | } while (tmp == 0 || tmp == 0x464fffffU); |
6354935c PK |
85 | insecure_rand_Rw = tmp; |
86 | } | |
87 | } | |
38276c6b S |
88 | |
89 | int GenIdentity(int n) | |
90 | { | |
91 | return n-1; | |
92 | } |