forked from albertobsd/keyhunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyhunt.c
2334 lines (2215 loc) · 71.6 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 <gmp.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <math.h>
#include <time.h>
#include "keccak/keccak-tiny.h"
#include "base58/libbase58.h"
#include "rmd160/rmd160.h"
#include "sha256/sha256.h"
#include "bloom/bloom.h"
#include "util.h"
#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
struct Point {
mpz_t x;
mpz_t y;
};
struct Elliptic_Curve {
mpz_t p;
mpz_t n;
};
struct bsgs_xvalue {
uint8_t value[32];
int64_t index;
};
struct tothread {
int nt; //Number thread
char *rs; //range start
char *rpt; //rng per thread
};
const char *version = "0.1.20210112 BSGS";
const char *EC_constant_N = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141";
const char *EC_constant_P = "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f";
const char *EC_constant_Gx = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798";
const char *EC_constant_Gy = "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8";
struct Point DoublingG[256];
void Point_Doubling(struct Point *P, struct Point *R);
void Point_Addition(struct Point *P, struct Point *Q, struct Point *R);
void Scalar_Multiplication(struct Point P, struct Point *R, mpz_t m);
void Point_Negation(struct Point *A, struct Point *S);
int searchbinary(char *BUFFER,char *data,int length,int _N);
void _sort(char *arr,int N);
void _insertionsort(char *arr, int n);
void _introsort(char *arr,int depthLimit, int n);
void swap(char *a,char *b);
int partition(char *arr, int n);
void heapsort(char *arr, int int64_t);
void heapify(char *arr, int n, int i);
void bsgs_sort(struct bsgs_xvalue *arr,int64_t n);
void bsgs_heapsort(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 *buffer,char *data,int64_t _N,int64_t *r_value);
void *thread_process(void *vargp);
void *thread_process_range(void *vargp);
void *thread_process_bsgs(void *vargp);
void *thread_process_bsgs_random(void *vargp);
void init_doublingG(struct Point *P);
char *pubkeytopubaddress(char *pkey,int length);
char *pubkeytopubaddress_eth(char *pkey,int length);
char *bit_range_str_min;
char *bit_range_str_max;
const char *modes[3] = {"xpoint","address","bsgs"};
const char *cryptos[3] = {"btc","eth","all"};
const char *default_filename = "addresses.txt";
pthread_t *tid = NULL;
pthread_mutex_t write_keys;
pthread_mutex_t write_range;
pthread_mutex_t write_random;
pthread_mutex_t threads_end;
pthread_mutex_t bsgs_thread;
struct Elliptic_Curve EC;
struct bloom bloom;
struct Point G;
int FLAGDEBUG = 1;
unsigned int *steps = NULL;
unsigned int *ends = NULL;
char *DATABUFFER;
uint32_t N = 0;
gmp_randstate_t state;
uint64_t N_SECUENTIAL_MAX = 0xffffffff;
uint64_t DEBUGCOUNT = 0x100000;
int MAXLENGTHADDRESS = -1;
int NTHREADS = 1;
int OUTPUTSECONDS = 30;
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 len_vanity;
int bitrange;
char *vanity;
char *range_start;
char *range_end;
uint64_t BSGS_BUFFERXPOINTLENGTH = 32;
uint64_t BSGS_BUFFERREGISTERLENGTH = 36;
/*
BSGS Variables
*/
int *bsgs_found;
struct Point *OriginalPointsBSGS;
struct bsgs_xvalue *bPtable;
struct bloom bloom_bPx;
uint64_t bsgs_m;
uint32_t bsgs_point_number;
mpz_t BSGS_CURRENT;
mpz_t BSGS_N;
mpz_t BSGS_M; //M is squareroot(N)
struct Point BSGS_P; //Original P is actually G, but this P value change over time for calculations
struct Point BSGS_MP; //MP values this is m * P
struct Point *BSGS_AMP;
struct Point point_temp,point_temp2; //Temp value for some process
mpz_t n_range_start;
mpz_t n_range_end;
mpz_t n_range_diff;
mpz_t n_range_per_threads;
mpz_t n_range_aux;
mpz_t n_range_r;
int main(int argc, char **argv) {
char temporal[65];
struct tothread *tt; //tothread
Tokenizer t,tokenizerbsgs; //tokenizer
char *filename,*precalculated_p_filename,*precalculated_mp_filename;
FILE *fd;
char *hextemp,*aux,*aux2,*pointx_str,*pointy_str;
uint64_t i;
uint32_t j;
int readed,s,continue_flag,check_flag,r,lenaux,lendiff;
mpz_t total;
mpz_t pretotal;
mpz_t debugcount_mpz;
uint32_t seconds = 0;
int c;
gmp_randinit_mt(state);
gmp_randseed_ui(state, ((int)clock()) * ((int)time(NULL)) );
printf("[+] Version %s\n",version);
mpz_init_set_str(EC.p, EC_constant_P, 16);
mpz_init_set_str(EC.n, EC_constant_N, 16);
mpz_init_set_str(G.x , EC_constant_Gx, 16);
mpz_init_set_str(G.y , EC_constant_Gy, 16);
init_doublingG(&G);
while ((c = getopt (argc, argv, "ehRwb:c:f:g:m:n:p:r:s:t:v:")) != -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("\t\teth option is under develop sorry :(\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("-m mode\t\tmode of search for cryptos. < address, xpoint, bsgs > 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("-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/Secuential this is the default behaivor, can't use this with range option -r\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("\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: 1H3TAVNZFZfiLUp9o9E93oTVY9WgYZ5knX\n");
printf("Thanks to Iceland always helping and sharing his ideas\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 ) {
bit_range_str_min = calloc(1,bitrange+1);
bit_range_str_max = calloc(1,bitrange+1);
if(bit_range_str_min == NULL||bit_range_str_max == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
memset(bit_range_str_min,'1',bitrange);
memset(bit_range_str_max,'1',bitrange);
bit_range_str_min[0] = '0';
printf("bit min range: %s\n",bit_range_str_min);
printf("bit 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 '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 'm':
switch(indexOf(optarg,modes,3)) {
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;
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 'p':
FLAGPRECALCUTED_P_FILE = 1;
precalculated_p_filename = optarg;
break;
case 'v':
FLAGVANITY = 1;
vanity = optarg;
len_vanity = strlen(optarg);
printf("[+] Added Vanity search : %s\n",vanity);
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 = (char*) EC_constant_N;
}
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 = strtol(optarg,NULL,10);
if(OUTPUTSECONDS < 0) {
OUTPUTSECONDS = 30;
}
if(OUTPUTSECONDS == 0) {
printf("[+] Turn off stats output\n");
}
else {
printf("[+] Stats output every %u seconds\n",OUTPUTSECONDS);
}
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 'w':
printf("[+] Data marked as RAW\n");
FLAGRAWDATA = 1;
break;
default:
printf("[E] Unknow opcion %c\n",c);
break;
}
}
if(FLAGMODE != MODE_BSGS && FLAGRANDOM == 1) {
FLAGRANGE = 0;
}
if(DEBUGCOUNT > N_SECUENTIAL_MAX) {
DEBUGCOUNT = N_SECUENTIAL_MAX - 1;
//printf("Setting debug count to %u",N_SECUENTIAL_MAX);
}
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) {
mpz_init_set_str(n_range_start,range_start,16);
mpz_init_set_str(n_range_end,range_end,16);
if(mpz_cmp(n_range_start,n_range_end) != 0 ) {
if(mpz_cmp(n_range_start,EC.n) < 0 && mpz_cmp(n_range_end,EC.n) <= 0) {
if(mpz_cmp(n_range_start,n_range_end) > 0) {
fprintf(stderr,"[W] Opps, start and range can't be great than End range. Swapping them\n");
mpz_init_set(n_range_aux,n_range_start);
mpz_set(n_range_start,n_range_end);
mpz_set(n_range_end,n_range_aux);
mpz_clear(n_range_aux);
}
mpz_init(n_range_per_threads);
mpz_init(n_range_diff);
mpz_init(n_range_r);
mpz_sub(n_range_diff,n_range_end,n_range_start);
mpz_fdiv_q_ui(n_range_per_threads,n_range_diff,NTHREADS);
mpz_mod_ui(n_range_r,n_range_diff,NTHREADS);
}
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;
}
}
N =0;
if(FLAGMODE != MODE_BSGS) {
if(FLAGRAWDATA) {
aux = malloc(32);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
}
while(!feof(fd)) {
if(fread(aux,1,32,fd) == 32) {
N++;
}
}
free(aux);
}
else {
aux = malloc(1000);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
}
while(!feof(fd)) {
hextemp = fgets(aux,1000,fd);
if(hextemp == aux) {
trim(aux," \t\n\r");
//printf("reading %s\n",aux);
r = strlen(aux);
if(r > 10) { //Any length for invalid Address?
if(r > MAXLENGTHADDRESS) {
MAXLENGTHADDRESS = r;
}
N++;
}
}
}
free(aux);
}
fseek(fd,0,SEEK_SET);
if(FLAGMODE == 0 || FLAGRAWDATA) {
MAXLENGTHADDRESS = 32;
}
printf("[+] Allocating memory for %u elements\n",N);
i = 0;
do {
DATABUFFER = malloc(MAXLENGTHADDRESS*N);
i++;
} while(DATABUFFER == NULL && i < 10);
if(DATABUFFER == NULL) {
fprintf(stderr,"[E] Can't alloc memory for %u elements\n",N);
exit(0);
}
printf("[+] Initializing bloom filter for %u elements.\n",N);
if(N <= 10000) {
if(bloom_init2(&bloom,10000,0.0001) == 1){
fprintf(stderr,"[E] error bloom_init for 10000 elements.\n");
exit(0);
}
}
else {
if(bloom_init2(&bloom,N,0.0001) == 1){
fprintf(stderr,"[E] error bloom_init for %u elements.\n",N);
exit(0);
}
}
printf("[+] Loading data to the bloomfilter\n");
i = 0;
if(FLAGMODE == MODE_ADDRESS) { //Address
aux = malloc(2*MAXLENGTHADDRESS);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
while(i < N) {
memset(aux,0,2*MAXLENGTHADDRESS);
memset(DATABUFFER + (i*MAXLENGTHADDRESS),0,MAXLENGTHADDRESS);
hextemp = fgets(aux,2*MAXLENGTHADDRESS,fd);
if(hextemp == aux) {
trim(aux," \t\n\r");
bloom_add(&bloom, aux,MAXLENGTHADDRESS);
memcpy(DATABUFFER + (i*MAXLENGTHADDRESS),aux,MAXLENGTHADDRESS);
i++;
}
else {
trim(aux," \t\n\r");
fprintf(stderr,"[E] Omiting line : %s\n",aux);
}
}
}
if(FLAGMODE == MODE_XPOINT) {
if(FLAGRAWDATA) {
aux = malloc(MAXLENGTHADDRESS);
if(aux == NULL) {
fprintf(stderr,"[E] error malloc()\n");
exit(0);
}
while(i < N) {
if(fread(aux,1,MAXLENGTHADDRESS,fd) == 32) {
memcpy(DATABUFFER + (i*MAXLENGTHADDRESS),aux,MAXLENGTHADDRESS);
bloom_add(&bloom, aux,MAXLENGTHADDRESS);
}
i++;
}
}
else {
aux = 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);
if(hextemp == aux) {
trim(aux," \t\n\r");
lenaux = strlen(aux);
memset(DATABUFFER + (i*MAXLENGTHADDRESS),0,MAXLENGTHADDRESS);
if(isValidHex(aux)) {
if(lenaux <= 64) {
if(lenaux < 64) {
aux2 = calloc(3*MAXLENGTHADDRESS,1);
lendiff = 64 - lenaux;
memcpy(aux2+lendiff,aux,lenaux);
memset(aux2,'0',lendiff);
memcpy(aux,aux2,3*MAXLENGTHADDRESS);
free(aux2);
}
if(hexs2bin(aux,(unsigned char*)(DATABUFFER + (uint64_t)(i*MAXLENGTHADDRESS)))) {
bloom_add(&bloom,(char*)( DATABUFFER + (uint64_t)(i*MAXLENGTHADDRESS)),MAXLENGTHADDRESS);
}
else {
fprintf(stderr,"[E] error hexs2bin\n");
}
}
else {
fprintf(stderr,"[E] Omiting line : %s\n",aux);
}
}
else {
fprintf(stderr,"[E] Ignoring invalid hexvalue %s\n",aux);
}
i++;
}
else {
fprintf(stderr,"[E] Omiting line : %s\n",aux);
}
}
}
}
free(aux);
fclose(fd);
printf("[+] Bloomfilter completed\n");
if(FLAGALREADYSORTED) {
printf("[+] File mark already sorted, skipping sort proccess\n");
printf("[+] %i values were loaded\n",N);
_sort(DATABUFFER,N);
_insertionsort(DATABUFFER,N);
}
else {
printf("[+] Sorting data\n");
_sort(DATABUFFER,N);
_insertionsort(DATABUFFER,N);
printf("[+] %i values were loaded and sorted\n",N);
}
}
if(FLAGMODE == MODE_BSGS) {
DEBUGCOUNT = N_SECUENTIAL_MAX ;
aux = 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++;
}
}
}
if(N == 0) {
fprintf(stderr,"[E] There is no valid data in the file\n");
exit(0);
}
bsgs_found = calloc(N,sizeof(int));
OriginalPointsBSGS = malloc(N*sizeof(struct Point));
pointx_str = malloc(65);
pointy_str = malloc(65);
if(OriginalPointsBSGS == NULL || 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) >= 128) {
stringtokenizer(aux,&tokenizerbsgs);
aux2 = nextToken(&tokenizerbsgs);
memset(pointx_str,0,65);
memset(pointy_str,0,65);
switch(strlen(aux2)) {
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
memcpy(pointx_str,aux2+2,64);
memcpy(pointy_str,aux2+2+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;
default:
printf("Invalid length: %s\n",aux2);
N--;
break;
}
freetokenizer(&tokenizerbsgs);
}
}
}
fclose(fd);
bsgs_point_number = N;
printf("[+] Added %u points from file\n",bsgs_point_number);
mpz_init(BSGS_N);
mpz_init(BSGS_M);
mpz_init(point_temp.x);
mpz_init(point_temp.y);
mpz_init(point_temp2.x);
mpz_init(point_temp2.y);
mpz_init_set_ui(BSGS_MP.x,0);
mpz_init_set_ui(BSGS_MP.y,0);
mpz_init_set(BSGS_P.x,G.x);
mpz_init_set(BSGS_P.y,G.y);
mpz_set_ui(BSGS_M,bsgs_m);
if(FLAG_N) { //Custom N by the -n param
memset(aux,0,100);
sprintf(aux,"%llu",N_SECUENTIAL_MAX);
mpz_set_str(BSGS_N,aux,10);
}
else { //Default N
mpz_set_str(BSGS_N,"100000000000",16);
}
if(!mpz_root(BSGS_M,BSGS_N,2)) { //If the root wasn't exact
mpz_add_ui(BSGS_M,BSGS_M,1); //Add an extra integer, This is like a CEIL Funtion
}
bsgs_m = mpz_get_ui(BSGS_M);
mpz_mul(BSGS_N,BSGS_M,BSGS_M);
DEBUGCOUNT = (uint64_t)((uint64_t)bsgs_m * (uint64_t)bsgs_m);
if(FLAGRANGE || FLAGBITRANGE) {
if(FLAGBITRANGE) { // Bit Range
mpz_init_set_str(n_range_start,bit_range_str_min,2);
mpz_init_set_str(n_range_end,bit_range_str_max,2);
mpz_init(n_range_diff);
mpz_sub(n_range_diff,n_range_end,n_range_start);
printf("Bit Range\n");
}
}
else {
mpz_init_set_ui(n_range_start,1);
mpz_init_set(n_range_end,EC.n);
mpz_urandomm(n_range_start,state,n_range_end);
mpz_init(n_range_diff);
mpz_sub(n_range_diff,n_range_end,n_range_start);
}
mpz_init_set(BSGS_CURRENT,n_range_start);
if(mpz_cmp(n_range_diff,BSGS_N) < 0 ) {
mpz_set(BSGS_N,n_range_diff);
if(!mpz_root(BSGS_M,BSGS_N,2)) { //If the root wasn't exact
mpz_add_ui(BSGS_M,BSGS_M,1); //Add an extra integer, This is CEIL Funtion
}
bsgs_m = mpz_get_ui(BSGS_M);
mpz_mul(BSGS_N,BSGS_M,BSGS_M);
DEBUGCOUNT = (uint64_t)((uint64_t)bsgs_m * (uint64_t)bsgs_m);
}
printf("[+] Setting N up to %llu.\n",DEBUGCOUNT);
if(bsgs_m > 1000) {
if(bsgs_m > 1000) {
if(bloom_init2(&bloom_bPx,bsgs_m,0.001) == 1){
fprintf(stderr,"[E] error bloom_init for %u elements\n",bsgs_m);
exit(0);
}
}
else {
/*
if(bloom_init(&bloom_bPx,bsgs_m,0.001) == 1){
fprintf(stderr,"[E] error bloom_init for %u elements\n",bsgs_m);
exit(0);
}
*/
}
}
else {
if(bloom_init2(&bloom_bPx,1000,0.001) == 1){
fprintf(stderr,"[E] error bloom_init for 1000 elements\n");
exit(0);
}
}
printf("[+] Init bloom filter for %u elements : %.2f MB\n",bsgs_m,(float)((uint32_t)bloom_bPx.bytes/(uint32_t)1048576));
//gmp_printf("BSGS_M: %0.64Zx\n",BSGS_M);
Scalar_Multiplication(G,&BSGS_MP,BSGS_M);
printf("[+] Allocating %.2f MB for aMP Points\n",(float)(((uint32_t)(bsgs_m*sizeof(struct Point)))/(uint32_t)1048576));
i = 0;
do {
BSGS_AMP = malloc((uint64_t)((uint64_t)bsgs_m*(uint64_t)sizeof(struct Point)));
i++;
if(BSGS_AMP == NULL) {
sleep(1);
}
} while( i <= 10 && BSGS_AMP == NULL);
if(BSGS_AMP == NULL) {
printf("[E] error malloc()\n");
exit(0);
}
i= 0;
if(FLAGPRECALCUTED_MP_FILE) {
printf("[+] Reading aMP points from file %s\n",precalculated_mp_filename);
fd = fopen(precalculated_mp_filename,"rb");
if(fd != NULL) {
while(!feof(fd) && i < bsgs_m ) {
if(fread(temporal,1,64,fd) == 64) {
hextemp = tohex(temporal,32);
mpz_init_set_str(BSGS_AMP[i].x,hextemp,16);
free(hextemp);
hextemp = tohex(temporal+32,32);
mpz_init_set_str(BSGS_AMP[i].y,hextemp,16);
free(hextemp);
i++;
}
}
if(i < bsgs_m) { //If the input file have less item than bsgs_m
printf("[+] Fixme file contains less items than the amount of items needed\n");
exit(0);
}
}
else {
fprintf(stderr,"[E] Can't open file %s falling back to the calculation mode\n",filename);
printf("[+] Precalculating %u aMP points\n",bsgs_m);
mpz_set(point_temp.x,BSGS_MP.x);
mpz_set(point_temp.y,BSGS_MP.y);
for(i = 0; i < bsgs_m; i++) {
mpz_init(BSGS_AMP[i].x);
mpz_init(BSGS_AMP[i].y);
Point_Negation(&point_temp,&BSGS_AMP[i]);
Point_Addition(&point_temp,&BSGS_MP,&point_temp2);
mpz_set(point_temp.x,point_temp2.x);
mpz_set(point_temp.y,point_temp2.y);
}
}
}
else {
printf("[+] Precalculating %u aMP points\n",bsgs_m);
mpz_set(point_temp.x,BSGS_MP.x);
mpz_set(point_temp.y,BSGS_MP.y);
for(i = 0; i < bsgs_m; i++) {
mpz_init(BSGS_AMP[i].x);
mpz_init(BSGS_AMP[i].y);
Point_Negation(&point_temp,&BSGS_AMP[i]);
Point_Addition(&point_temp,&BSGS_MP,&point_temp2);
mpz_set(point_temp.x,point_temp2.x);
mpz_set(point_temp.y,point_temp2.y);
}
}
printf("[+] Allocating %.2f MB for bP Points\n",(float)((uint64_t)((uint64_t)bsgs_m*(uint64_t)sizeof(struct bsgs_xvalue))/(uint64_t)1048576));
bPtable = calloc(bsgs_m,sizeof(struct bsgs_xvalue));
if(bPtable == NULL) {
printf("[E] error malloc()\n");
exit(0);
}
i = 0;
j = 0;
if(FLAGPRECALCUTED_P_FILE) {
printf("[+] Reading bP points from file %s\n",precalculated_p_filename);
fd = fopen(precalculated_p_filename,"rb");
if(fd != NULL) {
while(!feof(fd) && i < bsgs_m ) {
if(fread(temporal,1,32,fd) == 32) {
memcpy(bPtable[i].value,temporal,BSGS_BUFFERXPOINTLENGTH);
bPtable[i].index = j;
bloom_add(&bloom_bPx, bPtable[i].value,BSGS_BUFFERXPOINTLENGTH);
i++;
j++;
}
}
if(i < bsgs_m) { //If the input file have less item than bsgs_m
printf("[+] Fixme file contains less items than the amount of items needed\n");
exit(0);
}
}
else {
fprintf(stderr,"[E] Can't open file %s falling back to the calculation mode\n",filename);
printf("[+] Precalculating %u bP points\n",bsgs_m);
do {
mpz_set(point_temp.x,BSGS_P.x);
mpz_set(point_temp.y,BSGS_P.y);
gmp_sprintf(temporal,"%0.64Zx",BSGS_P.x);
hexs2bin(temporal,bPtable[i].value );
bPtable[i].index = j;
bloom_add(&bloom_bPx, bPtable[i].value,BSGS_BUFFERXPOINTLENGTH);
Point_Addition(&G,&point_temp,&BSGS_P);
i++;
j++;
} while( i < bsgs_m );
}
}
else {
printf("[+] precalculating %u bP points\n",bsgs_m);
do {
mpz_set(point_temp.x,BSGS_P.x);
mpz_set(point_temp.y,BSGS_P.y);
gmp_sprintf(temporal,"%0.64Zx",BSGS_P.x);
//printf("[+] %i: %s\n",i,temporal);
hexs2bin(temporal,bPtable[i].value );
bPtable[i].index = j;
bloom_add(&bloom_bPx, bPtable[i].value,BSGS_BUFFERXPOINTLENGTH);
Point_Addition(&G,&point_temp,&BSGS_P);
i++;
j++;
} while( i < bsgs_m );
}
printf("[+] Sorting %u elements\n",bsgs_m);
bsgs_sort(bPtable,bsgs_m);
i = 0;
steps = (unsigned int *) calloc(NTHREADS,sizeof(int));
ends = (unsigned int *) calloc(NTHREADS,sizeof(int));
tid = (pthread_t *) calloc(NTHREADS,sizeof(pthread_t));
DEBUGCOUNT = (uint64_t)((uint64_t)bsgs_m * (uint64_t)bsgs_m);
for(i= 0;i < NTHREADS; i++) {
tt = malloc(sizeof(struct tothread));
tt->nt = i;
if(FLAGRANDOM) {
s = pthread_create(&tid[i],NULL,thread_process_bsgs_random,(void *)tt);
}
else {
s = pthread_create(&tid[i],NULL,thread_process_bsgs,(void *)tt);
}
if(s != 0) {
fprintf(stderr,"[E] pthread_create thread_process\n");
exit(0);
}
}
free(aux);
}
if(FLAGMODE != MODE_BSGS) {
steps = (unsigned int *) calloc(NTHREADS,sizeof(int));
ends = (unsigned int *) calloc(NTHREADS,sizeof(int));
tid = (pthread_t *) calloc(NTHREADS,sizeof(pthread_t));
if(FLAGRANGE == 0) {
for(i= 0;i < NTHREADS; i++) {
tt = malloc(sizeof(struct tothread));
tt->nt = i;
steps[i] = 0;
s = pthread_create(&tid[i],NULL,thread_process,(void *)tt);
if(s != 0) {
fprintf(stderr,"[E] pthread_create thread_process\n");
exit(0);
}
}
}
else {
for(i= 0;i < NTHREADS; i++) {
if(i == (NTHREADS)-1) {
mpz_add(n_range_per_threads,n_range_per_threads,n_range_r);
}
tt = malloc(sizeof(struct tothread));
tt->nt = i;
tt->rs = malloc(65);
mpz_get_str(tt->rs,16,n_range_start);
tt->rpt = malloc(65);
mpz_get_str(tt->rpt,16,n_range_per_threads);
steps[i] = 0;
s = pthread_create(&tid[i],NULL,thread_process_range,(void *)tt);
if(s != 0) {
fprintf(stderr,"[E] pthread_create thread_process\n");
exit(0);
}
mpz_add(n_range_start,n_range_start,n_range_per_threads);
}
}
if(FLAGRANGE) {
mpz_clear(n_range_per_threads);
mpz_clear(n_range_start);
mpz_clear(n_range_end);
mpz_clear(n_range_diff);
mpz_clear(n_range_r);
}
}
continue_flag = 1;
mpz_init(total);
mpz_init(pretotal);
mpz_init(debugcount_mpz);
sprintf(temporal,"%llu",DEBUGCOUNT);
mpz_set_str(debugcount_mpz,temporal,10);
do {
sleep(1);
seconds+=1;
if(FLAGRANGE) {
check_flag = 1;
pthread_mutex_lock(&threads_end);
for(i = 0; i <NTHREADS && check_flag; i++) {
check_flag &= ends[i];
}
pthread_mutex_unlock(&threads_end);
if(check_flag) {
continue_flag = 0;
}
}
if(OUTPUTSECONDS > 0){
if(seconds % OUTPUTSECONDS == 0) {
mpz_set_ui(total,0);
i = 0;
while(i < NTHREADS) {
mpz_mul_ui(pretotal,debugcount_mpz,steps[i]);
mpz_add(total,total,pretotal);