-
Notifications
You must be signed in to change notification settings - Fork 4
/
Vanity.cpp
1936 lines (1553 loc) · 55.5 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 "Timer.h"
#include "hash/ripemd160.h"
#include <string.h>
#include <math.h>
#include <algorithm>
#ifndef WIN64
#include <pthread.h>
#endif
#include <chrono>
#include <iostream>
#include <fstream>
using namespace std;
Point Sp[256];
Int dS[256];
// ----------------------------------------------------------------------------
// max=2^48, with fractional part
char * prefSI_double(char *s, size_t s_size, double doNum) {
size_t ind_si = 0;
char prefSI_list[9] = { ' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
while ((uint64_t)(doNum/1000) > 0) {
ind_si += 1;
doNum /= 1000;
if (ind_si > 100) {
printf("\n[FATAL_ERROR] infinity loop in prefSI!\n");
exit(EXIT_FAILURE);
}
}
if (ind_si < sizeof(prefSI_list) / sizeof(prefSI_list[0])) {
snprintf(&s[0], s_size, "%5.1lf", doNum);
snprintf(&s[0+5], s_size-5, "%c", prefSI_list[ind_si]);
}
else {
snprintf(&s[0], s_size, "infini");
}
return s;
}
// max=2^256, without fractional part
char * prefSI_Int(char *s, size_t s_size, Int bnNum) {
size_t ind_si = 0;
char prefSI_list[9] = { ' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
Int bnZero; bnZero.SetInt32(0);
Int bn1000; bn1000.SetInt32(1000);
Int bnTmp; bnTmp = bnNum;
bnTmp.Div(&bn1000);
while (bnTmp.IsGreater(&bnZero)) {
ind_si += 1;
bnTmp.Div(&bn1000);
bnNum.Div(&bn1000);
if (ind_si > 100) {
printf("\n[FATAL_ERROR] infinity loop in prefSI!\n");
exit(EXIT_FAILURE);
}
}
if (ind_si < sizeof(prefSI_list) / sizeof(prefSI_list[0])) {
snprintf(&s[0], s_size, "%3s.0", bnNum.GetBase10().c_str());
snprintf(&s[0+5], s_size-5, "%c", prefSI_list[ind_si]);
}
else {
snprintf(&s[0], s_size, "infini");
}
return s;
}
void passtime_tm(char *s, size_t s_size, const struct tm *tm) {
size_t offset_start = 0;
if (tm->tm_year - 70 > 0) {
snprintf(&s[offset_start], s_size - offset_start, " %1iy", tm->tm_year - 70);
offset_start += 3;
if (((tm->tm_year - 70) / 10) != 0) offset_start += 1;
if (((tm->tm_year - 70) / 100) != 0) offset_start += 1;
if (((tm->tm_year - 70) / 1000) != 0) offset_start += 1;
if (((tm->tm_year - 70) / 10000) != 0) offset_start += 1;
if (((tm->tm_year - 70) / 100000) != 0) offset_start += 1;
if (((tm->tm_year - 70) / 1000000) != 0) offset_start += 1;
if (((tm->tm_year - 70) / 10000000) != 0) offset_start += 1;
if (((tm->tm_year - 70) / 100000000) != 0) offset_start += 1;
if (((tm->tm_year - 70) / 1000000000) != 0) offset_start += 1;
}
if (tm->tm_mon > 0 || tm->tm_year - 70 > 0) {
snprintf(&s[offset_start], s_size - offset_start, " %2im", tm->tm_mon);
offset_start += 4;
}
if (tm->tm_mday - 1 > 0 || tm->tm_mon > 0 || tm->tm_year - 70 > 0) {
snprintf(&s[offset_start], s_size - offset_start, " %2id", tm->tm_mday - 1);
offset_start += 4;
}
snprintf(&s[offset_start], s_size - offset_start, " %02i:%02i:%02is", tm->tm_hour, tm->tm_min, tm->tm_sec);
}
void passtime(char *s, size_t s_size, Int& bnSec, double usec = 0.0, char v[] = "11111100") {
size_t ofst = 0;
Int bnTmp;
Int bnZero; bnZero.SetInt32(0);
char buff_s6[6+1] = {0};
Int Y_tmp; Y_tmp.SetInt32(0);
Int M_tmp; M_tmp.SetInt32(0);
Int d_tmp; d_tmp.SetInt32(0);
Int h_tmp; h_tmp.SetInt32(0);
Int m_tmp; m_tmp.SetInt32(0);
Int s_tmp; s_tmp.SetInt32(0);
int ms_tmp = 0, us_tmp = 0;
if (usec != 0.0) {
ms_tmp = (int)((uint64_t)(usec * 1000) % 1000);
us_tmp = (int)((uint64_t)(usec * 1000000) % 1000);
}
if (v[0] != 48) { // year
Y_tmp = bnSec;
bnTmp.SetInt32(60*60*24*30*12); Y_tmp.Div(&bnTmp);
if (Y_tmp.IsGreater(&bnZero)) {
prefSI_Int(buff_s6, sizeof(buff_s6), Y_tmp);
snprintf(&s[ofst], s_size-ofst, " %6sy", buff_s6); ofst += 8;
}
}
if (v[1] != 48) { //month
M_tmp = bnSec;
bnTmp.SetInt32(60*60*24*30); M_tmp.Div(&bnTmp);
bnTmp.SetInt32(12); M_tmp.Mod(&bnTmp);
if (M_tmp.IsGreater(&bnZero) || Y_tmp.IsGreater(&bnZero)) {
snprintf(&s[ofst], s_size-ofst, " %2sm", M_tmp.GetBase10().c_str()); ofst += 4;
}
}
if (v[2] != 48) { //day
d_tmp = bnSec;
bnTmp.SetInt32(60*60*24); d_tmp.Div(&bnTmp);
bnTmp.SetInt32(30); d_tmp.Mod(&bnTmp);
if (d_tmp.IsGreater(&bnZero) || M_tmp.IsGreater(&bnZero) || Y_tmp.IsGreater(&bnZero)) {
snprintf(&s[ofst], s_size-ofst, " %02sd", d_tmp.GetBase10().c_str()); ofst += 4;
}
}
if (v[3] != 48) { //hour
h_tmp = bnSec;
bnTmp.SetInt32(60*60); h_tmp.Div(&bnTmp);
bnTmp.SetInt32(24); h_tmp.Mod(&bnTmp);
if (1) {
snprintf(&s[ofst], s_size-ofst, " %02s", h_tmp.GetBase10().c_str()); ofst += 3;
}
}
if (v[4] != 48) { //min
m_tmp = bnSec;
bnTmp.SetInt32(60); m_tmp.Div(&bnTmp);
bnTmp.SetInt32(60); m_tmp.Mod(&bnTmp);
if (1) {
snprintf(&s[ofst], s_size-ofst, ":%02s", m_tmp.GetBase10().c_str()); ofst += 3;
}
}
if (v[5] != 48) { //sec
s_tmp = bnSec;
bnTmp.SetInt32(60); s_tmp.Mod(&bnTmp);
if (1) {
snprintf(&s[ofst], s_size-ofst, ":%02s", s_tmp.GetBase10().c_str()); ofst += 3;
}
}
if (v[6] != 48) { //msec
if ((v[6] == 49)
|| (Y_tmp.IsEqual(&bnZero) && M_tmp.IsEqual(&bnZero) && d_tmp.IsEqual(&bnZero)
&& h_tmp.IsEqual(&bnZero) && m_tmp.IsEqual(&bnZero) && s_tmp.IsEqual(&bnZero)
)
) {
snprintf(&s[ofst], s_size-ofst, " %03dms", ms_tmp); ofst += 6;
}
}
if (v[7] != 48) { //usec
if ((v[7] == 49)
|| (Y_tmp.IsEqual(&bnZero) && M_tmp.IsEqual(&bnZero) && d_tmp.IsEqual(&bnZero)
&& h_tmp.IsEqual(&bnZero) && m_tmp.IsEqual(&bnZero) && s_tmp.IsEqual(&bnZero)
&& ms_tmp==0)
) {
snprintf(&s[ofst], s_size-ofst, " %03dus", us_tmp); ofst += 6;
}
}
}
// ----------------------------------------------------------------------------
//VanitySearch::VanitySearch(Secp256K1 *secp, Point targetPubKey, structW *stR, int nbThread, string outputFile, int flag_verbose){
VanitySearch::VanitySearch(Secp256K1 *secp, vector<std::string> &inputPrefixes, Point targetPubKey, structW *stR, int nbThread, int KangPower, bool stop, std::string outputFile, int flag_verbose, uint32_t maxFound, uint64_t rekey, bool flag_comparator)
:inputPrefixes(inputPrefixes) {
this->secp = secp;
this->inputPrefixes = inputPrefixes;
this->targetPubKey = targetPubKey;
this->stR = stR;
this->bnL = stR->bnL;
this->bnU = stR->bnU;
this->bnW = stR->bnW;
this->pow2L = stR->pow2L;
this->pow2U = stR->pow2U;
this->pow2W = stR->pow2W;
this->bnM = stR->bnM;
this->bnWsqrt = stR->bnWsqrt;
this->pow2Wsqrt = stR->pow2Wsqrt;
this->nbThread = nbThread;
this->KangPower = KangPower;
this->flag_comparator = flag_comparator;
this->nbGPUThread = 0;
this->maxFound = maxFound;
this->rekey = rekey;
//this->searchMode = 0;
//this->searchType = 0;
//this->searchMode = searchMode;
this->outputFile = outputFile;
this->flag_verbose = flag_verbose;
lastRekey = 0;
prefixes.clear();
// Create a 131072 items lookup table
PREFIX_TABLE_ITEM t;
t.found = true;
t.items = NULL;
for(int i=0;i<131072;i++)
prefixes.push_back(t);
printf("[rangeW] 2^%u..2^%u ; W = U - L = 2^%u\n"
, pow2L, pow2U
, pow2W
);
/////////////////////////////////////////////////
// hashtable for distinguished points
// DPht,DTht,DWht - points, distinguished points of Tp/Wp
// in hashtable, provides uniqueness distinguished points
countDT = countDW = countColl = 0;
//maxDP = 1 << 10; // 2^10=1024
maxDP = 1 << 20; // 2^20=1048576
printf("[DPsize] %llu (hashtable size)\n", maxDP);
HASH_SIZE = 2 * maxDP;
DPht = (hashtb_entry *)calloc(HASH_SIZE, sizeof(hashtb_entry));
if (NULL == DPht) {
printf("\n[FATAL ERROR] can't alloc mem %.2f %s \n", (float)(HASH_SIZE) * sizeof(hashtb_entry)/1024/1024/1024, "GiB");
exit(EXIT_FAILURE);
}
/////////////////////////////////////////////////
printf("[~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~]\n");
printf("[pubkey#%d] loaded\n", pow2U);
printf("[Xcoordinate] %064s\n", targetPubKey.x.GetBase16().c_str());
printf("[Ycoordinate] %064s\n", targetPubKey.y.GetBase16().c_str());
if (!secp->EC(targetPubKey)) {
printf("\n[FATAL_ERROR] invalid public key (Not lie on elliptic curve)\n");
exit(EXIT_FAILURE);
}
printf("[~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~]\n");
/////////////////////////////////////////////////
// pre-compute set S(i) jumps of pow2 jumpsize
Sp[0] = secp->G;
dS[0].SetInt32(1);
for (int i = 1; i < 256; ++i) {
dS[i].Add(&dS[i-1], &dS[i-1]);
Sp[i] = secp->DoubleAffine(Sp[i-1]);
}
printf("[+] Sp-table of pow2 points - ready \n");
//exit(1);
/*
string searchInfo = string(searchModes[searchMode]);
if (inputPrefixes.size() == 1) {
printf("Search: %s [%s]\n", inputPrefixes[0].c_str(), searchInfo.c_str());
} else {
printf("Search: %s [%s]\n", inputPrefixes[0].c_str(), searchInfo.c_str());
}
*/
}
// ----------------------------------------------------------------------------
bool VanitySearch::output(string msg) {
FILE *f = stdout;
f = fopen(outputFile.c_str(), "a");
if (f == NULL) {
printf("[error] Cannot open file '%s' for writing! \n", outputFile.c_str());
f = stdout;
return false;
}
else {
fprintf(f, "%s\n", msg.c_str());
fclose(f);
return true;
}
}
// ----------------------------------------------------------------------------
bool VanitySearch::outputgpu(string msg) {
FILE *f = stdout;
f = fopen(outputFile.c_str(), "a");
if (f == NULL) {
printf("[error] Cannot open file '%s' for writing! \n", outputFile.c_str());
f = stdout;
return false;
}
else {
fprintf(f, "\nPriv: %s\n", msg.c_str());
fprintf(f, "\nTips: 1NULY7DhzuNvSDtPkFzNo6oRTZQWBqXNE9 \n");
fclose(f);
return true;
}
}
// ----------------------------------------------------------------------------
/*
//bool VanitySearch::checkPrivKeyGPU(string addr, Int &key, int32_t incr, bool mode) {
bool VanitySearch::checkPrivKeyGPU(string addr, Int &key, int32_t incr, bool mode, uint32_t itThId, Int *greckey) {
Int k(&key);
printf("\n ItThId: %d ", itThId);
Int hincr;
//hincr.SetInt32(0);
//uint64_t kadd = (uint64_t)STEP_SIZE * 8388608 * (kadd_count);// STEP_SIZE 1024
//uint64_t kadd = 2048 * 8388608 * (kadd_count);// STEP_SIZE 2048
//hincr.Add(kadd);
hincr.SetInt32(1024);//hincr.SetInt32(2048);
hincr.Mult(8388608 * (kadd_count));
if (incr > 0) hincr.Add(((uint64_t)incr) * 8388608);
hincr.Neg();
k.Add(&hincr);
//if (incr > 0) k.Add(((uint64_t)incr) * 8388608);
printf("\n Incr: %d compressed: %d", incr, mode);
// Get line in addr.txt and recovery privkey
std::ifstream f;
f.open("addr.txt", std::ifstream::in);
std::string saddr = addr.c_str();
std::string gline;
uint64_t snum = 0 ;
uint64_t linenum = 0 ;
while (std::getline(f, gline)){
snum++;
if (saddr == gline){
linenum = snum;
}
}
f.close();
//
printf("\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n");
printf("\n !!! Address: %s Line num: %llu \n", addr.c_str(), linenum);
printf(" !!! KeyThId: %s \n", greckey[itThId].GetBase16().c_str());
printf(" !!! TempKey: %s \n", k.GetBase16().c_str());
//printf(" !!! Disable GPU and Use -bits KeyThId:TempKey \n");
printf(" !!! Disable GPU and Use -bits TempKey:KeyThId \n");
// recovery privkey
//linenum -= 1;
int keybit = k.GetBitLength();
printf(" !!! TempKey: %d bits \n", keybit);
//Int koff;
//koff.SetInt32(linenum);
//koff.SetInt32(1);
//printf(" !!! koff: %s \n", koff.GetBase16().c_str());
//koff.Mult(linenum);
//printf(" !!! temp: %s \n", koff.GetBase16().c_str());
//koff.Neg();
//k.Add(&koff);
// end recovery
//std::string realAddr;
//Point preal = secp->ComputePublicKey(&k);
//realAddr = secp->GetAddress(searchType, mode, preal);
//printf(" !!! Recovery Address: %s \n", realAddr.c_str());
//printf(" !!! Recovery Key: %s \n", k.GetBase16().c_str());
printf("\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n");
string outmsg = "KeyThId: " + greckey[itThId].GetBase16() + "\n" + "TempKey: " + k.GetBase16();
outputgpu(outmsg);
//outputgpu(k.GetBase16().c_str());
// Command save
printf("\n");
#ifdef WIN64
#else
string comm = "cp ./TempKey_gpu.txt ../drive/'My Drive'/TempKey_gpu.txt";
const char* scomm = comm.c_str();
bool syscomm = system(scomm);
#endif
// End Command save
Timer::SleepMillis(1000);
// Start kangaroo
string pkhex = secp->GetPublicKeyHex(true, targetPubKey);
printf("\n[PubKey: %s]\n", pkhex.c_str());
#ifdef WIN64
#else
//string comm2 = "./vs-kangaroo-hybrid -t 2 -bits " + greckey[itThId].GetBase16() + ":" + k.GetBase16() + " " + pkhex;
string comm2 = "./vs-kangaroo-hybrid -t 2 -bits " + k.GetBase16() + ":" + greckey[itThId].GetBase16() + " " + pkhex;
const char* scomm2 = comm2.c_str();
bool syscomm2 = system(scomm2);
#endif
//
flag_endOfSearch = true;
return true;
}
*/
// ----------------------------------------------------------------------------
/*
//void VanitySearch::checkAddr(int prefIdx, uint8_t *hash160, Int &key, int32_t incr, bool mode) {
void VanitySearch::checkAddr(int prefIdx, uint8_t *hash160, Int &key, int32_t incr, bool mode, uint32_t itThId, Int *reckey) {
// !!! new
vector<PREFIX_ITEM> *pi = prefixes[prefIdx].items;
if (1) {
// 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)) {
// convert
string addret;
char tmp[128];
for (int i = 0; i < 20; i++) {
sprintf(tmp,"%02X",hash160[i]);
addret.append(tmp);
}
string addr = addret;
// Found it !
*((*pi)[i].found) = true;
// You believe it ?
//if (checkPrivKeyGPU(addr, key, incr, mode)) {
if (checkPrivKeyGPU(addr, key, incr, mode, itThId, reckey)) {
nbFoundKey++;
printf(" FoundKey: %d \n", nbFoundKey);
}
}
}
return;
}
}
*/
// ----------------------------------------------------------------------------
bool VanitySearch::checkPrivKeyCPU(Int &checkPrvKey, Point &pSample) {
//Point checkPubKey = secp->MultKAffine(checkPrvKey, secp->G);
Point checkPubKey = secp->ComputePubKey(&checkPrvKey);
if (!checkPubKey.equals(pSample)) {
if (flag_verbose > 1) {
printf("[pubkeyX#%d] %064s \n", pow2U, checkPubKey.x.GetBase16().c_str());
printf("[originX#%d] %064s \n", pow2U, pSample.x.GetBase16().c_str());
printf("[pubkeyY#%d] %064s \n", pow2U, checkPubKey.y.GetBase16().c_str());
printf("[originY#%d] %064s \n", pow2U, pSample.y.GetBase16().c_str());
}
return false;
}
return true;
}
// ----------------------------------------------------------------------------
#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;
}
#ifdef WIN64
DWORD WINAPI _SolverGPU(LPVOID lpParam) {
#else
void *_SolverGPU(void *lpParam) {
#endif
TH_PARAM *p = (TH_PARAM *)lpParam;
p->obj->SolverGPU(p);
return 0;
}
// ----------------------------------------------------------------------------
void VanitySearch::FindKeyCPU(TH_PARAM *ph) {
int thId = ph->threadId;
if (flag_verbose > 0)
printf("[th][%s#%d] run.. \n", (ph->type ? "wild" : "tame"), (ph->type ? thId+1-xU : thId+1));
countj[thId] = 0;
ph->hasStarted = true;
while (!flag_endOfSearch) {
countj[thId] += 1;
// check, is it distinguished point ?
if (!(ph->Kp.x.bits64[0] % DPmodule)) {
//printf("[Xn0] 0x%016llx \n", ph->Kp.x.bits64[0]);
//printf("[Xcoord] %064s \n", ph->Kp.x.GetBase16().c_str());
// send new distinguished point to parent
#ifdef WIN64
WaitForSingleObject(ghMutex, INFINITE);
#else
pthread_mutex_lock(&ghMutex);
#endif
uint64_t entry = ph->Kp.x.bits64[0] & (HASH_SIZE-1);
while (DPht[entry].n0 != 0) {
if (DPht[entry].n0 == ph->Kp.x.bits64[0]
//&& DPht[entry].n1 == ph->Kp.x.bits64[1]
//&& DPht[entry].n2 == ph->Kp.x.bits64[2]
//&& DPht[entry].n3 == ph->Kp.x.bits64[3]
&& !flag_endOfSearch
) {
//printf("[X] 0x%064s\n", ph->Kp.x.GetBase16().c_str());
if (ph->dK.IsLower(&DPht[entry].distance))
resultPrvKey.Sub(&DPht[entry].distance, &ph->dK);
else if (ph->dK.IsGreater(&DPht[entry].distance))
resultPrvKey.Sub(&ph->dK, &DPht[entry].distance);
else {
printf("\n[FATAL_ERROR] dT == dW !!!\n");
//exit(EXIT_FAILURE);
}
if (checkPrivKeyCPU(resultPrvKey, targetPubKey)) {
flag_endOfSearch = true;
break;
}
else {
++countColl;
printf("\n");
printf("[warn] hashtable collision(#%llu) found! \n", countColl);
//printf("[warn] hashtable collision(#%llu) found! 0x%016llX \n", countColl, ph->Kp.x.bits64[0]);
//printf("[warn] Xcoord=%064s \n", ph->Kp.x.GetBase16().c_str());
//printf("[warn] wrong prvkey 0x%064s \n", resultPrvKey.GetBase16().c_str());
}
}
entry = (entry + (ph->Kp.x.bits64[1] | 1)) & (HASH_SIZE-1);
}
//if (flag_endOfSearch) break;
if (ph->type) { ++countDW; } else { ++countDT; }
if (flag_verbose > 1) {
printf("\n[th][%s#%d][DP %dT+%dW=%llu+%llu=%llu] new distinguished point!\n"
, (ph->type ? "wild" : "tame"), (ph->type ? thId+1 - xU : thId+1)
, xU, xV, countDT, countDW, countDT+countDW
);
}
if (countDT+countDW >= maxDP) {
printf("\n[FATAL_ERROR] DP hashtable overflow! %dT+%dW=%llu+%llu=%llu (max=%llu)\n"
, xU, xV, countDT, countDW, countDT+countDW, maxDP
);
exit(EXIT_FAILURE);
}
DPht[entry].distance = ph->dK;
DPht[entry].n0 = ph->Kp.x.bits64[0];
//DPht[entry].n1 = ST.n[1];
//DPht[entry].n2 = ST.n[2];
//DPht[entry].n3 = ST.n[3];
#ifdef WIN64
ReleaseMutex(ghMutex);
#else
pthread_mutex_unlock(&ghMutex);
#endif
}
//if (flag_endOfSearch) break;
uint64_t pw = ph->Kp.x.bits64[0] % JmaxofSp;
//nowjumpsize = 1 << pw
Int nowjumpsize = dS[pw];
ph->dK.Add(&nowjumpsize);
// Jacobian points addition
//ph->Kp = secp->AddJacobian(ph->Kp, Sp[pw]); ph->Kp.Reduce();
// Affine points addition
ph->Kp = secp->AddAffine(ph->Kp, Sp[pw]);
}
ph->isRunning = false;
}
// ----------------------------------------------------------------------------
void VanitySearch::getGPUStartingKeys(int thId, int groupSize, int nbThread, Int *keys, Point *p, uint64_t *n_count) {
int tl = 0;
int trbit = pow2W - tl;// pow2W-0 ?
Int sk;
sk.SetInt32(1);
sk.Neg();
//printf("\n GPU thId: %d ", thId);
int skeybit = bnL.GetBitLength();
printf("GPU Bits: %d ", skeybit);
printf("\nGPU Tame Points: [M] + Rand(pow2W-%d) ", tl);
key2 = bnL;
key3 = bnU;
key3.Add(&sk);
for (int i = 0; i < nbThread; i++) {
// Get Tame keys
Int offT((uint64_t)i);
if (rekey > 1) {
/*
keys[i].SetInt32(0);
keys[i].Rand(skeybit);
while(strcmp(keys[i].GetBase16().c_str(), key2.GetBase16().c_str()) < 0 || strcmp(keys[i].GetBase16().c_str(), key3.GetBase16().c_str()) > 0){
//printf("\n GPU Random Key: %s < bnL or > bnU New Random true \n", keys[i].GetBase16().c_str());
wkey.Rand(skeybit);
keys[i] = wkey;
}
*/
keys[i] = bnM;
if (i > 0) {
wkey.Rand(trbit);
keys[i].Add(&wkey);
keys[i].Add(&offT);// if bad Rand ?
}
// Check GPU Random Key
if (i < 10) printf("\nGPU Tame Starting Key %d: %s ", i, keys[i].GetBase16().c_str());
if (i == nbThread-1) printf("\nGPU Tame Starting Key %d: %s Thread: %d \n", i, keys[i].GetBase16().c_str(), nbThread);
} else {
// Get Tame keys
keys[i] = bnM;
if (i > 0) {
wkey.Rand(trbit);
keys[i].Add(&wkey);
keys[i].Add(&offT);// if bad Rand ?
}
// Check GPU Random Key
if (i < 10) printf("\nGPU Tame Starting Key %d: %s ", i, keys[i].GetBase16().c_str());
if (i == nbThread-1) printf("\nGPU Tame Starting Key %d: %s Thread: %d \n", i, keys[i].GetBase16().c_str(), nbThread);
}
// For check GPU code
//keys[i].SetBase16("FF");
//keys[i].SetInt32(10);
//Int k(keys + i); the error ?
Int k(keys[i]);
p[i] = secp->ComputePublicKey(&k);
}
++ *n_count;
}
// ----------------------------------------------------------------------------
void VanitySearch::getGPUStartingWKeys(int thId, int groupSize, int nbThread, Point w_targetPubKey, Int *w_keys, Point *w_p) {
int wl = 1;//int wl = 0;
int wrbit = pow2W - wl;// pow2W-1 ?
printf("\nGPU Wild Points: [Target] + Rand(pow2W-%d) ", wl);
for (int i = 0; i < nbThread; i++) {
// Get Wild keys
Int woffT((uint64_t)i);
w_keys[i].SetInt32(0);
wkey.Rand(wrbit);
w_keys[i].Add(&wkey);
w_keys[i].Add(&woffT);// if bad Rand
//
Int wk(w_keys[i]);
w_p[i] = secp->ComputePublicKey(&wk);
if (1) { // Set 0 for check compute points in GPU
w_p[i] = secp->AddAffine(w_targetPubKey, w_p[i]);
}
if (i < 10) printf("\nGPU Wild Starting Key %d: %s ", i, w_keys[i].GetBase16().c_str());
if (i == nbThread-1) printf("\nGPU Wild Starting Key %d: %s Thread: %d \n", i, w_keys[i].GetBase16().c_str(), nbThread);
}
}
// ----------------------------------------------------------------------------
bool VanitySearch::File2save(Int px, Int key, int stype) {
string str_px = px.GetBase16().c_str();
string str_key = key.GetBase16().c_str();
string getpx = "";
string getkey = "";
string p0 = "0";
string k0 = "0";
for (int i = (int)str_px.size(); i < 64; i++) {
getpx.append(p0);
}
getpx.append(str_px);
for (int i = (int)str_key.size(); i < 64; i++) {
getkey.append(k0);
}
getkey.append(str_key);
string str_write = getpx + " " + getkey;
int lenstr = (int)str_write.length();
//printf("\n lenstr: %d stype: %d \n", lenstr, stype);
//printf("\n str_write: %s \n", str_write.c_str());
// Write files
if (stype == 1 && lenstr == 129) {
FILE *f1 = fopen("tame.txt", "a");
if (f1 == NULL) {
printf("\n[error] Cannot open file tame.txt for writing! %s \n", strerror(errno));
f1 = stdout;
return false;
} else {
fprintf(f1, "%s\n", str_write.c_str());
fclose(f1);
return true;
}
}
if (stype == 2 && lenstr == 129) {
FILE *f2 = fopen("wild.txt", "a");
if (f2 == NULL) {
printf("\n[error] Cannot open file wild.txt for writing! %s \n", strerror(errno));
f2 = stdout;
return false;
} else {
fprintf(f2, "%s\n", str_write.c_str());
fclose(f2);
return true;
}
}
}
// ----------------------------------------------------------------------------
bool VanitySearch::Comparator() {
// Used chrono for get compare time
auto begin = std::chrono::steady_clock::now();
string WfileName = "wild.txt";
string TfileName = "tame.txt";
vector<string> Wpoint;
vector<string> Wkey;
vector<string> Tpoint;
vector<string> Tkey;
// Wild
// Get file size wild
FILE *fw = fopen(WfileName.c_str(), "rb");
if (fw == NULL) {
printf("Error: Cannot open %s %s\n", WfileName.c_str(), strerror(errno));
}
fseek(fw, 0L, SEEK_END);
size_t wsz = ftell(fw); // Get bytes
size_t nbWild = wsz / 129; // Get lines
fclose(fw);
// For check
//printf("File wild.txt: %llu bytes %llu lines \n", (uint64_t)wsz, (uint64_t)nbWild);
// Parse File Wild
int nbWLine = 0;
string Wline = "";
ifstream inFileW(WfileName);
Wpoint.reserve(nbWild);
Wkey.reserve(nbWild);
while (getline(inFileW, Wline)) {
// Remove ending \r\n
int l = (int)Wline.length() - 1;
while (l >= 0 && isspace(Wline.at(l))) {
Wline.pop_back();
l--;
}
if (Wline.length() == 129) {
Wpoint.push_back(Wline.substr(0, 64));
Wkey.push_back(Wline.substr(65, 129));
nbWLine++;
// For check
//printf(" %s %d \n", Wpoint[0].c_str(), nbWLine);
//printf(" %s %d \n", Wkey[0].c_str(), nbWLine);
}
}
// Tame
// Get file size tame
FILE *ft = fopen(TfileName.c_str(), "rb");
if (ft == NULL) {
printf("Error: Cannot open %s %s\n", TfileName.c_str(), strerror(errno));
}
fseek(ft, 0L, SEEK_END);
size_t tsz = ftell(ft); // Get bytes
size_t nbTame = tsz / 129; // Get lines
fclose(ft);
// For check
//printf("File tame.txt: %llu bytes %llu lines \n", (uint64_t)tsz, (uint64_t)nbTame);
// Parse File Tame
int nbTLine = 0;
string Tline = "";
ifstream inFileT(TfileName);
Tpoint.reserve(nbTame);
Tkey.reserve(nbTame);
while (getline(inFileT, Tline)) {
// Remove ending \r\n
int l = (int)Tline.length() - 1;
while (l >= 0 && isspace(Tline.at(l))) {
Tline.pop_back();
l--;
}
if (Tline.length() == 129) {
Tpoint.push_back(Tline.substr(0, 64));
Tkey.push_back(Tline.substr(65, 129));
nbTLine++;
// For check
//printf(" %s %d \n", Tpoint[0].c_str(), nbTLine);
//printf(" %s %d \n", Tkey[0].c_str(), nbTLine);
}
}
// Compare lines
int result = 0;
string WDistance = "";
string TDistance = "";
for (int wi = 0; wi < nbWLine; wi++) {
for (int ti = 0; ti < nbTLine; ti++) {
if (strcmp(Wpoint[wi].c_str(), Tpoint[ti].c_str()) == 0) {
result++;
if (result > 0) {
printf("\n%d Compared lines Tame %d = Wild %d ", result, ti+1, wi+1);
printf("\nTame Distance: 0x%s ", Tkey[ti].c_str());
printf("\nWild Distance: 0x%s ", Wkey[wi].c_str());
}
WDistance = Wkey[wi].c_str();
TDistance = Tkey[ti].c_str();
}
}
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli> elapsed_ms = end - begin;
if (flag_verbose > 0) {
printf("\n[i] Comparator time: %.*f msec %s %llu bytes %s %llu bytes \n", 3, elapsed_ms, WfileName.c_str(), (uint64_t)wsz, TfileName.c_str(), (uint64_t)tsz);
}
if (result > 0) {
// Get SOLVED
Int WDist;
Int TDist;
Int Priv;
char *wd = new char [WDistance.length()+1];
char *td = new char [TDistance.length()+1];
strcpy(wd, WDistance.c_str());
strcpy(td, TDistance.c_str());
WDist.SetBase16(wd);
TDist.SetBase16(td);
Priv.SetInt32(0);
if (TDist.IsLower(&WDist))
Priv.Sub(&WDist, &TDist);
else if (TDist.IsGreater(&WDist))
Priv.Sub(&TDist, &WDist);
else {
printf("\n[FATAL_ERROR] Wild Distance == Tame Distance !!!\n");
}
printf("\nSOLVED: 0x%s \n", Priv.GetBase16().c_str());
printf("Tame Distance: 0x%s \n", TDist.GetBase16().c_str());
printf("Wild Distance: 0x%s \n", WDist.GetBase16().c_str());
printf("Tips: 1NULY7DhzuNvSDtPkFzNo6oRTZQWBqXNE9 ;-) \n");
printf("\n[i] Comparator time: %.*f msec %s %llu bytes %s %llu bytes \n", 3, elapsed_ms, WfileName.c_str(), (uint64_t)wsz, TfileName.c_str(), (uint64_t)tsz);
// SAVE SOLVED
bool saved = outputgpu(Priv.GetBase16().c_str());
if (saved) {
printf("[i] Success saved to file %s\n", outputFile.c_str());
}
return true;
}
return false;
}
// ----------------------------------------------------------------------------
bool VanitySearch::TWSaveToDrive() {
#ifdef WIN64
#else
// Copy tame.txt and wild.txt in drive