Skip to content

Commit

Permalink
v 0.4.2, wif decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGorny committed Jan 24, 2022
1 parent d813434 commit 87c62b3
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 23 deletions.
24 changes: 8 additions & 16 deletions WifSolverCuda/Worker1.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ __device__ __constant__ uint64_t _stride[5];

__global__ void kernelUncompressed(bool* buffResult, bool* buffCollectorWork, uint64_t* const __restrict__ buffRangeStart, const int threadNumberOfChecks, const uint32_t checksum) {
uint64_t _start[5];
uint64_t _startStride[5];
beu32 d_hash[8];
_load(_start, buffRangeStart);

int64_t tIx = (threadIdx.x + blockIdx.x * blockDim.x) * threadNumberOfChecks;
IMult(_startStride, _stride, tIx);
_add(_start, _startStride);
IMult(_start, _stride, tIx);
_add(_start, buffRangeStart);
for (uint64_t i = 0, resultIx = tIx; i < threadNumberOfChecks; i++, resultIx++) {
if (_checksumDoubleSha256CheckUncompressed(checksum, d_hash, _start)) {
buffResult[resultIx] = true;
Expand All @@ -21,13 +19,11 @@ __global__ void kernelUncompressed(bool* buffResult, bool* buffCollectorWork, ui
}
__global__ void kernelCompressed(bool* buffResult, bool* buffCollectorWork, uint64_t* const __restrict__ buffRangeStart, const int threadNumberOfChecks, const uint32_t checksum) {
uint64_t _start[5];
uint64_t _startStride[5];
beu32 d_hash[8];
_load(_start, buffRangeStart);

int64_t tIx = (threadIdx.x + blockIdx.x * blockDim.x) * threadNumberOfChecks;
IMult(_startStride, _stride, tIx);
_add(_start, _startStride);
IMult(_start, _stride, tIx);
_add(_start, buffRangeStart);
for (uint64_t i = 0, resultIx = tIx; i < threadNumberOfChecks; i++, resultIx++) {
if (((_start[0] & 0xff00000000) >> 32) != 0x01) {
_add(_start, _stride);
Expand All @@ -42,13 +38,11 @@ __global__ void kernelCompressed(bool* buffResult, bool* buffCollectorWork, uint
}
__global__ void kernelUncompressed(bool* buffResult, bool* buffCollectorWork, uint64_t* const __restrict__ buffRangeStart, const int threadNumberOfChecks) {
uint64_t _start[5];
uint64_t _startStride[5];
beu32 d_hash[8];
_load(_start, buffRangeStart);

int64_t tIx = (threadIdx.x + blockIdx.x * blockDim.x) * threadNumberOfChecks;
IMult(_startStride, _stride, tIx);
_add(_start, _startStride);
IMult(_start, _stride, tIx);
_add(_start, buffRangeStart);
for (uint64_t i = 0, resultIx = tIx ; i < threadNumberOfChecks; i++, resultIx++) {
if (_checksumDoubleSha256CheckUncompressed(_start[0] & 0xffffffff, d_hash, _start)) {
buffResult[resultIx] = true;
Expand All @@ -59,13 +53,11 @@ __global__ void kernelUncompressed(bool* buffResult, bool* buffCollectorWork, ui
}
__global__ void kernelCompressed(bool* buffResult, bool* buffCollectorWork, uint64_t* const __restrict__ buffRangeStart, const int threadNumberOfChecks) {
uint64_t _start[5];
uint64_t _startStride[5];
beu32 d_hash[8];
_load(_start, buffRangeStart);

int64_t tIx = (threadIdx.x + blockIdx.x * blockDim.x) * threadNumberOfChecks;
IMult(_startStride, _stride, tIx);
_add(_start, _startStride);
IMult(_start, _stride, tIx);
_add(_start, buffRangeStart);
for (uint64_t i = 0, resultIx = tIx; i < threadNumberOfChecks; i++, resultIx++) {
if (((_start[0] & 0xff00000000) >> 32) != 0x01) {
_add(_start, _stride);
Expand Down
77 changes: 77 additions & 0 deletions WifSolverCuda/lib/base58.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,80 @@ bool b58enc(char* b58, size_t* b58sz, const void* data, size_t binsz)
free(buf);
return true;
}

bool b58tobin(void* bin, size_t* binszp, const char* b58, size_t b58sz)
{
size_t binsz = *binszp;
const unsigned char* b58u = (void*)b58;
unsigned char* binu = bin;
size_t outisz = (binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t);
b58_almostmaxint_t* outi;
outi = (int*)malloc(sizeof(b58_almostmaxint_t) * outisz);
b58_maxint_t t;
b58_almostmaxint_t c;
size_t i, j;
uint8_t bytesleft = binsz % sizeof(b58_almostmaxint_t);
b58_almostmaxint_t zeromask = bytesleft ? (b58_almostmaxint_mask << (bytesleft * 8)) : 0;
unsigned zerocount = 0;

if (!b58sz)
b58sz = strlen(b58);

for (i = 0; i < outisz; ++i) {
outi[i] = 0;
}

// Leading zeros, just count
for (i = 0; i < b58sz && b58u[i] == '1'; ++i)
++zerocount;

for (; i < b58sz; ++i)
{
if (b58u[i] & 0x80)
// High-bit set on invalid digit
return false;
if (b58digits_map[b58u[i]] == -1)
// Invalid base58 digit
return false;
c = (unsigned)b58digits_map[b58u[i]];
for (j = outisz; j--; )
{
t = ((b58_maxint_t)outi[j]) * 58 + c;
c = t >> b58_almostmaxint_bits;
outi[j] = t & b58_almostmaxint_mask;
}
if (c)
// Output number too big (carry to the next int32)
return false;
if (outi[0] & zeromask)
// Output number too big (last int32 filled too far)
return false;
}

j = 0;
if (bytesleft) {
for (i = bytesleft; i > 0; --i) {
*(binu++) = (outi[0] >> (8 * (i - 1))) & 0xff;
}
++j;
}

for (; j < outisz; ++j)
{
for (i = sizeof(*outi); i > 0; --i) {
*(binu++) = (outi[j] >> (8 * (i - 1))) & 0xff;
}
}

// Count canonical base58 byte count
binu = bin;
for (i = 0; i < binsz; ++i)
{
if (binu[i])
break;
--* binszp;
}
*binszp += zerocount;

return true;
}
1 change: 1 addition & 0 deletions WifSolverCuda/lib/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" {

extern bool (*b58_sha256_impl)(void*, const void*, size_t);
extern bool b58enc(char* b58, size_t* b58sz, const void* bin, size_t binsz);
extern bool b58tobin(void* bin, size_t* binsz, const char* b58, size_t b58sz);
#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 4 additions & 0 deletions WifSolverCuda/lib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ bool b58encode(char* b58, size_t* b58sz, const void* data, size_t binsz) {
return b58enc(b58, b58sz, data, binsz);
}

bool b58decode(unsigned char* bin, size_t* binszp, const char* b58, size_t b58sz) {
return b58tobin(bin, binszp, b58, b58sz);
}

std::string formatDouble(const char* formatStr, double value)
{
char buf[100] = { 0 };
Expand Down
1 change: 1 addition & 0 deletions WifSolverCuda/lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ void addressToBase58(char* rmd, char* dst);
std::string formatDouble(const char* formatStr, double value);

bool b58encode(char* b58, size_t* b58sz, const void* data, size_t binsz);
bool b58decode(unsigned char* bin, size_t* binszp, const char* b58, size_t b58sz);

#endif // CUSTOMUTILH
42 changes: 35 additions & 7 deletions WifSolverCuda/main.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void showHelp();
bool checkDevice();
void printConfig();
void printFooter();
void decodeWif();

cudaError_t processCuda();

Expand All @@ -41,6 +42,8 @@ string TARGET_ADDRESS = "";
Int CHECKSUM;
bool IS_CHECKSUM = false;

bool DECODE = false;
string WIF_TO_DECODE;

bool RESULT = false;
bool useCollector = false;
Expand All @@ -57,14 +60,20 @@ Secp256K1* secp;

int main(int argc, char** argv)
{
printf("WifSolver 0.4.1\n\n");
printf("WifSolver 0.4.2\n\n");

if (readArgs(argc, argv)) {
showHelp();
printFooter();
return 0;
}

if (DECODE) {
decodeWif();
printFooter();
return 0;
}

if (!checkDevice()) {
return -1;
}
Expand Down Expand Up @@ -256,7 +265,6 @@ Error:
return cudaStatus;
}


void processCandidate(Int &toTest) {
FILE* keys;
size_t dataLen = COMPRESSED ? 38 : 37;
Expand Down Expand Up @@ -302,8 +310,6 @@ void processCandidate(Int &toTest) {
}
}



void printConfig() {
printf("Range start: %s\n", RANGE_START.GetBase16().c_str());
printf("Range end : %s\n", RANGE_END.GetBase16().c_str());
Expand All @@ -328,8 +334,7 @@ void printConfig() {

void printFooter() {
printf("------------------------\n");
printf("source: https://github.com/PawelGorny/WifSolverCuda\n");
printf("If you found this program useful, please donate - bitcoin:34dEiyShGJcnGAg2jWhcoDDRxpennSZxg8\n");
printf("source: https://github.com/PawelGorny/WifSolverCuda\n\n");
}

bool checkDevice() {
Expand Down Expand Up @@ -361,7 +366,8 @@ bool checkDevice() {
void showHelp() {
printf("WifSolverCuda [-d deviceId] [-b NbBlocks] [-t NbThreads] [-s NbThreadChecks]\n");
printf(" [-fresultp reportFile] [-fresult resultFile] [-fstatus statusFile] [-a targetAddress]\n");
printf(" -stride hexKeyStride -rangeStart hexKeyStart [-rangeEnd hexKeyEnd] [-checksum hexChecksum] \n\n");
printf(" -stride hexKeyStride -rangeStart hexKeyStart [-rangeEnd hexKeyEnd] [-checksum hexChecksum] \n");
printf(" [-decode wifToDecode] \n\n");
printf("-rangeStart hexKeyStart: decoded initial key with compression flag and checksum \n");
printf("-rangeEnd hexKeyEnd: decoded end key with compression flag and checksum \n");
printf("-checksum hexChecksum: decoded checksum, cannot be modified with a stride \n");
Expand All @@ -376,6 +382,8 @@ void showHelp() {
printf("-b NbBlocks: default processorCount * 8\n");
printf("-t NbThreads: default deviceMax/8 * 5\n");
printf("-s NbThreadChecks: default 3364\n");
printf("\n");
printf("-decode wifToDecode: decodes given WIF\n");
}

bool readArgs(int argc, char** argv) {
Expand All @@ -386,6 +394,12 @@ bool readArgs(int argc, char** argv) {
while (a < argc) {
if (strcmp(argv[a], "-h") == 0) {
return true;
}else
if (strcmp(argv[a], "-decode") == 0) {
a++;
WIF_TO_DECODE = string(argv[a]);
DECODE = true;
return false;
}
else if (strcmp(argv[a], "-d") == 0) {
a++;
Expand Down Expand Up @@ -474,3 +488,17 @@ bool readArgs(int argc, char** argv) {
}
return false;
}

void decodeWif() {
const char* base58 = WIF_TO_DECODE.c_str();
size_t base58Length = WIF_TO_DECODE.size();
size_t keybuflen = base58Length == 52 ? 38 : 37;
unsigned char * keybuf = new unsigned char[keybuflen];
b58decode(keybuf, &keybuflen, base58, base58Length);
printf("WIF: %s\n", WIF_TO_DECODE.c_str());
printf("Decoded:\n");
for (int i = 0; i < keybuflen; i++) {
printf("%.2x", keybuf[i]);
}
printf("\n\n");
}

0 comments on commit 87c62b3

Please sign in to comment.