Skip to content

Commit 0d55dbd

Browse files
committed
-Can specify full 256-bit vlaues for --stride instead of 64-bit values
-Added missing error check in OpenCL initialization -Fixed bug where hex formatted with h suffix (e.g. 1234h) was not parsed properly
1 parent e19aea6 commit 0d55dbd

File tree

9 files changed

+44
-25
lines changed

9 files changed

+44
-25
lines changed

CLKeySearchDevice/CLKeySearchDevice.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ CLKeySearchDevice::CLKeySearchDevice(uint64_t device, int threads, int pointsPer
5252
// Create the context
5353
_clContext = new cl::CLContext(_device);
5454
Logger::log(LogLevel::Info, "Compiling OpenCL kernels...");
55-
//_clProgram = new cl::CLProgram(*_clContext, util::getExeDirectory() + "KeySearch.cl");
5655
_clProgram = new cl::CLProgram(*_clContext, _bitcrack_cl);
5756

5857
// Load the kernels
@@ -187,7 +186,7 @@ void CLKeySearchDevice::setIncrementor(secp256k1::ecpoint &p)
187186
_clContext->copyHostToDevice(buf, _yInc, 8 * sizeof(unsigned int));
188187
}
189188

190-
void CLKeySearchDevice::init(const secp256k1::uint256 &start, int compression, uint64_t stride)
189+
void CLKeySearchDevice::init(const secp256k1::uint256 &start, int compression, const secp256k1::uint256 &stride)
191190
{
192191
if(start.cmp(secp256k1::N) >= 0) {
193192
throw KeySearchException("Starting key is out of range");
@@ -199,15 +198,19 @@ void CLKeySearchDevice::init(const secp256k1::uint256 &start, int compression, u
199198

200199
_compression = compression;
201200

202-
allocateBuffers();
201+
try {
202+
allocateBuffers();
203203

204-
generateStartingPoints();
204+
generateStartingPoints();
205205

206-
// Set the incrementor
207-
secp256k1::ecpoint g = secp256k1::G();
208-
secp256k1::ecpoint p = secp256k1::multiplyPoint(secp256k1::uint256(_threads * _blocks * _pointsPerThread).mul(secp256k1::uint256(_stride)), g);
206+
// Set the incrementor
207+
secp256k1::ecpoint g = secp256k1::G();
208+
secp256k1::ecpoint p = secp256k1::multiplyPoint(secp256k1::uint256((uint64_t)_threads * _blocks * _pointsPerThread) * _stride, g);
209209

210-
setIncrementor(p);
210+
setIncrementor(p);
211+
} catch(cl::CLException ex) {
212+
throw KeySearchException(ex.msg);
213+
}
211214
}
212215

213216
void CLKeySearchDevice::doStep()

CLKeySearchDevice/CLKeySearchDevice.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class CLKeySearchDevice : public KeySearchDevice {
4242

4343
int _compression = PointCompressionType::COMPRESSED;
4444

45-
uint64_t _iterations;
45+
uint64_t _iterations = 0;
4646

47-
uint64_t _stride = 1;
47+
secp256k1::uint256 _stride = 1;
4848

4949
std::string _deviceName;
5050

@@ -113,7 +113,7 @@ class CLKeySearchDevice : public KeySearchDevice {
113113

114114

115115
// Initialize the device
116-
virtual void init(const secp256k1::uint256 &start, int compression, uint64_t stride);
116+
virtual void init(const secp256k1::uint256 &start, int compression, const secp256k1::uint256 &stride);
117117

118118
// Perform one iteration
119119
virtual void doStep();

CudaKeySearchDevice/CudaKeySearchDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ CudaKeySearchDevice::CudaKeySearchDevice(int device, int threads, int pointsPerT
5757
_pointsPerThread = pointsPerThread;
5858
}
5959

60-
void CudaKeySearchDevice::init(const secp256k1::uint256 &start, int compression, uint64_t stride)
60+
void CudaKeySearchDevice::init(const secp256k1::uint256 &start, int compression, const secp256k1::uint256 &stride)
6161
{
6262
if(start.cmp(secp256k1::N) >= 0) {
6363
throw KeySearchException("Starting key is out of range");
@@ -89,7 +89,7 @@ void CudaKeySearchDevice::init(const secp256k1::uint256 &start, int compression,
8989

9090
// Set the incrementor
9191
secp256k1::ecpoint g = secp256k1::G();
92-
secp256k1::ecpoint p = secp256k1::multiplyPoint(secp256k1::uint256(_threads * _blocks * _pointsPerThread).mul(secp256k1::uint256(_stride)), g);
92+
secp256k1::ecpoint p = secp256k1::multiplyPoint(secp256k1::uint256((uint64_t)_threads * _blocks * _pointsPerThread) * _stride, g);
9393

9494
cudaCall(_resultList.init(sizeof(CudaDeviceResult), 16));
9595

CudaKeySearchDevice/CudaKeySearchDevice.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ class CudaKeySearchDevice : public KeySearchDevice {
6363

6464
uint32_t getPrivateKeyOffset(int thread, int block, int point);
6565

66-
uint64_t _stride;
66+
secp256k1::uint256 _stride;
6767

6868
bool verifyKey(const secp256k1::uint256 &privateKey, const secp256k1::ecpoint &publicKey, const unsigned int hash[5], bool compressed);
6969

7070
public:
7171

7272
CudaKeySearchDevice(int device, int threads, int pointsPerThread, int blocks = 0);
7373

74-
virtual void init(const secp256k1::uint256 &start, int compression, uint64_t stride);
74+
virtual void init(const secp256k1::uint256 &start, int compression, const secp256k1::uint256 &stride);
7575

7676
virtual void doStep();
7777

KeyFinder/main.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ typedef struct {
5252

5353
uint64_t totalkeys = 0;
5454
unsigned int elapsed = 0;
55-
uint64_t stride = 1;
56-
55+
secp256k1::uint256 stride = 1;
5756
}RunConfig;
5857

5958
static RunConfig _config;
@@ -309,7 +308,7 @@ void writeCheckpoint(secp256k1::uint256 nextKey)
309308
tmp << "compression=" << getCompressionString(_config.compression) << std::endl;
310309
tmp << "device=" << _config.device << std::endl;
311310
tmp << "elapsed=" << (_config.elapsed + util::getSystemTime() - _startTime) << std::endl;
312-
tmp << "stride=" << (_config.stride);
311+
tmp << "stride=" << _config.stride.toString();
313312
tmp.close();
314313
}
315314

@@ -365,7 +364,7 @@ int run()
365364
Logger::log(LogLevel::Info, "Compression: " + getCompressionString(_config.compression));
366365
Logger::log(LogLevel::Info, "Starting at: " + _config.nextKey.toString());
367366
Logger::log(LogLevel::Info, "Ending at: " + _config.endKey.toString());
368-
Logger::log(LogLevel::Info, "Counting by: " + util::format(_config.stride));
367+
Logger::log(LogLevel::Info, "Counting by: " + _config.stride.toString());
369368

370369
try {
371370

@@ -563,7 +562,19 @@ int main(int argc, char **argv)
563562
}
564563
optShares = true;
565564
} else if(optArg.equals("", "--stride")) {
566-
_config.stride = util::parseUInt64(optArg.arg);
565+
try {
566+
_config.stride = secp256k1::uint256(optArg.arg);
567+
} catch(...) {
568+
throw std::string("invalid argument: : expected hex string");
569+
}
570+
571+
if(_config.stride.cmp(secp256k1::N) >= 0) {
572+
throw std::string("argument is out of range");
573+
}
574+
575+
if(_config.stride.cmp(0) == 0) {
576+
throw std::string("argument is out of range");
577+
}
567578
}
568579

569580
} catch(std::string err) {

KeyFinderLib/KeyFinder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void KeyFinder::defaultStatusCallback(KeySearchStatus status)
1818
// Do nothing
1919
}
2020

21-
KeyFinder::KeyFinder(const secp256k1::uint256 &startKey, const secp256k1::uint256 &endKey, int compression, KeySearchDevice* device, uint64_t stride)
21+
KeyFinder::KeyFinder(const secp256k1::uint256 &startKey, const secp256k1::uint256 &endKey, int compression, KeySearchDevice* device, const secp256k1::uint256 &stride)
2222
{
2323
_total = 0;
2424
_statusInterval = 1000;

KeyFinderLib/KeyFinder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class KeyFinder {
2121

2222
uint64_t _statusInterval;
2323

24-
uint64_t _stride = 1;
24+
secp256k1::uint256 _stride = 1;
2525
uint64_t _iterCount;
2626
uint64_t _total;
2727
uint64_t _totalTime;
@@ -45,7 +45,7 @@ class KeyFinder {
4545

4646
public:
4747

48-
KeyFinder(const secp256k1::uint256 &startKey, const secp256k1::uint256 &endKey, int compression, KeySearchDevice* device, uint64_t stride);
48+
KeyFinder(const secp256k1::uint256 &startKey, const secp256k1::uint256 &endKey, int compression, KeySearchDevice* device, const secp256k1::uint256 &stride);
4949

5050
~KeyFinder();
5151

KeyFinderLib/KeySearchDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class KeySearchDevice {
3939
public:
4040

4141
// Initialize the device
42-
virtual void init(const secp256k1::uint256 &start, int compression, uint64_t stride) = 0;
42+
virtual void init(const secp256k1::uint256 &start, int compression, const secp256k1::uint256 &stride) = 0;
4343

4444
// Perform one iteration
4545
virtual void doStep() = 0;

secp256k1lib/secp256k1.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace secp256k1 {
3333

3434
// 'h' suffix
3535
if(t.length() >= 1 && t[t.length() - 1] == 'h') {
36-
t = t.substr(0, t.length() - 2);
36+
t = t.substr(0, t.length() - 1);
3737
}
3838

3939
if(t.length() == 0) {
@@ -129,6 +129,11 @@ namespace secp256k1 {
129129
return mul(x);
130130
}
131131

132+
uint256 operator*(const uint256 &x) const
133+
{
134+
return mul(x);
135+
}
136+
132137
uint256 operator*(uint64_t x) const
133138
{
134139
return mul(x);

0 commit comments

Comments
 (0)