94 lines
2.1 KiB
C++
94 lines
2.1 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
|
|
|
#include "random.h"
|
|
|
|
#include "support/cleanse.h"
|
|
#ifdef WIN32
|
|
#include "compat.h" // for Windows API
|
|
#endif
|
|
#include "serialize.h" // for begin_ptr(vec)
|
|
#include "util.h" // for LogPrint()
|
|
#include "utilstrencodings.h" // for GetTime()
|
|
|
|
#include <limits>
|
|
|
|
#ifndef WIN32
|
|
#include <sys/time.h>
|
|
#endif
|
|
|
|
#include <librustzcash.h>
|
|
|
|
static inline int64_t GetPerformanceCounter()
|
|
{
|
|
int64_t nCounter = 0;
|
|
#ifdef WIN32
|
|
QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
|
|
#else
|
|
timeval t;
|
|
gettimeofday(&t, NULL);
|
|
nCounter = (int64_t)(t.tv_sec * 1000000 + t.tv_usec);
|
|
#endif
|
|
return nCounter;
|
|
}
|
|
|
|
void GetRandBytes(unsigned char* buf, size_t num)
|
|
{
|
|
librustzcash_getrandom(buf, num);
|
|
}
|
|
|
|
uint64_t GetRand(uint64_t nMax)
|
|
{
|
|
if (nMax == 0)
|
|
return 0;
|
|
|
|
// The range of the random source must be a multiple of the modulus
|
|
// to give every possible output value an equal possibility
|
|
uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
|
|
uint64_t nRand = 0;
|
|
do {
|
|
GetRandBytes((unsigned char*)&nRand, sizeof(nRand));
|
|
} while (nRand >= nRange);
|
|
return (nRand % nMax);
|
|
}
|
|
|
|
int GetRandInt(int nMax)
|
|
{
|
|
return GetRand(nMax);
|
|
}
|
|
|
|
uint256 GetRandHash()
|
|
{
|
|
uint256 hash;
|
|
GetRandBytes((unsigned char*)&hash, sizeof(hash));
|
|
return hash;
|
|
}
|
|
|
|
void FastRandomContext::RandomSeed()
|
|
{
|
|
uint256 seed = GetRandHash();
|
|
rng.SetKey(seed.begin(), 32);
|
|
requires_seed = false;
|
|
}
|
|
|
|
FastRandomContext::FastRandomContext(const uint256& seed) : requires_seed(false), bytebuf_size(0), bitbuf_size(0)
|
|
{
|
|
rng.SetKey(seed.begin(), 32);
|
|
}
|
|
|
|
int GenIdentity(int n)
|
|
{
|
|
return n-1;
|
|
}
|
|
|
|
FastRandomContext::FastRandomContext(bool fDeterministic) : requires_seed(!fDeterministic), bytebuf_size(0), bitbuf_size(0)
|
|
{
|
|
if (!fDeterministic) {
|
|
return;
|
|
}
|
|
uint256 seed;
|
|
rng.SetKey(seed.begin(), 32);
|
|
}
|