forked from albertobsd/keyhunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyhunt.c
2748 lines (2540 loc) · 80.3 KB
/
keyhunt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
/*
Develop by Luis Alberto
email: [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <math.h>
#include <time.h>
#include <vector>
#include <inttypes.h>
#include "base58/libbase58.h"
#include "rmd160/rmd160.h"
#include "sha256/sha256.h"
#include "bloom/bloom.h"
#include "sha3/sha3.h"
#include "secp256k1/SECP256k1.h"
#include "secp256k1/Point.h"
#include "secp256k1/Int.h"
#include "secp256k1/IntGroup.h"
#include "secp256k1/Random.h"
#include "util.h"
#ifdef WIN32
#include <windows.h>
#endif
#define CRYPTO_NONE 0
#define CRYPTO_BTC 1
#define CRYPTO_ETH 2
#define CRYPTO_ALL 3
#define MODE_XPOINT 0
#define MODE_ADDRESS 1
#define MODE_BSGS 2
#define MODE_RMD160 3
#define MODE_PUB2RMD 4
#define SEARCH_UNCOMPRESS 0
#define SEARCH_COMPRESS 1
#define SEARCH_BOTH 2
struct bsgs_xvalue {
uint8_t value[6];
uint64_t index;
};
struct address_value {
uint8_t value[20];
};
struct tothread {
int nt; //Number thread
char *rs; //range start
char *rpt; //rng per thread
};
struct bPload {
uint32_t threadid;
uint64_t from;
uint64_t to;
uint64_t counter;
};
struct __attribute__((__packed__)) publickey {
uint8_t parity;
union {
uint8_t data8[32];
uint32_t data32[8];
uint64_t data64[4];
} X;
};
const char *version = "0.1.20210412 secp256k1";
const char *bloomnames[256] = {"bloom_0","bloom_1","bloom_2","bloom_3","bloom_4","bloom_5","bloom_6","bloom_7","bloom_8","bloom_9","bloom_10","bloom_11","bloom_12","bloom_13","bloom_14","bloom_15","bloom_16","bloom_17","bloom_18","bloom_19","bloom_20","bloom_21","bloom_22","bloom_23","bloom_24","bloom_25","bloom_26","bloom_27","bloom_28","bloom_29","bloom_30","bloom_31","bloom_32","bloom_33","bloom_34","bloom_35","bloom_36","bloom_37","bloom_38","bloom_39","bloom_40","bloom_41","bloom_42","bloom_43","bloom_44","bloom_45","bloom_46","bloom_47","bloom_48","bloom_49","bloom_50","bloom_51","bloom_52","bloom_53","bloom_54","bloom_55","bloom_56","bloom_57","bloom_58","bloom_59","bloom_60","bloom_61","bloom_62","bloom_63","bloom_64","bloom_65","bloom_66","bloom_67","bloom_68","bloom_69","bloom_70","bloom_71","bloom_72","bloom_73","bloom_74","bloom_75","bloom_76","bloom_77","bloom_78","bloom_79","bloom_80","bloom_81","bloom_82","bloom_83","bloom_84","bloom_85","bloom_86","bloom_87","bloom_88","bloom_89","bloom_90","bloom_91","bloom_92","bloom_93","bloom_94","bloom_95","bloom_96","bloom_97","bloom_98","bloom_99","bloom_100","bloom_101","bloom_102","bloom_103","bloom_104","bloom_105","bloom_106","bloom_107","bloom_108","bloom_109","bloom_110","bloom_111","bloom_112","bloom_113","bloom_114","bloom_115","bloom_116","bloom_117","bloom_118","bloom_119","bloom_120","bloom_121","bloom_122","bloom_123","bloom_124","bloom_125","bloom_126","bloom_127","bloom_128","bloom_129","bloom_130","bloom_131","bloom_132","bloom_133","bloom_134","bloom_135","bloom_136","bloom_137","bloom_138","bloom_139","bloom_140","bloom_141","bloom_142","bloom_143","bloom_144","bloom_145","bloom_146","bloom_147","bloom_148","bloom_149","bloom_150","bloom_151","bloom_152","bloom_153","bloom_154","bloom_155","bloom_156","bloom_157","bloom_158","bloom_159","bloom_160","bloom_161","bloom_162","bloom_163","bloom_164","bloom_165","bloom_166","bloom_167","bloom_168","bloom_169","bloom_170","bloom_171","bloom_172","bloom_173","bloom_174","bloom_175","bloom_176","bloom_177","bloom_178","bloom_179","bloom_180","bloom_181","bloom_182","bloom_183","bloom_184","bloom_185","bloom_186","bloom_187","bloom_188","bloom_189","bloom_190","bloom_191","bloom_192","bloom_193","bloom_194","bloom_195","bloom_196","bloom_197","bloom_198","bloom_199","bloom_200","bloom_201","bloom_202","bloom_203","bloom_204","bloom_205","bloom_206","bloom_207","bloom_208","bloom_209","bloom_210","bloom_211","bloom_212","bloom_213","bloom_214","bloom_215","bloom_216","bloom_217","bloom_218","bloom_219","bloom_220","bloom_221","bloom_222","bloom_223","bloom_224","bloom_225","bloom_226","bloom_227","bloom_228","bloom_229","bloom_230","bloom_231","bloom_232","bloom_233","bloom_234","bloom_235","bloom_236","bloom_237","bloom_238","bloom_239","bloom_240","bloom_241","bloom_242","bloom_243","bloom_244","bloom_245","bloom_246","bloom_247","bloom_248","bloom_249","bloom_250","bloom_251","bloom_252","bloom_253","bloom_254","bloom_255"};
std::vector<Point> Gn;
Point _2Gn;
std::vector<Point> GSn;
Point _2GSn;
std::vector<Point> GS2n;
Point _2GS2n;
int CPU_GRP_SIZE = 1024;
void init_generator();
int searchbinary(struct address_value *buffer,char *data,int64_t _N);
void sleep_ms(int milliseconds);
void _sort(struct address_value *arr,int64_t N);
void _insertionsort(struct address_value *arr, int64_t n);
void _introsort(struct address_value *arr,uint32_t depthLimit, int64_t n);
void _swap(struct address_value *a,struct address_value *b);
int64_t _partition(struct address_value *arr, int64_t n);
void _myheapsort(struct address_value *arr, int64_t n);
void _heapify(struct address_value *arr, int64_t n, int64_t i);
void bsgs_sort(struct bsgs_xvalue *arr,int64_t n);
void bsgs_myheapsort(struct bsgs_xvalue *arr, int64_t n);
void bsgs_insertionsort(struct bsgs_xvalue *arr, int64_t n);
void bsgs_introsort(struct bsgs_xvalue *arr,uint32_t depthLimit, int64_t n);
void bsgs_swap(struct bsgs_xvalue *a,struct bsgs_xvalue *b);
void bsgs_heapify(struct bsgs_xvalue *arr, int64_t n, int64_t i);
int64_t bsgs_partition(struct bsgs_xvalue *arr, int64_t n);
int bsgs_searchbinary(struct bsgs_xvalue *arr,char *data,int64_t _N,uint64_t *r_value);
int bsgs_secondcheck(Int *start_range,uint32_t a,uint32_t k_index,Int *privatekey);
void *thread_process(void *vargp);
void *thread_process_bsgs(void *vargp);
void *thread_process_bsgs_random(void *vargp);
void *thread_bPload(void *vargp);
void *thread_bPloadFile(void *vargp);
void *thread_pub2rmd(void *vargp);
char *publickeytohashrmd160(char *pkey,int length);
char *pubkeytopubaddress(char *pkey,int length);
//char *pubkeytopubaddress_eth(char *pkey,int length);
int THREADOUTPUT = 0;
char *bit_range_str_min;
char *bit_range_str_max;
const char *modes[5] = {"xpoint","address","bsgs","rmd160","pub2rmd"};
const char *cryptos[3] = {"btc","eth","all"};
const char *publicsearch[3] = {"uncompress","compress","both"};
const char *default_filename = "addresses.txt";
pthread_t *tid = NULL;
pthread_mutex_t write_keys;
pthread_mutex_t write_random;
pthread_mutex_t bsgs_thread;
struct bloom dummybloom;
struct bloom bloom;
unsigned int *steps = NULL;
unsigned int *ends = NULL;
uint64_t N = 0;
uint64_t N_SECUENTIAL_MAX = 0xffffffff;
uint64_t DEBUGCOUNT = 0x100000;
Int OUTPUTSECONDS;
int FLAGDEBUG = 0;
int FLAGQUIET = 0;
int KFACTOR = 1;
int MAXLENGTHADDRESS = -1;
int NTHREADS = 1;
int FLAGSEARCH = 2;
int FLAGBITRANGE = 0;
int FLAGRANGE = 0;
int FLAGFILE = 0;
int FLAGVANITY = 0;
int FLAGMODE = MODE_ADDRESS;
int FLAGCRYPTO = 0;
int FLAGALREADYSORTED = 0;
int FLAGRAWDATA = 0;
int FLAGRANDOM = 0;
int FLAG_N = 0;
int FLAGPRECALCUTED_P_FILE = 0;
int FLAGPRECALCUTED_MP_FILE = 0;
int FLAGBLOOMFILTER = 0;
int len_vanity;
int bitrange;
char *vanity;
char *range_start;
char *range_end;
uint64_t BSGS_XVALUE_RAM = 6;
uint64_t BSGS_BUFFERXPOINTLENGTH = 32;
uint64_t BSGS_BUFFERREGISTERLENGTH = 36;
/*
BSGS Variables
*/
int *bsgs_found;
std::vector<Point> OriginalPointsBSGS;
bool *OriginalPointsBSGScompressed;
struct bsgs_xvalue *bPtable;
struct address_value *addressTable;
struct bloom bloom_bP[256];
struct bloom bloom_bPx2nd; //Second Bloom filter check
uint64_t bloom_bP_totalbytes = 0;
char *precalculated_p_filename;
uint64_t bsgs_m = 4194304;
uint64_t bsgs_m2;
unsigned long int bsgs_aux;
uint32_t bsgs_point_number;
Int BSGS_GROUP_SIZE;
Int BSGS_CURRENT;
Int BSGS_R;
Int BSGS_AUX;
Int BSGS_N;
Int BSGS_M; //M is squareroot(N)
Int BSGS_M2;
Int ONE;
Int ZERO;
Int MPZAUX;
Point BSGS_P; //Original P is actually G, but this P value change over time for calculations
Point BSGS_MP; //MP values this is m * P
Point BSGS_MP2; //MP values this is m2 * P
std::vector<Point> BSGS_AMP,BSGS_AMP2;
Point point_temp,point_temp2; //Temp value for some process
Int n_range_start;
Int n_range_end;
Int n_range_diff;
Int n_range_aux;
Secp256K1 *secp;
int main(int argc, char **argv) {
char buffer[1024];
char temporal[65];
char rawvalue[32];
struct tothread *tt; //tothread
Tokenizer t,tokenizerbsgs,tokenizer_xpoint; //tokenizer
char *filename,*precalculated_mp_filename,*hextemp,*aux,*aux2,*pointx_str,*pointy_str,*str_seconds,*str_total,*str_pretotal;
FILE *fd;
uint64_t j,total_precalculated,i,PERTHREAD,BASE,PERTHREAD_R,itemsbloom,itemsbloom2;
int readed,s,continue_flag,check_flag,r,lenaux,lendiff,c;
Int total,pretotal,debugcount_mpz,seconds;
struct bPload *temp;
secp = new Secp256K1();
secp->Init();
OUTPUTSECONDS.SetInt32(30);
ZERO.SetInt32(0);
ONE.SetInt32(1);
BSGS_GROUP_SIZE.SetInt32(CPU_GRP_SIZE);
rseed(clock() + time(NULL));
printf("[+] Version %s\n",version);
while ((c = getopt(argc, argv, "dehqRwzb:c:f:g:k:l:m:n:p:r:s:t:v:G:")) != -1) {
switch(c) {
case 'h':
printf("\nUsage:\n-h\t\tshow this help\n");
printf("-a file\t\tfile is a binary raw file with the aMP points precalculated. Just work with -m bsgs\n");
printf("-b bits\t\tFor some puzzles you only need some numbers of bits in the test keys.\n");
printf("\t\tThis option only is valid with the Random option -R\n");
printf("-c crypto\tSearch for specific crypo. < btc, eth, all > valid only w/ -m address \n");
printf("-e\t\tThe file is already Sorted descendent. This skip the sorting process.\n");
printf("\t\tYour file MUST be sordted if no you are going to lose collisions\n");
printf("-f file\t\tSpecify filename with addresses or xpoints or uncompressed public keys\n");
printf("-g count\tJust for the stats, mark as counted every debugcount keys \n");
printf("-k value\tUse this only with bsgs mode, k value is factor for M, more speed but more RAM use wisely\n");
printf("-l look\tWhat type of address/hash160 are you looking for < compress , uncompress , both>\n");
printf("-m mode\t\tmode of search for cryptos. ( bsgs , xpoint , rmd160 , address ) default: address (more slow)\n");
printf("-n uptoN\tCheck for N secuential numbers before the random chossen this only work with -R option\n");
printf("\t\tUse -n to set the N for the BSGS process. Bigger N more RAM needed\n");
printf("-p file\t\tfile is a binary raw file with the bP points precalculated. Just work with -m bsgs\n");
printf("-q\t\tset quiet the thread output\n");
printf("-r SR:EN\tStarRange:EndRange, the end range can be omited for search from start range to N-1 ECC value\n");
printf("-R\t\tRandom this is the default behaivor\n");
printf("-s ns\t\tNumber of seconds for the stats output, 0 to omit output.\n");
printf("-t tn\t\tThreads number, must be positive integer\n");
printf("-v va\t\tSearch for vanity Address, only with -m address\n");
printf("-w\t\tMark the input file as RAW data xpoint fixed 32 byte each point. Valid only with -m xpoint\n");
//printf("-z\t\tSave and load bloom bloomfilter from File\n");
printf("\t\tUse the hexcharstoraw tool to create a raw file from your current hexadecimal file\n");
printf("\nExample\n\n");
printf("%s -t 16 -r 00000001:FFFFFFFF -s 0\n\n",argv[0]);
printf("This line run the program with 16 threads from the range 00000001 to FFFFFFFF without stats output\n\n");
printf("Developed by AlbertoBSD\tTips BTC: 1ABSD1rMTmNZHJrJP8AJhDNG1XbQjWcRz7\n");
printf("Thanks to Iceland always helping and sharing his ideas, Tips to Iceland: bc1q39meky2mn5qjq704zz0nnkl0v7kj4uz6r529at\n\n");
exit(0);
break;
case 'a':
FLAGPRECALCUTED_MP_FILE = 1;
precalculated_mp_filename = optarg;
break;
case 'b':
bitrange = strtol(optarg,NULL,10);
if(bitrange > 0 && bitrange <=256 ) {
MPZAUX.Set(&ONE);
MPZAUX.ShiftL(bitrange-1);
bit_range_str_min = MPZAUX.GetBase16();
MPZAUX.Set(&ONE);
MPZAUX.ShiftL(bitrange);
if(MPZAUX.IsGreater(&secp->order)) {
MPZAUX.Set(&secp->order);
}
bit_range_str_max = MPZAUX.GetBase16();
if(bit_range_str_min == NULL||bit_range_str_max == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
printf("[+] Min range: %s\n",bit_range_str_min);
printf("[+] Max range: %s\n",bit_range_str_max);
FLAGBITRANGE = 1;
}
else {
fprintf(stderr,"[E] invalid bits param: %s.\n",optarg);
}
break;
case 'c':
switch(indexOf(optarg,cryptos,3)) {
case 0: //btc
FLAGCRYPTO = CRYPTO_BTC;
printf("[+] Setting search for btc adddress.\n");
break;
case 1: //eth
FLAGCRYPTO = CRYPTO_ETH;
printf("[+] Setting search for eth adddress.\n");
break;
case 2: //all
FLAGCRYPTO = CRYPTO_ALL;
printf("[+] Setting search for all cryptocurrencies avaible [btc].\n");
break;
default:
FLAGCRYPTO = CRYPTO_NONE;
fprintf(stderr,"[E] Unknow crypto value %s\n",optarg);
break;
}
break;
case 'd':
FLAGDEBUG = 1;
break;
case 'e':
FLAGALREADYSORTED = 1;
break;
case 'f':
FLAGFILE = 1;
filename = optarg;
break;
case 'g':
DEBUGCOUNT = strtol(optarg,NULL,10);
if(DEBUGCOUNT == 0) {
DEBUGCOUNT = 0x100000;
fprintf(stderr,"[E] invalid -g option value: %s.\n",optarg);
}
break;
case 'k':
KFACTOR = (int)strtol(optarg,NULL,10);
if(KFACTOR <= 0) {
KFACTOR = 1;
}
printf("[+] Setting k factor to %i\n",KFACTOR);
break;
case 'l':
switch(indexOf(optarg,publicsearch,3)) {
case SEARCH_UNCOMPRESS:
FLAGSEARCH = SEARCH_UNCOMPRESS;
printf("[+] Search uncompress only\n");
break;
case SEARCH_COMPRESS:
FLAGSEARCH = SEARCH_COMPRESS;
printf("[+] Search compress only\n");
break;
case SEARCH_BOTH:
FLAGSEARCH = SEARCH_BOTH;
printf("[+] Search both compress and uncompress\n");
break;
}
break;
case 'm':
switch(indexOf(optarg,modes,5)) {
case MODE_XPOINT: //xpoint
FLAGMODE = MODE_XPOINT;
printf("[+] Setting mode xpoint\n");
break;
case MODE_ADDRESS: //address
FLAGMODE = MODE_ADDRESS;
printf("[+] Setting mode address\n");
break;
case MODE_BSGS:
FLAGMODE = MODE_BSGS;
printf("[+] Setting mode BSGS\n");
break;
case MODE_RMD160:
FLAGMODE = MODE_RMD160;
printf("[+] Setting mode rmd160\n");
break;
case MODE_PUB2RMD:
FLAGMODE = MODE_PUB2RMD;
printf("[+] Setting mode pub2rmd\n");
break;
default:
FLAGMODE = MODE_ADDRESS;
fprintf(stderr,"[+] Unknow mode value %s.\n",optarg);
break;
}
break;
case 'n':
FLAG_N = 1;
N_SECUENTIAL_MAX = strtol(optarg,NULL,10);
if(N_SECUENTIAL_MAX <= 0) {
FLAG_N = 0;
N_SECUENTIAL_MAX = 0xFFFFFFFF;
}
break;
case 'q':
FLAGQUIET = 1;
printf("[+] Set quiet thread output\n");
break;
case 'p':
FLAGPRECALCUTED_P_FILE = 1;
precalculated_p_filename = optarg;
break;
case 'R':
FLAGRANDOM = 1;
printf("[+] Setting random mode.\n");
break;
case 'r':
if(optarg != NULL) {
stringtokenizer(optarg,&t);
switch(t.n) {
case 1:
range_start = nextToken(&t);
if(isValidHex(range_start)) {
FLAGRANGE = 1;
range_end = secp->order.GetBase16();
}
else {
fprintf(stderr,"[E] Invalid hexstring : %s.\n",range_start);
}
break;
case 2:
range_start = nextToken(&t);
range_end = nextToken(&t);
if(isValidHex(range_start) && isValidHex(range_end)) {
FLAGRANGE = 1;
}
else {
if(isValidHex(range_start)) {
printf("[E] Invalid hexstring : %s\n",range_start);
}
else {
printf("[E] Invalid hexstring : %s\n",range_end);
}
}
break;
default:
printf("[E] Unknow number of Range Params: %i\n",t.n);
break;
}
}
break;
case 's':
OUTPUTSECONDS.SetBase10(optarg);
if(OUTPUTSECONDS.IsLower(&ZERO)) {
OUTPUTSECONDS.SetInt32(30);
}
if(OUTPUTSECONDS.IsZero()) {
printf("[+] Turn off stats output\n");
}
else {
hextemp = OUTPUTSECONDS.GetBase10();
printf("[+] Stats output every %s seconds\n",hextemp);
free(hextemp);
}
break;
case 't':
NTHREADS = strtol(optarg,NULL,10);
if(NTHREADS <= 0) {
NTHREADS = 1;
}
printf((NTHREADS > 1) ? "[+] Setting %u threads\n": "[+] Setting %u thread\n",NTHREADS);
break;
case 'v':
FLAGVANITY = 1;
vanity = optarg;
len_vanity = strlen(optarg);
printf("[+] Added Vanity search : %s\n",vanity);
break;
case 'w':
printf("[+] Data marked as RAW\n");
FLAGRAWDATA = 1;
break;
case 'z':
printf("[+] Bloom filter marked to be saved\n");
FLAGBLOOMFILTER = 1;
break;
default:
printf("[E] Unknow opcion %c\n",c);
break;
}
}
init_generator();
if(DEBUGCOUNT > N_SECUENTIAL_MAX) {
DEBUGCOUNT = N_SECUENTIAL_MAX - 1;
}
if(FLAGFILE == 0) {
filename =(char*) default_filename;
}
printf("[+] Opening file %s\n",filename);
fd = fopen(filename,"rb");
if(fd == NULL) {
fprintf(stderr,"[E] Can't open file %s\n",filename);
exit(0);
}
if(FLAGMODE == MODE_ADDRESS && FLAGCRYPTO == CRYPTO_NONE) { //When none crypto is defined the default search is for Bitcoin
FLAGCRYPTO = CRYPTO_BTC;
printf("[+] Setting search for btc adddress\n");
}
if(FLAGRANGE) {
n_range_start.SetBase16(range_start);
if(n_range_start.IsZero()) {
n_range_start.AddOne();
}
n_range_end.SetBase16(range_end);
if(n_range_start.IsEqual(&n_range_end) == false ) {
if( n_range_start.IsLower(&secp->order) && n_range_end.IsLowerOrEqual(&secp->order) ) {
if( n_range_start.IsGreater(&n_range_end)) {
fprintf(stderr,"[W] Opps, start range can't be great than end range. Swapping them\n");
n_range_aux.Set(&n_range_start);
n_range_start.Set(&n_range_end);
n_range_end.Set(&n_range_aux);
}
n_range_diff.Set(&n_range_end);
n_range_diff.Sub(&n_range_start);
}
else {
fprintf(stderr,"[E] Start and End range can't be great than N\nFallback to random mode!\n");
FLAGRANGE = 0;
}
}
else {
fprintf(stderr,"[E] Start and End range can't be the same\nFallback to random mode!\n");
FLAGRANGE = 0;
}
}
if(FLAGMODE != MODE_BSGS) {
if(FLAGRANGE == 0 && FLAGBITRANGE == 0) {
n_range_start.SetInt32(1);
n_range_end.Set(&secp->order);
n_range_diff.Set(&n_range_end);
n_range_diff.Sub(&n_range_start);
}
else {
if(FLAGBITRANGE) {
n_range_start.SetBase16(bit_range_str_min);
n_range_end.SetBase16(bit_range_str_max);
n_range_diff.Set(&n_range_end);
n_range_diff.Sub(&n_range_start);
}
else {
if(FLAGRANGE == 0) {
fprintf(stderr,"[W] WTF!\n");
}
}
}
}
N = 0;
if(FLAGMODE != MODE_BSGS) {
aux =(char*) malloc(1000);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
}
switch(FLAGMODE) {
case MODE_ADDRESS:
while(!feof(fd)) {
hextemp = fgets(aux,998,fd);
if(hextemp == aux) {
trim(aux," \t\n\r");
r = strlen(aux);
if(r > 10) { //Any length for invalid Address?
if(r > MAXLENGTHADDRESS) {
MAXLENGTHADDRESS = r;
}
N++;
}
}
}
MAXLENGTHADDRESS = 32;
break;
case MODE_PUB2RMD:
case MODE_RMD160:
if(FLAGRAWDATA) {
while(!feof(fd)) {
if(fread(aux,1,20,fd) == 20) {
N++;
}
}
}
else {
while(!feof(fd)) {
hextemp = fgets(aux,998,fd);
if(hextemp == aux) {
trim(aux," \t\n\r");
r = strlen(aux);
if(r == 40) { //Any length for invalid Address?
N++;
}
}
}
}
MAXLENGTHADDRESS = 20;
break;
case MODE_XPOINT:
if(FLAGRAWDATA) {
while(!feof(fd)) {
if(fread(aux,1,32,fd) == 32) {
N++;
}
}
}
else {
while(!feof(fd)) {
hextemp = fgets(aux,998,fd);
if(hextemp == aux) {
trim(aux," \t\n\r");
r = strlen(aux);
if(r >= 32) { //Any length for invalid Address?
N++;
}
}
}
}
MAXLENGTHADDRESS = 32;
break;
}
free(aux);
if(N == 0) {
fprintf(stderr,"[E] There is no valid data in the file\n");
exit(0);
}
fseek(fd,0,SEEK_SET);
printf("[+] Allocating memory for %" PRIu64 " elements: %.2f MB\n",N,(double)(((double) sizeof(struct address_value)*N)/(double)1048576));
i = 0;
addressTable = (struct address_value*) malloc(sizeof(struct address_value)*N);
if(addressTable == NULL) {
fprintf(stderr,"[E] Can't alloc memory for %" PRIu64 " elements\n",N);
exit(0);
}
printf("[+] Initializing bloom filter for %" PRIu64 " elements.\n",N);
if(N <= 1000) {
if(bloom_init2(&bloom,1000,0.00001) == 1){
fprintf(stderr,"[E] error bloom_init for 10000 elements.\n");
exit(0);
}
}
else {
if(bloom_init2(&bloom,N,0.00001) == 1){
fprintf(stderr,"[E] error bloom_init for %" PRIu64 " elements.\n",N);
fprintf(stderr,"[+] man enough is enough stop it\n");
exit(0);
}
}
printf("[+] Loading data to the bloomfilter total: %.2f MB\n",(double)(((double) bloom.bytes)/(double)1048576));
i = 0;
switch (FLAGMODE) {
case MODE_ADDRESS:
aux =(char*) malloc(2*MAXLENGTHADDRESS);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
while(i < N) {
memset(aux,0,2*MAXLENGTHADDRESS);
memset((void *)&addressTable[i],0,sizeof(struct address_value));
hextemp = fgets(aux,2*MAXLENGTHADDRESS,fd);
if(hextemp == aux) {
trim(aux," \t\n\r");
bloom_add(&bloom, aux,MAXLENGTHADDRESS);
memcpy(addressTable[i].value,aux,20);
i++;
}
else {
trim(aux," \t\n\r");
fprintf(stderr,"[E] Omiting line : %s\n",aux);
}
}
break;
case MODE_XPOINT:
if(FLAGRAWDATA) {
aux = (char*)malloc(MAXLENGTHADDRESS);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
while(i < N) {
if(fread(aux,1,MAXLENGTHADDRESS,fd) == 32) {
memcpy(addressTable[i].value,aux,20);
bloom_add(&bloom, aux,MAXLENGTHADDRESS);
}
i++;
}
}
else {
aux = (char*) malloc(5*MAXLENGTHADDRESS);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
while(i < N) {
memset(aux,0,5*MAXLENGTHADDRESS);
hextemp = fgets(aux,(5*MAXLENGTHADDRESS) -2,fd);
memset((void *)&addressTable[i],0,sizeof(struct address_value));
if(hextemp == aux) {
trim(aux," \t\n\r");
stringtokenizer(aux,&tokenizer_xpoint);
hextemp = nextToken(&tokenizer_xpoint);
lenaux = strlen(hextemp);
if(isValidHex(hextemp)) {
switch(lenaux) {
case 64: /*X value*/
r = hexs2bin(aux,(uint8_t*) rawvalue);
if(r) {
memcpy(addressTable[i].value,rawvalue,20);
bloom_add(&bloom,rawvalue,MAXLENGTHADDRESS);
}
else {
fprintf(stderr,"[E] error hexs2bin\n");
}
break;
case 66: /*Compress publickey*/
r = hexs2bin(aux+2, (uint8_t*)rawvalue);
if(r) {
memcpy(addressTable[i].value,rawvalue,20);
bloom_add(&bloom,rawvalue,MAXLENGTHADDRESS);
}
else {
fprintf(stderr,"[E] error hexs2bin\n");
}
break;
case 130: /* Uncompress publickey length*/
memset(temporal,0,65);
memcpy(temporal,aux+2,64);
r = hexs2bin(temporal, (uint8_t*) rawvalue);
if(r) {
memcpy(addressTable[i].value,rawvalue,20);
bloom_add(&bloom,rawvalue,MAXLENGTHADDRESS);
}
else {
fprintf(stderr,"[E] error hexs2bin\n");
}
break;
default:
fprintf(stderr,"[E] Omiting line unknow length size %i: %s\n",lenaux,aux);
break;
}
}
else {
fprintf(stderr,"[E] Ignoring invalid hexvalue %s\n",aux);
}
freetokenizer(&tokenizer_xpoint);
}
else {
fprintf(stderr,"[E] Omiting line : %s\n",aux);
N--;
}
i++;
}
}
break;
case MODE_PUB2RMD:
case MODE_RMD160:
if(FLAGRAWDATA) {
aux = (char*) malloc(MAXLENGTHADDRESS);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
while(i < N) {
if(fread(aux,1,MAXLENGTHADDRESS,fd) == 20) {
memcpy(addressTable[i].value,aux,20);
bloom_add(&bloom, aux,MAXLENGTHADDRESS);
}
i++;
}
}
else {
aux = (char*) malloc(3*MAXLENGTHADDRESS);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
while(i < N) {
memset(aux,0,3*MAXLENGTHADDRESS);
hextemp = fgets(aux,3*MAXLENGTHADDRESS,fd);
memset(addressTable[i].value,0,20);
if(hextemp == aux) {
trim(aux," \t\n\r");
lenaux = strlen(aux);
if(isValidHex(aux)) {
if(lenaux == 40) {
if(hexs2bin(aux,addressTable[i].value)) {
bloom_add(&bloom,addressTable[i].value,MAXLENGTHADDRESS);
}
else {
fprintf(stderr,"[E] error hexs2bin\n");
}
}
else {
fprintf(stderr,"[E] Ignoring invalid length line %s\n",aux);
}
}
else {
fprintf(stderr,"[E] Ignoring invalid hexvalue %s\n",aux);
}
}
else {
fprintf(stderr,"[E] Omiting line : %s\n",aux);
}
i++;
}
}
break;
}
free(aux);
fclose(fd);
printf("[+] Bloomfilter completed\n");
if(FLAGALREADYSORTED) {
printf("[+] File mark already sorted, skipping sort proccess\n");
printf("[+] %" PRIu64 " values were loaded\n",N);
_sort(addressTable,N);
}
else {
printf("[+] Sorting data ...");
_sort(addressTable,N);
printf(" done! %" PRIu64 " values were loaded and sorted\n",N);
}
}
if(FLAGMODE == MODE_BSGS) {
DEBUGCOUNT = N_SECUENTIAL_MAX ;
aux = (char*) malloc(1024);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
while(!feof(fd)) {
if(fgets(aux,1022,fd) == aux) {
trim(aux," \t\n\r");
if(strlen(aux) >= 128) { //Length of a full address in hexadecimal without 04
N++;
}else {
if(strlen(aux) >= 66) {
N++;
}
}
}
}
if(N == 0) {
fprintf(stderr,"[E] There is no valid data in the file\n");
exit(0);
}
bsgs_found = (int*) calloc(N,sizeof(int));
OriginalPointsBSGS.reserve(N);
OriginalPointsBSGScompressed = (bool*) malloc(N*sizeof(bool));
pointx_str = (char*) malloc(65);
pointy_str = (char*) malloc(65);
if(pointy_str == NULL || pointx_str == NULL || bsgs_found == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
fseek(fd,0,SEEK_SET);
i = 0;
while(!feof(fd)) {
if(fgets(aux,1022,fd) == aux) {
trim(aux," \t\n\r");
if(strlen(aux) >= 66) {
stringtokenizer(aux,&tokenizerbsgs);
aux2 = nextToken(&tokenizerbsgs);
memset(pointx_str,0,65);
memset(pointy_str,0,65);
switch(strlen(aux2)) {
case 66: //Compress
if(secp->ParsePublicKeyHex(aux2,OriginalPointsBSGS[i],OriginalPointsBSGScompressed[i])) {
i++;
}
else {
N--;
}
break;
/* Somebody use this? To be removed 5/Nov */
/*
case 128: //Without the 04
memcpy(pointx_str,aux2,64);
memcpy(pointy_str,aux2+64,64);
if(isValidHex(pointx_str) && isValidHex(pointy_str)) {
mpz_init_set_str(OriginalPointsBSGS[i].x,pointx_str,16);
mpz_init_set_str(OriginalPointsBSGS[i].y,pointy_str,16);
//printf("Adding point ( %s , %s )\n",pointx_str,pointy_str);
i++;
}
else {
fprintf(stderr,"[E] Some invalid hexdata in the file: %s\n",aux2);
N--;
}
break;
*/
case 130: //With the 04
if(secp->ParsePublicKeyHex(aux2,OriginalPointsBSGS[i],OriginalPointsBSGScompressed[i])) {
i++;
}
else {
N--;
}
break;
default:
printf("Invalid length: %s\n",aux2);
N--;
break;
}
freetokenizer(&tokenizerbsgs);
}
}
}
fclose(fd);
bsgs_point_number = N;
if(bsgs_point_number > 0) {
printf("[+] Added %u points from file\n",bsgs_point_number);
}
else {
printf("[E] The file don't have any valid publickeys\n");
exit(0);
}
BSGS_N.SetInt32(0);
BSGS_M.SetInt32(0);
/*
hextemp = BSGS_N.GetBase10();
printf("[+] BSGS_N: %s\n",hextemp);
free(hextemp);
hextemp = BSGS_M.GetBase10();
printf("[+] BSGS_M: %s\n",hextemp);
free(hextemp);
*/
BSGS_M.SetInt64(bsgs_m);
//printf("[+] bsgs_m: %"PRIu64"\n",bsgs_m);
/*
hextemp = BSGS_N.GetBase10();
printf("[+] BSGS_N: %s\n",hextemp);
free(hextemp);
hextemp = BSGS_M.GetBase10();
printf("[+] BSGS_M: %s\n",hextemp);
free(hextemp);
*/
if(FLAG_N) { //Custom N by the -n param
BSGS_N.SetInt64(N_SECUENTIAL_MAX);
}
else { //Default N
BSGS_N.SetInt64((uint64_t)0x100000000000);
}
if(BSGS_N.HasSqrt()) { //If the root is exact
BSGS_M.Set(&BSGS_N);
BSGS_M.ModSqrt();
}
else {
fprintf(stderr,"[E] -n param doesn't have exact square root\n");
exit(0);
}
/*
hextemp = BSGS_N.GetBase16();
printf("[+] BSGS_N: %s\n",hextemp);
free(hextemp);
hextemp = BSGS_M.GetBase16();
printf("[+] BSGS_M: %s\n",hextemp);
free(hextemp);
*/
BSGS_AUX.Set(&BSGS_M);
BSGS_AUX.Mod(&BSGS_GROUP_SIZE);
if(!BSGS_AUX.IsZero()){
hextemp = BSGS_GROUP_SIZE.GetBase10();