-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkey_search.cpp
More file actions
executable file
·859 lines (697 loc) · 43.7 KB
/
key_search.cpp
File metadata and controls
executable file
·859 lines (697 loc) · 43.7 KB
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
#include <iostream>
#include <fstream>
#include <vector>
#include <thread>
#include "secp256k1/SECP256k1.h"
#include "secp256k1/Int.h"
#include "secp256k1/IntGroup.h"
#include "bloom/filter.hpp"
#include "util/util.h"
using namespace std;
using filter = boost::bloom::filter<boost::uint64_t, 32>;
const int cpuCores = std::thread::hardware_concurrency(); // number of processing cores
const int POINTS_BATCH_SIZE = 1024; // Batch addition / Batch inversion (IntGroup.h class)
auto main() -> int {
Secp256K1* secp256k1 = new Secp256K1();
secp256k1->Init(); // initialize secp256k1 context
Int gm;
gm.SetBase16("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140");
Point Gm = secp256k1->ScalarMultiplication(&gm);
Int pk; pk.SetInt32(1); // generating power of two values (2^0..2^256) table
uint64_t mult = 2;
vector<Int> S_table;
for (int i = 0; i < 256; i++)
{
S_table.push_back(pk);
pk.Mult(mult);
}
print_time(); cout << "S_table generated" << endl;
uint64_t range_start, range_end, block_width; // block_width = number of elements in the bloomfilter and a stride size to walk the range
string temp, search_pub;
ifstream inFile("settings.txt"); // load settings from file
getline(inFile, temp); range_start = std::stoull(temp);
getline(inFile, temp); range_end = std::stoull(temp);
getline(inFile, temp); block_width = std::stoull(temp);
getline(inFile, temp); search_pub = trim(temp);
inFile.close();
print_time(); cout << "Range Start: " << range_start << " bits" << endl;
print_time(); cout << "Range End : " << range_end << " bits" << endl;
print_time(); cout << "Block Width: 2^" << block_width << endl;
print_time(); cout << "Search Pub : " << search_pub << endl;
Point TargetP = secp256k1->ParsePublicKeyHex(search_pub);
uint64_t stride_bits = pow(2, block_width);
string bloomfile = "bloom.bf";
filter bf;
print_time(); cout << "Loading Bloomfilter image" << endl;
std::ifstream in1(bloomfile, std::ios::binary);
std::size_t c1;
in1.read((char*) &c1, sizeof(c1));
bf.reset(c1); // restore capacity
boost::span<unsigned char> s1 = bf.array();
in1.read((char*) s1.data(), s1.size()); // load array
in1.close();
auto pow10_nums = break_down_into_pow10(stride_bits); // decomposing the 2^block_width to the power of ten values
size_t arr_size = pow10_nums.size(); // to get the offset from the target point based on the bloomfilter hits fast
Point pow10_points_Pos[arr_size];
Point pow10_points_Neg[arr_size];
Int pow_key;
Point Pm;
int arr_index = 0;
for (auto& n : pow10_nums) { // calculating points corresponding to the decomposition components
pow_key.SetInt64(n);
Pm = secp256k1->ScalarMultiplication(&pow_key);
pow10_points_Pos[arr_index] = Pm;
Pm.y.ModNeg();
pow10_points_Neg[arr_index] = Pm;
arr_index += 1;
}
auto chrono_start = std::chrono::high_resolution_clock::now();
auto key_search = [&]() {
string temp;
Int stride_sum;
ifstream inFile("stride_sum.txt");
getline(inFile, temp);
stride_sum.SetBase10(trim(temp).data());
inFile.close();
Int two, int_Cores, range_Start, range_End, partition_Size, center_Num, rem;
two.SetInt32(2);
int_Cores.SetInt32(cpuCores);
range_Start.Set(&S_table[range_start]);
range_End.Set(&S_table[range_end]);
partition_Size.Set(&S_table[range_start]);
partition_Size.Div(&int_Cores, &rem);
center_Num.Set(&partition_Size);
center_Num.Div(&two, &rem);
Int range_Nums[cpuCores + 1];
for (int i = 0; i < cpuCores + 1; i++) {
range_Nums[i] = range_Start;
range_Start.Add(&partition_Size);
}
Int center_Int;
Int center_Nums[cpuCores];
for (int i = 0; i < cpuCores; i++) {
center_Int.Add(&range_Nums[i], ¢er_Num);
center_Nums[i] = center_Int;
}
vector<Point> center_Points;
for (int i = 0; i < cpuCores; i++) {
center_Points.push_back(secp256k1->ScalarMultiplication(¢er_Nums[i]));
}
vector<Point> sideway_Points;
for (int i = 0; i < cpuCores + 1; i++) {
sideway_Points.push_back(secp256k1->ScalarMultiplication(&range_Nums[i]));
}
vector<Point> pos_Points;
vector<Point> neg_Points;
vector<Point> pos_PointsSw;
vector<Point> neg_PointsSw;
Point offset_PosPoint = secp256k1->ScalarMultiplication(&stride_sum);
Point offset_NegPoint = offset_PosPoint; offset_NegPoint.y.ModNeg();
for (int i = 0; i < cpuCores; i++) {
pos_Points.push_back(secp256k1->AddPoints2(center_Points[i], offset_PosPoint));
neg_Points.push_back(secp256k1->AddPoints2(center_Points[i], offset_NegPoint));
pos_PointsSw.push_back(secp256k1->AddPoints2(sideway_Points[i], offset_PosPoint));
neg_PointsSw.push_back(secp256k1->AddPoints2(sideway_Points[i + 1], offset_NegPoint));
}
Int stride(stride_bits);
Point stride_point = secp256k1->ScalarMultiplication(&stride);
Point addPointsPos[POINTS_BATCH_SIZE]; // array for batch addition points positive
Point addPointsNeg[POINTS_BATCH_SIZE]; // array for batch addition points negative
Point batch_Add = secp256k1->DoublePoint(stride_point);
addPointsPos[0] = stride_point;
addPointsNeg[0] = stride_point; addPointsNeg[0].y.ModNeg();
addPointsPos[1] = batch_Add;
addPointsNeg[1] = batch_Add; addPointsNeg[1].y.ModNeg();
for (int i = 2; i < POINTS_BATCH_SIZE; i++)
{
batch_Add = secp256k1->AddPoints(batch_Add, stride_point);
addPointsPos[i] = batch_Add;
addPointsNeg[i] = batch_Add;
addPointsNeg[i].y.ModNeg();
}
// center_key_search_save
auto center_key_search_save = [&](Point posStartP, Point negStartP, Int center_Num, Int stride_Sum) {
int save_counter = 0;
Int stride_sum; stride_sum.Set(&stride_Sum);
Int center_num; center_num.Set(¢er_Num);
Int Int_steps, Int_temp, privkey;
int index, count;
uint64_t steps;
vector<uint64_t> privkey_num;
IntGroup modGroupPos(POINTS_BATCH_SIZE); // group of deltaX (x1 - x2) set for batch inversion
IntGroup modGroupNeg(POINTS_BATCH_SIZE); // group of deltaX (x1 - x2) set for batch inversion
Int deltaXPos[POINTS_BATCH_SIZE]; // here we store (x1 - x2) batch that will be inverted for later multiplication
Int deltaXNeg[POINTS_BATCH_SIZE]; // here we store (x1 - x2) batch that will be inverted for later multiplication
modGroupPos.Set(deltaXPos); // assign array deltaX to modGroup for batch inversion (JLP way set it once)
modGroupNeg.Set(deltaXNeg); // assign array deltaX to modGroup for batch inversion (JLP way set it once)
Int pointBatchXPos[POINTS_BATCH_SIZE]; // X coordinates of the batch
Int pointBatchYPos[POINTS_BATCH_SIZE]; // Y coordinates of the batch
Int pointBatchXNeg[POINTS_BATCH_SIZE]; // X coordinates of the batch
Int pointBatchYNeg[POINTS_BATCH_SIZE]; // Y coordinates of the batch
Int deltaYPos, deltaYNeg; // values to store the results of points addition formula
Int slopePos[POINTS_BATCH_SIZE];
Int slopeNeg[POINTS_BATCH_SIZE];
Point startPointPos = posStartP; // start point positive
Point startPointNeg = negStartP; // start point negative
Point BloomP, CheckP, calc_point;
Int batch_stride, batch_index;
batch_stride.Mult(&stride, uint64_t(POINTS_BATCH_SIZE));
while (true) {
for (int i = 0; i < POINTS_BATCH_SIZE; i++) { // we compute (x1 - x2) for each entry of the entire batch
deltaXPos[i].ModSub(&startPointPos.x, &addPointsPos[i].x);
deltaXNeg[i].ModSub(&startPointNeg.x, &addPointsNeg[i].x);
}
modGroupPos.ModInv();
modGroupNeg.ModInv();
int i;
for (i = 0; i < POINTS_BATCH_SIZE - 1; i++) { // follow points addition formula logic
deltaYPos.ModSub(&startPointPos.y, &addPointsPos[i].y);
slopePos[i].ModMulK1(&deltaYPos, &deltaXPos[i]); // deltaX already inverted for each entry of the batch
deltaYNeg.ModSub(&startPointNeg.y, &addPointsNeg[i].y);
slopeNeg[i].ModMulK1(&deltaYNeg, &deltaXNeg[i]); // deltaX already inverted for each entry of the batch
pointBatchXPos[i].ModSquareK1(&slopePos[i]); // computing just x coordinate for the (batch_size - 1)
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &startPointPos.x);
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &addPointsPos[i].x);
pointBatchXNeg[i].ModSquareK1(&slopeNeg[i]); // computing just x coordinate for the (batch_size - 1)
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &startPointNeg.x);
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &addPointsNeg[i].x);
}
deltaYPos.ModSub(&startPointPos.y, &addPointsPos[i].y);
slopePos[i].ModMulK1(&deltaYPos, &deltaXPos[i]);
deltaYNeg.ModSub(&startPointNeg.y, &addPointsNeg[i].y);
slopeNeg[i].ModMulK1(&deltaYNeg, &deltaXNeg[i]);
pointBatchXPos[i].ModSquareK1(&slopePos[i]);
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &startPointPos.x);
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &addPointsPos[i].x);
pointBatchXNeg[i].ModSquareK1(&slopeNeg[i]);
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &startPointNeg.x);
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &addPointsNeg[i].x);
pointBatchYPos[i].ModSub(&startPointPos.x, &pointBatchXPos[i]);
pointBatchYPos[i].ModMulK1(&slopePos[i], &pointBatchYPos[i]);
pointBatchYPos[i].ModSub(&pointBatchYPos[i], &startPointPos.y);
pointBatchYNeg[i].ModSub(&startPointNeg.x, &pointBatchXNeg[i]);
pointBatchYNeg[i].ModMulK1(&slopeNeg[i], &pointBatchYNeg[i]);
pointBatchYNeg[i].ModSub(&pointBatchYNeg[i], &startPointNeg.y);
for (int i = 0; i < POINTS_BATCH_SIZE; i++) {
// check positive
if (bf.may_contain(pointBatchXPos[i].bits64[3])) {
BloomP.x.Set(&pointBatchXPos[i]);
BloomP.y.ModSub(&startPointPos.x, &pointBatchXPos[i]);
BloomP.y.ModMulK1(&slopePos[i], &BloomP.y);
BloomP.y.ModSub(&BloomP.y, &startPointPos.y);
if (BloomP.x_equals(TargetP)) {
Int_steps.SetInt64(0);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
center_num.Add(&Int_temp);
privkey.Sub(¢er_num, &Int_steps);
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
CheckP = secp256k1->AddPoints(BloomP, Gm);
if (bf.may_contain(CheckP.x.bits64[3])) {
privkey_num.clear();
index = 0;
for (size_t i = 0; i < arr_size; i++) {
count = 0;
do {
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Neg[i]);
count += 1;
} while (bf.may_contain(BloomP.x.bits64[3]));
privkey_num.push_back(pow10_nums[index] * (count - 1));
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Pos[i]);
index += 1;
}
steps = 0;
for (auto& n : privkey_num) { steps += n; }
Int_steps.SetInt64(steps);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
center_num.Add(&Int_temp);
privkey.Sub(¢er_num, &Int_steps);
calc_point = secp256k1->ScalarMultiplication(&privkey);
if (calc_point.x_equals(TargetP)) {
//print_time(); cout << "Center_num save Pos" << endl;
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
}
}
// check negative
if (bf.may_contain(pointBatchXNeg[i].bits64[3])) {
BloomP.x.Set(&pointBatchXNeg[i]);
BloomP.y.ModSub(&startPointNeg.x, &pointBatchXNeg[i]);
BloomP.y.ModMulK1(&slopeNeg[i], &BloomP.y);
BloomP.y.ModSub(&BloomP.y, &startPointNeg.y);
if (BloomP.x_equals(TargetP)) {
Int_steps.SetInt64(0);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
center_num.Sub(&Int_temp);
privkey.Sub(¢er_num, &Int_steps);
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
CheckP = secp256k1->AddPoints(BloomP, Gm);
if (bf.may_contain(CheckP.x.bits64[3])) {
privkey_num.clear();
index = 0;
for (size_t i = 0; i < arr_size; i++) {
count = 0;
do {
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Neg[i]);
count += 1;
} while (bf.may_contain(BloomP.x.bits64[3]));
privkey_num.push_back(pow10_nums[index] * (count - 1));
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Pos[i]);
index += 1;
}
steps = 0;
for (auto& n : privkey_num) { steps += n; }
Int_steps.SetInt64(steps);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
center_num.Sub(&Int_temp);
privkey.Sub(¢er_num, &Int_steps);
calc_point = secp256k1->ScalarMultiplication(&privkey);
if (calc_point.x_equals(TargetP)) {
//print_time(); cout << "Center_num save Neg" << endl;
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
}
}
}
startPointPos.x.Set(&pointBatchXPos[POINTS_BATCH_SIZE - 1]); // last batch entry as the new startPoint for the next batch iteration
startPointPos.y.Set(&pointBatchYPos[POINTS_BATCH_SIZE - 1]);
startPointNeg.x.Set(&pointBatchXNeg[POINTS_BATCH_SIZE - 1]); // last batch entry as the new startPoint for the next batch iteration
startPointNeg.y.Set(&pointBatchYNeg[POINTS_BATCH_SIZE - 1]);
stride_sum.Add(&batch_stride);
save_counter += 1;
if (save_counter % 100000 == 0) {
ofstream outFile;
outFile.open("stride_sum.txt");
outFile << stride_sum.GetBase10() << '\n';
outFile.close();
save_counter = 0;
print_time(); cout << "Progress written to stride_sum.txt" << endl;
}
} // while (true) loop end curly brace
}; // center_key_search_save
// center_key_search
auto center_key_search = [&](Point posStartP, Point negStartP, Int center_Num, Int stride_Sum) {
Int stride_sum; stride_sum.Set(&stride_Sum);
Int center_num; center_num.Set(¢er_Num);
Int Int_steps, Int_temp, privkey;
int index, count;
uint64_t steps;
vector<uint64_t> privkey_num;
IntGroup modGroupPos(POINTS_BATCH_SIZE); // group of deltaX (x1 - x2) set for batch inversion
IntGroup modGroupNeg(POINTS_BATCH_SIZE); // group of deltaX (x1 - x2) set for batch inversion
Int deltaXPos[POINTS_BATCH_SIZE]; // here we store (x1 - x2) batch that will be inverted for later multiplication
Int deltaXNeg[POINTS_BATCH_SIZE]; // here we store (x1 - x2) batch that will be inverted for later multiplication
modGroupPos.Set(deltaXPos); // assign array deltaX to modGroup for batch inversion (JLP way set it once)
modGroupNeg.Set(deltaXNeg); // assign array deltaX to modGroup for batch inversion (JLP way set it once)
Int pointBatchXPos[POINTS_BATCH_SIZE]; // X coordinates of the batch
Int pointBatchYPos[POINTS_BATCH_SIZE]; // Y coordinates of the batch
Int pointBatchXNeg[POINTS_BATCH_SIZE]; // X coordinates of the batch
Int pointBatchYNeg[POINTS_BATCH_SIZE]; // Y coordinates of the batch
Int deltaYPos, deltaYNeg; // values to store the results of points addition formula
Int slopePos[POINTS_BATCH_SIZE];
Int slopeNeg[POINTS_BATCH_SIZE];
Point startPointPos = posStartP; // start point positive
Point startPointNeg = negStartP; // start point negative
Point BloomP, CheckP, calc_point;
Int batch_stride, batch_index;
batch_stride.Mult(&stride, uint64_t(POINTS_BATCH_SIZE));
while (true) {
for (int i = 0; i < POINTS_BATCH_SIZE; i++) { // we compute (x1 - x2) for each entry of the entire batch
deltaXPos[i].ModSub(&startPointPos.x, &addPointsPos[i].x);
deltaXNeg[i].ModSub(&startPointNeg.x, &addPointsNeg[i].x);
}
modGroupPos.ModInv();
modGroupNeg.ModInv();
int i;
for (i = 0; i < POINTS_BATCH_SIZE - 1; i++) { // follow points addition formula logic
deltaYPos.ModSub(&startPointPos.y, &addPointsPos[i].y);
slopePos[i].ModMulK1(&deltaYPos, &deltaXPos[i]); // deltaX already inverted for each entry of the batch
deltaYNeg.ModSub(&startPointNeg.y, &addPointsNeg[i].y);
slopeNeg[i].ModMulK1(&deltaYNeg, &deltaXNeg[i]); // deltaX already inverted for each entry of the batch
pointBatchXPos[i].ModSquareK1(&slopePos[i]); // computing just x coordinate for the (batch_size - 1)
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &startPointPos.x);
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &addPointsPos[i].x);
pointBatchXNeg[i].ModSquareK1(&slopeNeg[i]); // computing just x coordinate for the (batch_size - 1)
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &startPointNeg.x);
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &addPointsNeg[i].x);
}
deltaYPos.ModSub(&startPointPos.y, &addPointsPos[i].y);
slopePos[i].ModMulK1(&deltaYPos, &deltaXPos[i]);
deltaYNeg.ModSub(&startPointNeg.y, &addPointsNeg[i].y);
slopeNeg[i].ModMulK1(&deltaYNeg, &deltaXNeg[i]);
pointBatchXPos[i].ModSquareK1(&slopePos[i]);
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &startPointPos.x);
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &addPointsPos[i].x);
pointBatchXNeg[i].ModSquareK1(&slopeNeg[i]);
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &startPointNeg.x);
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &addPointsNeg[i].x);
pointBatchYPos[i].ModSub(&startPointPos.x, &pointBatchXPos[i]);
pointBatchYPos[i].ModMulK1(&slopePos[i], &pointBatchYPos[i]);
pointBatchYPos[i].ModSub(&pointBatchYPos[i], &startPointPos.y);
pointBatchYNeg[i].ModSub(&startPointNeg.x, &pointBatchXNeg[i]);
pointBatchYNeg[i].ModMulK1(&slopeNeg[i], &pointBatchYNeg[i]);
pointBatchYNeg[i].ModSub(&pointBatchYNeg[i], &startPointNeg.y);
for (int i = 0; i < POINTS_BATCH_SIZE; i++) {
// check positive
if (bf.may_contain(pointBatchXPos[i].bits64[3])) {
BloomP.x.Set(&pointBatchXPos[i]);
BloomP.y.ModSub(&startPointPos.x, &pointBatchXPos[i]);
BloomP.y.ModMulK1(&slopePos[i], &BloomP.y);
BloomP.y.ModSub(&BloomP.y, &startPointPos.y);
if (BloomP.x_equals(TargetP)) {
Int_steps.SetInt64(0);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
center_num.Add(&Int_temp);
privkey.Sub(¢er_num, &Int_steps);
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
CheckP = secp256k1->AddPoints(BloomP, Gm);
if (bf.may_contain(CheckP.x.bits64[3])) {
privkey_num.clear();
index = 0;
for (size_t i = 0; i < arr_size; i++) {
count = 0;
do {
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Neg[i]);
count += 1;
} while (bf.may_contain(BloomP.x.bits64[3]));
privkey_num.push_back(pow10_nums[index] * (count - 1));
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Pos[i]);
index += 1;
}
steps = 0;
for (auto& n : privkey_num) { steps += n; }
Int_steps.SetInt64(steps);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
center_num.Add(&Int_temp);
privkey.Sub(¢er_num, &Int_steps);
calc_point = secp256k1->ScalarMultiplication(&privkey);
if (calc_point.x_equals(TargetP)) {
//print_time(); cout << "Center_num Pos" << endl;
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
}
}
// check negative
if (bf.may_contain(pointBatchXNeg[i].bits64[3])) {
BloomP.x.Set(&pointBatchXNeg[i]);
BloomP.y.ModSub(&startPointNeg.x, &pointBatchXNeg[i]);
BloomP.y.ModMulK1(&slopeNeg[i], &BloomP.y);
BloomP.y.ModSub(&BloomP.y, &startPointNeg.y);
if (BloomP.x_equals(TargetP)) {
Int_steps.SetInt64(steps);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
center_num.Sub(&Int_temp);
privkey.Sub(¢er_num, &Int_steps);
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
CheckP = secp256k1->AddPoints(BloomP, Gm);
if (bf.may_contain(CheckP.x.bits64[3])) {
privkey_num.clear();
index = 0;
for (size_t i = 0; i < arr_size; i++) {
count = 0;
do {
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Neg[i]);
count += 1;
} while (bf.may_contain(BloomP.x.bits64[3]));
privkey_num.push_back(pow10_nums[index] * (count - 1));
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Pos[i]);
index += 1;
}
steps = 0;
for (auto& n : privkey_num) { steps += n; }
Int_steps.SetInt64(steps);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
center_num.Sub(&Int_temp);
privkey.Sub(¢er_num, &Int_steps);
calc_point = secp256k1->ScalarMultiplication(&privkey);
if (calc_point.x_equals(TargetP)) {
//print_time(); cout << "Center_num Neg" << endl;
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
}
}
}
startPointPos.x.Set(&pointBatchXPos[POINTS_BATCH_SIZE - 1]); // last batch entry as the new startPoint for the next batch iteration
startPointPos.y.Set(&pointBatchYPos[POINTS_BATCH_SIZE - 1]);
startPointNeg.x.Set(&pointBatchXNeg[POINTS_BATCH_SIZE - 1]); // last batch entry as the new startPoint for the next batch iteration
startPointNeg.y.Set(&pointBatchYNeg[POINTS_BATCH_SIZE - 1]);
stride_sum.Add(&batch_stride);
} // while (true) loop end curly brace
}; // center_key_search
// sideway_key_search
auto sideway_key_search = [&](Point posStartP, Point negStartP, Int range_NumPos, Int range_NumNeg, Int stride_Sum) {
Int stride_sum; stride_sum.Set(&stride_Sum);
Int range_num_pos; range_num_pos.Set(&range_NumPos);
Int range_num_neg; range_num_neg.Set(&range_NumNeg);
Int Int_steps, Int_temp, privkey;
int index, count;
uint64_t steps;
vector<uint64_t> privkey_num;
IntGroup modGroupPos(POINTS_BATCH_SIZE); // group of deltaX (x1 - x2) set for batch inversion
IntGroup modGroupNeg(POINTS_BATCH_SIZE); // group of deltaX (x1 - x2) set for batch inversion
Int deltaXPos[POINTS_BATCH_SIZE]; // here we store (x1 - x2) batch that will be inverted for later multiplication
Int deltaXNeg[POINTS_BATCH_SIZE]; // here we store (x1 - x2) batch that will be inverted for later multiplication
modGroupPos.Set(deltaXPos); // assign array deltaX to modGroup for batch inversion (JLP way set it once)
modGroupNeg.Set(deltaXNeg); // assign array deltaX to modGroup for batch inversion (JLP way set it once)
Int pointBatchXPos[POINTS_BATCH_SIZE]; // X coordinates of the batch
Int pointBatchYPos[POINTS_BATCH_SIZE]; // Y coordinates of the batch
Int pointBatchXNeg[POINTS_BATCH_SIZE]; // X coordinates of the batch
Int pointBatchYNeg[POINTS_BATCH_SIZE]; // Y coordinates of the batch
Int deltaYPos, deltaYNeg; // values to store the results of points addition formula
Int slopePos[POINTS_BATCH_SIZE];
Int slopeNeg[POINTS_BATCH_SIZE];
Point startPointPos = posStartP; // start point positive
Point startPointNeg = negStartP; // start point negative
Point BloomP, CheckP, calc_point;
Int batch_stride, batch_index;
batch_stride.Mult(&stride, uint64_t(POINTS_BATCH_SIZE));
while (true) {
for (int i = 0; i < POINTS_BATCH_SIZE; i++) { // we compute (x1 - x2) for each entry of the entire batch
deltaXPos[i].ModSub(&startPointPos.x, &addPointsPos[i].x);
deltaXNeg[i].ModSub(&startPointNeg.x, &addPointsNeg[i].x);
}
modGroupPos.ModInv();
modGroupNeg.ModInv();
int i;
for (i = 0; i < POINTS_BATCH_SIZE - 1; i++) { // follow points addition formula logic
deltaYPos.ModSub(&startPointPos.y, &addPointsPos[i].y);
slopePos[i].ModMulK1(&deltaYPos, &deltaXPos[i]); // deltaX already inverted for each entry of the batch
deltaYNeg.ModSub(&startPointNeg.y, &addPointsNeg[i].y);
slopeNeg[i].ModMulK1(&deltaYNeg, &deltaXNeg[i]); // deltaX already inverted for each entry of the batch
pointBatchXPos[i].ModSquareK1(&slopePos[i]); // computing just x coordinate for the (batch_size - 1)
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &startPointPos.x);
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &addPointsPos[i].x);
pointBatchXNeg[i].ModSquareK1(&slopeNeg[i]); // computing just x coordinate for the (batch_size - 1)
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &startPointNeg.x);
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &addPointsNeg[i].x);
}
deltaYPos.ModSub(&startPointPos.y, &addPointsPos[i].y);
slopePos[i].ModMulK1(&deltaYPos, &deltaXPos[i]);
deltaYNeg.ModSub(&startPointNeg.y, &addPointsNeg[i].y);
slopeNeg[i].ModMulK1(&deltaYNeg, &deltaXNeg[i]);
pointBatchXPos[i].ModSquareK1(&slopePos[i]);
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &startPointPos.x);
pointBatchXPos[i].ModSub(&pointBatchXPos[i], &addPointsPos[i].x);
pointBatchXNeg[i].ModSquareK1(&slopeNeg[i]);
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &startPointNeg.x);
pointBatchXNeg[i].ModSub(&pointBatchXNeg[i], &addPointsNeg[i].x);
pointBatchYPos[i].ModSub(&startPointPos.x, &pointBatchXPos[i]);
pointBatchYPos[i].ModMulK1(&slopePos[i], &pointBatchYPos[i]);
pointBatchYPos[i].ModSub(&pointBatchYPos[i], &startPointPos.y);
pointBatchYNeg[i].ModSub(&startPointNeg.x, &pointBatchXNeg[i]);
pointBatchYNeg[i].ModMulK1(&slopeNeg[i], &pointBatchYNeg[i]);
pointBatchYNeg[i].ModSub(&pointBatchYNeg[i], &startPointNeg.y);
for (int i = 0; i < POINTS_BATCH_SIZE; i++) {
// check positive
if (bf.may_contain(pointBatchXPos[i].bits64[3])) {
BloomP.x.Set(&pointBatchXPos[i]);
BloomP.y.ModSub(&startPointPos.x, &pointBatchXPos[i]);
BloomP.y.ModMulK1(&slopePos[i], &BloomP.y);
BloomP.y.ModSub(&BloomP.y, &startPointPos.y);
if (BloomP.x_equals(TargetP)) {
Int_steps.SetInt64(0);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
range_num_pos.Add(&Int_temp);
privkey.Sub(&range_num_pos, &Int_steps);
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
CheckP = secp256k1->AddPoints(BloomP, Gm);
if (bf.may_contain(CheckP.x.bits64[3])) {
privkey_num.clear();
index = 0;
for (size_t i = 0; i < arr_size; i++) {
count = 0;
do {
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Neg[i]);
count += 1;
} while (bf.may_contain(BloomP.x.bits64[3]));
privkey_num.push_back(pow10_nums[index] * (count - 1));
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Pos[i]);
index += 1;
}
steps = 0;
for (auto& n : privkey_num) { steps += n; }
Int_steps.SetInt64(steps);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
range_num_pos.Add(&Int_temp);
privkey.Sub(&range_num_pos, &Int_steps);
calc_point = secp256k1->ScalarMultiplication(&privkey);
if (calc_point.x_equals(TargetP)) {
//print_time(); cout << "Sideway_Num Pos" << endl;
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
}
}
// check negative
if (bf.may_contain(pointBatchXNeg[i].bits64[3])) {
BloomP.x.Set(&pointBatchXNeg[i]);
BloomP.y.ModSub(&startPointNeg.x, &pointBatchXNeg[i]);
BloomP.y.ModMulK1(&slopeNeg[i], &BloomP.y);
BloomP.y.ModSub(&BloomP.y, &startPointNeg.y);
if (BloomP.x_equals(TargetP)) {
Int_steps.SetInt64(0);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
range_num_neg.Sub(&Int_temp);
privkey.Sub(&range_num_neg, &Int_steps);
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
CheckP = secp256k1->AddPoints(BloomP, Gm);
if (bf.may_contain(CheckP.x.bits64[3])) {
privkey_num.clear();
index = 0;
for (size_t i = 0; i < arr_size; i++) {
count = 0;
do {
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Neg[i]);
count += 1;
} while (bf.may_contain(BloomP.x.bits64[3]));
privkey_num.push_back(pow10_nums[index] * (count - 1));
BloomP = secp256k1->AddPoints(BloomP, pow10_points_Pos[i]);
index += 1;
}
steps = 0;
for (auto& n : privkey_num) { steps += n; }
Int_steps.SetInt64(steps);
batch_index.Mult(&stride, uint64_t(i + 1));
Int_temp.Add(&stride_sum, &batch_index);
range_num_neg.Sub(&Int_temp);
privkey.Sub(&range_num_neg, &Int_steps);
calc_point = secp256k1->ScalarMultiplication(&privkey);
if (calc_point.x_equals(TargetP)) {
//print_time(); cout << "Sideway_Num Neg" << endl;
print_time(); cout << "Private key: " << privkey.GetBase10() << endl;
ofstream outFile;
outFile.open("found.txt", ios::app);
outFile << privkey.GetBase10() << '\n';
outFile.close();
print_elapsed_time(chrono_start);
exit(0);
}
}
}
}
startPointPos.x.Set(&pointBatchXPos[POINTS_BATCH_SIZE - 1]); // last batch entry as the new startPoint for the next batch iteration
startPointPos.y.Set(&pointBatchYPos[POINTS_BATCH_SIZE - 1]);
startPointNeg.x.Set(&pointBatchXNeg[POINTS_BATCH_SIZE - 1]); // last batch entry as the new startPoint for the next batch iteration
startPointNeg.y.Set(&pointBatchYNeg[POINTS_BATCH_SIZE - 1]);
stride_sum.Add(&batch_stride);
} // while (true) loop end curly brace
}; // sideway_key_search
std::thread keySearch_CThreads[cpuCores];
std::thread keySearch_SThreads[cpuCores];
keySearch_CThreads[0] = std::thread(center_key_search_save, pos_Points[0], neg_Points[0], center_Nums[0], stride_sum);
for (int i = 1; i < cpuCores; i++) {
keySearch_CThreads[i] = std::thread(center_key_search, pos_Points[i], neg_Points[i], center_Nums[i], stride_sum);
}
for (int i = 0; i < cpuCores; i++) {
keySearch_SThreads[i] = std::thread(sideway_key_search, pos_PointsSw[i], neg_PointsSw[i], range_Nums[i], range_Nums[i + 1], stride_sum);
}
for (int i = 0; i < cpuCores; i++) {
keySearch_CThreads[i].join();
keySearch_SThreads[i].join();
}
};
print_time(); cout << "Key Search in progress..." << endl;
std::thread thread(key_search);
thread.join();
}