forked from samr7/vanitygen
-
Notifications
You must be signed in to change notification settings - Fork 8
/
oclvanitygen.c
490 lines (460 loc) · 12.5 KB
/
oclvanitygen.c
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
/*
* Vanitygen, vanity bitcoin address generator
* Copyright (C) 2011 <[email protected]>
*
* Vanitygen is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Vanitygen 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Vanitygen. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/rand.h>
#include "oclengine.h"
#include "pattern.h"
#include "util.h"
const char *version = VANITYGEN_VERSION;
const int debug = 0;
void usage(const char *name) {
// clang-format off
fprintf(stderr,
COLOR44
"oclVanitygen Cash %s" COLOR0 " (" OPENSSL_VERSION_TEXT ")\n"
"Usage: %s [-vcqk1NTS] [-d <device>] [-f <filename>|-] [<pattern>...]\n"
"Generates a Bitcoin Cash receiving address matching <pattern>, and outputs\n"
"the address and associated private key. The private key may be stored in a\n"
"safe location or imported into a wallet client to spend any balance\n"
"received on the address.\n"
"By default, <pattern> is interpreted as an exact prefix.\n"
"By default, if no device is specified, and the system has exactly one OpenCL\n"
"device, it will be selected automatically, otherwise if the system has\n"
"multiple OpenCL devices and no device is specified, an error will be\n"
"reported. To use multiple devices simultaneously, specify the -D option for\n"
"each device.\n"
"\n"
"Options:\n"
"-v Verbose output\n"
"-c Print conditions for a valid address prefix\n"
" (i.e. alphabet) and quit\n"
"-q Quiet output\n"
"-k Keep pattern and continue search after finding a match\n"
"-1 Stop after first match\n"
"-T Generate Bitcoin Cash testnet address\n"
"-P <pubkey> Specify base public key for piecewise key generation\n"
"-l <hex> Choose prefix for the hex format of private key (EXPERTS)\n"
"-p <platform> Select OpenCL platform\n"
"-d <device> Select OpenCL device\n"
"-D <devstr> Use OpenCL device, identified by device string\n"
" Form: <platform>:<devicenumber>[,<options>]\n"
" Example: 0:0,grid=1024x1024\n"
"-S Safe mode, disable OpenCL loop unrolling optimizations\n"
"-w <worksize> Set work items per thread in a work unit\n"
"-t <threads> Set target thread count per multiprocessor\n"
"-g <x>x<y> Set grid size\n"
"-b <invsize> Set modular inverse ops per thread\n"
"-V Enable kernel/OpenCL/hardware verification (SLOW)\n"
"-G <file> Rangegen pattern file\n"
"-f <file> File containing list of patterns, one per line\n"
" (Use \"-\" as the file name for stdin)\n"
"-o <file> Write pattern matches to <file> in TSV format (readable)\n"
"-O <file> Write pattern matches to <file> in CSV format\n"
" (importable e.g. Excel)\n"
"-s <file> Seed random number generator from <file>\n",
version, name);
// clang-format on
}
#define MAX_DEVS 32
#define MAX_FILE 4
int main(int argc, char **argv) {
int testnet = 0;
int addrtype = 0;
int privtype = 128;
int opt;
int platformidx = -1, deviceidx = -1;
char *seedfile = NULL;
char **patterns, *pend;
int verbose = 1;
int npatterns = 0;
int nthreads = 0;
int worksize = 0;
int nrows = 0, ncols = 0;
int invsize = 0;
int remove_on_match = 1;
int only_one = 0;
int verify_mode = 0;
int safe_mode = 0;
vg_context_t *vcp = NULL;
vg_ocl_context_t *vocp = NULL;
EC_POINT *pubkey_base = NULL;
const char *result_file = NULL;
const char *result_file_csv = NULL;
char *devstrs[MAX_DEVS];
int ndevstrs = 0;
int opened = 0;
char privkey_prefix[32];
unsigned int privkey_prefix_length = 0;
FILE *pattfp[MAX_FILE], *fp;
int npattfp = 0;
int pattstdin = 0;
const char **rangef = (const char **) malloc(4 * sizeof(const char *));
int range_alloc = 4;
int nrange = 0;
int i;
while ((opt = getopt(argc, argv,
"vcqk1Tp:P:l:d:w:t:g:b:VSh?f:G:o:O:s:D:")) != -1) {
switch (opt) {
case 'v': verbose = 2; break;
case 'c':
fprintf(stderr, "%s\n",
"Conditions:\n"
"• The alphabet is 023456789acdefghjklmnpqrstuvwxyz\n"
"• The first character must be 'q' for standard addresses or 'p' for P2SH\n"
"• The second character must be either 'p', 'q', 'r' or 'z'.\n"
"• The prefix must be lowercase and typed without the CashAddr "
"prefix (e.g. no \"bitcoincash:\")\n");
return 0;
case 'q': verbose = 0; break;
case 'k': remove_on_match = 0; break;
case '1': only_one = 1; break;
case 'T':
privtype = 239;
testnet = 1;
break;
case 'l':
if (privkey_prefix_length != 0) {
fprintf(stderr,
"Hex privkey prefix has been set "
"before.\n");
return 1;
}
privkey_prefix_length = strlen(optarg);
if (privkey_prefix_length % 2) {
fprintf(stderr,
"Hex privkey prefix length must be a "
"multiple of 2.\n");
return 1;
}
privkey_prefix_length /= 2;
if (privkey_prefix_length > 15) {
fprintf(stderr,
"At most 30 characters, please.\n");
return 1;
}
if (privkey_prefix_length > 24) {
fprintf(stderr,
"Pkey prefix is longer than 48 "
"characters, will try, "
"but no promise.\n"
"Pkey prefix with at most 48 "
"characters is advised for security, "
"or "
"a private key may not be found at "
"all.\n");
}
for (unsigned int i = 0;
i < (unsigned int) privkey_prefix_length; i++) {
int value; // Can't sscanf directly to char
// array because of overlapping on
// Win32
sscanf(&optarg[i * 2], "%2x",
(unsigned int *) &value);
privkey_prefix[i] = value;
}
break;
case 'p': platformidx = atoi(optarg); break;
case 'd': deviceidx = atoi(optarg); break;
case 'w':
worksize = atoi(optarg);
if (worksize == 0) {
fprintf(stderr, "Invalid work size '%s'\n",
optarg);
return 1;
}
break;
case 't':
nthreads = atoi(optarg);
if (nthreads == 0) {
fprintf(stderr, "Invalid thread count '%s'\n",
optarg);
return 1;
}
break;
case 'g':
nrows = 0;
ncols = strtol(optarg, &pend, 0);
if (pend && *pend == 'x') {
nrows = strtol(pend + 1, NULL, 0);
}
if (!nrows || !ncols) {
fprintf(stderr, "Invalid grid size '%s'\n",
optarg);
return 1;
}
break;
case 'b':
invsize = atoi(optarg);
if (!invsize) {
fprintf(stderr,
"Invalid modular inverse size '%s'\n",
optarg);
return 1;
}
if (invsize & (invsize - 1)) {
fprintf(stderr,
"Modular inverse size must be "
"a power of 2\n");
return 1;
}
break;
case 'V': verify_mode = 1; break;
case 'S': safe_mode = 1; break;
case 'D':
if (ndevstrs >= MAX_DEVS) {
fprintf(stderr,
"Too many OpenCL devices (limit %d)\n",
MAX_DEVS);
return 1;
}
devstrs[ndevstrs++] = optarg;
break;
case 'P': {
if (pubkey_base != NULL) {
fprintf(stderr,
"Multiple base pubkeys specified\n");
return 1;
}
EC_KEY *pkey = vg_exec_context_new_key();
pubkey_base = EC_POINT_hex2point(
EC_KEY_get0_group(pkey), optarg, NULL, NULL);
EC_KEY_free(pkey);
if (pubkey_base == NULL) {
fprintf(stderr, "Invalid base pubkey\n");
return 1;
}
break;
}
case 'f':
if (npattfp >= MAX_FILE) {
fprintf(stderr,
"Too many input files specified\n");
return 1;
}
if (!strcmp(optarg, "-")) {
if (pattstdin) {
fprintf(stderr,
"ERROR: stdin "
"specified multiple times\n");
return 1;
}
fp = stdin;
} else {
fp = fopen(optarg, "r");
if (!fp) {
fprintf(stderr,
"Could not open %s: %s\n",
optarg, strerror(errno));
return 1;
}
}
pattfp[npattfp] = fp;
npattfp++;
break;
case 'G':
if (nrange == range_alloc) {
range_alloc *= 2;
rangef = (const char **) realloc(rangef,
range_alloc * sizeof(const char *));
}
rangef[nrange++] = optarg;
break;
case 'o':
if (result_file) {
fprintf(stderr,
"Multiple TSV output files "
"specified\n");
return 1;
}
result_file = optarg;
break;
case 'O':
if (result_file_csv) {
fprintf(stderr,
"Multiple CSV output files "
"specified\n");
return 1;
}
result_file_csv = optarg;
break;
case 's':
if (seedfile != NULL) {
fprintf(stderr,
"Multiple RNG seeds specified\n");
return 1;
}
seedfile = optarg;
break;
default: usage(argv[0]); return 1;
}
}
#if !defined(_WIN32)
if (!seedfile) {
struct stat st1;
if (stat("/dev/urandom", &st1) == 0) {
seedfile = (char *) "/dev/urandom";
}
}
#endif
if (seedfile) {
opt = -1;
#if !defined(_WIN32)
{
struct stat st;
if (!stat(seedfile, &st) &&
(st.st_mode & (S_IFBLK | S_IFCHR))) {
opt = 32;
}
}
#endif
opt = RAND_load_file(seedfile, opt);
if (!opt) {
fprintf(stderr, "Could not load RNG seed %s\n", optarg);
return 1;
}
if (verbose > 0 && strcmp(seedfile, (char *) "/dev/urandom")) {
fprintf(stderr, "Read %d bytes from RNG seed file\n",
opt);
}
}
vcp = vg_prefix_context_new(addrtype, privtype, testnet);
if (result_file) {
FILE *fp = fopen(result_file, "a");
if (!fp) {
fprintf(stderr,
"ERROR: could not open TSV result file: %s\n",
strerror(errno));
return 1;
} else {
fprintf(fp, "Pattern\tAddress\t");
if (pubkey_base == NULL)
fprintf(fp, "Private Key\n");
else
fprintf(fp, "Private Key Part\n");
fclose(fp);
}
}
if (result_file_csv) {
FILE *fp = fopen(result_file_csv, "a");
if (!fp) {
fprintf(stderr,
"ERROR: could not open CSV result file: %s\n",
strerror(errno));
return 1;
} else {
fprintf(fp, "Pattern\tAddress\t");
if (pubkey_base == NULL)
fprintf(fp, "Private Key\n");
else
fprintf(fp, "Private Key Part\n");
fclose(fp);
}
}
vcp->vc_verbose = verbose;
vcp->vc_result_file = result_file;
vcp->vc_result_file_csv = result_file_csv;
vcp->vc_remove_on_match = remove_on_match;
vcp->vc_only_one = only_one;
vcp->vc_addrtype = addrtype;
vcp->vc_pubkey_base = pubkey_base;
memcpy(vcp->vc_privkey_prefix, privkey_prefix, privkey_prefix_length);
vcp->vc_privkey_prefix_length = privkey_prefix_length;
vcp->vc_output_match = vg_output_match_console;
vcp->vc_output_timing = vg_output_timing_console;
if (!npattfp && !nrange) {
if (optind >= argc) {
usage(argv[0]);
return 1;
}
patterns = &argv[optind];
npatterns = argc - optind;
if (!vg_context_add_patterns(
vcp, (const char **) patterns, npatterns))
return 1;
}
for (i = 0; i < npattfp; i++) {
fp = pattfp[i];
if (!vg_read_file(fp, &patterns, &npatterns)) {
fprintf(stderr, "Failed to load pattern file\n");
return 1;
}
if (fp != stdin) fclose(fp);
if (!vg_context_add_patterns(
vcp, (const char **) patterns, npatterns))
return 1;
}
for (i = 0; i < nrange; i++) {
fp = fopen(rangef[i], "r");
if (!fp) {
fprintf(stderr, "Could not open %s: %s\n", rangef[i],
strerror(errno));
return 1;
}
char **range_list = (char **) malloc(8 * sizeof(char *));
int range_count = 0;
int range_alloc = 8;
if (!vg_read_range_file(fp, (const char ***) &range_list,
&range_count, &range_alloc))
return 1;
fclose(fp);
int r = vg_prefix_context_add_ranges(vcp, rangef[i],
(const char **) range_list, range_count);
for (int k = 0; k < range_count; k++) {
free(range_list[k]);
}
free(range_list);
if (!r) return 1;
}
if (!vcp->vc_npatterns) {
fprintf(stderr, "No patterns to search\n");
return 1;
}
if (ndevstrs) {
for (opt = 0; opt < ndevstrs; opt++) {
vocp = vg_ocl_context_new_from_devstr(
vcp, devstrs[opt], safe_mode, verify_mode);
if (!vocp) {
fprintf(stderr,
"Could not open device '%s', "
"ignoring\n",
devstrs[opt]);
} else {
opened++;
}
}
} else {
vocp = vg_ocl_context_new(vcp, platformidx, deviceidx,
safe_mode, verify_mode, worksize, nthreads, nrows,
ncols, invsize);
if (vocp) opened++;
}
if (!opened) {
vg_ocl_enumerate_devices();
return 1;
}
opt = vg_context_start_threads(vcp);
if (opt) return 1;
vg_context_wait_for_completion(vcp);
vg_ocl_context_free(vocp);
return 0;
}