Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random mode option for each single starting point. #148

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions CLKeySearchDevice/CLKeySearchDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cmath>

#include "Logger.h"
#include "util.h"
#include "CLKeySearchDevice.h"
Expand Down Expand Up @@ -186,27 +187,36 @@ void CLKeySearchDevice::setIncrementor(secp256k1::ecpoint &p)
_clContext->copyHostToDevice(buf, _yInc, 8 * sizeof(unsigned int));
}

void CLKeySearchDevice::init(const secp256k1::uint256 &start, int compression, const secp256k1::uint256 &stride)
void CLKeySearchDevice::init(const secp256k1::uint256 &start, const secp256k1::uint256 &end, int compression, const secp256k1::uint256 &stride, bool randomMode)
{
if(start.cmp(secp256k1::N) >= 0) {
throw KeySearchException("Starting key is out of range");
}

_start = start;

_end = end;

_stride = stride;

_compression = compression;

_randomMode = randomMode;

try {
allocateBuffers();

generateStartingPoints();

// Set the incrementor
secp256k1::ecpoint g = secp256k1::G();
secp256k1::ecpoint p = secp256k1::multiplyPoint(secp256k1::uint256((uint64_t)_threads * _blocks * _pointsPerThread) * _stride, g);
secp256k1::ecpoint p;

if (_randomMode) {
p = secp256k1::multiplyPoint(_stride, g);
} else {
p = secp256k1::multiplyPoint(secp256k1::uint256((uint64_t)_threads * _blocks * _pointsPerThread) * _stride, g);
}
setIncrementor(p);
} catch(cl::CLException ex) {
throw KeySearchException(ex.msg);
Expand All @@ -218,7 +228,7 @@ void CLKeySearchDevice::doStep()
try {
uint64_t numKeys = (uint64_t)_blocks * _threads * _pointsPerThread;

if(_iterations < 2 && _start.cmp(numKeys) <= 0) {
if(!_randomMode && _iterations < 2 && _start.cmp(numKeys) <= 0) {

_stepKernelWithDouble->call(
_blocks,
Expand Down Expand Up @@ -420,8 +430,19 @@ void CLKeySearchDevice::getResultsInternal()
KeySearchResult minerResult;

// Calculate the private key based on the number of iterations and the current thread
secp256k1::uint256 offset = (secp256k1::uint256((uint64_t)_blocks * _threads * _pointsPerThread * _iterations) + secp256k1::uint256(getPrivateKeyOffset(ptr[i].thread, ptr[i].block, ptr[i].idx))) * _stride;
secp256k1::uint256 privateKey = secp256k1::addModN(_start, offset);
secp256k1::uint256 offset;
secp256k1::uint256 privateKey;

uint32_t privateKeyOffset = getPrivateKeyOffset(ptr[i].thread, ptr[i].block, ptr[i].idx);

if (!_randomMode) {
offset = (secp256k1::uint256((uint64_t)_blocks * _threads * _pointsPerThread * _iterations) + privateKeyOffset) * _stride;
privateKey = secp256k1::addModN(_start, offset);
} else {
offset = secp256k1::uint256(_iterations) * _stride;
privateKey = exponents[privateKeyOffset];
privateKey = secp256k1::addModN(privateKey, offset);
}

minerResult.privateKey = privateKey;
minerResult.compressed = ptr[i].compressed;
Expand Down Expand Up @@ -581,8 +602,6 @@ void CLKeySearchDevice::generateStartingPoints()
uint64_t totalPoints = (uint64_t)_pointsPerThread * _threads * _blocks;
uint64_t totalMemory = totalPoints * 40;

std::vector<secp256k1::uint256> exponents;

initializeBasePoints();

_pointsMemSize = totalPoints * sizeof(unsigned int) * 16 + _pointsPerThread * sizeof(unsigned int) * 8;
Expand All @@ -592,10 +611,22 @@ void CLKeySearchDevice::generateStartingPoints()
// Generate key pairs for k, k+1, k+2 ... k + <total points in parallel - 1>
secp256k1::uint256 privKey = _start;

exponents.push_back(privKey);
if (!_randomMode) {
exponents.push_back(privKey);
}

for(uint64_t i = !_randomMode ? 1 : 0; i < totalPoints; i++) {

for(uint64_t i = 1; i < totalPoints; i++) {
privKey = privKey.add(_stride);
if (_randomMode) {
privKey = secp256k1::getRandomRange(_start, _end);
} else {
privKey = privKey.add(_stride);
}

if (_randomMode && i < 3) {
Logger::log(LogLevel::Info, "Starting point sample: " + privKey.toString() + " (" + std::to_string(privKey.getBitRange()) +" bit range)");
}

exponents.push_back(privKey);
}

Expand Down Expand Up @@ -629,6 +660,10 @@ void CLKeySearchDevice::generateStartingPoints()
}

Logger::log(LogLevel::Info, "Done");

if (!_randomMode) {
exponents.clear();
}
}


Expand Down
7 changes: 6 additions & 1 deletion CLKeySearchDevice/CLKeySearchDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CLKeySearchDevice : public KeySearchDevice {
CLTargetList _deviceTargetList;

secp256k1::uint256 _start;
secp256k1::uint256 _end;

std::vector<hash160> _targetList;

Expand All @@ -41,6 +42,8 @@ class CLKeySearchDevice : public KeySearchDevice {
cl_device_id _device;

int _compression = PointCompressionType::COMPRESSED;

bool _randomMode = false;

uint64_t _iterations = 0;

Expand Down Expand Up @@ -106,14 +109,16 @@ class CLKeySearchDevice : public KeySearchDevice {

uint64_t getOptimalBloomFilterMask(double p, size_t n);

std::vector<secp256k1::uint256> exponents;

public:

CLKeySearchDevice(uint64_t device, int threads, int pointsPerThread, int blocks = 0);
~CLKeySearchDevice();


// Initialize the device
virtual void init(const secp256k1::uint256 &start, int compression, const secp256k1::uint256 &stride);
virtual void init(const secp256k1::uint256 &start, const secp256k1::uint256 &end, int compression, const secp256k1::uint256 &stride, bool randomMode);

// Perform one iteration
virtual void doStep();
Expand Down
Loading