forked from JeanLucPons/VanitySearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
541 lines (441 loc) · 14.9 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
* This file is part of the VanitySearch distribution (https://github.com/JeanLucPons/VanitySearch).
* Copyright (c) 2019 Jean Luc PONS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Timer.h"
#include "Vanity.h"
#include "SECP256k1.h"
#include <fstream>
#include <string>
#include <string.h>
#include <stdexcept>
#include "hash/sha512.h"
#include "hash/sha256.h"
#define RELEASE "1.15"
using namespace std;
// ------------------------------------------------------------------------------------------
void printUsage() {
printf("VanitySeacrh [-check] [-v] [-u] [-b] [-c] [-gpu] [-stop] [-i inputfile]\n");
printf(" [-gpuId gpuId1[,gpuId2,...]] [-g gridSize1[,gridSize2,...]]\n");
printf(" [-o outputfile] [-m maxFound] [-ps seed] [-s seed] [-t nbThread]\n");
printf(" [-nosse] [-r rekey] [-check] [-kp] [-sp startPubKey]\n");
printf(" [-rp privkey partialkeyfile] [prefix]\n\n");
printf(" prefix: prefix to search (Can contains wildcard '?' or '*')\n");
printf(" -v: Print version\n");
printf(" -u: Search uncompressed addresses\n");
printf(" -b: Search both uncompressed or compressed addresses\n");
printf(" -c: Case unsensitive search\n");
printf(" -gpu: Enable gpu calculation\n");
printf(" -stop: Stop when all prefixes are found\n");
printf(" -i inputfile: Get list of prefixes to search from specified file\n");
printf(" -o outputfile: Output results to the specified file\n");
printf(" -gpu gpuId1,gpuId2,...: List of GPU(s) to use, default is 0\n");
printf(" -g gridSize1,gridSize2,...: Specify GPU(s) kernel gridsize, default is 8*(MP number)\n");
printf(" -m: Specify maximun number of prefixes found by each kernel call\n");
printf(" -s seed: Specify a seed for the base key, default is random\n");
printf(" -ps seed: Specify a seed concatened with a crypto secure random seed\n");
printf(" -t threadNumber: Specify number of CPU thread, default is number of core\n");
printf(" -nosse: Disable SSE hash function\n");
printf(" -l: List cuda enabled devices\n");
printf(" -check: Check CPU and GPU kernel vs CPU\n");
printf(" -kp: Generate key pair\n");
printf(" -rp privkey partialkeyfile: Reconstruct final private key(s) from partial key(s) info.\n");
printf(" -sp startPubKey: Start the search with a pubKey (for private key splitting)\n");
printf(" -r rekey: Rekey interval in MegaKey, default is disabled\n");
exit(-1);
}
// ------------------------------------------------------------------------------------------
int getInt(string name,char *v) {
int r;
try {
r = std::stoi(string(v));
} catch(std::invalid_argument&) {
printf("Invalid %s argument, number expected\n",name.c_str());
exit(-1);
}
return r;
}
// ------------------------------------------------------------------------------------------
void getInts(string name,vector<int> &tokens, const string &text, char sep) {
size_t start = 0, end = 0;
tokens.clear();
int item;
try {
while ((end = text.find(sep, start)) != string::npos) {
item = std::stoi(text.substr(start, end - start));
tokens.push_back(item);
start = end + 1;
}
item = std::stoi(text.substr(start));
tokens.push_back(item);
} catch(std::invalid_argument &) {
printf("Invalid %s argument, number expected\n",name.c_str());
exit(-1);
}
}
// ------------------------------------------------------------------------------------------
void parseFile(string fileName, vector<string> &lines) {
// Get file size
FILE *fp = fopen(fileName.c_str(), "rb");
if (fp == NULL) {
printf("Error: Cannot open %s %s\n", fileName.c_str(), strerror(errno));
exit(-1);
}
fseek(fp, 0L, SEEK_END);
size_t sz = ftell(fp);
size_t nbAddr = sz / 33; /* Upper approximation */
bool loaddingProgress = sz > 100000;
fclose(fp);
// Parse file
int nbLine = 0;
string line;
ifstream inFile(fileName);
lines.reserve(nbAddr);
while (getline(inFile, line)) {
// Remove ending \r\n
int l = (int)line.length() - 1;
while (l >= 0 && isspace(line.at(l))) {
line.pop_back();
l--;
}
if (line.length() > 0) {
lines.push_back(line);
nbLine++;
if (loaddingProgress) {
if ((nbLine % 50000) == 0)
printf("[Loading input file %5.1f%%]\r", ((double)nbLine*100.0) / ((double)(nbAddr)*33.0 / 34.0));
}
}
}
if (loaddingProgress)
printf("[Loading input file 100.0%%]\n");
}
// ------------------------------------------------------------------------------------------
void generateKeyPair(Secp256K1 *secp, string seed, int searchMode,bool paranoiacSeed) {
if (seed.length() < 8) {
printf("Error: Use a seed of at leats 8 characters to generate a key pair\n");
printf("Ex: VanitySearch -s \"A Strong Password\" -kp\n");
exit(0);
}
if(paranoiacSeed)
seed = seed + Timer::getSeed(32);
if (searchMode == SEARCH_BOTH) {
printf("Error: Use compressed or uncompressed to generate a key pair\n");
exit(0);
}
bool compressed = (searchMode == SEARCH_COMPRESSED);
string salt = "VanitySearch";
unsigned char hseed[64];
pbkdf2_hmac_sha512(hseed, 64, (const uint8_t *)seed.c_str(), seed.length(),
(const uint8_t *)salt.c_str(), salt.length(),
2048);
Int privKey;
privKey.SetInt32(0);
sha256(hseed, 64, (unsigned char *)privKey.bits64);
Point p = secp->ComputePublicKey(&privKey);
printf("Priv : %s\n", secp->GetPrivAddress(compressed,privKey).c_str());
printf("Pub : %s\n", secp->GetPublicKeyHex(compressed,p).c_str());
}
// ------------------------------------------------------------------------------------------
void outputAdd(string outputFile, int addrType, string addr, string pAddr, string pAddrHex) {
FILE *f = stdout;
bool needToClose = false;
if (outputFile.length() > 0) {
f = fopen(outputFile.c_str(), "a");
if (f == NULL) {
printf("Cannot open %s for writing\n", outputFile.c_str());
f = stdout;
} else {
needToClose = true;
}
}
fprintf(f, "\nPub Addr: %s\n", addr.c_str());
switch (addrType) {
case P2PKH:
fprintf(f, "Priv (WIF): p2pkh:%s\n", pAddr.c_str());
break;
case P2SH:
fprintf(f, "Priv (WIF): p2wpkh-p2sh:%s\n", pAddr.c_str());
break;
case BECH32:
fprintf(f, "Priv (WIF): p2wpkh:%s\n", pAddr.c_str());
break;
}
fprintf(f, "Priv (HEX): 0x%s\n", pAddrHex.c_str());
if (needToClose)
fclose(f);
}
// ------------------------------------------------------------------------------------------
#define CHECK_ADDR() \
fullPriv.ModAddK1order(&e, &partialPrivKey); \
p = secp->ComputePublicKey(&fullPriv); \
cAddr = secp->GetAddress(addrType, compressed, p); \
if (cAddr == addr) { \
found = true; \
string pAddr = secp->GetPrivAddress(compressed, fullPriv); \
string pAddrHex = fullPriv.GetBase16(); \
outputAdd(outputFile, addrType, addr, pAddr, pAddrHex); \
}
void reconstructAdd(Secp256K1 *secp, string fileName, string outputFile, string privAddr) {
bool compressed;
int addrType;
Int lambda;
Int lambda2;
lambda.SetBase16("5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72");
lambda2.SetBase16("ac9c52b33fa3cf1f5ad9e3fd77ed9ba4a880b9fc8ec739c2e0cfc810b51283ce");
Int privKey = secp->DecodePrivateKey((char *)privAddr.c_str(),&compressed);
if(privKey.IsNegative())
exit(-1);
vector<string> lines;
parseFile(fileName,lines);
for (int i = 0; i < (int)lines.size(); i+=2) {
string addr;
string partialPrivAddr;
if (lines[i].substr(0, 10) == "Pub Addr: ") {
addr = lines[i].substr(10);
switch (addr.data()[0]) {
case '1':
addrType = P2PKH; break;
case '3':
addrType = P2SH; break;
case 'b':
case 'B':
addrType = BECH32; break;
default:
printf("Invalid partialkey info file at line %d\n", i);
printf("%s Address format not supported\n", addr.c_str());
continue;
}
} else {
printf("Invalid partialkey info file at line %d (\"Pub Addr: \" expected)\n",i);
exit(-1);
}
if (lines[i+1].substr(0, 13) == "PartialPriv: ") {
partialPrivAddr = lines[i+1].substr(13);
} else {
printf("Invalid partialkey info file at line %d (\"PartialPriv: \" expected)\n", i);
exit(-1);
}
bool partialMode;
Int partialPrivKey = secp->DecodePrivateKey((char *)partialPrivAddr.c_str(), &partialMode);
if (privKey.IsNegative()) {
printf("Invalid partialkey info file at line %d\n", i);
exit(-1);
}
if (partialMode != compressed) {
printf("Warning, Invalid partialkey at line %d (Wrong compression mode, ignoring key)\n", i);
continue;
} else {
// Reconstruct the address
Int fullPriv;
Point p;
Int e;
string cAddr;
bool found = false;
// No sym, no endo
e.Set(&privKey);
CHECK_ADDR();
// No sym, endo 1
e.Set(&privKey);
e.ModMulK1order(&lambda);
CHECK_ADDR();
// No sym, endo 2
e.Set(&privKey);
e.ModMulK1order(&lambda2);
CHECK_ADDR();
// sym, no endo
e.Set(&privKey);
e.Neg();
e.Add(&secp->order);
CHECK_ADDR();
// sym, endo 1
e.Set(&privKey);
e.ModMulK1order(&lambda);
e.Neg();
e.Add(&secp->order);
CHECK_ADDR();
// sym, endo 2
e.Set(&privKey);
e.ModMulK1order(&lambda2);
e.Neg();
e.Add(&secp->order);
CHECK_ADDR();
if (!found) {
printf("Unable to reconstruct final key from partialkey line %d\n Addr: %s\n PartKey: %s\n",
i, addr.c_str(),partialPrivAddr.c_str());
}
}
}
}
// ------------------------------------------------------------------------------------------
int main(int argc, char* argv[]) {
// Global Init
Timer::Init();
rseed((unsigned long)time(NULL));
// Init SecpK1
Secp256K1 *secp = new Secp256K1();
secp->Init();
// Browse arguments
if (argc < 2) {
printf("Not enough argument\n");
printUsage();
}
int a = 1;
bool gpuEnable = false;
bool stop = false;
int searchMode = SEARCH_COMPRESSED;
vector<int> gpuId = {0};
vector<int> gridSize = {-1};
string seed = "";
vector<string> prefix;
string outputFile = "";
int nbCPUThread = Timer::getCoreNumber();
bool tSpecified = false;
bool sse = true;
uint32_t maxFound = 65536;
uint64_t rekey = 0;
Point startPuKey;
startPuKey.Clear();
bool startPubKeyCompressed;
bool caseSensitive = true;
bool paranoiacSeed = false;
while (a < argc) {
if (strcmp(argv[a], "-gpu")==0) {
gpuEnable = true;
a++;
} else if (strcmp(argv[a], "-gpuId")==0) {
a++;
getInts("gpuId",gpuId,string(argv[a]),',');
a++;
} else if (strcmp(argv[a], "-stop") == 0) {
stop = true;
a++;
} else if (strcmp(argv[a], "-c") == 0) {
caseSensitive = false;
a++;
} else if (strcmp(argv[a], "-v") == 0) {
printf("%s\n",RELEASE);
exit(0);
} else if (strcmp(argv[a], "-check") == 0) {
Int::Check();
secp->Check();
#ifdef WITHGPU
GPUEngine g(gridSize[0],gpuId[0],maxFound,false);
g.SetSearchMode(searchMode);
g.Check(secp);
#else
printf("GPU code not compiled, use -DWITHGPU when compiling.\n");
#endif
exit(0);
} else if (strcmp(argv[a], "-l") == 0) {
#ifdef WITHGPU
GPUEngine::PrintCudaInfo();
#else
printf("GPU code not compiled, use -DWITHGPU when compiling.\n");
#endif
exit(0);
} else if (strcmp(argv[a], "-kp") == 0) {
generateKeyPair(secp,seed,searchMode,paranoiacSeed);
exit(0);
} else if (strcmp(argv[a], "-sp") == 0) {
a++;
string pub = string(argv[a]);
startPuKey = secp->ParsePublicKeyHex(pub, startPubKeyCompressed);
a++;
} else if (strcmp(argv[a], "-rp") == 0) {
a++;
string priv = string(argv[a]);
a++;
string file = string(argv[a]);
a++;
reconstructAdd(secp,file,outputFile,priv);
exit(0);
} else if (strcmp(argv[a], "-u") == 0) {
searchMode = SEARCH_UNCOMPRESSED;
a++;
} else if (strcmp(argv[a], "-b") == 0) {
searchMode = SEARCH_BOTH;
a++;
} else if (strcmp(argv[a], "-nosse") == 0) {
sse = false;
a++;
} else if (strcmp(argv[a], "-g") == 0) {
a++;
getInts("gridSize",gridSize,string(argv[a]),',');
a++;
} else if (strcmp(argv[a], "-s") == 0) {
a++;
seed = string(argv[a]);
a++;
} else if (strcmp(argv[a], "-ps") == 0) {
a++;
seed = string(argv[a]);
paranoiacSeed = true;
a++;
} else if (strcmp(argv[a], "-o") == 0) {
a++;
outputFile = string(argv[a]);
a++;
} else if (strcmp(argv[a], "-i") == 0) {
a++;
parseFile(string(argv[a]),prefix);
a++;
} else if (strcmp(argv[a], "-t") == 0) {
a++;
nbCPUThread = getInt("nbCPUThread",argv[a]);
a++;
tSpecified = true;
} else if (strcmp(argv[a], "-m") == 0) {
a++;
maxFound = getInt("maxFound", argv[a]);
a++;
} else if (strcmp(argv[a], "-r") == 0) {
a++;
rekey = (uint64_t)getInt("rekey", argv[a]);
a++;
} else if (a == argc - 1) {
prefix.push_back(string(argv[a]));
a++;
} else {
printf("Unexpected %s argument\n",argv[a]);
printUsage();
}
}
printf("VanitySearch v" RELEASE "\n");
if(gpuId.size()!=gridSize.size()) {
if(gridSize.size()==1 && gridSize[0]==-1) {
gridSize.clear();
for(int i=0;i<gpuId.size();i++)
gridSize.push_back(-1);
} else {
printf("Invalid gridSize or gpuId argument, must have same size\n");
printUsage();
}
}
// Let one CPU core free per gpu is gpu is enabled
// It will avoid to hang the system
if( !tSpecified && nbCPUThread>1 && gpuEnable)
nbCPUThread-=(int)gpuId.size();
if(nbCPUThread<0)
nbCPUThread = 0;
// If a starting public key is specified, force the search mode according to the key
if (!startPuKey.isZero()) {
searchMode = (startPubKeyCompressed)?SEARCH_COMPRESSED:SEARCH_UNCOMPRESSED;
}
VanitySearch *v = new VanitySearch(secp, prefix, seed, searchMode, gpuEnable, stop, outputFile, sse,
maxFound, rekey, caseSensitive, startPuKey, paranoiacSeed);
v->Search(nbCPUThread,gpuId,gridSize);
return 0;
}