Browse Source

Merge pull request #2829 from arkpar/random-fix

Removed duplicate random devices
cl-refactor
Gav Wood 9 years ago
parent
commit
a3b5006b15
  1. 1
      libdevcore/CommonData.cpp
  2. 12
      libdevcore/FixedHash.h
  3. 17
      libdevcore/vector_ref.h
  4. 3
      libethcore/EthashCPUMiner.cpp

1
libdevcore/CommonData.cpp

@ -20,6 +20,7 @@
*/
#include "CommonData.h"
#include <random>
#include <boost/random/uniform_int_distribution.hpp>
#include "Exceptions.h"
#include "Log.h"

12
libdevcore/FixedHash.h

@ -149,18 +149,16 @@ public:
/// @returns a constant reference to the object's data as an STL array.
std::array<byte, N> const& asArray() const { return m_data; }
/// @returns a randomly-valued hash
/// Populate with random data.
template <class Engine>
static FixedHash random(Engine& _eng)
void randomize(Engine& _eng)
{
FixedHash ret;
for (auto& i: ret.m_data)
for (auto& i: m_data)
i = (uint8_t)boost::random::uniform_int_distribution<uint16_t>(0, 255)(_eng);
return ret;
}
/// @returns a random valued object.
static FixedHash random() { return random(s_fixedHashEngine); }
static FixedHash random() { FixedHash ret; ret.randomize(s_fixedHashEngine); return ret; }
struct hash
{
@ -293,7 +291,7 @@ public:
bytesConstRef ref() const { return FixedHash<T>::ref(); }
byte const* data() const { return FixedHash<T>::data(); }
static SecureFixedHash<T> random() { SecureFixedHash<T> ret; ret.FixedHash<T>::ref().randomize(); return ret; }
static SecureFixedHash<T> random() { SecureFixedHash<T> ret; ret.randomize(s_fixedHashEngine); return ret; }
using FixedHash<T>::firstBitSet;
void clear() { ref().cleanse(); }

17
libdevcore/vector_ref.h

@ -5,16 +5,10 @@
#include <type_traits>
#include <vector>
#include <string>
#include <random>
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
namespace dev
{
static unsigned char s_cleanseCounter = 0;
static boost::random_device s_vectorRefEngine;
/**
* A modifiable reference to an existing object or vector in memory.
*/
@ -71,20 +65,11 @@ public:
void copyTo(vector_ref<typename std::remove_const<_T>::type> _t) const { if (overlapsWith(_t)) memmove(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T)); else memcpy(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T)); }
/// Copies the contents of this vector_ref to the contents of @a _t, and zeros further trailing elements in @a _t.
void populate(vector_ref<typename std::remove_const<_T>::type> _t) const { copyTo(_t); memset(_t.data() + m_count, 0, std::max(_t.size(), m_count) - m_count); }
/// Populate with random data.
template <class Engine>
void randomize(Engine& _eng)
{
uint8_t* e = (uint8_t*)end();
for (uint8_t* i = (uint8_t*)begin(); i != e; ++i)
*i = (uint8_t)boost::random::uniform_int_distribution<uint16_t>(0, 255)(_eng);
}
/// @returns a random valued object.
void randomize() { randomize(s_vectorRefEngine); }
/// Securely overwrite the memory.
/// @note adapted from OpenSSL's implementation.
void cleanse()
{
static unsigned char s_cleanseCounter = 0;
uint8_t* p = (uint8_t*)begin();
size_t const len = (uint8_t*)end() - p;
size_t loop = len;

3
libethcore/EthashCPUMiner.cpp

@ -25,6 +25,7 @@
#include <thread>
#include <chrono>
#include <boost/algorithm/string.hpp>
#include <random>
#if ETH_CPUID || !ETH_TRUE
#define HAVE_STDINT_H
#include <libcpuid/libcpuid.h>
@ -78,7 +79,7 @@ void EthashCPUMiner::workLoop()
auto tid = std::this_thread::get_id();
static std::mt19937_64 s_eng((time(0) + std::hash<decltype(tid)>()(tid)));
uint64_t tryNonce = (uint64_t)(u64)Nonce::random(s_eng);
uint64_t tryNonce = s_eng();
ethash_return_value ethashReturn;
WorkPackage w = work();

Loading…
Cancel
Save