-
Notifications
You must be signed in to change notification settings - Fork 209
/
Vanity.cpp
1786 lines (1354 loc) · 44.6 KB
/
Vanity.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "Vanity.h"
#include "Base58.h"
#include "Bech32.h"
#include "hash/sha256.h"
#include "hash/sha512.h"
#include "IntGroup.h"
#include "Wildcard.h"
#include "Timer.h"
#include "hash/ripemd160.h"
#include <string.h>
#include <math.h>
#include <algorithm>
#ifndef WIN64
#include <pthread.h>
#endif
using namespace std;
Point Gn[CPU_GRP_SIZE / 2];
Point _2Gn;
// ----------------------------------------------------------------------------
VanitySearch::VanitySearch(Secp256K1 *secp, vector<std::string> &inputPrefixes,string seed,int searchMode,
bool useGpu, bool stop, string outputFile, bool useSSE, uint32_t maxFound,
uint64_t rekey, bool caseSensitive, Point &startPubKey, bool paranoiacSeed)
:inputPrefixes(inputPrefixes) {
this->secp = secp;
this->searchMode = searchMode;
this->useGpu = useGpu;
this->stopWhenFound = stop;
this->outputFile = outputFile;
this->useSSE = useSSE;
this->nbGPUThread = 0;
this->maxFound = maxFound;
this->rekey = rekey;
this->searchType = -1;
this->startPubKey = startPubKey;
this->hasPattern = false;
this->caseSensitive = caseSensitive;
this->startPubKeySpecified = !startPubKey.isZero();
lastRekey = 0;
prefixes.clear();
// Create a 65536 items lookup table
PREFIX_TABLE_ITEM t;
t.found = true;
t.items = NULL;
for(int i=0;i<65536;i++)
prefixes.push_back(t);
// Check is inputPrefixes contains wildcard character
for (int i = 0; i < (int)inputPrefixes.size() && !hasPattern; i++) {
hasPattern = ((inputPrefixes[i].find('*') != std::string::npos) ||
(inputPrefixes[i].find('?') != std::string::npos) );
}
if (!hasPattern) {
// No wildcard used, standard search
// Insert prefixes
bool loadingProgress = (inputPrefixes.size() > 1000);
if (loadingProgress)
printf("[Building lookup16 0.0%%]\r");
nbPrefix = 0;
onlyFull = true;
for (int i = 0; i < (int)inputPrefixes.size(); i++) {
PREFIX_ITEM it;
std::vector<PREFIX_ITEM> itPrefixes;
if (!caseSensitive) {
// For caseunsensitive search, loop through all possible combination
// and fill up lookup table
vector<string> subList;
enumCaseUnsentivePrefix(inputPrefixes[i], subList);
bool *found = new bool;
*found = false;
for (int j = 0; j < (int)subList.size(); j++) {
if (initPrefix(subList[j], &it)) {
it.found = found;
it.prefix = strdup(it.prefix); // We need to allocate here, subList will be destroyed
itPrefixes.push_back(it);
}
}
if (itPrefixes.size() > 0) {
// Compute difficulty for case unsensitive search
// Not obvious to perform the right calculation here using standard double
// Improvement are welcome
// Get the min difficulty and divide by the number of item having the same difficulty
// Should give good result when difficulty is large enough
double dMin = itPrefixes[0].difficulty;
int nbMin = 1;
for (int j = 1; j < (int)itPrefixes.size(); j++) {
if (itPrefixes[j].difficulty == dMin) {
nbMin++;
} else if (itPrefixes[j].difficulty < dMin) {
dMin = itPrefixes[j].difficulty;
nbMin = 1;
}
}
dMin /= (double)nbMin;
// Updates
for (int j = 0; j < (int)itPrefixes.size(); j++)
itPrefixes[j].difficulty = dMin;
}
} else {
if (initPrefix(inputPrefixes[i], &it)) {
bool *found = new bool;
*found = false;
it.found = found;
itPrefixes.push_back(it);
}
}
if (itPrefixes.size() > 0) {
// Add the item to all correspoding prefixes in the lookup table
for (int j = 0; j < (int)itPrefixes.size(); j++) {
prefix_t p = itPrefixes[j].sPrefix;
if (prefixes[p].items == NULL) {
prefixes[p].items = new vector<PREFIX_ITEM>();
prefixes[p].found = false;
usedPrefix.push_back(p);
}
(*prefixes[p].items).push_back(itPrefixes[j]);
}
onlyFull &= it.isFull;
nbPrefix++;
}
if (loadingProgress && i % 1000 == 0)
printf("[Building lookup16 %5.1f%%]\r", (((double)i) / (double)(inputPrefixes.size() - 1)) * 100.0);
}
if (loadingProgress)
printf("\n");
//dumpPrefixes();
if (!caseSensitive && searchType == BECH32) {
printf("Error, case unsensitive search with BECH32 not allowed.\n");
exit(1);
}
if (nbPrefix == 0) {
printf("VanitySearch: nothing to search !\n");
exit(1);
}
// Second level lookup
uint32_t unique_sPrefix = 0;
uint32_t minI = 0xFFFFFFFF;
uint32_t maxI = 0;
for (int i = 0; i < (int)prefixes.size(); i++) {
if (prefixes[i].items) {
LPREFIX lit;
lit.sPrefix = i;
if (prefixes[i].items) {
for (int j = 0; j < (int)prefixes[i].items->size(); j++) {
lit.lPrefixes.push_back((*prefixes[i].items)[j].lPrefix);
}
}
sort(lit.lPrefixes.begin(), lit.lPrefixes.end());
usedPrefixL.push_back(lit);
if ((uint32_t)lit.lPrefixes.size() > maxI) maxI = (uint32_t)lit.lPrefixes.size();
if ((uint32_t)lit.lPrefixes.size() < minI) minI = (uint32_t)lit.lPrefixes.size();
unique_sPrefix++;
}
if (loadingProgress)
printf("[Building lookup32 %.1f%%]\r", ((double)i*100.0) / (double)prefixes.size());
}
if (loadingProgress)
printf("\n");
_difficulty = getDiffuclty();
string seachInfo = string(searchModes[searchMode]) + (startPubKeySpecified ? ", with public key" : "");
if (nbPrefix == 1) {
if (!caseSensitive) {
// Case unsensitive search
printf("Difficulty: %.0f\n", _difficulty);
printf("Search: %s [%s, Case unsensitive] (Lookup size %d)\n", inputPrefixes[0].c_str(), seachInfo.c_str(), unique_sPrefix);
} else {
printf("Difficulty: %.0f\n", _difficulty);
printf("Search: %s [%s]\n", inputPrefixes[0].c_str(), seachInfo.c_str());
}
} else {
if (onlyFull) {
printf("Search: %d addresses (Lookup size %d,[%d,%d]) [%s]\n", nbPrefix, unique_sPrefix, minI, maxI, seachInfo.c_str());
} else {
printf("Search: %d prefixes (Lookup size %d) [%s]\n", nbPrefix, unique_sPrefix, seachInfo.c_str());
}
}
} else {
// Wild card search
switch (inputPrefixes[0].data()[0]) {
case '1':
searchType = P2PKH;
break;
case '3':
searchType = P2SH;
break;
case 'b':
case 'B':
searchType = BECH32;
break;
default:
printf("Invalid start character 1,3 or b, expected");
exit(1);
}
string searchInfo = string(searchModes[searchMode]) + (startPubKeySpecified ? ", with public key" : "");
if (inputPrefixes.size() == 1) {
printf("Search: %s [%s]\n", inputPrefixes[0].c_str(), searchInfo.c_str());
} else {
printf("Search: %d patterns [%s]\n", (int)inputPrefixes.size(), searchInfo.c_str());
}
patternFound = (bool *)malloc(inputPrefixes.size()*sizeof(bool));
memset(patternFound,0, inputPrefixes.size() * sizeof(bool));
}
// Compute Generator table G[n] = (n+1)*G
Point g = secp->G;
Gn[0] = g;
g = secp->DoubleDirect(g);
Gn[1] = g;
for (int i = 2; i < CPU_GRP_SIZE/2; i++) {
g = secp->AddDirect(g,secp->G);
Gn[i] = g;
}
// _2Gn = CPU_GRP_SIZE*G
_2Gn = secp->DoubleDirect(Gn[CPU_GRP_SIZE/2-1]);
// Constant for endomorphism
// if a is a nth primitive root of unity, a^-1 is also a nth primitive root.
// beta^3 = 1 mod p implies also beta^2 = beta^-1 mop (by multiplying both side by beta^-1)
// (beta^3 = 1 mod p), beta2 = beta^-1 = beta^2
// (lambda^3 = 1 mod n), lamba2 = lamba^-1 = lamba^2
beta.SetBase16("7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee");
lambda.SetBase16("5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72");
beta2.SetBase16("851695d49a83f8ef919bb86153cbcb16630fb68aed0a766a3ec693d68e6afa40");
lambda2.SetBase16("ac9c52b33fa3cf1f5ad9e3fd77ed9ba4a880b9fc8ec739c2e0cfc810b51283ce");
// Seed
if (seed.length() == 0) {
// Default seed
seed = Timer::getSeed(32);
}
if (paranoiacSeed) {
seed += Timer::getSeed(32);
}
// Protect seed against "seed search attack" using pbkdf2_hmac_sha512
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);
startKey.SetInt32(0);
sha256(hseed, 64, (unsigned char *)startKey.bits64);
char *ctimeBuff;
time_t now = time(NULL);
ctimeBuff = ctime(&now);
printf("Start %s", ctimeBuff);
if (rekey > 0) {
printf("Base Key: Randomly changed every %.0f Mkeys\n",(double)rekey);
} else {
printf("Base Key: %s\n", startKey.GetBase16().c_str());
}
}
// ----------------------------------------------------------------------------
bool VanitySearch::isSingularPrefix(std::string pref) {
// check is the given prefix contains only 1
bool only1 = true;
int i=0;
while (only1 && i < (int)pref.length()) {
only1 = pref.data()[i] == '1';
i++;
}
return only1;
}
// ----------------------------------------------------------------------------
bool VanitySearch::initPrefix(std::string &prefix,PREFIX_ITEM *it) {
std::vector<unsigned char> result;
string dummy1 = prefix;
int nbDigit = 0;
bool wrong = false;
if (prefix.length() < 2) {
printf("Ignoring prefix \"%s\" (too short)\n",prefix.c_str());
return false;
}
int aType = -1;
switch (prefix.data()[0]) {
case '1':
aType = P2PKH;
break;
case '3':
aType = P2SH;
break;
case 'b':
case 'B':
std::transform(prefix.begin(), prefix.end(), prefix.begin(), ::tolower);
if(strncmp(prefix.c_str(), "bc1q", 4) == 0)
aType = BECH32;
break;
}
if (aType==-1) {
printf("Ignoring prefix \"%s\" (must start with 1 or 3 or bc1q)\n", prefix.c_str());
return false;
}
if (searchType == -1) searchType = aType;
if (aType != searchType) {
printf("Ignoring prefix \"%s\" (P2PKH, P2SH or BECH32 allowed at once)\n", prefix.c_str());
return false;
}
if (aType == BECH32) {
// BECH32
uint8_t witprog[40];
size_t witprog_len;
int witver;
const char* hrp = "bc";
int ret = segwit_addr_decode(&witver, witprog, &witprog_len, hrp, prefix.c_str());
// Try to attack a full address ?
if (ret && witprog_len==20) {
// mamma mia !
it->difficulty = pow(2, 160);
it->isFull = true;
memcpy(it->hash160, witprog, 20);
it->sPrefix = *(prefix_t *)(it->hash160);
it->lPrefix = *(prefixl_t *)(it->hash160);
it->prefix = (char *)prefix.c_str();
it->prefixLength = (int)prefix.length();
return true;
}
if (prefix.length() < 5) {
printf("Ignoring prefix \"%s\" (too short, length<5 )\n", prefix.c_str());
return false;
}
if (prefix.length() >= 36) {
printf("Ignoring prefix \"%s\" (too long, length>36 )\n", prefix.c_str());
return false;
}
uint8_t data[64];
memset(data,0,64);
size_t data_length;
if(!bech32_decode_nocheck(data,&data_length,prefix.c_str()+4)) {
printf("Ignoring prefix \"%s\" (Only \"023456789acdefghjklmnpqrstuvwxyz\" allowed)\n", prefix.c_str());
return false;
}
// Difficulty
it->sPrefix = *(prefix_t *)data;
it->difficulty = pow(2, 5*(prefix.length()-4));
it->isFull = false;
it->lPrefix = 0;
it->prefix = (char *)prefix.c_str();
it->prefixLength = (int)prefix.length();
return true;
} else {
// P2PKH/P2SH
wrong = !DecodeBase58(prefix, result);
if (wrong) {
if (caseSensitive)
printf("Ignoring prefix \"%s\" (0, I, O and l not allowed)\n", prefix.c_str());
return false;
}
// Try to attack a full address ?
if (result.size() > 21) {
// mamma mia !
//if (!secp.CheckPudAddress(prefix)) {
// printf("Warning, \"%s\" (address checksum may never match)\n", prefix.c_str());
//}
it->difficulty = pow(2, 160);
it->isFull = true;
memcpy(it->hash160, result.data() + 1, 20);
it->sPrefix = *(prefix_t *)(it->hash160);
it->lPrefix = *(prefixl_t *)(it->hash160);
it->prefix = (char *)prefix.c_str();
it->prefixLength = (int)prefix.length();
return true;
}
// Prefix containing only '1'
if (isSingularPrefix(prefix)) {
if (prefix.length() > 21) {
printf("Ignoring prefix \"%s\" (Too much 1)\n", prefix.c_str());
return false;
}
// Difficulty
it->difficulty = pow(256, prefix.length() - 1);
it->isFull = false;
it->sPrefix = 0;
it->lPrefix = 0;
it->prefix = (char *)prefix.c_str();
it->prefixLength = (int)prefix.length();
return true;
}
// Search for highest hash160 16bit prefix (most probable)
while (result.size() < 25) {
DecodeBase58(dummy1, result);
if (result.size() < 25) {
dummy1.append("1");
nbDigit++;
}
}
if (searchType == P2SH) {
if (result.data()[0] != 5) {
if(caseSensitive)
printf("Ignoring prefix \"%s\" (Unreachable, 31h1 to 3R2c only)\n", prefix.c_str());
return false;
}
}
if (result.size() != 25) {
printf("Ignoring prefix \"%s\" (Invalid size)\n", prefix.c_str());
return false;
}
//printf("VanitySearch: Found prefix %s\n",GetHex(result).c_str() );
it->sPrefix = *(prefix_t *)(result.data() + 1);
dummy1.append("1");
DecodeBase58(dummy1, result);
if (result.size() == 25) {
//printf("VanitySearch: Found prefix %s\n", GetHex(result).c_str());
it->sPrefix = *(prefix_t *)(result.data() + 1);
nbDigit++;
}
// Difficulty
it->difficulty = pow(2, 192) / pow(58, nbDigit);
it->isFull = false;
it->lPrefix = 0;
it->prefix = (char *)prefix.c_str();
it->prefixLength = (int)prefix.length();
return true;
}
}
// ----------------------------------------------------------------------------
void VanitySearch::dumpPrefixes() {
for (int i = 0; i < 0xFFFF; i++) {
if (prefixes[i].items) {
printf("%04X\n", i);
for (int j = 0; j < (int)prefixes[i].items->size(); j++) {
printf(" %d\n", (*prefixes[i].items)[j].sPrefix);
printf(" %g\n", (*prefixes[i].items)[j].difficulty);
printf(" %s\n", (*prefixes[i].items)[j].prefix);
}
}
}
}
// ----------------------------------------------------------------------------
void VanitySearch::enumCaseUnsentivePrefix(std::string s, std::vector<std::string> &list) {
char letter[64];
int letterpos[64];
int nbLetter = 0;
int length = (int)s.length();
for (int i = 1; i < length; i++) {
char c = s.data()[i];
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ) {
letter[nbLetter] = tolower(c);
letterpos[nbLetter] = i;
nbLetter++;
}
}
int total = 1 << nbLetter;
for (int i = 0; i < total; i++) {
char tmp[64];
strcpy(tmp, s.c_str());
for (int j = 0; j < nbLetter; j++) {
int mask = 1 << j;
if (mask&i) tmp[letterpos[j]] = toupper(letter[j]);
else tmp[letterpos[j]] = letter[j];
}
list.push_back(string(tmp));
}
}
// ----------------------------------------------------------------------------
double VanitySearch::getDiffuclty() {
double min = pow(2,160);
if (onlyFull)
return min;
for (int i = 0; i < (int)usedPrefix.size(); i++) {
int p = usedPrefix[i];
if (prefixes[p].items) {
for (int j = 0; j < (int)prefixes[p].items->size(); j++) {
if (!*((*prefixes[p].items)[j].found)) {
if ((*prefixes[p].items)[j].difficulty < min)
min = (*prefixes[p].items)[j].difficulty;
}
}
}
}
return min;
}
double log1(double x) {
// Use taylor series to approximate log(1-x)
return -x - (x*x)/2.0 - (x*x*x)/3.0 - (x*x*x*x)/4.0;
}
string VanitySearch::GetExpectedTime(double keyRate,double keyCount) {
char tmp[128];
string ret;
if(hasPattern)
return "";
double P = 1.0/ _difficulty;
// pow(1-P,keyCount) is the probality of failure after keyCount tries
double cP = 1.0 - pow(1-P,keyCount);
sprintf(tmp,"[Prob %.1f%%]",cP*100.0);
ret = string(tmp);
double desiredP = 0.5;
while(desiredP<cP)
desiredP += 0.1;
if(desiredP>=0.99) desiredP = 0.99;
double k = log(1.0-desiredP)/log(1.0-P);
if (isinf(k)) {
// Try taylor
k = log(1.0 - desiredP)/log1(P);
}
double dTime = (k-keyCount)/keyRate; // Time to perform k tries
if(dTime<0) dTime = 0;
double nbDay = dTime / 86400.0;
if (nbDay >= 1) {
double nbYear = nbDay/365.0;
if (nbYear > 1) {
if(nbYear<5)
sprintf(tmp, "[%.f%% in %.1fy]", desiredP*100.0, nbYear);
else
sprintf(tmp, "[%.f%% in %gy]", desiredP*100.0, nbYear);
} else {
sprintf(tmp, "[%.f%% in %.1fd]", desiredP*100.0, nbDay);
}
} else {
int iTime = (int)dTime;
int nbHour = (int)((iTime % 86400) / 3600);
int nbMin = (int)(((iTime % 86400) % 3600) / 60);
int nbSec = (int)(iTime % 60);
sprintf(tmp, "[%.f%% in %02d:%02d:%02d]", desiredP*100.0, nbHour, nbMin, nbSec);
}
return ret + string(tmp);
}
// ----------------------------------------------------------------------------
void VanitySearch::output(string addr,string pAddr,string pAddrHex) {
#ifdef WIN64
WaitForSingleObject(ghMutex,INFINITE);
#else
pthread_mutex_lock(&ghMutex);
#endif
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;
}
}
if(!needToClose)
printf("\n");
fprintf(f, "PubAddress: %s\n", addr.c_str());
if (startPubKeySpecified) {
fprintf(f, "PartialPriv: %s\n", pAddr.c_str());
} else {
switch (searchType) {
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);
#ifdef WIN64
ReleaseMutex(ghMutex);
#else
pthread_mutex_unlock(&ghMutex);
#endif
}
// ----------------------------------------------------------------------------
void VanitySearch::updateFound() {
// Check if all prefixes has been found
// Needed only if stopWhenFound is asked
if (stopWhenFound) {
if (hasPattern) {
bool allFound = true;
for (int i = 0; i < (int)inputPrefixes.size(); i++) {
allFound &= patternFound[i];
}
endOfSearch = allFound;
} else {
bool allFound = true;
for (int i = 0; i < (int)usedPrefix.size(); i++) {
bool iFound = true;
prefix_t p = usedPrefix[i];
if (!prefixes[p].found) {
if (prefixes[p].items) {
for (int j = 0; j < (int)prefixes[p].items->size(); j++) {
iFound &= *((*prefixes[p].items)[j].found);
}
}
prefixes[usedPrefix[i]].found = iFound;
}
allFound &= iFound;
}
endOfSearch = allFound;
// Update difficulty to the next most probable item
_difficulty = getDiffuclty();
}
}
}
// ----------------------------------------------------------------------------
bool VanitySearch::checkPrivKey(string addr, Int &key, int32_t incr, int endomorphism, bool mode) {
Int k(&key);
Point sp = startPubKey;
if (incr < 0) {
k.Add((uint64_t)(-incr));
k.Neg();
k.Add(&secp->order);
if (startPubKeySpecified) sp.y.ModNeg();
} else {
k.Add((uint64_t)incr);
}
// Endomorphisms
switch (endomorphism) {
case 1:
k.ModMulK1order(&lambda);
if(startPubKeySpecified) sp.x.ModMulK1(&beta);
break;
case 2:
k.ModMulK1order(&lambda2);
if (startPubKeySpecified) sp.x.ModMulK1(&beta2);
break;
}
// Check addresses
Point p = secp->ComputePublicKey(&k);
if (startPubKeySpecified) p = secp->AddDirect(p, sp);
string chkAddr = secp->GetAddress(searchType, mode, p);
if (chkAddr != addr) {
//Key may be the opposite one (negative zero or compressed key)
k.Neg();
k.Add(&secp->order);
p = secp->ComputePublicKey(&k);
if (startPubKeySpecified) {
sp.y.ModNeg();
p = secp->AddDirect(p, sp);
}
string chkAddr = secp->GetAddress(searchType, mode, p);
if (chkAddr != addr) {
printf("\nWarning, wrong private key generated !\n");
printf(" Addr :%s\n", addr.c_str());
printf(" Check:%s\n", chkAddr.c_str());
printf(" Endo:%d incr:%d comp:%d\n", endomorphism, incr, mode);
return false;
}
}
output(addr, secp->GetPrivAddress(mode ,k), k.GetBase16());
return true;
}
void VanitySearch::checkAddrSSE(uint8_t *h1, uint8_t *h2, uint8_t *h3, uint8_t *h4,
int32_t incr1, int32_t incr2, int32_t incr3, int32_t incr4,
Int &key, int endomorphism, bool mode) {
vector<string> addr = secp->GetAddress(searchType, mode, h1,h2,h3,h4);
for (int i = 0; i < (int)inputPrefixes.size(); i++) {
if (Wildcard::match(addr[0].c_str(), inputPrefixes[i].c_str(), caseSensitive)) {
// Found it !
//*((*pi)[i].found) = true;
if (checkPrivKey(addr[0], key, incr1, endomorphism, mode)) {
nbFoundKey++;
patternFound[i] = true;
updateFound();
}
}
if (Wildcard::match(addr[1].c_str(), inputPrefixes[i].c_str(), caseSensitive)) {
// Found it !
//*((*pi)[i].found) = true;
if (checkPrivKey(addr[1], key, incr2, endomorphism, mode)) {
nbFoundKey++;
patternFound[i] = true;
updateFound();
}
}
if (Wildcard::match(addr[2].c_str(), inputPrefixes[i].c_str(), caseSensitive)) {
// Found it !
//*((*pi)[i].found) = true;
if (checkPrivKey(addr[2], key, incr3, endomorphism, mode)) {
nbFoundKey++;
patternFound[i] = true;
updateFound();
}
}
if (Wildcard::match(addr[3].c_str(), inputPrefixes[i].c_str(), caseSensitive)) {
// Found it !
//*((*pi)[i].found) = true;
if (checkPrivKey(addr[3], key, incr4, endomorphism, mode)) {
nbFoundKey++;
patternFound[i] = true;
updateFound();
}
}
}
}
void VanitySearch::checkAddr(int prefIdx, uint8_t *hash160, Int &key, int32_t incr, int endomorphism, bool mode) {
if (hasPattern) {
// Wildcard search
string addr = secp->GetAddress(searchType, mode, hash160);
for (int i = 0; i < (int)inputPrefixes.size(); i++) {
if (Wildcard::match(addr.c_str(), inputPrefixes[i].c_str(), caseSensitive)) {
// Found it !
//*((*pi)[i].found) = true;
if (checkPrivKey(addr, key, incr, endomorphism, mode)) {
nbFoundKey++;
patternFound[i] = true;
updateFound();
}
}
}
return;
}
vector<PREFIX_ITEM> *pi = prefixes[prefIdx].items;
if (onlyFull) {
// Full addresses
for (int i = 0; i < (int)pi->size(); i++) {
if (stopWhenFound && *((*pi)[i].found))
continue;
if (ripemd160_comp_hash((*pi)[i].hash160, hash160)) {
// Found it !
*((*pi)[i].found) = true;
// You believe it ?
if (checkPrivKey(secp->GetAddress(searchType, mode, hash160), key, incr, endomorphism, mode)) {
nbFoundKey++;
updateFound();
}
}
}
} else {
char a[64];
string addr = secp->GetAddress(searchType, mode, hash160);
for (int i = 0; i < (int)pi->size(); i++) {
if (stopWhenFound && *((*pi)[i].found))
continue;
strncpy(a, addr.c_str(), (*pi)[i].prefixLength);
a[(*pi)[i].prefixLength] = 0;
if (strcmp((*pi)[i].prefix, a) == 0) {
// Found it !
*((*pi)[i].found) = true;
if (checkPrivKey(addr, key, incr, endomorphism, mode)) {
nbFoundKey++;
updateFound();
}
}
}
}
}
// ----------------------------------------------------------------------------
#ifdef WIN64
DWORD WINAPI _FindKey(LPVOID lpParam) {
#else
void *_FindKey(void *lpParam) {
#endif
TH_PARAM *p = (TH_PARAM *)lpParam;
p->obj->FindKeyCPU(p);
return 0;
}
#ifdef WIN64
DWORD WINAPI _FindKeyGPU(LPVOID lpParam) {
#else
void *_FindKeyGPU(void *lpParam) {
#endif
TH_PARAM *p = (TH_PARAM *)lpParam;
p->obj->FindKeyGPU(p);
return 0;
}
// ----------------------------------------------------------------------------
void VanitySearch::checkAddresses(bool compressed, Int key, int i, Point p1) {
unsigned char h0[20];