Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
samr7
GitHub Repository: samr7/vanitygen
Path: blob/master/pattern.c
239 views
1
/*
2
* Vanitygen, vanity bitcoin address generator
3
* Copyright (C) 2011 <[email protected]>
4
*
5
* Vanitygen is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU Affero General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* any later version.
9
*
10
* Vanitygen is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Affero General Public License for more details.
14
*
15
* You should have received a copy of the GNU Affero General Public License
16
* along with Vanitygen. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include <stdio.h>
20
#include <string.h>
21
#include <math.h>
22
#include <assert.h>
23
24
#include <pthread.h>
25
26
#include <openssl/sha.h>
27
#include <openssl/ripemd.h>
28
#include <openssl/bn.h>
29
#include <openssl/ec.h>
30
#include <openssl/obj_mac.h>
31
32
#include <pcre.h>
33
34
#include "pattern.h"
35
#include "util.h"
36
#include "avl.h"
37
38
39
/*
40
* Common code for execution helper
41
*/
42
43
EC_KEY *
44
vg_exec_context_new_key(void)
45
{
46
return EC_KEY_new_by_curve_name(NID_secp256k1);
47
}
48
49
/*
50
* Thread synchronization helpers
51
*/
52
53
static pthread_mutex_t vg_thread_lock = PTHREAD_MUTEX_INITIALIZER;
54
static pthread_cond_t vg_thread_rdcond = PTHREAD_COND_INITIALIZER;
55
static pthread_cond_t vg_thread_wrcond = PTHREAD_COND_INITIALIZER;
56
static pthread_cond_t vg_thread_upcond = PTHREAD_COND_INITIALIZER;
57
58
static void
59
__vg_exec_context_yield(vg_exec_context_t *vxcp)
60
{
61
vxcp->vxc_lockmode = 0;
62
while (vxcp->vxc_vc->vc_thread_excl) {
63
if (vxcp->vxc_stop) {
64
assert(vxcp->vxc_vc->vc_thread_excl);
65
vxcp->vxc_stop = 0;
66
pthread_cond_signal(&vg_thread_upcond);
67
}
68
pthread_cond_wait(&vg_thread_rdcond, &vg_thread_lock);
69
}
70
assert(!vxcp->vxc_stop);
71
assert(!vxcp->vxc_lockmode);
72
vxcp->vxc_lockmode = 1;
73
}
74
75
int
76
vg_exec_context_upgrade_lock(vg_exec_context_t *vxcp)
77
{
78
vg_exec_context_t *tp;
79
vg_context_t *vcp;
80
81
if (vxcp->vxc_lockmode == 2)
82
return 0;
83
84
pthread_mutex_lock(&vg_thread_lock);
85
86
assert(vxcp->vxc_lockmode == 1);
87
vxcp->vxc_lockmode = 0;
88
vcp = vxcp->vxc_vc;
89
90
if (vcp->vc_thread_excl++) {
91
assert(vxcp->vxc_stop);
92
vxcp->vxc_stop = 0;
93
pthread_cond_signal(&vg_thread_upcond);
94
pthread_cond_wait(&vg_thread_wrcond, &vg_thread_lock);
95
96
for (tp = vcp->vc_threads; tp != NULL; tp = tp->vxc_next) {
97
assert(!tp->vxc_lockmode);
98
assert(!tp->vxc_stop);
99
}
100
101
} else {
102
for (tp = vcp->vc_threads; tp != NULL; tp = tp->vxc_next) {
103
if (tp->vxc_lockmode) {
104
assert(tp->vxc_lockmode != 2);
105
tp->vxc_stop = 1;
106
}
107
}
108
109
do {
110
for (tp = vcp->vc_threads;
111
tp != NULL;
112
tp = tp->vxc_next) {
113
if (tp->vxc_lockmode) {
114
assert(tp->vxc_lockmode != 2);
115
pthread_cond_wait(&vg_thread_upcond,
116
&vg_thread_lock);
117
break;
118
}
119
}
120
} while (tp);
121
}
122
123
vxcp->vxc_lockmode = 2;
124
pthread_mutex_unlock(&vg_thread_lock);
125
return 1;
126
}
127
128
void
129
vg_exec_context_downgrade_lock(vg_exec_context_t *vxcp)
130
{
131
pthread_mutex_lock(&vg_thread_lock);
132
assert(vxcp->vxc_lockmode == 2);
133
assert(!vxcp->vxc_stop);
134
if (!--vxcp->vxc_vc->vc_thread_excl) {
135
vxcp->vxc_lockmode = 1;
136
pthread_cond_broadcast(&vg_thread_rdcond);
137
pthread_mutex_unlock(&vg_thread_lock);
138
return;
139
}
140
pthread_cond_signal(&vg_thread_wrcond);
141
__vg_exec_context_yield(vxcp);
142
pthread_mutex_unlock(&vg_thread_lock);
143
}
144
145
int
146
vg_exec_context_init(vg_context_t *vcp, vg_exec_context_t *vxcp)
147
{
148
pthread_mutex_lock(&vg_thread_lock);
149
150
memset(vxcp, 0, sizeof(*vxcp));
151
152
vxcp->vxc_vc = vcp;
153
154
BN_init(&vxcp->vxc_bntarg);
155
BN_init(&vxcp->vxc_bnbase);
156
BN_init(&vxcp->vxc_bntmp);
157
BN_init(&vxcp->vxc_bntmp2);
158
159
BN_set_word(&vxcp->vxc_bnbase, 58);
160
161
vxcp->vxc_bnctx = BN_CTX_new();
162
assert(vxcp->vxc_bnctx);
163
vxcp->vxc_key = vg_exec_context_new_key();
164
assert(vxcp->vxc_key);
165
EC_KEY_precompute_mult(vxcp->vxc_key, vxcp->vxc_bnctx);
166
167
vxcp->vxc_lockmode = 0;
168
vxcp->vxc_stop = 0;
169
170
vxcp->vxc_next = vcp->vc_threads;
171
vcp->vc_threads = vxcp;
172
__vg_exec_context_yield(vxcp);
173
pthread_mutex_unlock(&vg_thread_lock);
174
return 1;
175
}
176
177
void
178
vg_exec_context_del(vg_exec_context_t *vxcp)
179
{
180
vg_exec_context_t *tp, **pprev;
181
182
if (vxcp->vxc_lockmode == 2)
183
vg_exec_context_downgrade_lock(vxcp);
184
185
pthread_mutex_lock(&vg_thread_lock);
186
assert(vxcp->vxc_lockmode == 1);
187
vxcp->vxc_lockmode = 0;
188
189
for (pprev = &vxcp->vxc_vc->vc_threads, tp = *pprev;
190
(tp != vxcp) && (tp != NULL);
191
pprev = &tp->vxc_next, tp = *pprev);
192
193
assert(tp == vxcp);
194
*pprev = tp->vxc_next;
195
196
if (tp->vxc_stop)
197
pthread_cond_signal(&vg_thread_upcond);
198
199
BN_clear_free(&vxcp->vxc_bntarg);
200
BN_clear_free(&vxcp->vxc_bnbase);
201
BN_clear_free(&vxcp->vxc_bntmp);
202
BN_clear_free(&vxcp->vxc_bntmp2);
203
BN_CTX_free(vxcp->vxc_bnctx);
204
vxcp->vxc_bnctx = NULL;
205
pthread_mutex_unlock(&vg_thread_lock);
206
}
207
208
void
209
vg_exec_context_yield(vg_exec_context_t *vxcp)
210
{
211
if (vxcp->vxc_lockmode == 2)
212
vg_exec_context_downgrade_lock(vxcp);
213
214
else if (vxcp->vxc_stop) {
215
assert(vxcp->vxc_lockmode == 1);
216
pthread_mutex_lock(&vg_thread_lock);
217
__vg_exec_context_yield(vxcp);
218
pthread_mutex_unlock(&vg_thread_lock);
219
}
220
221
assert(vxcp->vxc_lockmode == 1);
222
}
223
224
void
225
vg_exec_context_consolidate_key(vg_exec_context_t *vxcp)
226
{
227
if (vxcp->vxc_delta) {
228
BN_clear(&vxcp->vxc_bntmp);
229
BN_set_word(&vxcp->vxc_bntmp, vxcp->vxc_delta);
230
BN_add(&vxcp->vxc_bntmp2,
231
EC_KEY_get0_private_key(vxcp->vxc_key),
232
&vxcp->vxc_bntmp);
233
vg_set_privkey(&vxcp->vxc_bntmp2, vxcp->vxc_key);
234
vxcp->vxc_delta = 0;
235
}
236
}
237
238
void
239
vg_exec_context_calc_address(vg_exec_context_t *vxcp)
240
{
241
EC_POINT *pubkey;
242
const EC_GROUP *pgroup;
243
unsigned char eckey_buf[96], hash1[32], hash2[20];
244
int len;
245
246
vg_exec_context_consolidate_key(vxcp);
247
pgroup = EC_KEY_get0_group(vxcp->vxc_key);
248
pubkey = EC_POINT_new(pgroup);
249
EC_POINT_copy(pubkey, EC_KEY_get0_public_key(vxcp->vxc_key));
250
if (vxcp->vxc_vc->vc_pubkey_base) {
251
EC_POINT_add(pgroup,
252
pubkey,
253
pubkey,
254
vxcp->vxc_vc->vc_pubkey_base,
255
vxcp->vxc_bnctx);
256
}
257
len = EC_POINT_point2oct(pgroup,
258
pubkey,
259
POINT_CONVERSION_UNCOMPRESSED,
260
eckey_buf,
261
sizeof(eckey_buf),
262
vxcp->vxc_bnctx);
263
SHA256(eckey_buf, len, hash1);
264
RIPEMD160(hash1, sizeof(hash1), hash2);
265
memcpy(&vxcp->vxc_binres[1],
266
hash2, 20);
267
EC_POINT_free(pubkey);
268
}
269
270
enum {
271
timing_hist_size = 5
272
};
273
274
typedef struct _timing_info_s {
275
struct _timing_info_s *ti_next;
276
pthread_t ti_thread;
277
unsigned long ti_last_rate;
278
279
unsigned long long ti_hist_time[timing_hist_size];
280
unsigned long ti_hist_work[timing_hist_size];
281
int ti_hist_last;
282
} timing_info_t;
283
284
static pthread_mutex_t timing_mutex = PTHREAD_MUTEX_INITIALIZER;
285
286
int
287
vg_output_timing(vg_context_t *vcp, int cycle, struct timeval *last)
288
{
289
pthread_t me;
290
struct timeval tvnow, tv;
291
timing_info_t *tip, *mytip;
292
unsigned long long rate, myrate = 0, mytime, total, sincelast;
293
int p, i;
294
295
/* Compute the rate */
296
gettimeofday(&tvnow, NULL);
297
timersub(&tvnow, last, &tv);
298
memcpy(last, &tvnow, sizeof(*last));
299
mytime = tv.tv_usec + (1000000ULL * tv.tv_sec);
300
if (!mytime)
301
mytime = 1;
302
rate = 0;
303
304
pthread_mutex_lock(&timing_mutex);
305
me = pthread_self();
306
for (tip = vcp->vc_timing_head, mytip = NULL;
307
tip != NULL; tip = tip->ti_next) {
308
if (pthread_equal(tip->ti_thread, me)) {
309
mytip = tip;
310
p = ((tip->ti_hist_last + 1) % timing_hist_size);
311
tip->ti_hist_time[p] = mytime;
312
tip->ti_hist_work[p] = cycle;
313
tip->ti_hist_last = p;
314
315
mytime = 0;
316
myrate = 0;
317
for (i = 0; i < timing_hist_size; i++) {
318
mytime += tip->ti_hist_time[i];
319
myrate += tip->ti_hist_work[i];
320
}
321
myrate = (myrate * 1000000) / mytime;
322
tip->ti_last_rate = myrate;
323
rate += myrate;
324
325
} else
326
rate += tip->ti_last_rate;
327
}
328
if (!mytip) {
329
mytip = (timing_info_t *) malloc(sizeof(*tip));
330
mytip->ti_next = vcp->vc_timing_head;
331
mytip->ti_thread = me;
332
vcp->vc_timing_head = mytip;
333
mytip->ti_hist_last = 0;
334
mytip->ti_hist_time[0] = mytime;
335
mytip->ti_hist_work[0] = cycle;
336
for (i = 1; i < timing_hist_size; i++) {
337
mytip->ti_hist_time[i] = 1;
338
mytip->ti_hist_work[i] = 0;
339
}
340
myrate = ((unsigned long long)cycle * 1000000) / mytime;
341
mytip->ti_last_rate = myrate;
342
rate += myrate;
343
}
344
345
vcp->vc_timing_total += cycle;
346
if (vcp->vc_timing_prevfound != vcp->vc_found) {
347
vcp->vc_timing_prevfound = vcp->vc_found;
348
vcp->vc_timing_sincelast = 0;
349
}
350
vcp->vc_timing_sincelast += cycle;
351
352
if (mytip != vcp->vc_timing_head) {
353
pthread_mutex_unlock(&timing_mutex);
354
return myrate;
355
}
356
total = vcp->vc_timing_total;
357
sincelast = vcp->vc_timing_sincelast;
358
pthread_mutex_unlock(&timing_mutex);
359
360
vcp->vc_output_timing(vcp, sincelast, rate, total);
361
return myrate;
362
}
363
364
void
365
vg_context_thread_exit(vg_context_t *vcp)
366
{
367
timing_info_t *tip, **ptip;
368
pthread_t me;
369
370
pthread_mutex_lock(&timing_mutex);
371
me = pthread_self();
372
for (ptip = &vcp->vc_timing_head, tip = *ptip;
373
tip != NULL;
374
ptip = &tip->ti_next, tip = *ptip) {
375
if (!pthread_equal(tip->ti_thread, me))
376
continue;
377
*ptip = tip->ti_next;
378
free(tip);
379
break;
380
}
381
pthread_mutex_unlock(&timing_mutex);
382
383
}
384
385
static void
386
vg_timing_info_free(vg_context_t *vcp)
387
{
388
timing_info_t *tp;
389
while (vcp->vc_timing_head != NULL) {
390
tp = vcp->vc_timing_head;
391
vcp->vc_timing_head = tp->ti_next;
392
free(tp);
393
}
394
}
395
396
void
397
vg_output_timing_console(vg_context_t *vcp, double count,
398
unsigned long long rate, unsigned long long total)
399
{
400
double prob, time, targ;
401
char *unit;
402
char linebuf[80];
403
int rem, p, i;
404
405
const double targs[] = { 0.5, 0.75, 0.8, 0.9, 0.95, 1.0 };
406
407
targ = rate;
408
unit = "key/s";
409
if (targ > 1000) {
410
unit = "Kkey/s";
411
targ /= 1000.0;
412
if (targ > 1000) {
413
unit = "Mkey/s";
414
targ /= 1000.0;
415
}
416
}
417
418
rem = sizeof(linebuf);
419
p = snprintf(linebuf, rem, "[%.2f %s][total %lld]",
420
targ, unit, total);
421
assert(p > 0);
422
rem -= p;
423
if (rem < 0)
424
rem = 0;
425
426
if (vcp->vc_chance >= 1.0) {
427
prob = 1.0f - exp(-count/vcp->vc_chance);
428
429
if (prob <= 0.999) {
430
p = snprintf(&linebuf[p], rem, "[Prob %.1f%%]",
431
prob * 100);
432
assert(p > 0);
433
rem -= p;
434
if (rem < 0)
435
rem = 0;
436
p = sizeof(linebuf) - rem;
437
}
438
439
for (i = 0; i < sizeof(targs)/sizeof(targs[0]); i++) {
440
targ = targs[i];
441
if ((targ < 1.0) && (prob <= targ))
442
break;
443
}
444
445
if (targ < 1.0) {
446
time = ((-vcp->vc_chance * log(1.0 - targ)) - count) /
447
rate;
448
unit = "s";
449
if (time > 60) {
450
time /= 60;
451
unit = "min";
452
if (time > 60) {
453
time /= 60;
454
unit = "h";
455
if (time > 24) {
456
time /= 24;
457
unit = "d";
458
if (time > 365) {
459
time /= 365;
460
unit = "y";
461
}
462
}
463
}
464
}
465
466
if (time > 1000000) {
467
p = snprintf(&linebuf[p], rem,
468
"[%d%% in %e%s]",
469
(int) (100 * targ), time, unit);
470
} else {
471
p = snprintf(&linebuf[p], rem,
472
"[%d%% in %.1f%s]",
473
(int) (100 * targ), time, unit);
474
}
475
assert(p > 0);
476
rem -= p;
477
if (rem < 0)
478
rem = 0;
479
p = sizeof(linebuf) - rem;
480
}
481
}
482
483
if (vcp->vc_found) {
484
if (vcp->vc_remove_on_match)
485
p = snprintf(&linebuf[p], rem, "[Found %lld/%ld]",
486
vcp->vc_found, vcp->vc_npatterns_start);
487
else
488
p = snprintf(&linebuf[p], rem, "[Found %lld]",
489
vcp->vc_found);
490
assert(p > 0);
491
rem -= p;
492
if (rem < 0)
493
rem = 0;
494
}
495
496
if (rem) {
497
memset(&linebuf[sizeof(linebuf)-rem], 0x20, rem);
498
linebuf[sizeof(linebuf)-1] = '\0';
499
}
500
printf("\r%s", linebuf);
501
fflush(stdout);
502
}
503
504
void
505
vg_output_match_console(vg_context_t *vcp, EC_KEY *pkey, const char *pattern)
506
{
507
unsigned char key_buf[512], *pend;
508
char addr_buf[64], addr2_buf[64];
509
char privkey_buf[VG_PROTKEY_MAX_B58];
510
const char *keytype = "Privkey";
511
int len;
512
int isscript = (vcp->vc_format == VCF_SCRIPT);
513
514
EC_POINT *ppnt;
515
int free_ppnt = 0;
516
if (vcp->vc_pubkey_base) {
517
ppnt = EC_POINT_new(EC_KEY_get0_group(pkey));
518
EC_POINT_copy(ppnt, EC_KEY_get0_public_key(pkey));
519
EC_POINT_add(EC_KEY_get0_group(pkey),
520
ppnt,
521
ppnt,
522
vcp->vc_pubkey_base,
523
NULL);
524
free_ppnt = 1;
525
keytype = "PrivkeyPart";
526
} else {
527
ppnt = (EC_POINT *) EC_KEY_get0_public_key(pkey);
528
}
529
530
assert(EC_KEY_check_key(pkey));
531
vg_encode_address(ppnt,
532
EC_KEY_get0_group(pkey),
533
vcp->vc_pubkeytype, addr_buf);
534
if (isscript)
535
vg_encode_script_address(ppnt,
536
EC_KEY_get0_group(pkey),
537
vcp->vc_addrtype, addr2_buf);
538
539
if (vcp->vc_key_protect_pass) {
540
len = vg_protect_encode_privkey(privkey_buf,
541
pkey, vcp->vc_privtype,
542
VG_PROTKEY_DEFAULT,
543
vcp->vc_key_protect_pass);
544
if (len) {
545
keytype = "Protkey";
546
} else {
547
fprintf(stderr,
548
"ERROR: could not password-protect key\n");
549
vcp->vc_key_protect_pass = NULL;
550
}
551
}
552
if (!vcp->vc_key_protect_pass) {
553
vg_encode_privkey(pkey, vcp->vc_privtype, privkey_buf);
554
}
555
556
if (!vcp->vc_result_file || (vcp->vc_verbose > 0)) {
557
printf("\r%79s\rPattern: %s\n", "", pattern);
558
}
559
560
if (vcp->vc_verbose > 0) {
561
if (vcp->vc_verbose > 1) {
562
pend = key_buf;
563
len = i2o_ECPublicKey(pkey, &pend);
564
printf("Pubkey (hex): ");
565
dumphex(key_buf, len);
566
printf("Privkey (hex): ");
567
dumpbn(EC_KEY_get0_private_key(pkey));
568
pend = key_buf;
569
len = i2d_ECPrivateKey(pkey, &pend);
570
printf("Privkey (ASN1): ");
571
dumphex(key_buf, len);
572
}
573
574
}
575
576
if (!vcp->vc_result_file || (vcp->vc_verbose > 0)) {
577
if (isscript)
578
printf("P2SHAddress: %s\n", addr2_buf);
579
printf("Address: %s\n"
580
"%s: %s\n",
581
addr_buf, keytype, privkey_buf);
582
}
583
584
if (vcp->vc_result_file) {
585
FILE *fp = fopen(vcp->vc_result_file, "a");
586
if (!fp) {
587
fprintf(stderr,
588
"ERROR: could not open result file: %s\n",
589
strerror(errno));
590
} else {
591
fprintf(fp,
592
"Pattern: %s\n"
593
, pattern);
594
if (isscript)
595
fprintf(fp, "P2SHAddress: %s\n", addr2_buf);
596
fprintf(fp,
597
"Address: %s\n"
598
"%s: %s\n",
599
addr_buf, keytype, privkey_buf);
600
fclose(fp);
601
}
602
}
603
if (free_ppnt)
604
EC_POINT_free(ppnt);
605
}
606
607
608
void
609
vg_context_free(vg_context_t *vcp)
610
{
611
vg_timing_info_free(vcp);
612
vcp->vc_free(vcp);
613
}
614
615
int
616
vg_context_add_patterns(vg_context_t *vcp,
617
const char ** const patterns, int npatterns)
618
{
619
vcp->vc_pattern_generation++;
620
return vcp->vc_add_patterns(vcp, patterns, npatterns);
621
}
622
623
void
624
vg_context_clear_all_patterns(vg_context_t *vcp)
625
{
626
vcp->vc_clear_all_patterns(vcp);
627
vcp->vc_pattern_generation++;
628
}
629
630
int
631
vg_context_hash160_sort(vg_context_t *vcp, void *buf)
632
{
633
if (!vcp->vc_hash160_sort)
634
return 0;
635
return vcp->vc_hash160_sort(vcp, buf);
636
}
637
638
int
639
vg_context_start_threads(vg_context_t *vcp)
640
{
641
vg_exec_context_t *vxcp;
642
int res;
643
644
for (vxcp = vcp->vc_threads; vxcp != NULL; vxcp = vxcp->vxc_next) {
645
res = pthread_create((pthread_t *) &vxcp->vxc_pthread,
646
NULL,
647
(void *(*)(void *)) vxcp->vxc_threadfunc,
648
vxcp);
649
if (res) {
650
fprintf(stderr, "ERROR: could not create thread: %d\n",
651
res);
652
vg_context_stop_threads(vcp);
653
return -1;
654
}
655
vxcp->vxc_thread_active = 1;
656
}
657
return 0;
658
}
659
660
void
661
vg_context_stop_threads(vg_context_t *vcp)
662
{
663
vcp->vc_halt = 1;
664
vg_context_wait_for_completion(vcp);
665
vcp->vc_halt = 0;
666
}
667
668
void
669
vg_context_wait_for_completion(vg_context_t *vcp)
670
{
671
vg_exec_context_t *vxcp;
672
673
for (vxcp = vcp->vc_threads; vxcp != NULL; vxcp = vxcp->vxc_next) {
674
if (!vxcp->vxc_thread_active)
675
continue;
676
pthread_join((pthread_t) vxcp->vxc_pthread, NULL);
677
vxcp->vxc_thread_active = 0;
678
}
679
}
680
681
682
/*
683
* Find the bignum ranges that produce a given prefix.
684
*/
685
static int
686
get_prefix_ranges(int addrtype, const char *pfx, BIGNUM **result,
687
BN_CTX *bnctx)
688
{
689
int i, p, c;
690
int zero_prefix = 0;
691
int check_upper = 0;
692
int b58pow, b58ceil, b58top = 0;
693
int ret = -1;
694
695
BIGNUM bntarg, bnceil, bnfloor;
696
BIGNUM bnbase;
697
BIGNUM *bnap, *bnbp, *bntp;
698
BIGNUM *bnhigh = NULL, *bnlow = NULL, *bnhigh2 = NULL, *bnlow2 = NULL;
699
BIGNUM bntmp, bntmp2;
700
701
BN_init(&bntarg);
702
BN_init(&bnceil);
703
BN_init(&bnfloor);
704
BN_init(&bnbase);
705
BN_init(&bntmp);
706
BN_init(&bntmp2);
707
708
BN_set_word(&bnbase, 58);
709
710
p = strlen(pfx);
711
712
for (i = 0; i < p; i++) {
713
c = vg_b58_reverse_map[(int)pfx[i]];
714
if (c == -1) {
715
fprintf(stderr,
716
"Invalid character '%c' in prefix '%s'\n",
717
pfx[i], pfx);
718
goto out;
719
}
720
if (i == zero_prefix) {
721
if (c == 0) {
722
/* Add another zero prefix */
723
zero_prefix++;
724
if (zero_prefix > 19) {
725
fprintf(stderr,
726
"Prefix '%s' is too long\n",
727
pfx);
728
goto out;
729
}
730
continue;
731
}
732
733
/* First non-zero character */
734
b58top = c;
735
BN_set_word(&bntarg, c);
736
737
} else {
738
BN_set_word(&bntmp2, c);
739
BN_mul(&bntmp, &bntarg, &bnbase, bnctx);
740
BN_add(&bntarg, &bntmp, &bntmp2);
741
}
742
}
743
744
/* Power-of-two ceiling and floor values based on leading 1s */
745
BN_clear(&bntmp);
746
BN_set_bit(&bntmp, 200 - (zero_prefix * 8));
747
BN_sub(&bnceil, &bntmp, BN_value_one());
748
BN_set_bit(&bnfloor, 192 - (zero_prefix * 8));
749
750
bnlow = BN_new();
751
bnhigh = BN_new();
752
753
if (b58top) {
754
/*
755
* If a non-zero was given in the prefix, find the
756
* numeric boundaries of the prefix.
757
*/
758
759
BN_copy(&bntmp, &bnceil);
760
bnap = &bntmp;
761
bnbp = &bntmp2;
762
b58pow = 0;
763
while (BN_cmp(bnap, &bnbase) > 0) {
764
b58pow++;
765
BN_div(bnbp, NULL, bnap, &bnbase, bnctx);
766
bntp = bnap;
767
bnap = bnbp;
768
bnbp = bntp;
769
}
770
b58ceil = BN_get_word(bnap);
771
772
if ((b58pow - (p - zero_prefix)) < 6) {
773
/*
774
* Do not allow the prefix to constrain the
775
* check value, this is ridiculous.
776
*/
777
fprintf(stderr, "Prefix '%s' is too long\n", pfx);
778
goto out;
779
}
780
781
BN_set_word(&bntmp2, b58pow - (p - zero_prefix));
782
BN_exp(&bntmp, &bnbase, &bntmp2, bnctx);
783
BN_mul(bnlow, &bntmp, &bntarg, bnctx);
784
BN_sub(&bntmp2, &bntmp, BN_value_one());
785
BN_add(bnhigh, bnlow, &bntmp2);
786
787
if (b58top <= b58ceil) {
788
/* Fill out the upper range too */
789
check_upper = 1;
790
bnlow2 = BN_new();
791
bnhigh2 = BN_new();
792
793
BN_mul(bnlow2, bnlow, &bnbase, bnctx);
794
BN_mul(&bntmp2, bnhigh, &bnbase, bnctx);
795
BN_set_word(&bntmp, 57);
796
BN_add(bnhigh2, &bntmp2, &bntmp);
797
798
/*
799
* Addresses above the ceiling will have one
800
* fewer "1" prefix in front than we require.
801
*/
802
if (BN_cmp(&bnceil, bnlow2) < 0) {
803
/* High prefix is above the ceiling */
804
check_upper = 0;
805
BN_free(bnhigh2);
806
bnhigh2 = NULL;
807
BN_free(bnlow2);
808
bnlow2 = NULL;
809
}
810
else if (BN_cmp(&bnceil, bnhigh2) < 0)
811
/* High prefix is partly above the ceiling */
812
BN_copy(bnhigh2, &bnceil);
813
814
/*
815
* Addresses below the floor will have another
816
* "1" prefix in front instead of our target.
817
*/
818
if (BN_cmp(&bnfloor, bnhigh) >= 0) {
819
/* Low prefix is completely below the floor */
820
assert(check_upper);
821
check_upper = 0;
822
BN_free(bnhigh);
823
bnhigh = bnhigh2;
824
bnhigh2 = NULL;
825
BN_free(bnlow);
826
bnlow = bnlow2;
827
bnlow2 = NULL;
828
}
829
else if (BN_cmp(&bnfloor, bnlow) > 0) {
830
/* Low prefix is partly below the floor */
831
BN_copy(bnlow, &bnfloor);
832
}
833
}
834
835
} else {
836
BN_copy(bnhigh, &bnceil);
837
BN_clear(bnlow);
838
}
839
840
/* Limit the prefix to the address type */
841
BN_clear(&bntmp);
842
BN_set_word(&bntmp, addrtype);
843
BN_lshift(&bntmp2, &bntmp, 192);
844
845
if (check_upper) {
846
if (BN_cmp(&bntmp2, bnhigh2) > 0) {
847
check_upper = 0;
848
BN_free(bnhigh2);
849
bnhigh2 = NULL;
850
BN_free(bnlow2);
851
bnlow2 = NULL;
852
}
853
else if (BN_cmp(&bntmp2, bnlow2) > 0)
854
BN_copy(bnlow2, &bntmp2);
855
}
856
857
if (BN_cmp(&bntmp2, bnhigh) > 0) {
858
if (!check_upper)
859
goto not_possible;
860
check_upper = 0;
861
BN_free(bnhigh);
862
bnhigh = bnhigh2;
863
bnhigh2 = NULL;
864
BN_free(bnlow);
865
bnlow = bnlow2;
866
bnlow2 = NULL;
867
}
868
else if (BN_cmp(&bntmp2, bnlow) > 0) {
869
BN_copy(bnlow, &bntmp2);
870
}
871
872
BN_set_word(&bntmp, addrtype + 1);
873
BN_lshift(&bntmp2, &bntmp, 192);
874
875
if (check_upper) {
876
if (BN_cmp(&bntmp2, bnlow2) < 0) {
877
check_upper = 0;
878
BN_free(bnhigh2);
879
bnhigh2 = NULL;
880
BN_free(bnlow2);
881
bnlow2 = NULL;
882
}
883
else if (BN_cmp(&bntmp2, bnhigh2) < 0)
884
BN_copy(bnlow2, &bntmp2);
885
}
886
887
if (BN_cmp(&bntmp2, bnlow) < 0) {
888
if (!check_upper)
889
goto not_possible;
890
check_upper = 0;
891
BN_free(bnhigh);
892
bnhigh = bnhigh2;
893
bnhigh2 = NULL;
894
BN_free(bnlow);
895
bnlow = bnlow2;
896
bnlow2 = NULL;
897
}
898
else if (BN_cmp(&bntmp2, bnhigh) < 0) {
899
BN_copy(bnhigh, &bntmp2);
900
}
901
902
/* Address ranges are complete */
903
assert(check_upper || ((bnlow2 == NULL) && (bnhigh2 == NULL)));
904
result[0] = bnlow;
905
result[1] = bnhigh;
906
result[2] = bnlow2;
907
result[3] = bnhigh2;
908
bnlow = NULL;
909
bnhigh = NULL;
910
bnlow2 = NULL;
911
bnhigh2 = NULL;
912
ret = 0;
913
914
if (0) {
915
not_possible:
916
ret = -2;
917
}
918
919
out:
920
BN_clear_free(&bntarg);
921
BN_clear_free(&bnceil);
922
BN_clear_free(&bnfloor);
923
BN_clear_free(&bnbase);
924
BN_clear_free(&bntmp);
925
BN_clear_free(&bntmp2);
926
if (bnhigh)
927
BN_free(bnhigh);
928
if (bnlow)
929
BN_free(bnlow);
930
if (bnhigh2)
931
BN_free(bnhigh2);
932
if (bnlow2)
933
BN_free(bnlow2);
934
935
return ret;
936
}
937
938
static void
939
free_ranges(BIGNUM **ranges)
940
{
941
BN_free(ranges[0]);
942
BN_free(ranges[1]);
943
ranges[0] = NULL;
944
ranges[1] = NULL;
945
if (ranges[2]) {
946
BN_free(ranges[2]);
947
BN_free(ranges[3]);
948
ranges[2] = NULL;
949
ranges[3] = NULL;
950
}
951
}
952
953
954
/*
955
* Address prefix AVL tree node
956
*/
957
958
const int vpk_nwords = (25 + sizeof(BN_ULONG) - 1) / sizeof(BN_ULONG);
959
960
typedef struct _vg_prefix_s {
961
avl_item_t vp_item;
962
struct _vg_prefix_s *vp_sibling;
963
const char *vp_pattern;
964
BIGNUM *vp_low;
965
BIGNUM *vp_high;
966
} vg_prefix_t;
967
968
static void
969
vg_prefix_free(vg_prefix_t *vp)
970
{
971
if (vp->vp_low)
972
BN_free(vp->vp_low);
973
if (vp->vp_high)
974
BN_free(vp->vp_high);
975
free(vp);
976
}
977
978
static vg_prefix_t *
979
vg_prefix_avl_search(avl_root_t *rootp, BIGNUM *targ)
980
{
981
vg_prefix_t *vp;
982
avl_item_t *itemp = rootp->ar_root;
983
984
while (itemp) {
985
vp = avl_item_entry(itemp, vg_prefix_t, vp_item);
986
if (BN_cmp(vp->vp_low, targ) > 0) {
987
itemp = itemp->ai_left;
988
} else {
989
if (BN_cmp(vp->vp_high, targ) < 0) {
990
itemp = itemp->ai_right;
991
} else
992
return vp;
993
}
994
}
995
return NULL;
996
}
997
998
static vg_prefix_t *
999
vg_prefix_avl_insert(avl_root_t *rootp, vg_prefix_t *vpnew)
1000
{
1001
vg_prefix_t *vp;
1002
avl_item_t *itemp = NULL;
1003
avl_item_t **ptrp = &rootp->ar_root;
1004
while (*ptrp) {
1005
itemp = *ptrp;
1006
vp = avl_item_entry(itemp, vg_prefix_t, vp_item);
1007
if (BN_cmp(vp->vp_low, vpnew->vp_high) > 0) {
1008
ptrp = &itemp->ai_left;
1009
} else {
1010
if (BN_cmp(vp->vp_high, vpnew->vp_low) < 0) {
1011
ptrp = &itemp->ai_right;
1012
} else
1013
return vp;
1014
}
1015
}
1016
vpnew->vp_item.ai_up = itemp;
1017
itemp = &vpnew->vp_item;
1018
*ptrp = itemp;
1019
avl_insert_fix(rootp, itemp);
1020
return NULL;
1021
}
1022
1023
static vg_prefix_t *
1024
vg_prefix_first(avl_root_t *rootp)
1025
{
1026
avl_item_t *itemp;
1027
itemp = avl_first(rootp);
1028
if (itemp)
1029
return avl_item_entry(itemp, vg_prefix_t, vp_item);
1030
return NULL;
1031
}
1032
1033
static vg_prefix_t *
1034
vg_prefix_next(vg_prefix_t *vp)
1035
{
1036
avl_item_t *itemp = &vp->vp_item;
1037
itemp = avl_next(itemp);
1038
if (itemp)
1039
return avl_item_entry(itemp, vg_prefix_t, vp_item);
1040
return NULL;
1041
}
1042
1043
static vg_prefix_t *
1044
vg_prefix_add(avl_root_t *rootp, const char *pattern, BIGNUM *low, BIGNUM *high)
1045
{
1046
vg_prefix_t *vp, *vp2;
1047
assert(BN_cmp(low, high) < 0);
1048
vp = (vg_prefix_t *) malloc(sizeof(*vp));
1049
if (vp) {
1050
avl_item_init(&vp->vp_item);
1051
vp->vp_sibling = NULL;
1052
vp->vp_pattern = pattern;
1053
vp->vp_low = low;
1054
vp->vp_high = high;
1055
vp2 = vg_prefix_avl_insert(rootp, vp);
1056
if (vp2 != NULL) {
1057
fprintf(stderr,
1058
"Prefix '%s' ignored, overlaps '%s'\n",
1059
pattern, vp2->vp_pattern);
1060
vg_prefix_free(vp);
1061
vp = NULL;
1062
}
1063
}
1064
return vp;
1065
}
1066
1067
static void
1068
vg_prefix_delete(avl_root_t *rootp, vg_prefix_t *vp)
1069
{
1070
vg_prefix_t *sibp, *delp;
1071
1072
avl_remove(rootp, &vp->vp_item);
1073
sibp = vp->vp_sibling;
1074
while (sibp && sibp != vp) {
1075
avl_remove(rootp, &sibp->vp_item);
1076
delp = sibp;
1077
sibp = sibp->vp_sibling;
1078
vg_prefix_free(delp);
1079
}
1080
vg_prefix_free(vp);
1081
}
1082
1083
static vg_prefix_t *
1084
vg_prefix_add_ranges(avl_root_t *rootp, const char *pattern, BIGNUM **ranges,
1085
vg_prefix_t *master)
1086
{
1087
vg_prefix_t *vp, *vp2 = NULL;
1088
1089
assert(ranges[0]);
1090
vp = vg_prefix_add(rootp, pattern, ranges[0], ranges[1]);
1091
if (!vp)
1092
return NULL;
1093
1094
if (ranges[2]) {
1095
vp2 = vg_prefix_add(rootp, pattern, ranges[2], ranges[3]);
1096
if (!vp2) {
1097
vg_prefix_delete(rootp, vp);
1098
return NULL;
1099
}
1100
}
1101
1102
if (!master) {
1103
vp->vp_sibling = vp2;
1104
if (vp2)
1105
vp2->vp_sibling = vp;
1106
} else if (vp2) {
1107
vp->vp_sibling = vp2;
1108
vp2->vp_sibling = (master->vp_sibling ?
1109
master->vp_sibling :
1110
master);
1111
master->vp_sibling = vp;
1112
} else {
1113
vp->vp_sibling = (master->vp_sibling ?
1114
master->vp_sibling :
1115
master);
1116
master->vp_sibling = vp;
1117
}
1118
return vp;
1119
}
1120
1121
static void
1122
vg_prefix_range_sum(vg_prefix_t *vp, BIGNUM *result, BIGNUM *tmp1)
1123
{
1124
vg_prefix_t *startp;
1125
1126
startp = vp;
1127
BN_clear(result);
1128
do {
1129
BN_sub(tmp1, vp->vp_high, vp->vp_low);
1130
BN_add(result, result, tmp1);
1131
vp = vp->vp_sibling;
1132
} while (vp && (vp != startp));
1133
}
1134
1135
1136
typedef struct _prefix_case_iter_s {
1137
char ci_prefix[32];
1138
char ci_case_map[32];
1139
char ci_nbits;
1140
int ci_value;
1141
} prefix_case_iter_t;
1142
1143
static const unsigned char b58_case_map[256] = {
1144
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1145
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1146
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1147
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1148
0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 0, 1, 1, 2,
1149
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
1150
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 2, 1, 1, 0,
1151
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
1152
};
1153
1154
static int
1155
prefix_case_iter_init(prefix_case_iter_t *cip, const char *pfx)
1156
{
1157
int i;
1158
1159
cip->ci_nbits = 0;
1160
cip->ci_value = 0;
1161
for (i = 0; pfx[i]; i++) {
1162
if (i > sizeof(cip->ci_prefix))
1163
return 0;
1164
if (!b58_case_map[(int)pfx[i]]) {
1165
/* Character isn't case-swappable, ignore it */
1166
cip->ci_prefix[i] = pfx[i];
1167
continue;
1168
}
1169
if (b58_case_map[(int)pfx[i]] == 2) {
1170
/* Character invalid, but valid in swapped case */
1171
cip->ci_prefix[i] = pfx[i] ^ 0x20;
1172
continue;
1173
}
1174
/* Character is case-swappable */
1175
cip->ci_prefix[i] = pfx[i] | 0x20;
1176
cip->ci_case_map[(int)cip->ci_nbits] = i;
1177
cip->ci_nbits++;
1178
}
1179
cip->ci_prefix[i] = '\0';
1180
return 1;
1181
}
1182
1183
static int
1184
prefix_case_iter_next(prefix_case_iter_t *cip)
1185
{
1186
unsigned long val, max, mask;
1187
int i, nbits;
1188
1189
nbits = cip->ci_nbits;
1190
max = (1UL << nbits) - 1;
1191
val = cip->ci_value + 1;
1192
if (val > max)
1193
return 0;
1194
1195
for (i = 0, mask = 1; i < nbits; i++, mask <<= 1) {
1196
if (val & mask)
1197
cip->ci_prefix[(int)cip->ci_case_map[i]] &= 0xdf;
1198
else
1199
cip->ci_prefix[(int)cip->ci_case_map[i]] |= 0x20;
1200
}
1201
cip->ci_value = val;
1202
return 1;
1203
}
1204
1205
1206
typedef struct _vg_prefix_context_s {
1207
vg_context_t base;
1208
avl_root_t vcp_avlroot;
1209
BIGNUM vcp_difficulty;
1210
int vcp_caseinsensitive;
1211
} vg_prefix_context_t;
1212
1213
void
1214
vg_prefix_context_set_case_insensitive(vg_context_t *vcp, int caseinsensitive)
1215
{
1216
((vg_prefix_context_t *) vcp)->vcp_caseinsensitive = caseinsensitive;
1217
}
1218
1219
static void
1220
vg_prefix_context_clear_all_patterns(vg_context_t *vcp)
1221
{
1222
vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vcp;
1223
vg_prefix_t *vp;
1224
unsigned long npfx_left = 0;
1225
1226
while (!avl_root_empty(&vcpp->vcp_avlroot)) {
1227
vp = avl_item_entry(vcpp->vcp_avlroot.ar_root,
1228
vg_prefix_t, vp_item);
1229
vg_prefix_delete(&vcpp->vcp_avlroot, vp);
1230
npfx_left++;
1231
}
1232
1233
assert(npfx_left == vcpp->base.vc_npatterns);
1234
vcpp->base.vc_npatterns = 0;
1235
vcpp->base.vc_npatterns_start = 0;
1236
vcpp->base.vc_found = 0;
1237
BN_clear(&vcpp->vcp_difficulty);
1238
}
1239
1240
static void
1241
vg_prefix_context_free(vg_context_t *vcp)
1242
{
1243
vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vcp;
1244
vg_prefix_context_clear_all_patterns(vcp);
1245
BN_clear_free(&vcpp->vcp_difficulty);
1246
free(vcpp);
1247
}
1248
1249
static void
1250
vg_prefix_context_next_difficulty(vg_prefix_context_t *vcpp,
1251
BIGNUM *bntmp, BIGNUM *bntmp2, BN_CTX *bnctx)
1252
{
1253
char *dbuf;
1254
1255
BN_clear(bntmp);
1256
BN_set_bit(bntmp, 192);
1257
BN_div(bntmp2, NULL, bntmp, &vcpp->vcp_difficulty, bnctx);
1258
1259
dbuf = BN_bn2dec(bntmp2);
1260
if (vcpp->base.vc_verbose > 0) {
1261
if (vcpp->base.vc_npatterns > 1)
1262
fprintf(stderr,
1263
"Next match difficulty: %s (%ld prefixes)\n",
1264
dbuf, vcpp->base.vc_npatterns);
1265
else
1266
fprintf(stderr, "Difficulty: %s\n", dbuf);
1267
}
1268
vcpp->base.vc_chance = atof(dbuf);
1269
OPENSSL_free(dbuf);
1270
}
1271
1272
static int
1273
vg_prefix_context_add_patterns(vg_context_t *vcp,
1274
const char ** const patterns, int npatterns)
1275
{
1276
vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vcp;
1277
prefix_case_iter_t caseiter;
1278
vg_prefix_t *vp, *vp2;
1279
BN_CTX *bnctx;
1280
BIGNUM bntmp, bntmp2, bntmp3;
1281
BIGNUM *ranges[4];
1282
int ret = 0;
1283
int i, impossible = 0;
1284
int case_impossible;
1285
unsigned long npfx;
1286
char *dbuf;
1287
1288
bnctx = BN_CTX_new();
1289
BN_init(&bntmp);
1290
BN_init(&bntmp2);
1291
BN_init(&bntmp3);
1292
1293
npfx = 0;
1294
for (i = 0; i < npatterns; i++) {
1295
if (!vcpp->vcp_caseinsensitive) {
1296
vp = NULL;
1297
ret = get_prefix_ranges(vcpp->base.vc_addrtype,
1298
patterns[i],
1299
ranges, bnctx);
1300
if (!ret) {
1301
vp = vg_prefix_add_ranges(&vcpp->vcp_avlroot,
1302
patterns[i],
1303
ranges, NULL);
1304
}
1305
1306
} else {
1307
/* Case-enumerate the prefix */
1308
if (!prefix_case_iter_init(&caseiter, patterns[i])) {
1309
fprintf(stderr,
1310
"Prefix '%s' is too long\n",
1311
patterns[i]);
1312
continue;
1313
}
1314
1315
if (caseiter.ci_nbits > 16) {
1316
fprintf(stderr,
1317
"WARNING: Prefix '%s' has "
1318
"2^%d case-varied derivatives\n",
1319
patterns[i], caseiter.ci_nbits);
1320
}
1321
1322
case_impossible = 0;
1323
vp = NULL;
1324
do {
1325
ret = get_prefix_ranges(vcpp->base.vc_addrtype,
1326
caseiter.ci_prefix,
1327
ranges, bnctx);
1328
if (ret == -2) {
1329
case_impossible++;
1330
ret = 0;
1331
continue;
1332
}
1333
if (ret)
1334
break;
1335
vp2 = vg_prefix_add_ranges(&vcpp->vcp_avlroot,
1336
patterns[i],
1337
ranges,
1338
vp);
1339
if (!vp2) {
1340
ret = -1;
1341
break;
1342
}
1343
if (!vp)
1344
vp = vp2;
1345
1346
} while (prefix_case_iter_next(&caseiter));
1347
1348
if (!vp && case_impossible)
1349
ret = -2;
1350
1351
if (ret && vp) {
1352
vg_prefix_delete(&vcpp->vcp_avlroot, vp);
1353
vp = NULL;
1354
}
1355
}
1356
1357
if (ret == -2) {
1358
fprintf(stderr,
1359
"Prefix '%s' not possible\n", patterns[i]);
1360
impossible++;
1361
}
1362
1363
if (!vp)
1364
continue;
1365
1366
npfx++;
1367
1368
/* Determine the probability of finding a match */
1369
vg_prefix_range_sum(vp, &bntmp, &bntmp2);
1370
BN_add(&bntmp2, &vcpp->vcp_difficulty, &bntmp);
1371
BN_copy(&vcpp->vcp_difficulty, &bntmp2);
1372
1373
if (vcp->vc_verbose > 1) {
1374
BN_clear(&bntmp2);
1375
BN_set_bit(&bntmp2, 192);
1376
BN_div(&bntmp3, NULL, &bntmp2, &bntmp, bnctx);
1377
1378
dbuf = BN_bn2dec(&bntmp3);
1379
fprintf(stderr,
1380
"Prefix difficulty: %20s %s\n",
1381
dbuf, patterns[i]);
1382
OPENSSL_free(dbuf);
1383
}
1384
}
1385
1386
vcpp->base.vc_npatterns += npfx;
1387
vcpp->base.vc_npatterns_start += npfx;
1388
1389
if (!npfx && impossible) {
1390
const char *ats = "bitcoin", *bw = "\"1\"";
1391
switch (vcpp->base.vc_addrtype) {
1392
case 5:
1393
ats = "bitcoin script";
1394
bw = "\"3\"";
1395
break;
1396
case 111:
1397
ats = "testnet";
1398
bw = "\"m\" or \"n\"";
1399
break;
1400
case 52:
1401
ats = "namecoin";
1402
bw = "\"M\" or \"N\"";
1403
break;
1404
default:
1405
break;
1406
}
1407
fprintf(stderr,
1408
"Hint: valid %s addresses begin with %s\n", ats, bw);
1409
}
1410
1411
if (npfx)
1412
vg_prefix_context_next_difficulty(vcpp, &bntmp, &bntmp2, bnctx);
1413
1414
ret = (npfx != 0);
1415
1416
BN_clear_free(&bntmp);
1417
BN_clear_free(&bntmp2);
1418
BN_clear_free(&bntmp3);
1419
BN_CTX_free(bnctx);
1420
return ret;
1421
}
1422
1423
double
1424
vg_prefix_get_difficulty(int addrtype, const char *pattern)
1425
{
1426
BN_CTX *bnctx;
1427
BIGNUM result, bntmp;
1428
BIGNUM *ranges[4];
1429
char *dbuf;
1430
int ret;
1431
double diffret = 0.0;
1432
1433
bnctx = BN_CTX_new();
1434
BN_init(&result);
1435
BN_init(&bntmp);
1436
1437
ret = get_prefix_ranges(addrtype,
1438
pattern, ranges, bnctx);
1439
1440
if (ret == 0) {
1441
BN_sub(&bntmp, ranges[1], ranges[0]);
1442
BN_add(&result, &result, &bntmp);
1443
if (ranges[2]) {
1444
BN_sub(&bntmp, ranges[3], ranges[2]);
1445
BN_add(&result, &result, &bntmp);
1446
}
1447
free_ranges(ranges);
1448
1449
BN_clear(&bntmp);
1450
BN_set_bit(&bntmp, 192);
1451
BN_div(&result, NULL, &bntmp, &result, bnctx);
1452
1453
dbuf = BN_bn2dec(&result);
1454
diffret = strtod(dbuf, NULL);
1455
OPENSSL_free(dbuf);
1456
}
1457
1458
BN_clear_free(&result);
1459
BN_clear_free(&bntmp);
1460
BN_CTX_free(bnctx);
1461
return diffret;
1462
}
1463
1464
1465
static int
1466
vg_prefix_test(vg_exec_context_t *vxcp)
1467
{
1468
vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vxcp->vxc_vc;
1469
vg_prefix_t *vp;
1470
int res = 0;
1471
1472
/*
1473
* We constrain the prefix so that we can check for
1474
* a match without generating the lower four byte
1475
* check code.
1476
*/
1477
1478
BN_bin2bn(vxcp->vxc_binres, 25, &vxcp->vxc_bntarg);
1479
1480
research:
1481
vp = vg_prefix_avl_search(&vcpp->vcp_avlroot, &vxcp->vxc_bntarg);
1482
if (vp) {
1483
if (vg_exec_context_upgrade_lock(vxcp))
1484
goto research;
1485
1486
vg_exec_context_consolidate_key(vxcp);
1487
vcpp->base.vc_output_match(&vcpp->base, vxcp->vxc_key,
1488
vp->vp_pattern);
1489
1490
vcpp->base.vc_found++;
1491
1492
if (vcpp->base.vc_only_one) {
1493
return 2;
1494
}
1495
1496
if (vcpp->base.vc_remove_on_match) {
1497
/* Subtract the range from the difficulty */
1498
vg_prefix_range_sum(vp,
1499
&vxcp->vxc_bntarg,
1500
&vxcp->vxc_bntmp);
1501
BN_sub(&vxcp->vxc_bntmp,
1502
&vcpp->vcp_difficulty,
1503
&vxcp->vxc_bntarg);
1504
BN_copy(&vcpp->vcp_difficulty, &vxcp->vxc_bntmp);
1505
1506
vg_prefix_delete(&vcpp->vcp_avlroot,vp);
1507
vcpp->base.vc_npatterns--;
1508
1509
if (!avl_root_empty(&vcpp->vcp_avlroot))
1510
vg_prefix_context_next_difficulty(
1511
vcpp, &vxcp->vxc_bntmp,
1512
&vxcp->vxc_bntmp2,
1513
vxcp->vxc_bnctx);
1514
vcpp->base.vc_pattern_generation++;
1515
}
1516
res = 1;
1517
}
1518
if (avl_root_empty(&vcpp->vcp_avlroot)) {
1519
return 2;
1520
}
1521
return res;
1522
}
1523
1524
static int
1525
vg_prefix_hash160_sort(vg_context_t *vcp, void *buf)
1526
{
1527
vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vcp;
1528
vg_prefix_t *vp;
1529
unsigned char *cbuf = (unsigned char *) buf;
1530
unsigned char bnbuf[25];
1531
int nbytes, ncopy, nskip, npfx = 0;
1532
1533
/*
1534
* Walk the prefix tree in order, copy the upper and lower bound
1535
* values into the hash160 buffer. Skip the lower four bytes
1536
* and anything above the 24th byte.
1537
*/
1538
for (vp = vg_prefix_first(&vcpp->vcp_avlroot);
1539
vp != NULL;
1540
vp = vg_prefix_next(vp)) {
1541
npfx++;
1542
if (!buf)
1543
continue;
1544
1545
/* Low */
1546
nbytes = BN_bn2bin(vp->vp_low, bnbuf);
1547
ncopy = ((nbytes >= 24) ? 20 :
1548
((nbytes > 4) ? (nbytes - 4) : 0));
1549
nskip = (nbytes >= 24) ? (nbytes - 24) : 0;
1550
if (ncopy < 20)
1551
memset(cbuf, 0, 20 - ncopy);
1552
memcpy(cbuf + (20 - ncopy),
1553
bnbuf + nskip,
1554
ncopy);
1555
cbuf += 20;
1556
1557
/* High */
1558
nbytes = BN_bn2bin(vp->vp_high, bnbuf);
1559
ncopy = ((nbytes >= 24) ? 20 :
1560
((nbytes > 4) ? (nbytes - 4) : 0));
1561
nskip = (nbytes >= 24) ? (nbytes - 24) : 0;
1562
if (ncopy < 20)
1563
memset(cbuf, 0, 20 - ncopy);
1564
memcpy(cbuf + (20 - ncopy),
1565
bnbuf + nskip,
1566
ncopy);
1567
cbuf += 20;
1568
}
1569
return npfx;
1570
}
1571
1572
vg_context_t *
1573
vg_prefix_context_new(int addrtype, int privtype, int caseinsensitive)
1574
{
1575
vg_prefix_context_t *vcpp;
1576
1577
vcpp = (vg_prefix_context_t *) malloc(sizeof(*vcpp));
1578
if (vcpp) {
1579
memset(vcpp, 0, sizeof(*vcpp));
1580
vcpp->base.vc_addrtype = addrtype;
1581
vcpp->base.vc_privtype = privtype;
1582
vcpp->base.vc_npatterns = 0;
1583
vcpp->base.vc_npatterns_start = 0;
1584
vcpp->base.vc_found = 0;
1585
vcpp->base.vc_chance = 0.0;
1586
vcpp->base.vc_free = vg_prefix_context_free;
1587
vcpp->base.vc_add_patterns = vg_prefix_context_add_patterns;
1588
vcpp->base.vc_clear_all_patterns =
1589
vg_prefix_context_clear_all_patterns;
1590
vcpp->base.vc_test = vg_prefix_test;
1591
vcpp->base.vc_hash160_sort = vg_prefix_hash160_sort;
1592
avl_root_init(&vcpp->vcp_avlroot);
1593
BN_init(&vcpp->vcp_difficulty);
1594
vcpp->vcp_caseinsensitive = caseinsensitive;
1595
}
1596
return &vcpp->base;
1597
}
1598
1599
1600
1601
1602
typedef struct _vg_regex_context_s {
1603
vg_context_t base;
1604
pcre **vcr_regex;
1605
pcre_extra **vcr_regex_extra;
1606
const char **vcr_regex_pat;
1607
unsigned long vcr_nalloc;
1608
} vg_regex_context_t;
1609
1610
static int
1611
vg_regex_context_add_patterns(vg_context_t *vcp,
1612
const char ** const patterns, int npatterns)
1613
{
1614
vg_regex_context_t *vcrp = (vg_regex_context_t *) vcp;
1615
const char *pcre_errptr;
1616
int pcre_erroffset;
1617
unsigned long i, nres, count;
1618
void **mem;
1619
1620
if (!npatterns)
1621
return 1;
1622
1623
if (npatterns > (vcrp->vcr_nalloc - vcrp->base.vc_npatterns)) {
1624
count = npatterns + vcrp->base.vc_npatterns;
1625
if (count < (2 * vcrp->vcr_nalloc)) {
1626
count = (2 * vcrp->vcr_nalloc);
1627
}
1628
if (count < 16) {
1629
count = 16;
1630
}
1631
mem = (void **) malloc(3 * count * sizeof(void*));
1632
if (!mem)
1633
return 0;
1634
1635
for (i = 0; i < vcrp->base.vc_npatterns; i++) {
1636
mem[i] = vcrp->vcr_regex[i];
1637
mem[count + i] = vcrp->vcr_regex_extra[i];
1638
mem[(2 * count) + i] = (void *) vcrp->vcr_regex_pat[i];
1639
}
1640
1641
if (vcrp->vcr_nalloc)
1642
free(vcrp->vcr_regex);
1643
vcrp->vcr_regex = (pcre **) mem;
1644
vcrp->vcr_regex_extra = (pcre_extra **) &mem[count];
1645
vcrp->vcr_regex_pat = (const char **) &mem[2 * count];
1646
vcrp->vcr_nalloc = count;
1647
}
1648
1649
nres = vcrp->base.vc_npatterns;
1650
for (i = 0; i < npatterns; i++) {
1651
vcrp->vcr_regex[nres] =
1652
pcre_compile(patterns[i], 0,
1653
&pcre_errptr, &pcre_erroffset, NULL);
1654
if (!vcrp->vcr_regex[nres]) {
1655
const char *spaces = " ";
1656
fprintf(stderr, "%s\n", patterns[i]);
1657
while (pcre_erroffset > 16) {
1658
fprintf(stderr, "%s", spaces);
1659
pcre_erroffset -= 16;
1660
}
1661
if (pcre_erroffset > 0)
1662
fprintf(stderr,
1663
"%s", &spaces[16 - pcre_erroffset]);
1664
fprintf(stderr, "^\nRegex error: %s\n", pcre_errptr);
1665
continue;
1666
}
1667
vcrp->vcr_regex_extra[nres] =
1668
pcre_study(vcrp->vcr_regex[nres], 0, &pcre_errptr);
1669
if (pcre_errptr) {
1670
fprintf(stderr, "Regex error: %s\n", pcre_errptr);
1671
pcre_free(vcrp->vcr_regex[nres]);
1672
continue;
1673
}
1674
vcrp->vcr_regex_pat[nres] = patterns[i];
1675
nres += 1;
1676
}
1677
1678
if (nres == vcrp->base.vc_npatterns)
1679
return 0;
1680
1681
vcrp->base.vc_npatterns_start += (nres - vcrp->base.vc_npatterns);
1682
vcrp->base.vc_npatterns = nres;
1683
return 1;
1684
}
1685
1686
static void
1687
vg_regex_context_clear_all_patterns(vg_context_t *vcp)
1688
{
1689
vg_regex_context_t *vcrp = (vg_regex_context_t *) vcp;
1690
int i;
1691
for (i = 0; i < vcrp->base.vc_npatterns; i++) {
1692
if (vcrp->vcr_regex_extra[i])
1693
pcre_free(vcrp->vcr_regex_extra[i]);
1694
pcre_free(vcrp->vcr_regex[i]);
1695
}
1696
vcrp->base.vc_npatterns = 0;
1697
vcrp->base.vc_npatterns_start = 0;
1698
vcrp->base.vc_found = 0;
1699
}
1700
1701
static void
1702
vg_regex_context_free(vg_context_t *vcp)
1703
{
1704
vg_regex_context_t *vcrp = (vg_regex_context_t *) vcp;
1705
vg_regex_context_clear_all_patterns(vcp);
1706
if (vcrp->vcr_nalloc)
1707
free(vcrp->vcr_regex);
1708
free(vcrp);
1709
}
1710
1711
static int
1712
vg_regex_test(vg_exec_context_t *vxcp)
1713
{
1714
vg_regex_context_t *vcrp = (vg_regex_context_t *) vxcp->vxc_vc;
1715
1716
unsigned char hash1[32], hash2[32];
1717
int i, zpfx, p, d, nres, re_vec[9];
1718
char b58[40];
1719
BIGNUM bnrem;
1720
BIGNUM *bn, *bndiv, *bnptmp;
1721
int res = 0;
1722
1723
pcre *re;
1724
1725
BN_init(&bnrem);
1726
1727
/* Hash the hash and write the four byte check code */
1728
SHA256(vxcp->vxc_binres, 21, hash1);
1729
SHA256(hash1, sizeof(hash1), hash2);
1730
memcpy(&vxcp->vxc_binres[21], hash2, 4);
1731
1732
bn = &vxcp->vxc_bntmp;
1733
bndiv = &vxcp->vxc_bntmp2;
1734
1735
BN_bin2bn(vxcp->vxc_binres, 25, bn);
1736
1737
/* Compute the complete encoded address */
1738
for (zpfx = 0; zpfx < 25 && vxcp->vxc_binres[zpfx] == 0; zpfx++);
1739
p = sizeof(b58) - 1;
1740
b58[p] = '\0';
1741
while (!BN_is_zero(bn)) {
1742
BN_div(bndiv, &bnrem, bn, &vxcp->vxc_bnbase, vxcp->vxc_bnctx);
1743
bnptmp = bn;
1744
bn = bndiv;
1745
bndiv = bnptmp;
1746
d = BN_get_word(&bnrem);
1747
b58[--p] = vg_b58_alphabet[d];
1748
}
1749
while (zpfx--) {
1750
b58[--p] = vg_b58_alphabet[0];
1751
}
1752
1753
/*
1754
* Run the regular expressions on it
1755
* SLOW, runs in linear time with the number of REs
1756
*/
1757
restart_loop:
1758
nres = vcrp->base.vc_npatterns;
1759
if (!nres) {
1760
res = 2;
1761
goto out;
1762
}
1763
for (i = 0; i < nres; i++) {
1764
d = pcre_exec(vcrp->vcr_regex[i],
1765
vcrp->vcr_regex_extra[i],
1766
&b58[p], (sizeof(b58) - 1) - p, 0,
1767
0,
1768
re_vec, sizeof(re_vec)/sizeof(re_vec[0]));
1769
1770
if (d <= 0) {
1771
if (d != PCRE_ERROR_NOMATCH) {
1772
fprintf(stderr, "PCRE error: %d\n", d);
1773
res = 2;
1774
goto out;
1775
}
1776
continue;
1777
}
1778
1779
re = vcrp->vcr_regex[i];
1780
1781
if (vg_exec_context_upgrade_lock(vxcp) &&
1782
((i >= vcrp->base.vc_npatterns) ||
1783
(vcrp->vcr_regex[i] != re)))
1784
goto restart_loop;
1785
1786
vg_exec_context_consolidate_key(vxcp);
1787
vcrp->base.vc_output_match(&vcrp->base, vxcp->vxc_key,
1788
vcrp->vcr_regex_pat[i]);
1789
vcrp->base.vc_found++;
1790
1791
if (vcrp->base.vc_only_one) {
1792
res = 2;
1793
goto out;
1794
}
1795
1796
if (vcrp->base.vc_remove_on_match) {
1797
pcre_free(vcrp->vcr_regex[i]);
1798
if (vcrp->vcr_regex_extra[i])
1799
pcre_free(vcrp->vcr_regex_extra[i]);
1800
nres -= 1;
1801
vcrp->base.vc_npatterns = nres;
1802
if (!nres) {
1803
res = 2;
1804
goto out;
1805
}
1806
vcrp->vcr_regex[i] = vcrp->vcr_regex[nres];
1807
vcrp->vcr_regex_extra[i] =
1808
vcrp->vcr_regex_extra[nres];
1809
vcrp->vcr_regex_pat[i] = vcrp->vcr_regex_pat[nres];
1810
vcrp->base.vc_npatterns = nres;
1811
vcrp->base.vc_pattern_generation++;
1812
}
1813
res = 1;
1814
}
1815
out:
1816
BN_clear_free(&bnrem);
1817
return res;
1818
}
1819
1820
vg_context_t *
1821
vg_regex_context_new(int addrtype, int privtype)
1822
{
1823
vg_regex_context_t *vcrp;
1824
1825
vcrp = (vg_regex_context_t *) malloc(sizeof(*vcrp));
1826
if (vcrp) {
1827
memset(vcrp, 0, sizeof(*vcrp));
1828
vcrp->base.vc_addrtype = addrtype;
1829
vcrp->base.vc_privtype = privtype;
1830
vcrp->base.vc_npatterns = 0;
1831
vcrp->base.vc_npatterns_start = 0;
1832
vcrp->base.vc_found = 0;
1833
vcrp->base.vc_chance = 0.0;
1834
vcrp->base.vc_free = vg_regex_context_free;
1835
vcrp->base.vc_add_patterns = vg_regex_context_add_patterns;
1836
vcrp->base.vc_clear_all_patterns =
1837
vg_regex_context_clear_all_patterns;
1838
vcrp->base.vc_test = vg_regex_test;
1839
vcrp->base.vc_hash160_sort = NULL;
1840
vcrp->vcr_regex = NULL;
1841
vcrp->vcr_nalloc = 0;
1842
}
1843
return &vcrp->base;
1844
}
1845
1846