-
Notifications
You must be signed in to change notification settings - Fork 435
/
bsgsd.cpp
2468 lines (2050 loc) · 69.5 KB
/
bsgsd.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
/*
Develop by Alberto
email: [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <vector>
#include <inttypes.h>
#include "base58/libbase58.h"
#include "rmd160/rmd160.h"
#include "oldbloom/oldbloom.h"
#include "bloom/bloom.h"
#include "sha3/sha3.h"
#include "util.h"
#include "secp256k1/SECP256k1.h"
#include "secp256k1/Point.h"
#include "secp256k1/Int.h"
#include "secp256k1/IntGroup.h"
#include "secp256k1/Random.h"
#include "hash/sha256.h"
#include "hash/ripemd160.h"
#include <unistd.h>
#include <pthread.h>
#include <sys/random.h>
#include <linux/random.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> // for inet_addr()
#include <pthread.h> // for pthread functions
#define PORT 8080
#define BUFFER_SIZE 1024
#define MODE_BSGS 2
uint32_t THREADBPWORKLOAD = 1048576;
struct checksumsha256 {
char data[32];
char backup[32];
};
struct bsgs_xvalue {
uint8_t value[6];
uint64_t index;
};
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;
uint64_t workload;
uint32_t aux;
uint32_t finished;
};
const char *version = "0.2.230519 Satoshi Quest";
const char *ip_default = "127.0.0.1";
char *IP;
int port;
#define CPU_GRP_SIZE 1024
std::vector<Point> Gn;
Point _2Gn;
std::vector<Point> GSn;
Point _2GSn;
void menu();
void init_generator();
int sendstr(int client_fd,const char *str);
void sleep_ms(int milliseconds);
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 array_length,uint64_t *r_value);
int bsgs_secondcheck(Int *start_range,uint32_t a,Int *privatekey);
int bsgs_thirdcheck(Int *start_range,uint32_t a,Int *privatekey);
void writekey(bool compressed,Int *key);
void checkpointer(void *ptr,const char *file,const char *function,const char *name,int line);
void* client_handler(void* arg);
void calcualteindex(int i,Int *key);
void *thread_process_bsgs(void *vargp);
void *thread_bPload(void *vargp);
void *thread_bPload_2blooms(void *vargp);
char *publickeytohashrmd160(char *pkey,int length);
void publickeytohashrmd160_dst(char *pkey,int length,char *dst);
char *pubkeytopubaddress(char *pkey,int length);
void pubkeytopubaddress_dst(char *pkey,int length,char *dst);
void rmd160toaddress_dst(char *rmd,char *dst);
int THREADOUTPUT = 0;
char *bit_range_str_min;
char *bit_range_str_max;
const char *bsgs_modes[5] = {"secuential","backward","both","random","dance"};
pthread_t *tid = NULL;
pthread_mutex_t write_keys;
pthread_mutex_t write_random;
pthread_mutex_t mutex_bsgs_thread;
pthread_mutex_t *bPload_mutex;
uint64_t FINISHED_THREADS_COUNTER = 0;
uint64_t FINISHED_THREADS_BP = 0;
uint64_t THREADCYCLES = 0;
uint64_t THREADCOUNTER = 0;
uint64_t FINISHED_ITEMS = 0;
uint64_t OLDFINISHED_ITEMS = -1;
uint8_t byte_encode_crypto = 0x00; /* Bitcoin */
struct bloom bloom;
uint64_t N = 0;
uint64_t N_SECUENTIAL_MAX = 0x100000000;
uint64_t DEBUGCOUNT = 0x400;
uint64_t u64range;
Int BSGSkeyfound;
int FLAGSKIPCHECKSUM = 0;
int FLAGBSGSMODE = 0;
int FLAGDEBUG = 0;
int KFACTOR = 1;
int MAXLENGTHADDRESS = 20;
int NTHREADS = 1;
int FLAGSAVEREADFILE = 1;
int FLAGREADEDFILE1 = 0;
int FLAGREADEDFILE2 = 0;
int FLAGREADEDFILE3 = 0;
int FLAGREADEDFILE4 = 0;
int FLAGUPDATEFILE1 = 0;
int FLAGBITRANGE = 0;
int FLAGRANGE = 0;
int FLAGMODE = MODE_BSGS;
int FLAG_N = 0;
int bitrange;
char *str_N;
char *range_start;
char *range_end;
char *str_stride;
Int stride;
uint64_t BSGS_XVALUE_RAM = 6;
uint64_t BSGS_BUFFERXPOINTLENGTH = 32;
uint64_t BSGS_BUFFERREGISTERLENGTH = 36;
/*
BSGS Variables
*/
int bsgs_found;
Point OriginalPointsBSGS;
bool OriginalPointsBSGScompressed;
uint64_t bytes;
char checksum[32],checksum_backup[32];
char buffer_bloom_file[1024];
struct bsgs_xvalue *bPtable;
struct oldbloom oldbloom_bP;
struct bloom *bloom_bP;
struct bloom *bloom_bPx2nd; //2nd Bloom filter check
struct bloom *bloom_bPx3rd; //3rd Bloom filter check
struct checksumsha256 *bloom_bP_checksums;
struct checksumsha256 *bloom_bPx2nd_checksums;
struct checksumsha256 *bloom_bPx3rd_checksums;
pthread_mutex_t *bloom_bP_mutex;
pthread_mutex_t *bloom_bPx2nd_mutex;
pthread_mutex_t *bloom_bPx3rd_mutex;
uint64_t bloom_bP_totalbytes = 0;
uint64_t bloom_bP2_totalbytes = 0;
uint64_t bloom_bP3_totalbytes = 0;
uint64_t bsgs_m = 4194304;
uint64_t bsgs_m2;
uint64_t bsgs_m3;
unsigned long int bsgs_aux;
//int32_t bsgs_point_number;
const char *str_limits_prefixs[7] = {"Mkeys/s","Gkeys/s","Tkeys/s","Pkeys/s","Ekeys/s","Zkeys/s","Ykeys/s"};
const char *str_limits[7] = {"1000000","1000000000","1000000000000","1000000000000000","1000000000000000000","1000000000000000000000","1000000000000000000000000"};
Int int_limits[7];
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_M_double;
Int BSGS_M2; //M2 is M/32
Int BSGS_M2_double; //M2_double is M2 * 2
Int BSGS_M3; //M3 is M2/32
Int BSGS_M3_double; //M3_double is M3 * 2
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; //MP2 values this is m2 * P
Point BSGS_MP3; //MP3 values this is m3 * P
Point BSGS_MP_double; //MP2 values this is m2 * P * 2
Point BSGS_MP2_double; //MP2 values this is m2 * P * 2
Point BSGS_MP3_double; //MP3 values this is m3 * P * 2
std::vector<Point> BSGS_AMP2;
std::vector<Point> BSGS_AMP3;
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) {
// File pointers
FILE *fd_aux1, *fd_aux2, *fd_aux3;
// Strings
char *hextemp = NULL;
char *bf_ptr = NULL;
char *bPload_threads_available;
// Buffers
char rawvalue[32];
// 64-bit integers
uint64_t BASE, PERTHREAD_R, itemsbloom, itemsbloom2, itemsbloom3;
// 32-bit integers
uint32_t finished;
int readed, c, salir,i,s;
// Custom integers
Int total, pretotal, debugcount_mpz, seconds, div_pretotal, int_aux, int_r, int_q, int58;
// Pointers
struct bPload *bPload_temp_ptr;
// Sizes
size_t rsize;
pthread_mutex_init(&write_keys,NULL);
pthread_mutex_init(&write_random,NULL);
pthread_mutex_init(&mutex_bsgs_thread,NULL);
srand(time(NULL));
secp = new Secp256K1();
secp->Init();
ZERO.SetInt32(0);
ONE.SetInt32(1);
BSGS_GROUP_SIZE.SetInt32(CPU_GRP_SIZE);
unsigned long rseedvalue;
int bytes_read = getrandom(&rseedvalue, sizeof(unsigned long), GRND_NONBLOCK);
if(bytes_read > 0) {
rseed(rseedvalue);
/*
In any case that seed is for a failsafe RNG, the default source on linux is getrandom function
See https://www.2uo.de/myths-about-urandom/
*/
}
else {
/*
what year is??
WTF linux without RNG ?
*/
fprintf(stderr,"[E] Error getrandom() ?\n");
exit(0);
rseed(clock() + time(NULL) + rand()*rand());
}
port = PORT;
IP = (char*)ip_default;
printf("[+] Version %s, developed by AlbertoBSD\n",version);
while ((c = getopt(argc, argv, "6hk:n:t:p:i:")) != -1) {
switch(c) {
case '6':
FLAGSKIPCHECKSUM = 1;
fprintf(stderr,"[W] Skipping checksums on files\n");
break;
case 'h':
// Show help menu
menu();
break;
case 'k':
// Set KFACTOR
KFACTOR = (int)strtol(optarg,NULL,10);
if(KFACTOR <= 0) {
KFACTOR = 1;
}
printf("[+] K factor %i\n",KFACTOR);
break;
case 'n':
// Set FLAG_N and str_N
FLAG_N = 1;
str_N = optarg;
break;
case 't':
// Set number of threads (NTHREADS)
NTHREADS = strtol(optarg,NULL,10);
if(NTHREADS <= 0) {
NTHREADS = 1;
}
printf((NTHREADS > 1) ? "[+] Threads : %u\n": "[+] Thread : %u\n",NTHREADS);
break;
case 'p':
port = (int) strtol(optarg,NULL,10);
if(port <= 0 || port > 65535 ) {
port = PORT;
}
break;
case 'i':
IP = optarg;
break;
default:
// Handle unknown options
fprintf(stderr,"[E] Unknow opcion -%c\n",c);
exit(0);
break;
}
}
stride.Set(&ONE);
init_generator();
if(FLAGMODE == MODE_BSGS ) {
printf("[+] Mode BSGS %s\n",bsgs_modes[FLAGBSGSMODE]);
}
if(FLAGMODE == MODE_BSGS ) {
BSGS_N.SetInt32(0);
BSGS_M.SetInt32(0);
BSGS_M.SetInt64(bsgs_m);
if(FLAG_N) { //Custom N by the -n param
/* Here we need to validate if the given string is a valid hexadecimal number or a base 10 number*/
/* Now the conversion*/
if(str_N[0] == '0' && (str_N[1] == 'x' || str_N[1] == 'X')) { /*We expected a hexadecimal value after 0x -> str_N +2 */
BSGS_N.SetBase16((char*)(str_N+2));
}
else {
BSGS_N.SetBase10(str_N);
}
}
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);
}
BSGS_AUX.Set(&BSGS_M);
BSGS_AUX.Mod(&BSGS_GROUP_SIZE);
if(!BSGS_AUX.IsZero()){ //If M is not divisible by BSGS_GROUP_SIZE (1024)
hextemp = BSGS_GROUP_SIZE.GetBase10();
fprintf(stderr,"[E] M value is not divisible by %s\n",hextemp);
exit(0);
}
/*
M 2199023255552
109951162777.6
M2 109951162778
5497558138.9
M3 5497558139
*/
BSGS_M.Mult((uint64_t)KFACTOR);
BSGS_AUX.SetInt32(32);
BSGS_R.Set(&BSGS_M);
BSGS_R.Mod(&BSGS_AUX);
BSGS_M2.Set(&BSGS_M);
BSGS_M2.Div(&BSGS_AUX);
if(!BSGS_R.IsZero()) { /* If BSGS_M modulo 32 is not 0*/
BSGS_M2.AddOne();
}
BSGS_M_double.SetInt32(2);
BSGS_M_double.Mult(&BSGS_M);
BSGS_M2_double.SetInt32(2);
BSGS_M2_double.Mult(&BSGS_M2);
BSGS_R.Set(&BSGS_M2);
BSGS_R.Mod(&BSGS_AUX);
BSGS_M3.Set(&BSGS_M2);
BSGS_M3.Div(&BSGS_AUX);
if(!BSGS_R.IsZero()) { /* If BSGS_M2 modulo 32 is not 0*/
BSGS_M3.AddOne();
}
BSGS_M3_double.SetInt32(2);
BSGS_M3_double.Mult(&BSGS_M3);
bsgs_m2 = BSGS_M2.GetInt64();
bsgs_m3 = BSGS_M3.GetInt64();
BSGS_AUX.Set(&BSGS_N);
BSGS_AUX.Div(&BSGS_M);
BSGS_R.Set(&BSGS_N);
BSGS_R.Mod(&BSGS_M);
if(!BSGS_R.IsZero()) { /* if BSGS_N modulo BSGS_M is not 0*/
BSGS_N.Set(&BSGS_M);
BSGS_N.Mult(&BSGS_AUX);
}
bsgs_m = BSGS_M.GetInt64();
bsgs_aux = BSGS_AUX.GetInt64();
hextemp = BSGS_N.GetBase16();
printf("[+] N = 0x%s\n",hextemp);
bsgs_m = BSGS_M.GetInt64();
free(hextemp);
if(((uint64_t)(bsgs_m/256)) > 10000) {
itemsbloom = (uint64_t)(bsgs_m / 256);
if(bsgs_m % 256 != 0 ) {
itemsbloom++;
}
}
else{
itemsbloom = 1000;
}
if(((uint64_t)(bsgs_m2/256)) > 1000) {
itemsbloom2 = (uint64_t)(bsgs_m2 / 256);
if(bsgs_m2 % 256 != 0) {
itemsbloom2++;
}
}
else {
itemsbloom2 = 1000;
}
if(((uint64_t)(bsgs_m3/256)) > 1000) {
itemsbloom3 = (uint64_t)(bsgs_m3/256);
if(bsgs_m3 % 256 != 0 ) {
itemsbloom3++;
}
}
else {
itemsbloom3 = 1000;
}
printf("[+] Bloom filter for %" PRIu64 " elements ",bsgs_m);
bloom_bP = (struct bloom*)calloc(256,sizeof(struct bloom));
checkpointer((void *)bloom_bP,__FILE__,"calloc","bloom_bP" ,__LINE__ -1 );
bloom_bP_checksums = (struct checksumsha256*)calloc(256,sizeof(struct checksumsha256));
checkpointer((void *)bloom_bP_checksums,__FILE__,"calloc","bloom_bP_checksums" ,__LINE__ -1 );
bloom_bP_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t));
checkpointer((void *)bloom_bP_mutex,__FILE__,"calloc","bloom_bP_mutex" ,__LINE__ -1 );
fflush(stdout);
bloom_bP_totalbytes = 0;
for(i=0; i< 256; i++) {
pthread_mutex_init(&bloom_bP_mutex[i],NULL);
if(bloom_init2(&bloom_bP[i],itemsbloom,0.000001) == 1){
fprintf(stderr,"[E] error bloom_init _ %i\n",i);
exit(0);
}
bloom_bP_totalbytes += bloom_bP[i].bytes;
}
printf(": %.2f MB\n",(float)((float)(uint64_t)bloom_bP_totalbytes/(float)(uint64_t)1048576));
printf("[+] Bloom filter for %" PRIu64 " elements ",bsgs_m2);
bloom_bPx2nd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t));
checkpointer((void *)bloom_bPx2nd_mutex,__FILE__,"calloc","bloom_bPx2nd_mutex" ,__LINE__ -1 );
bloom_bPx2nd = (struct bloom*)calloc(256,sizeof(struct bloom));
checkpointer((void *)bloom_bPx2nd,__FILE__,"calloc","bloom_bPx2nd" ,__LINE__ -1 );
bloom_bPx2nd_checksums = (struct checksumsha256*) calloc(256,sizeof(struct checksumsha256));
checkpointer((void *)bloom_bPx2nd_checksums,__FILE__,"calloc","bloom_bPx2nd_checksums" ,__LINE__ -1 );
bloom_bP2_totalbytes = 0;
for(i=0; i< 256; i++) {
pthread_mutex_init(&bloom_bPx2nd_mutex[i],NULL);
if(bloom_init2(&bloom_bPx2nd[i],itemsbloom2,0.000001) == 1){
fprintf(stderr,"[E] error bloom_init _ %i\n",i);
exit(0);
}
bloom_bP2_totalbytes += bloom_bPx2nd[i].bytes;
}
printf(": %.2f MB\n",(float)((float)(uint64_t)bloom_bP2_totalbytes/(float)(uint64_t)1048576));
bloom_bPx3rd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t));
checkpointer((void *)bloom_bPx3rd_mutex,__FILE__,"calloc","bloom_bPx3rd_mutex" ,__LINE__ -1 );
bloom_bPx3rd = (struct bloom*)calloc(256,sizeof(struct bloom));
checkpointer((void *)bloom_bPx3rd,__FILE__,"calloc","bloom_bPx3rd" ,__LINE__ -1 );
bloom_bPx3rd_checksums = (struct checksumsha256*) calloc(256,sizeof(struct checksumsha256));
checkpointer((void *)bloom_bPx3rd_checksums,__FILE__,"calloc","bloom_bPx3rd_checksums" ,__LINE__ -1 );
printf("[+] Bloom filter for %" PRIu64 " elements ",bsgs_m3);
bloom_bP3_totalbytes = 0;
for(i=0; i< 256; i++) {
pthread_mutex_init(&bloom_bPx3rd_mutex[i],NULL);
if(bloom_init2(&bloom_bPx3rd[i],itemsbloom3,0.000001) == 1){
fprintf(stderr,"[E] error bloom_init %i\n",i);
exit(0);
}
bloom_bP3_totalbytes += bloom_bPx3rd[i].bytes;
}
printf(": %.2f MB\n",(float)((float)(uint64_t)bloom_bP3_totalbytes/(float)(uint64_t)1048576));
BSGS_MP = secp->ComputePublicKey(&BSGS_M);
BSGS_MP_double = secp->ComputePublicKey(&BSGS_M_double);
BSGS_MP2 = secp->ComputePublicKey(&BSGS_M2);
BSGS_MP2_double = secp->ComputePublicKey(&BSGS_M2_double);
BSGS_MP3 = secp->ComputePublicKey(&BSGS_M3);
BSGS_MP3_double = secp->ComputePublicKey(&BSGS_M3_double);
BSGS_AMP2.reserve(32);
BSGS_AMP3.reserve(32);
GSn.reserve(CPU_GRP_SIZE/2);
i= 0;
/* New aMP table just to keep the same code of JLP */
/* Auxiliar Points to speed up calculations for the main bloom filter check */
Point bsP = secp->Negation(BSGS_MP_double);
Point g = bsP;
GSn[0] = g;
g = secp->DoubleDirect(g);
GSn[1] = g;
for(int i = 2; i < CPU_GRP_SIZE / 2; i++) {
g = secp->AddDirect(g,bsP);
GSn[i] = g;
}
/* For next center point */
_2GSn = secp->DoubleDirect(GSn[CPU_GRP_SIZE / 2 - 1]);
i = 0;
point_temp.Set(BSGS_MP2);
BSGS_AMP2[0] = secp->Negation(point_temp);
BSGS_AMP2[0].Reduce();
point_temp.Set(BSGS_MP2_double);
point_temp = secp->Negation(point_temp);
for(i = 1; i < 32; i++) {
BSGS_AMP2[i] = secp->AddDirect(BSGS_AMP2[i-1],point_temp);
BSGS_AMP2[i].Reduce();
}
i = 0;
point_temp.Set(BSGS_MP3);
BSGS_AMP3[0] = secp->Negation(point_temp);
BSGS_AMP3[0].Reduce();
point_temp.Set(BSGS_MP3_double);
point_temp = secp->Negation(point_temp);
for(i = 1; i < 32; i++) {
BSGS_AMP3[i] = secp->AddDirect(BSGS_AMP3[i-1],point_temp);
BSGS_AMP3[i].Reduce();
}
bytes = (uint64_t)bsgs_m3 * (uint64_t) sizeof(struct bsgs_xvalue);
printf("[+] Allocating %.2f MB for %" PRIu64 " bP Points\n",(double)(bytes/1048576),bsgs_m3);
bPtable = (struct bsgs_xvalue*) malloc(bytes);
checkpointer((void *)bPtable,__FILE__,"malloc","bPtable" ,__LINE__ -1 );
memset(bPtable,0,bytes);
if(FLAGSAVEREADFILE) {
/*Reading file for 1st bloom filter */
snprintf(buffer_bloom_file,1024,"keyhunt_bsgs_4_%" PRIu64 ".blm",bsgs_m);
fd_aux1 = fopen(buffer_bloom_file,"rb");
if(fd_aux1 != NULL) {
printf("[+] Reading bloom filter from file %s ",buffer_bloom_file);
fflush(stdout);
for(i = 0; i < 256;i++) {
bf_ptr = (char*) bloom_bP[i].bf; /*We need to save the current bf pointer*/
readed = fread(&bloom_bP[i],sizeof(struct bloom),1,fd_aux1);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
bloom_bP[i].bf = (uint8_t*)bf_ptr; /* Restoring the bf pointer*/
readed = fread(bloom_bP[i].bf,bloom_bP[i].bytes,1,fd_aux1);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
readed = fread(&bloom_bP_checksums[i],sizeof(struct checksumsha256),1,fd_aux1);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
memset(rawvalue,0,32);
if(FLAGSKIPCHECKSUM == 0) {
sha256((uint8_t*)bloom_bP[i].bf,bloom_bP[i].bytes,(uint8_t*)rawvalue);
if(memcmp(bloom_bP_checksums[i].data,rawvalue,32) != 0 || memcmp(bloom_bP_checksums[i].backup,rawvalue,32) != 0 ) { /* Verification */
fprintf(stderr,"[E] Error checksum file mismatch! %s\n",buffer_bloom_file);
exit(0);
}
}
if(i % 64 == 0 ) {
printf(".");
fflush(stdout);
}
}
printf(" Done!\n");
fclose(fd_aux1);
memset(buffer_bloom_file,0,1024);
snprintf(buffer_bloom_file,1024,"keyhunt_bsgs_3_%" PRIu64 ".blm",bsgs_m);
fd_aux1 = fopen(buffer_bloom_file,"rb");
if(fd_aux1 != NULL) {
printf("[W] Unused file detected %s you can delete it without worry\n",buffer_bloom_file);
fclose(fd_aux1);
}
FLAGREADEDFILE1 = 1;
}
else { /*Checking for old file keyhunt_bsgs_3_ */
snprintf(buffer_bloom_file,1024,"keyhunt_bsgs_3_%" PRIu64 ".blm",bsgs_m);
fd_aux1 = fopen(buffer_bloom_file,"rb");
if(fd_aux1 != NULL) {
printf("[+] Reading bloom filter from file %s ",buffer_bloom_file);
fflush(stdout);
for(i = 0; i < 256;i++) {
bf_ptr = (char*) bloom_bP[i].bf; /*We need to save the current bf pointer*/
readed = fread(&oldbloom_bP,sizeof(struct oldbloom),1,fd_aux1);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
memcpy(&bloom_bP[i],&oldbloom_bP,sizeof(struct bloom));//We only need to copy the part data to the new bloom size, not from the old size
bloom_bP[i].bf = (uint8_t*)bf_ptr; /* Restoring the bf pointer*/
readed = fread(bloom_bP[i].bf,bloom_bP[i].bytes,1,fd_aux1);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
memcpy(bloom_bP_checksums[i].data,oldbloom_bP.checksum,32);
memcpy(bloom_bP_checksums[i].backup,oldbloom_bP.checksum_backup,32);
memset(rawvalue,0,32);
if(FLAGSKIPCHECKSUM == 0) {
sha256((uint8_t*)bloom_bP[i].bf,bloom_bP[i].bytes,(uint8_t*)rawvalue);
if(memcmp(bloom_bP_checksums[i].data,rawvalue,32) != 0 || memcmp(bloom_bP_checksums[i].backup,rawvalue,32) != 0 ) { /* Verification */
fprintf(stderr,"[E] Error checksum file mismatch! %s\n",buffer_bloom_file);
exit(0);
}
}
if(i % 32 == 0 ) {
printf(".");
fflush(stdout);
}
}
printf(" Done!\n");
fclose(fd_aux1);
FLAGUPDATEFILE1 = 1; /* Flag to migrate the data to the new File keyhunt_bsgs_4_ */
FLAGREADEDFILE1 = 1;
}
else {
FLAGREADEDFILE1 = 0;
//Flag to make the new file
}
}
/*Reading file for 2nd bloom filter */
snprintf(buffer_bloom_file,1024,"keyhunt_bsgs_6_%" PRIu64 ".blm",bsgs_m2);
fd_aux2 = fopen(buffer_bloom_file,"rb");
if(fd_aux2 != NULL) {
printf("[+] Reading bloom filter from file %s ",buffer_bloom_file);
fflush(stdout);
for(i = 0; i < 256;i++) {
bf_ptr = (char*) bloom_bPx2nd[i].bf; /*We need to save the current bf pointer*/
readed = fread(&bloom_bPx2nd[i],sizeof(struct bloom),1,fd_aux2);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
bloom_bPx2nd[i].bf = (uint8_t*)bf_ptr; /* Restoring the bf pointer*/
readed = fread(bloom_bPx2nd[i].bf,bloom_bPx2nd[i].bytes,1,fd_aux2);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
readed = fread(&bloom_bPx2nd_checksums[i],sizeof(struct checksumsha256),1,fd_aux2);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
memset(rawvalue,0,32);
if(FLAGSKIPCHECKSUM == 0) {
sha256((uint8_t*)bloom_bPx2nd[i].bf,bloom_bPx2nd[i].bytes,(uint8_t*)rawvalue);
if(memcmp(bloom_bPx2nd_checksums[i].data,rawvalue,32) != 0 || memcmp(bloom_bPx2nd_checksums[i].backup,rawvalue,32) != 0 ) { /* Verification */
fprintf(stderr,"[E] Error checksum file mismatch! %s\n",buffer_bloom_file);
exit(0);
}
}
if(i % 64 == 0) {
printf(".");
fflush(stdout);
}
}
fclose(fd_aux2);
printf(" Done!\n");
memset(buffer_bloom_file,0,1024);
snprintf(buffer_bloom_file,1024,"keyhunt_bsgs_5_%" PRIu64 ".blm",bsgs_m2);
fd_aux2 = fopen(buffer_bloom_file,"rb");
if(fd_aux2 != NULL) {
printf("[W] Unused file detected %s you can delete it without worry\n",buffer_bloom_file);
fclose(fd_aux2);
}
memset(buffer_bloom_file,0,1024);
snprintf(buffer_bloom_file,1024,"keyhunt_bsgs_1_%" PRIu64 ".blm",bsgs_m2);
fd_aux2 = fopen(buffer_bloom_file,"rb");
if(fd_aux2 != NULL) {
printf("[W] Unused file detected %s you can delete it without worry\n",buffer_bloom_file);
fclose(fd_aux2);
}
FLAGREADEDFILE2 = 1;
}
else {
FLAGREADEDFILE2 = 0;
}
/*Reading file for bPtable */
snprintf(buffer_bloom_file,1024,"keyhunt_bsgs_2_%" PRIu64 ".tbl",bsgs_m3);
fd_aux3 = fopen(buffer_bloom_file,"rb");
if(fd_aux3 != NULL) {
printf("[+] Reading bP Table from file %s .",buffer_bloom_file);
fflush(stdout);
rsize = fread(bPtable,bytes,1,fd_aux3);
if(rsize != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
rsize = fread(checksum,32,1,fd_aux3);
if(FLAGSKIPCHECKSUM == 0) {
sha256((uint8_t*)bPtable,bytes,(uint8_t*)checksum_backup);
if(memcmp(checksum,checksum_backup,32) != 0) {
fprintf(stderr,"[E] Error checksum file mismatch! %s\n",buffer_bloom_file);
exit(0);
}
}
printf("... Done!\n");
fclose(fd_aux3);
FLAGREADEDFILE3 = 1;
}
else {
FLAGREADEDFILE3 = 0;
}
/*Reading file for 3rd bloom filter */
snprintf(buffer_bloom_file,1024,"keyhunt_bsgs_7_%" PRIu64 ".blm",bsgs_m3);
fd_aux2 = fopen(buffer_bloom_file,"rb");
if(fd_aux2 != NULL) {
printf("[+] Reading bloom filter from file %s ",buffer_bloom_file);
fflush(stdout);
for(i = 0; i < 256;i++) {
bf_ptr = (char*) bloom_bPx3rd[i].bf; /*We need to save the current bf pointer*/
readed = fread(&bloom_bPx3rd[i],sizeof(struct bloom),1,fd_aux2);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
bloom_bPx3rd[i].bf = (uint8_t*)bf_ptr; /* Restoring the bf pointer*/
readed = fread(bloom_bPx3rd[i].bf,bloom_bPx3rd[i].bytes,1,fd_aux2);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
readed = fread(&bloom_bPx3rd_checksums[i],sizeof(struct checksumsha256),1,fd_aux2);
if(readed != 1) {
fprintf(stderr,"[E] Error reading the file %s\n",buffer_bloom_file);
exit(0);
}
memset(rawvalue,0,32);
if(FLAGSKIPCHECKSUM == 0) {
sha256((uint8_t*)bloom_bPx3rd[i].bf,bloom_bPx3rd[i].bytes,(uint8_t*)rawvalue);
if(memcmp(bloom_bPx3rd_checksums[i].data,rawvalue,32) != 0 || memcmp(bloom_bPx3rd_checksums[i].backup,rawvalue,32) != 0 ) { /* Verification */
fprintf(stderr,"[E] Error checksum file mismatch! %s\n",buffer_bloom_file);
exit(0);
}
}
if(i % 64 == 0) {
printf(".");
fflush(stdout);
}
}
fclose(fd_aux2);
printf(" Done!\n");
FLAGREADEDFILE4 = 1;
}
else {
FLAGREADEDFILE4 = 0;
}
}
if(!FLAGREADEDFILE1 || !FLAGREADEDFILE2 || !FLAGREADEDFILE3 || !FLAGREADEDFILE4) {
if(FLAGREADEDFILE1 == 1) {
/*
We need just to make File 2 to File 4 this is
- Second bloom filter 5%
- third bloom fitler 0.25 %
- bp Table 0.25 %
*/
printf("[I] We need to recalculate some files, don't worry this is only 3%% of the previous work\n");
FINISHED_THREADS_COUNTER = 0;
FINISHED_THREADS_BP = 0;
FINISHED_ITEMS = 0;
salir = 0;
BASE = 0;
THREADCOUNTER = 0;
if(THREADBPWORKLOAD >= bsgs_m2) {
THREADBPWORKLOAD = bsgs_m2;
}
THREADCYCLES = bsgs_m2 / THREADBPWORKLOAD;
PERTHREAD_R = bsgs_m2 % THREADBPWORKLOAD;
if(PERTHREAD_R != 0) {
THREADCYCLES++;
}
printf("\r[+] processing %lu/%lu bP points : %i%%\r",FINISHED_ITEMS,bsgs_m,(int) (((double)FINISHED_ITEMS/(double)bsgs_m)*100));
fflush(stdout);
tid = (pthread_t *) calloc(NTHREADS,sizeof(pthread_t));
bPload_mutex = (pthread_mutex_t*) calloc(NTHREADS,sizeof(pthread_mutex_t));
checkpointer((void *)bPload_mutex,__FILE__,"calloc","bPload_mutex" ,__LINE__ -1 );
bPload_temp_ptr = (struct bPload*) calloc(NTHREADS,sizeof(struct bPload));
checkpointer((void *)bPload_temp_ptr,__FILE__,"calloc","bPload_temp_ptr" ,__LINE__ -1 );
bPload_threads_available = (char*) calloc(NTHREADS,sizeof(char));
checkpointer((void *)bPload_threads_available,__FILE__,"calloc","bPload_threads_available" ,__LINE__ -1 );
memset(bPload_threads_available,1,NTHREADS);
for(i = 0; i < NTHREADS; i++) {
pthread_mutex_init(&bPload_mutex[i],NULL);
}
do {
for(i = 0; i < NTHREADS && !salir; i++) {
if(bPload_threads_available[i] && !salir) {
bPload_threads_available[i] = 0;
bPload_temp_ptr[i].from = BASE;
bPload_temp_ptr[i].threadid = i;
bPload_temp_ptr[i].finished = 0;
if( THREADCOUNTER < THREADCYCLES-1) {
bPload_temp_ptr[i].to = BASE + THREADBPWORKLOAD;
bPload_temp_ptr[i].workload = THREADBPWORKLOAD;
}
else {
bPload_temp_ptr[i].to = BASE + THREADBPWORKLOAD + PERTHREAD_R;
bPload_temp_ptr[i].workload = THREADBPWORKLOAD + PERTHREAD_R;
salir = 1;
}
s = pthread_create(&tid[i],NULL,thread_bPload_2blooms,(void*) &bPload_temp_ptr[i]);
if(s != 0){
printf("Thread creation failed. Error code: %d\n", s);
exit(EXIT_FAILURE);
}
pthread_detach(tid[i]);
BASE+=THREADBPWORKLOAD;
THREADCOUNTER++;
}
}
if(OLDFINISHED_ITEMS != FINISHED_ITEMS) {
printf("\r[+] processing %lu/%lu bP points : %i%%\r",FINISHED_ITEMS,bsgs_m2,(int) (((double)FINISHED_ITEMS/(double)bsgs_m2)*100));
fflush(stdout);
OLDFINISHED_ITEMS = FINISHED_ITEMS;
}
for(i = 0 ; i < NTHREADS ; i++) {
pthread_mutex_lock(&bPload_mutex[i]);
finished = bPload_temp_ptr[i].finished;
pthread_mutex_unlock(&bPload_mutex[i]);
if(finished) {
bPload_temp_ptr[i].finished = 0;
bPload_threads_available[i] = 1;
FINISHED_ITEMS += bPload_temp_ptr[i].workload;