Repository navigation
Expand file tree
/
Copy pathmutagen.cpp
More file actions
961 lines (821 loc) · 32.7 KB
/
Copy pathmutagen.cpp
File metadata and controls
961 lines (821 loc) · 32.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
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
#include <iostream>
#include <array>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <thread>
#include <atomic>
#include <chrono>
#include <queue>
#include <mutex>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <immintrin.h>
#include <omp.h>
#include <csignal>
#include <random>
#include <algorithm>
#include <getopt.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include "sha256_avx2.h"
#include "ripemd160_avx2.h"
#include "SECP256K1.h"
#include "Point.h"
#include "Int.h"
#include "IntGroup.h"
using namespace std;
void initConsole()
{
#ifdef _WIN32
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(hConsole, &mode);
SetConsoleMode(hConsole, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#endif
}
void clearTerminal()
{
#ifdef _WIN32
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
SetConsoleCursorPosition(hStdOut, coord);
#else
std::cout << "\033[2J\033[H";
#endif
std::cout.flush();
}
void moveCursorTo(int x, int y)
{
#ifdef _WIN32
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {(SHORT)x, (SHORT)y};
SetConsoleCursorPosition(hStdOut, coord);
#else
std::cout << "\033[" << y << ";" << x << "H";
#endif
std::cout.flush();
}
int EXCLUDE_BITS = 0;
int PUZZLE_NUM = 20;
int WORKERS = omp_get_num_procs();
int FLIP_COUNT = -1;
static constexpr int POINTS_BATCH_SIZE = 256;
static constexpr int HASH_BATCH_SIZE = 8;
const unordered_map<int, tuple<int, string, string>> PUZZLE_DATA = {
{20, {8, "b907c3a2a3b27789dfb509b730dd47703c272868", "357535"}},
{21, {9, "29a78213caa9eea824acf08022ab9dfc83414f56", "863317"}},
{22, {11, "7ff45303774ef7a52fffd8011981034b258cb86b", "1811764"}},
{23, {12, "d0a79df189fe1ad5c306cc70497b358415da579e", "3007503"}},
{24, {9, "0959e80121f36aea13b3bad361c15dac26189e2f", "5598802"}},
{25, {12, "2f396b29b27324300d0c59b17c3abc1835bd3dbb", "14428676"}},
{26, {14, "bfebb73562d4541b32a02ba664d140b5a574792f", "33185509"}},
{27, {13, "0c7aaf6caa7e5424b63d317f0f8f1f9fa40d5560", "54538862"}},
{28, {16, "1306b9e4ff56513a476841bac7ba48d69516b1da", "111949941"}},
{29, {18, "5a416cc9148f4a377b672c8ae5d3287adaafadec", "227634408"}},
{30, {16, "d39c4704664e1deb76c9331e637564c257d68a08", "400708894"}},
{31, {13, "d805f6f251f7479ebd853b3d0f4b9b2656d92f1d", "1033162084"}},
{32, {14, "9e42601eeaedc244e15f17375adb0e2cd08efdc9", "2102388551"}},
{33, {15, "4e15e5189752d1eaf444dfd6bff399feb0443977", "3093472814"}},
{34, {16, "f6d67d7983bf70450f295c9cb828daab265f1bfa", "7137437912"}},
{35, {19, "f6d8ce225ffbdecec170f8298c3fc28ae686df25", "14133072157"}},
{36, {14, "74b1e012be1521e5d8d75e745a26ced845ea3d37", "20112871792"}},
{37, {23, "28c30fb11ed1da72e7c4f89c0164756e8a021d", "42387769980"}},
{38, {21, "b190e2d40cfdeee2cee072954a2be89e7ba39364", "100251560595"}},
{39, {23, "0b304f2a79a027270276533fe1ed4eff30910876", "146971536592"}},
{40, {20, "95a156cd21b4a69de969eb6716864f4c8b82a82a", "323724968937"}},
{41, {25, "d1562eb37357f9e6fc41cb2359f4d3eda4032329", "1003651412950"}},
{42, {24, "8efb85f9c5b5db2d55973a04128dc7510075ae23", "1458252205147"}},
{43, {19, "f92044c7924e5525c61207972c253c9fc9f086f7", "2895374552463"}},
{44, {24, "80df54e1f612f2fc5bdc05c9d21a83aa8d20791e", "7409811047825"}},
{45, {21, "f0225bfc68a6e17e87cd8b5e60ae3be18f120753", "15404761757071"}},
{46, {24, "9a012260d01c5113df66c8a8438c9f7a1e3d5dac", "19996463086597"}},
{47, {27, "f828005d41b0f4fed4c8dca3b06011072cfb07d4", "51408670348612"}},
{48, {21, "8661cb56d9df0a61f01328b55af7e56a3fe7a2b2", "119666659114170"}},
{49, {30, "0d2f533966c6578e1111978ca698f8add7fffdf3", "191206974700443"}},
{50, {29, "de081b76f840e462fa2cdf360173dfaf4a976a47", "409118905032525"}},
{51, {25, "ef6419cffd7fad7027994354eb8efae223c2dbe7", "611140496167764"}},
{52, {27, "36af659edbe94453f6344e920d143f1778653ae7", "2058769515153876"}},
{53, {26, "2f4870ef54fa4b048c1365d42594cc7d3d269551", "4216495639600700"}},
{54, {30, "cb66763cf7fde659869ae7f06884d9a0f879a092", "6763683971478124"}},
{55, {31, "db53d9bbd1f3a83b094eeca7dd970bd85b492fa2", "9974455244496707"}},
{56, {31, "48214c5969ae9f43f75070cea1e2cb41d5bdcccd", "30045390491869460"}},
{57, {33, "328660ef43f66abe2653fa178452a5dfc594c2a1", "44218742292676575"}},
{58, {28, "8c2a6071f89c90c4dab5ab295d7729d1b54ea60f", "138245758910846492"}},
{59, {30, "b14ed3146f5b2c9bde1703deae9ef33af8110210", "199976667976342049"}},
{60, {31, "cdf8e5c7503a9d22642e3ecfc87817672787b9c5", "525070384258266191"}},
{61, {25, "68133e19b2dfb9034edf9830a200cfdf38c90cbd", "1135041350219496382"}},
{62, {35, "e26646db84b0602f32b34b5a62ca3cae1f91b779", "1425787542618654982"}},
{63, {34, "ef58afb697b094423ce90721fbb19a359ef7c50e", "3908372542507822062"}},
{64, {34, "3ee4133d991f52fdf6a25c9834e0745ac74248a4", "8993229949524469768"}},
{65, {37, "52e763a7ddc1aa4fa811578c491c1bc7fd570137", "17799667357578236628"}},
{66, {35, "20d45a6a762535700ce9e0b216e31994335db8a5", "30568377312064202855"}},
{67, {31, "739437bb3dd6d1983e66629c5f08c70e52769371", "46346217550346335726"}},
{68, {34, "e0b8a2baee1b77fc703455f39d51477451fc8cfc", "132656943602386256302"}},
{69, {35, "61eb8a50c86b0584bb727dd65bed8d2400d6d5aa", "554726235030114422279"}},
{71, {15, "f6f5431d25bbf7b12e8add9af5e3475c44a0a5b8", "970436974005023690481"}},
{76, {42, "86f9fea5cdecf033161dd2f8f8560768ae0a6d14", "22538323240989823823367"}},
{81, {46, "351e605fac813965951ba433b7c2956bf8ad95ce", "1105520030589234487939456"}},
{86, {24, "c60111ed3d63b49665747b0e31eb382da5193535", "21090315766411506144426920"}},
{91, {38, "9978f61b92d16c5f1a463a0995df70da1f7a7d2a", "868012190417726402719548863"}},
{96, {48, "2da63cbd251d23c7b633cb287c09e6cf888b3fe4", "25525831956644113617013748212"}},
{101, {26, "7c1a77205c03b9909663b2034faa0b544e6bc96b", "868221233689326498340379183142"}}};
vector<unsigned char> TARGET_HASH160_RAW(20);
string TARGET_HASH160;
Int BASE_KEY;
atomic<bool> stop_event(false);
mutex result_mutex;
queue<tuple<string, __uint128_t, int>> results;
union AVXCounter
{
__m256i vec;
uint64_t u64[4];
__uint128_t u128[2];
AVXCounter() : vec(_mm256_setzero_si256()) {}
AVXCounter(__uint128_t value)
{
store(value);
}
void increment()
{
__m256i one = _mm256_set_epi64x(0, 0, 0, 1);
vec = _mm256_add_epi64(vec, one);
if (u64[0] == 0)
{
__m256i carry = _mm256_set_epi64x(0, 0, 1, 0);
vec = _mm256_add_epi64(vec, carry);
}
}
void add(__uint128_t value)
{
__m256i add_val = _mm256_set_epi64x(0, 0, value >> 64, value);
vec = _mm256_add_epi64(vec, add_val);
if (u64[0] < (value & 0xFFFFFFFFFFFFFFFFULL))
{
__m256i carry = _mm256_set_epi64x(0, 0, 1, 0);
vec = _mm256_add_epi64(vec, carry);
}
}
__uint128_t load() const
{
return (static_cast<__uint128_t>(u64[1]) << 64) | u64[0];
}
void store(__uint128_t value)
{
u64[0] = static_cast<uint64_t>(value);
u64[1] = static_cast<uint64_t>(value >> 64);
u64[2] = 0;
u64[3] = 0;
}
bool operator<(const AVXCounter &other) const
{
if (u64[1] != other.u64[1])
return u64[1] < other.u64[1];
return u64[0] < other.u64[0];
}
bool operator>=(const AVXCounter &other) const
{
if (u64[1] != other.u64[1])
return u64[1] > other.u64[1];
return u64[0] >= other.u64[0];
}
static AVXCounter div(const AVXCounter &num, uint64_t denom)
{
__uint128_t n = num.load();
__uint128_t q = n / denom;
return AVXCounter(q);
}
static uint64_t mod(const AVXCounter &num, uint64_t denom)
{
__uint128_t n = num.load();
return n % denom;
}
static AVXCounter mul(uint64_t a, uint64_t b)
{
__uint128_t result = static_cast<__uint128_t>(a) * b;
return AVXCounter(result);
}
};
static AVXCounter total_checked_avx;
__uint128_t total_combinations = 0;
vector<string> g_threadPrivateKeys;
mutex progress_mutex;
atomic<uint64_t> globalComparedCount(0);
atomic<uint64_t> localComparedCount(0);
double globalElapsedTime = 0.0;
double mkeysPerSec = 0.0;
chrono::time_point<chrono::high_resolution_clock> tStart;
static std::string formatElapsedTime(double seconds)
{
int hrs = static_cast<int>(seconds) / 3600;
int mins = (static_cast<int>(seconds) % 3600) / 60;
int secs = static_cast<int>(seconds) % 60;
std::ostringstream oss;
oss << std::setw(2) << std::setfill('0') << hrs << ":"
<< std::setw(2) << std::setfill('0') << mins << ":"
<< std::setw(2) << std::setfill('0') << secs;
return oss.str();
}
static std::string to_string_128(__uint128_t value)
{
if (value == 0)
return "0";
char buffer[50];
char *p = buffer + sizeof(buffer);
*--p = '\0';
while (value != 0)
{
*--p = "0123456789"[value % 10];
value /= 10;
}
return std::string(p);
}
void signalHandler(int signum)
{
stop_event.store(true);
cout << "\nInterrupt received, shutting down...\n";
}
class CombinationGenerator
{
int n, k;
std::vector<int> current;
public:
CombinationGenerator(int n, int k) : n(n), k(k), current(k)
{
if (k > n)
k = n;
for (int i = 0; i < k; ++i)
current[i] = i;
}
static __uint128_t combinations_count(int n, int k)
{
if (k > n)
return 0;
if (k * 2 > n)
k = n - k;
if (k == 0)
return 1;
__uint128_t result = n;
for (int i = 2; i <= k; ++i)
{
result *= (n - i + 1);
result /= i;
}
return result;
}
static __m256i combinations_count_avx(int n, int k)
{
alignas(32) uint64_t counts[4];
for (int i = 0; i < 4; i++)
{
counts[i] = combinations_count(n + i, k);
}
return _mm256_load_si256((__m256i *)counts);
}
const std::vector<int> &get() const { return current; }
bool next()
{
int i = k - 1;
while (i >= 0 && current[i] == n - k + i)
--i;
if (i < 0)
return false;
++current[i];
for (int j = i + 1; j < k; ++j)
current[j] = current[j - 1] + 1;
return true;
}
void unrank(__uint128_t rank)
{
__uint128_t total = combinations_count(n, k);
if (rank >= total)
{
current.clear();
return;
}
current.resize(k);
__uint128_t remaining_rank = rank;
int a = n;
int b = k;
__uint128_t x = (total - 1) - rank;
for (int i = 0; i < k; i++)
{
a = largest_a_where_comb_a_b_le_x(a, b, x);
current[i] = (n - 1) - a;
x -= combinations_count(a, b);
b--;
}
}
private:
int largest_a_where_comb_a_b_le_x(int a, int b, __uint128_t x) const
{
while (a >= b && combinations_count(a, b) > x)
{
a--;
}
return a;
}
};
inline void prepareShaBlock(const uint8_t *dataSrc, __uint128_t dataLen, uint8_t *outBlock)
{
std::fill_n(outBlock, 64, 0);
std::memcpy(outBlock, dataSrc, dataLen);
outBlock[dataLen] = 0x80;
const uint32_t bitLen = (uint32_t)(dataLen * 8);
outBlock[60] = (uint8_t)((bitLen >> 24) & 0xFF);
outBlock[61] = (uint8_t)((bitLen >> 16) & 0xFF);
outBlock[62] = (uint8_t)((bitLen >> 8) & 0xFF);
outBlock[63] = (uint8_t)(bitLen & 0xFF);
}
inline void prepareRipemdBlock(const uint8_t *dataSrc, uint8_t *outBlock)
{
std::fill_n(outBlock, 64, 0);
std::memcpy(outBlock, dataSrc, 32);
outBlock[32] = 0x80;
const uint32_t bitLen = 256;
outBlock[60] = (uint8_t)((bitLen >> 24) & 0xFF);
outBlock[61] = (uint8_t)((bitLen >> 16) & 0xFF);
outBlock[62] = (uint8_t)((bitLen >> 8) & 0xFF);
outBlock[63] = (uint8_t)(bitLen & 0xFF);
}
static void computeHash160BatchBinSingle(int numKeys,
uint8_t pubKeys[][33],
uint8_t hashResults[][20])
{
alignas(32) std::array<std::array<uint8_t, 64>, HASH_BATCH_SIZE> shaInputs;
alignas(32) std::array<std::array<uint8_t, 32>, HASH_BATCH_SIZE> shaOutputs;
alignas(32) std::array<std::array<uint8_t, 64>, HASH_BATCH_SIZE> ripemdInputs;
alignas(32) std::array<std::array<uint8_t, 20>, HASH_BATCH_SIZE> ripemdOutputs;
const __uint128_t totalBatches = (numKeys + (HASH_BATCH_SIZE - 1)) / HASH_BATCH_SIZE;
for (__uint128_t batch = 0; batch < totalBatches; batch++)
{
const __uint128_t batchCount = std::min<__uint128_t>(HASH_BATCH_SIZE, numKeys - batch * HASH_BATCH_SIZE);
for (__uint128_t i = 0; i < batchCount; i++)
{
prepareShaBlock(pubKeys[batch * HASH_BATCH_SIZE + i], 33, shaInputs[i].data());
}
if (batchCount < HASH_BATCH_SIZE)
{
static std::array<uint8_t, 64> shaPadding = {};
prepareShaBlock(pubKeys[0], 33, shaPadding.data());
for (__uint128_t i = batchCount; i < HASH_BATCH_SIZE; i++)
{
std::memcpy(shaInputs[i].data(), shaPadding.data(), 64);
}
}
const uint8_t *inPtr[HASH_BATCH_SIZE];
uint8_t *outPtr[HASH_BATCH_SIZE];
for (int i = 0; i < HASH_BATCH_SIZE; i++)
{
inPtr[i] = shaInputs[i].data();
outPtr[i] = shaOutputs[i].data();
}
sha256avx2_8B(inPtr[0], inPtr[1], inPtr[2], inPtr[3],
inPtr[4], inPtr[5], inPtr[6], inPtr[7],
outPtr[0], outPtr[1], outPtr[2], outPtr[3],
outPtr[4], outPtr[5], outPtr[6], outPtr[7]);
for (__uint128_t i = 0; i < batchCount; i++)
{
prepareRipemdBlock(shaOutputs[i].data(), ripemdInputs[i].data());
}
if (batchCount < HASH_BATCH_SIZE)
{
static std::array<uint8_t, 64> ripemdPadding = {};
prepareRipemdBlock(shaOutputs[0].data(), ripemdPadding.data());
for (__uint128_t i = batchCount; i < HASH_BATCH_SIZE; i++)
{
std::memcpy(ripemdInputs[i].data(), ripemdPadding.data(), 64);
}
}
for (int i = 0; i < HASH_BATCH_SIZE; i++)
{
inPtr[i] = ripemdInputs[i].data();
outPtr[i] = ripemdOutputs[i].data();
}
ripemd160avx2::ripemd160avx2_32(
(unsigned char *)inPtr[0], (unsigned char *)inPtr[1],
(unsigned char *)inPtr[2], (unsigned char *)inPtr[3],
(unsigned char *)inPtr[4], (unsigned char *)inPtr[5],
(unsigned char *)inPtr[6], (unsigned char *)inPtr[7],
outPtr[0], outPtr[1], outPtr[2], outPtr[3],
outPtr[4], outPtr[5], outPtr[6], outPtr[7]);
for (__uint128_t i = 0; i < batchCount; i++)
{
std::memcpy(hashResults[batch * HASH_BATCH_SIZE + i], ripemdOutputs[i].data(), 20);
}
}
}
void worker(Secp256K1 *secp, int bit_length, int flip_count, int threadId, AVXCounter start, AVXCounter end)
{
auto last_report_time = chrono::high_resolution_clock::now();
const auto report_interval = chrono::seconds(1); // Iterval 1 seconds
const int fullBatchSize = 2 * POINTS_BATCH_SIZE;
alignas(32) uint8_t localPubKeys[HASH_BATCH_SIZE][33];
alignas(32) uint8_t localHashResults[HASH_BATCH_SIZE][20];
alignas(32) int pointIndices[HASH_BATCH_SIZE];
__m256i target16 = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(TARGET_HASH160_RAW.data()));
alignas(32) Point plusPoints[POINTS_BATCH_SIZE];
alignas(32) Point minusPoints[POINTS_BATCH_SIZE];
for (int i = 0; i < POINTS_BATCH_SIZE; i++)
{
Int tmp;
tmp.SetInt32(i);
plusPoints[i] = secp->ComputePublicKey(&tmp);
minusPoints[i] = plusPoints[i];
minusPoints[i].y.ModNeg();
}
alignas(32) Int deltaX[POINTS_BATCH_SIZE];
IntGroup modGroup(POINTS_BATCH_SIZE);
alignas(32) Int pointBatchX[fullBatchSize];
alignas(32) Int pointBatchY[fullBatchSize];
CombinationGenerator gen(bit_length - EXCLUDE_BITS, flip_count);
gen.unrank(start.load());
AVXCounter count;
count.store(start.load());
uint64_t actual_work_done = 0;
auto last_report = chrono::high_resolution_clock::now();
while (!stop_event.load() && count < end)
{
Int currentKey;
currentKey.Set(&BASE_KEY);
const vector<int> &flips = gen.get();
for (int pos : flips)
{
int adjusted_pos = pos;
Int mask;
mask.SetInt32(1);
mask.ShiftL(adjusted_pos);
currentKey.Xor(&mask);
}
string keyStr = currentKey.GetBase16();
keyStr = string(64 - keyStr.length(), '0') + keyStr;
#pragma omp critical
{
g_threadPrivateKeys[threadId] = keyStr;
}
Point startPoint = secp->ComputePublicKey(¤tKey);
Int startPointX, startPointY, startPointXNeg;
startPointX.Set(&startPoint.x);
startPointY.Set(&startPoint.y);
startPointXNeg.Set(&startPointX);
startPointXNeg.ModNeg();
for (int i = 0; i < POINTS_BATCH_SIZE; i += 4)
{
deltaX[i].ModSub(&plusPoints[i].x, &startPointX);
deltaX[i + 1].ModSub(&plusPoints[i + 1].x, &startPointX);
deltaX[i + 2].ModSub(&plusPoints[i + 2].x, &startPointX);
deltaX[i + 3].ModSub(&plusPoints[i + 3].x, &startPointX);
}
modGroup.Set(deltaX);
modGroup.ModInv();
for (int i = 0; i < POINTS_BATCH_SIZE; i++)
{
Int deltaY;
deltaY.ModSub(&plusPoints[i].y, &startPointY);
Int slope;
slope.ModMulK1(&deltaY, &deltaX[i]);
Int slopeSq;
slopeSq.ModSquareK1(&slope);
pointBatchX[i].Set(&startPointXNeg);
pointBatchX[i].ModAdd(&slopeSq);
pointBatchX[i].ModSub(&plusPoints[i].x);
Int diffX;
diffX.Set(&startPointX);
diffX.ModSub(&pointBatchX[i]);
diffX.ModMulK1(&slope);
pointBatchY[i].Set(&startPointY);
pointBatchY[i].ModNeg();
pointBatchY[i].ModAdd(&diffX);
}
for (int i = 0; i < POINTS_BATCH_SIZE; i++)
{
Int deltaY;
deltaY.ModSub(&minusPoints[i].y, &startPointY);
Int slope;
slope.ModMulK1(&deltaY, &deltaX[i]);
Int slopeSq;
slopeSq.ModSquareK1(&slope);
pointBatchX[POINTS_BATCH_SIZE + i].Set(&startPointXNeg);
pointBatchX[POINTS_BATCH_SIZE + i].ModAdd(&slopeSq);
pointBatchX[POINTS_BATCH_SIZE + i].ModSub(&minusPoints[i].x);
Int diffX;
diffX.Set(&startPointX);
diffX.ModSub(&pointBatchX[POINTS_BATCH_SIZE + i]);
diffX.ModMulK1(&slope);
pointBatchY[POINTS_BATCH_SIZE + i].Set(&startPointY);
pointBatchY[POINTS_BATCH_SIZE + i].ModNeg();
pointBatchY[POINTS_BATCH_SIZE + i].ModAdd(&diffX);
}
int localBatchCount = 0;
for (int i = 0; i < fullBatchSize && localBatchCount < HASH_BATCH_SIZE; i++)
{
Point tempPoint;
tempPoint.x.Set(&pointBatchX[i]);
tempPoint.y.Set(&pointBatchY[i]);
localPubKeys[localBatchCount][0] = tempPoint.y.IsEven() ? 0x02 : 0x03;
for (int j = 0; j < 32; j++)
{
localPubKeys[localBatchCount][1 + j] = pointBatchX[i].GetByte(31 - j);
}
pointIndices[localBatchCount] = i;
localBatchCount++;
if (localBatchCount == HASH_BATCH_SIZE)
{
computeHash160BatchBinSingle(localBatchCount, localPubKeys, localHashResults);
actual_work_done += HASH_BATCH_SIZE;
localComparedCount += HASH_BATCH_SIZE;
for (int j = 0; j < HASH_BATCH_SIZE; j++)
{
__m256i cand = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(localHashResults[j]));
__m256i cmp = _mm256_cmpeq_epi8(cand, target16);
int mask = _mm256_movemask_epi8(cmp);
if ((mask & 0x0F) == 0x0F)
{
bool fullMatch = true;
for (int k = 0; k < 20; k++)
{
if (localHashResults[j][k] != TARGET_HASH160_RAW[k])
{
fullMatch = false;
break;
}
}
if (fullMatch)
{
auto tEndTime = chrono::high_resolution_clock::now();
globalElapsedTime = chrono::duration<double>(tEndTime - tStart).count();
{
lock_guard<mutex> lock(progress_mutex);
globalComparedCount += actual_work_done;
mkeysPerSec = (double)globalComparedCount / globalElapsedTime / 1e6;
}
Int foundKey;
foundKey.Set(¤tKey);
int idx = pointIndices[j];
if (idx < 256)
{
Int offset;
offset.SetInt32(idx);
foundKey.Add(&offset);
}
else
{
Int offset;
offset.SetInt32(idx - 256);
foundKey.Sub(&offset);
}
string hexKey = foundKey.GetBase16();
hexKey = string(64 - hexKey.length(), '0') + hexKey;
lock_guard<mutex> lock(result_mutex);
results.push(make_tuple(hexKey, total_checked_avx.load(), flip_count));
stop_event.store(true);
return;
}
}
}
total_checked_avx.increment();
localBatchCount = 0;
auto now = chrono::high_resolution_clock::now();
if (now - last_report_time >= report_interval)
{
__uint128_t current_total = total_checked_avx.load();
globalElapsedTime = chrono::duration<double>(now - tStart).count();
globalComparedCount += localComparedCount;
localComparedCount = 0;
mkeysPerSec = (double)globalComparedCount / globalElapsedTime / 1e6;
double progress = min(100.0, (double)current_total / total_combinations * 100.0);
lock_guard<mutex> lock(progress_mutex);
moveCursorTo(0, 11);
cout << "Progress: " << fixed << setprecision(6) << progress << "%\n";
cout << "Processed: " << to_string_128(current_total) << "\n";
cout << "Speed: " << fixed << setprecision(2) << mkeysPerSec << " Mkeys/s\n";
cout << "Elapsed Time: " << formatElapsedTime(globalElapsedTime) << "\n";
cout.flush();
last_report_time = now;
if (current_total >= total_combinations)
{
stop_event.store(true);
break;
}
}
}
}
if (!gen.next())
{
break;
}
if (count >= end)
{
break;
}
}
if (!stop_event.load() && total_checked_avx.load() >= total_combinations)
{
stop_event.store(true);
}
}
void printUsage(const char *programName)
{
cout << "Usage: " << programName << " [options]\n";
cout << "Options:\n";
cout << " -p, --puzzle NUM Puzzle number to solve (default: 20)\n";
cout << " -t, --threads NUM Number of CPU cores to use (default: all)\n";
cout << " -f, --flips NUM Override default flip count for puzzle\n";
cout << " -k, --key KEY Custom base key (hex or decimal)\n";
cout << " -x, --exclude NUM Number of bits to exclude from mutation (default: 0)\n";
cout << " -h, --help Show this help message\n";
cout << "\nExample:\n";
cout << " " << programName << " -p 38 -t 8 -f 21 -k 12345 -x 4\n";
}
int main(int argc, char *argv[])
{
signal(SIGINT, signalHandler);
int opt;
int option_index = 0;
static struct option long_options[] = {
{"puzzle", required_argument, 0, 'p'},
{"threads", required_argument, 0, 't'},
{"flips", required_argument, 0, 'f'},
{"key", required_argument, 0, 'k'},
{"exclude", required_argument, 0, 'x'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}};
string custom_base_key;
while ((opt = getopt_long(argc, argv, "p:t:f:k:x:h", long_options, &option_index)) != -1)
{
if (opt == -1)
break;
switch (opt)
{
case 'p':
PUZZLE_NUM = atoi(optarg);
if (PUZZLE_NUM < 20 || PUZZLE_NUM > 101)
{
cerr << "Error: Puzzle number must be between 20 and 68\n";
return 1;
}
break;
case 't':
WORKERS = atoi(optarg);
if (WORKERS < 1)
{
cerr << "Error: Thread count must be at least 1\n";
return 1;
}
break;
case 'f':
FLIP_COUNT = atoi(optarg);
if (FLIP_COUNT < 1)
{
cerr << "Error: Flip count must be at least 1\n";
return 1;
}
break;
case 'k':
custom_base_key = optarg;
break;
case 'x':
EXCLUDE_BITS = atoi(optarg);
if (EXCLUDE_BITS < 0 || EXCLUDE_BITS >= PUZZLE_NUM)
{
cerr << "Error: Exclude bits must be between 0 and puzzle bit length - 1\n";
return 1;
}
break;
case 'h':
printUsage(argv[0]);
return 0;
default:
printUsage(argv[0]);
return 1;
}
}
tStart = chrono::high_resolution_clock::now();
Secp256K1 secp;
secp.Init();
auto puzzle_it = PUZZLE_DATA.find(PUZZLE_NUM);
if (puzzle_it == PUZZLE_DATA.end())
{
cerr << "Error: Invalid puzzle number\n";
return 1;
}
auto [DEFAULT_FLIP_COUNT, TARGET_HASH160_HEX, PRIVATE_KEY_DECIMAL] = puzzle_it->second;
if (FLIP_COUNT == -1)
{
FLIP_COUNT = DEFAULT_FLIP_COUNT;
}
TARGET_HASH160 = TARGET_HASH160_HEX;
for (__uint128_t i = 0; i < 20; i++)
{
TARGET_HASH160_RAW[i] = stoul(TARGET_HASH160.substr(i * 2, 2), nullptr, 16);
}
if (!custom_base_key.empty())
{
BASE_KEY.SetBase10(const_cast<char *>(custom_base_key.c_str()));
}
else
{
BASE_KEY.SetBase10(const_cast<char *>(PRIVATE_KEY_DECIMAL.c_str()));
}
Int testKey;
if (!custom_base_key.empty())
{
testKey.SetBase10(const_cast<char *>(custom_base_key.c_str()));
}
else
{
testKey.SetBase10(const_cast<char *>(PRIVATE_KEY_DECIMAL.c_str()));
}
if (!testKey.IsEqual(&BASE_KEY))
{
cerr << "Base key initialization failed!\n";
return 1;
}
total_combinations = CombinationGenerator::combinations_count(PUZZLE_NUM - EXCLUDE_BITS, FLIP_COUNT);
string paddedKey = BASE_KEY.GetBase16();
__uint128_t firstNonZero = paddedKey.find_first_not_of('0');
paddedKey = paddedKey.substr(firstNonZero);
paddedKey = "0x" + paddedKey;
clearTerminal();
cout << "=======================================\n";
cout << "== Mutagen Puzzle Solver by Denevron ==\n";
cout << "=======================================\n";
cout << "Starting puzzle: " << PUZZLE_NUM << " (" << PUZZLE_NUM << "-bit)\n";
cout << "Target HASH160: " << TARGET_HASH160.substr(0, 10) << "..." << TARGET_HASH160.substr(TARGET_HASH160.length() - 10) << "\n";
cout << "Base Key: " << paddedKey << "\n";
cout << "Using: " << WORKERS << " threads\n";
cout << "Flip count: " << FLIP_COUNT << " bits\n";
cout << "Unchanged bits: " << EXCLUDE_BITS << " bits\n";
cout << "Total Flips: " << to_string_128(total_combinations) << "\n";
cout << "\n";
g_threadPrivateKeys.resize(WORKERS, "0");
vector<thread> threads;
AVXCounter total_combinations_avx;
total_combinations_avx.store(total_combinations);
AVXCounter comb_per_thread = AVXCounter::div(total_combinations_avx, WORKERS);
uint64_t remainder = AVXCounter::mod(total_combinations_avx, WORKERS);
for (int i = 0; i < WORKERS; i++)
{
AVXCounter start, end;
AVXCounter base = AVXCounter::mul(i, comb_per_thread.load());
uint64_t extra = min(static_cast<uint64_t>(i), remainder);
start.store(base.load() + extra);
end.store(start.load() + comb_per_thread.load() + (i < remainder ? 1 : 0));
threads.emplace_back(worker, &secp, PUZZLE_NUM, FLIP_COUNT, i, start, end);
}
for (auto &t : threads)
{
if (t.joinable())
{
t.join();
}
}
if (!results.empty())
{
auto [hex_key, checked, flips] = results.front();
globalElapsedTime = chrono::duration<double>(chrono::high_resolution_clock::now() - tStart).count();
mkeysPerSec = (double)globalComparedCount / globalElapsedTime / 1e6;
string compactHex = hex_key;
__uint128_t firstNonZero = compactHex.find_first_not_of('0');
compactHex = "0x" + compactHex.substr(firstNonZero);
cout << "=======================================\n";
cout << "=========== SOLUTION FOUND ============\n";
cout << "=======================================\n";
cout << "Private key: " << compactHex << "\n";
cout << "Checked " << to_string_128(checked) << " combinations\n";
cout << "Bit flips: " << flips << endl;
cout << "Unchanged bits: " << EXCLUDE_BITS << " bits\n";
cout << "Time: " << fixed << setprecision(2) << globalElapsedTime << " seconds ("
<< formatElapsedTime(globalElapsedTime) << ")\n";
cout << "Speed: " << fixed << setprecision(2) << mkeysPerSec << " Mkeys/s\n";
ofstream out("puzzle_" + to_string(PUZZLE_NUM) + "_solution.txt");
if (out)
{
out << hex_key;
out.close();
cout << "Solution saved to puzzle_" << PUZZLE_NUM << "_solution.txt\n";
}
else
{
cerr << "Failed to save solution to file!\n";
}
}
else
{
__uint128_t final_count = total_checked_avx.load();
globalElapsedTime = chrono::duration<double>(chrono::high_resolution_clock::now() - tStart).count();
mkeysPerSec = (double)globalComparedCount / globalElapsedTime / 1e6;
cout << "\n\nNo solution found. Checked " << to_string_128(final_count) << " combinations\n";
cout << "Time: " << fixed << setprecision(2) << globalElapsedTime << " seconds ("
<< formatElapsedTime(globalElapsedTime) << ")\n";
cout << "Speed: " << fixed << setprecision(2) << mkeysPerSec << " Mkeys/s\n";
}
return 0;
}