-
Notifications
You must be signed in to change notification settings - Fork 10
/
Main.cpp
5685 lines (4953 loc) · 255 KB
/
Main.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
#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <unordered_set>
#include <cstring>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <cstring>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <queue>
#include <thread>
#include <locale>
#include <codecvt>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include "ed25519.h"
#include "dict.hpp"
#include <mutex>
#include <atomic>
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW64__)
#include <windows.h>
#include <condition_variable>
HANDLE fileMutex;
HANDLE printMutex;
#else
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t fileMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t printMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t inputMutex = PTHREAD_MUTEX_INITIALIZER;
#endif
#include <cstring>
#include <random>
#if defined(__x86_64__) || defined(_M_X64)
#include <immintrin.h>
static void Random_Bytes(unsigned char* bytes, size_t size) {
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW64__)
uint64_t randomValue;
#else
unsigned long long randomValue;
#endif
for (size_t i = 0; i < size; i += 8) {
try {
if (_rdrand64_step(&randomValue)) {
size_t copySize = (i + 8 <= size) ? 8 : size - i;
memcpy(bytes + i, &randomValue, copySize);
}
else {
std::random_device rd;
std::mt19937 gen(rd());
for (size_t i = 0; i < size; ++i) {
bytes[i] = gen();
}
}
}
catch (...) {
std::random_device rd;
std::mt19937_64 gen(rd());
for (size_t j = i; j < size; ++j) {
bytes[j] = gen();
}
return;
}
}
}
#elif defined(__aarch64__) || defined(_M_ARM64)
#if defined(__APPLE__)
#include <stdlib.h>
static void Random_Bytes(unsigned char* bytes, size_t size) {
try {
arc4random_buf(bytes, size);
}
catch (...) {
std::random_device rd;
std::mt19937 gen(rd());
for (size_t i = 0; i < size; ++i) {
bytes[i] = gen();
}
}
}
#else
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/random.h>
static void Random_Bytes(unsigned char* bytes, size_t size) {
try {
ssize_t result = syscall(SYS_getrandom, bytes, size, 0);
if (result < 0) {
std::random_device rd;
std::mt19937 gen(rd());
for (size_t i = 0; i < size; ++i) {
bytes[i] = gen();
}
}
catch (...) {
std::random_device rd;
std::mt19937 gen(rd());
for (size_t i = 0; i < size; ++i) {
bytes[i] = gen();
}
}
}
#endif
#else
static void Random_Bytes(unsigned char* bytes, size_t size) {
std::random_device rd;
std::mt19937 gen(rd());
for (size_t i = 0; i < size; ++i) {
bytes[i] = gen();
}
}
#endif
using namespace std;
static bool Ethereum = false;
static bool Compressed = true;
static bool Uncompressed = true;
static bool Segwit = true;
static bool Xpoint = false;
static bool Solana = false;
static bool isEd25519 = false;
static bool isSecp256k1 = false;
static unsigned char* blooms[100];
static int blooms_count = 0;
static string inputFile;
static string outputFile = "FOUND.txt";
static bool usingFile = false;
static bool isHex = false;
static bool isRunning = true;
static bool ADDRESSES = false;
static unsigned long long totalMnemonics = 0;
static unsigned long long totalHashes = 0;
static string path_custom = "m/\n";
static unsigned long long FOUND = 0;
const char* const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const size_t ALPHABET_SIZE = strlen(ALPHABET);
const uint8_t salt[12] = { 109, 110, 101, 109, 111, 110, 105, 99, 0, 0, 0, 1 };
const uint8_t salt_swap[16] = { 99, 105, 110, 111, 109, 101, 110, 109, 0, 0, 0, 0, 1, 0, 0, 0 };
const uint8_t key001[16] = { 0x42, 0x69, 0x74, 0x63, 0x6f, 0x69, 0x6e, 0x20, 0x73, 0x65, 0x65, 0x64 , 0, 0, 0, 1 };
const uint8_t key[12] = { 0x42, 0x69, 0x74, 0x63, 0x6f, 0x69, 0x6e, 0x20, 0x73, 0x65, 0x65, 0x64 };
const uint8_t key_swap[16] = { 0x20, 0x6e, 0x69, 0x6f, 0x63, 0x74, 0x69, 0x42, 0, 0, 0, 0, 0x64, 0x65, 0x65, 0x73 };
const uint8_t ed_key_swap[16] = { 0x20, 0x39, 0x31, 0x35, 0x35, 0x32, 0x64, 0x65, 0, 0, 0, 0, 0x64, 0x65, 0x65, 0x73 };
bool cust = false;
bool israndom = true;
string start_point = "";
string custom_file = "";
bool entropy_file_generator = false;
string entropy_file = "";
int threads = 2;
int step = 1;
string lang = "EN";
const wchar_t** current_dict;
int words_count_min = 3;
int entropy_count_min = 4;
uint64_t n = 0;
int words_count = 12;
int entropy_len = 16;
bool round_ent = true;
std::string encodeBase58(const uint8_t* bytes, size_t length) {
std::vector<unsigned char> v;
while (length--) {
v.push_back(*bytes++);
}
std::vector<unsigned char> result;
std::vector<unsigned int> bignum(v.size() * 138 / 100, 0);
for (auto byte : v) {
int carry = byte;
for (auto& num : bignum) {
int x = num * 256 + carry;
num = x % 58;
carry = x / 58;
}
}
while (bignum.size() > 0 && bignum.back() == 0) {
bignum.pop_back();
}
for (auto num : bignum) {
result.push_back(ALPHABET[num]);
}
while (v.size() > 0 && v[0] == 0) {
result.push_back(ALPHABET[0]);
v.erase(v.begin());
}
return std::string(result.rbegin(), result.rend());
}
uint32_t bech32_polymod_step(uint32_t pre) {
uint8_t b = pre >> 25;
return ((pre & 0x1FFFFFF) << 5) ^
(-((b >> 0) & 1) & 0x3b6a57b2UL) ^
(-((b >> 1) & 1) & 0x26508e6dUL) ^
(-((b >> 2) & 1) & 0x1ea119faUL) ^
(-((b >> 3) & 1) & 0x3d4233ddUL) ^
(-((b >> 4) & 1) & 0x2a1462b3UL);
}
static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
static const int8_t charset_rev[128] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1,
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1,
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
};
int bech32_encode(char* output, const char* hrp, const uint8_t* data, size_t data_len) {
uint32_t chk = 1;
size_t i = 0;
while (hrp[i] != 0) {
int ch = hrp[i];
if (ch < 33 || ch > 126) {
return 0;
}
if (ch >= 'A' && ch <= 'Z') return 0;
chk = bech32_polymod_step(chk) ^ (ch >> 5);
++i;
}
if (i + 7 + data_len > 90) return 0;
chk = bech32_polymod_step(chk);
while (*hrp != 0) {
chk = bech32_polymod_step(chk) ^ (*hrp & 0x1f);
*(output++) = *(hrp++);
}
*(output++) = '1';
for (i = 0; i < data_len; ++i) {
if (*data >> 5) return 0;
chk = bech32_polymod_step(chk) ^ (*data);
*(output++) = charset[*(data++)];
}
for (i = 0; i < 6; ++i) {
chk = bech32_polymod_step(chk);
}
chk ^= 1;
for (i = 0; i < 6; ++i) {
*(output++) = charset[(chk >> ((5 - i) * 5)) & 0x1f];
}
*output = 0;
return 1;
}
static int convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) {
uint32_t val = 0;
int bits = 0;
uint32_t maxv = (((uint32_t)1) << outbits) - 1;
while (inlen--) {
val = (val << inbits) | *(in++);
bits += inbits;
while (bits >= outbits) {
bits -= outbits;
out[(*outlen)++] = (val >> bits) & maxv;
}
}
if (pad) {
if (bits) {
out[(*outlen)++] = (val << (outbits - bits)) & maxv;
}
}
else if (((val << (outbits - bits)) & maxv) || bits >= inbits) {
return 0;
}
return 1;
}
int segwit_addr_encode(char* output, const char* hrp, int witver, const uint8_t* witprog, size_t witprog_len) {
uint8_t data[65] = { 0 };
size_t datalen = 0;
if (witver > 16) return 0;
if (witver == 0 && witprog_len != 20 && witprog_len != 32) return 0;
if (witprog_len < 2 || witprog_len > 40) return 0;
data[0] = witver;
convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1);
++datalen;
return bech32_encode(output, hrp, data, datalen);
}
int bech32_decode_nocheck(uint8_t* data, size_t* data_len, const char* input)
{
uint8_t acc = 0;
uint8_t acc_len = 8;
size_t out_len = 0;
size_t input_len = strlen(input);
for (int i = 0; i < input_len; i++) {
if (input[i] & 0x80)
return false;
int8_t c = charset_rev[tolower(input[i])];
if (c < 0)
return false;
if (acc_len >= 5) {
acc |= c << (acc_len - 5);
acc_len -= 5;
}
else {
int shift = 5 - acc_len;
data[out_len++] = acc | (c >> shift);
acc_len = 8 - shift;
acc = c << acc_len;
}
}
data[out_len++] = acc;
*data_len = out_len;
return true;
}
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW64__)
class FileReaderThread {
public:
FileReaderThread(const std::string& filename) : inputFile(filename), stop(false) {}
void start() {
thread = std::thread(&FileReaderThread::run, this);
}
void run() {
std::ifstream file(inputFile);
std::string line;
while (std::getline(file, line) && !stop) {
std::unique_lock<std::mutex> lock(queueMutex);
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
queue.push(line);
condVar.notify_all();
}
file.close();
done = true;
condVar.notify_all();
}
bool popLine(std::string& line) {
std::unique_lock<std::mutex> lock(queueMutex);
while (queue.empty() && !done) {
condVar.wait(lock);
}
if (queue.empty() && done) {
isRunning = false;
return false;
}
line = queue.front();
queue.pop();
return true;
}
void stopThread() {
stop = true;
thread.join();
}
private:
std::string inputFile;
std::thread thread;
std::queue<std::string> queue;
std::mutex queueMutex;
std::condition_variable condVar;
bool stop;
bool done = false;
};
class InputStreamReaderThread {
public:
InputStreamReaderThread() : stop(false), done(false) {}
void start() {
thread = std::thread(&InputStreamReaderThread::run, this);
}
void run() {
std::string line;
while (std::getline(std::cin, line) && !stop) {
std::unique_lock<std::mutex> lock(queueMutex);
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
queue.push(line);
condVar.notify_all();
}
done = true;
condVar.notify_all();
}
bool popLine(std::string& line) {
std::unique_lock<std::mutex> lock(queueMutex);
while (queue.empty() && !done) {
condVar.wait(lock);
}
if (queue.empty() && done) {
isRunning = false;
return false;
}
line = queue.front();
queue.pop();
return true;
}
void stopThread() {
stop = true;
thread.join();
}
private:
std::thread thread;
std::queue<std::string> queue;
std::mutex queueMutex;
std::condition_variable condVar;
bool stop;
bool done = false;
};
#else
class FileReaderThread {
public:
FileReaderThread(const std::string& filename) : inputFile(filename), stop(false), done(false) {
pthread_mutex_init(&queueMutex, NULL);
pthread_cond_init(&condVar, NULL);
}
~FileReaderThread() {
pthread_mutex_destroy(&queueMutex);
pthread_cond_destroy(&condVar);
}
static void* runWrapper(void* arg) {
reinterpret_cast<FileReaderThread*>(arg)->run();
return NULL;
}
void start() {
pthread_create(&thread, NULL, runWrapper, this);
}
void run() {
std::ifstream file(inputFile);
std::string line;
while (std::getline(file, line) && !stop) {
pthread_mutex_lock(&queueMutex);
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
queue.push(line);
pthread_cond_signal(&condVar);
pthread_mutex_unlock(&queueMutex);
}
file.close();
done = true;
pthread_cond_broadcast(&condVar);
}
bool popLine(std::string& line) {
pthread_mutex_lock(&queueMutex);
while (queue.empty() && !done) {
pthread_cond_wait(&condVar, &queueMutex);
}
if (queue.empty() && done) {
pthread_mutex_unlock(&queueMutex);
isRunning = false;
return false;
}
line = queue.front();
queue.pop();
pthread_mutex_unlock(&queueMutex);
return true;
}
void stopThread() {
stop = true;
pthread_join(thread, NULL);
}
private:
std::string inputFile;
pthread_t thread;
std::queue<std::string> queue;
pthread_mutex_t queueMutex;
pthread_cond_t condVar;
bool stop;
bool done;
};
class InputStreamReaderThread {
public:
InputStreamReaderThread() : stop(false), done(false) {
pthread_mutex_init(&queueMutex, NULL);
pthread_cond_init(&condVar, NULL);
}
~InputStreamReaderThread() {
pthread_mutex_destroy(&queueMutex);
pthread_cond_destroy(&condVar);
}
static void* runWrapper(void* arg) {
reinterpret_cast<InputStreamReaderThread*>(arg)->run();
return NULL;
}
void start() {
pthread_create(&thread, NULL, runWrapper, this);
}
void run() {
std::string line;
while (std::getline(std::cin, line) && !stop) {
pthread_mutex_lock(&queueMutex);
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
queue.push(line);
pthread_cond_signal(&condVar);
pthread_mutex_unlock(&queueMutex);
}
done = true;
pthread_cond_broadcast(&condVar);
}
bool popLine(std::string& line) {
pthread_mutex_lock(&queueMutex);
while (queue.empty() && !done) {
pthread_cond_wait(&condVar, &queueMutex);
}
if (queue.empty() && done) {
pthread_mutex_unlock(&queueMutex);
isRunning = false;
return false;
}
line = queue.front();
queue.pop();
pthread_mutex_unlock(&queueMutex);
return true;
}
void stopThread() {
stop = true;
pthread_join(thread, NULL);
}
private:
pthread_t thread;
std::queue<std::string> queue;
pthread_mutex_t queueMutex;
pthread_cond_t condVar;
bool stop;
bool done;
};
#endif
#define rotate(n,d) (((n) << (d)) | ((n) >> (64 - (d))))
#define TH_ELT(t, c0, c1, c2, c3, c4, d0, d1, d2, d3, d4) \
{ \
t = rotate((uint64_t)(d0 ^ d1 ^ d2 ^ d3 ^ d4), (uint64_t)1) ^ (c0 ^ c1 ^ c2 ^ c3 ^ c4); \
}
#define THETA(s00, s01, s02, s03, s04, \
s10, s11, s12, s13, s14, \
s20, s21, s22, s23, s24, \
s30, s31, s32, s33, s34, \
s40, s41, s42, s43, s44) \
{ \
TH_ELT(t0, s40, s41, s42, s43, s44, s10, s11, s12, s13, s14); \
TH_ELT(t1, s00, s01, s02, s03, s04, s20, s21, s22, s23, s24); \
TH_ELT(t2, s10, s11, s12, s13, s14, s30, s31, s32, s33, s34); \
TH_ELT(t3, s20, s21, s22, s23, s24, s40, s41, s42, s43, s44); \
TH_ELT(t4, s30, s31, s32, s33, s34, s00, s01, s02, s03, s04); \
s00 ^= t0; s01 ^= t0; s02 ^= t0; s03 ^= t0; s04 ^= t0; \
s10 ^= t1; s11 ^= t1; s12 ^= t1; s13 ^= t1; s14 ^= t1; \
s20 ^= t2; s21 ^= t2; s22 ^= t2; s23 ^= t2; s24 ^= t2; \
s30 ^= t3; s31 ^= t3; s32 ^= t3; s33 ^= t3; s34 ^= t3; \
s40 ^= t4; s41 ^= t4; s42 ^= t4; s43 ^= t4; s44 ^= t4; \
}
#define RHOPI(s00, s01, s02, s03, s04, \
s10, s11, s12, s13, s14, \
s20, s21, s22, s23, s24, \
s30, s31, s32, s33, s34, \
s40, s41, s42, s43, s44) \
{ \
t0 = rotate(s10, (uint64_t) 1); \
s10 = rotate(s11, (uint64_t)44); \
s11 = rotate(s41, (uint64_t)20); \
s41 = rotate(s24, (uint64_t)61); \
s24 = rotate(s42, (uint64_t)39); \
s42 = rotate(s04, (uint64_t)18); \
s04 = rotate(s20, (uint64_t)62); \
s20 = rotate(s22, (uint64_t)43); \
s22 = rotate(s32, (uint64_t)25); \
s32 = rotate(s43, (uint64_t) 8); \
s43 = rotate(s34, (uint64_t)56); \
s34 = rotate(s03, (uint64_t)41); \
s03 = rotate(s40, (uint64_t)27); \
s40 = rotate(s44, (uint64_t)14); \
s44 = rotate(s14, (uint64_t) 2); \
s14 = rotate(s31, (uint64_t)55); \
s31 = rotate(s13, (uint64_t)45); \
s13 = rotate(s01, (uint64_t)36); \
s01 = rotate(s30, (uint64_t)28); \
s30 = rotate(s33, (uint64_t)21); \
s33 = rotate(s23, (uint64_t)15); \
s23 = rotate(s12, (uint64_t)10); \
s12 = rotate(s21, (uint64_t) 6); \
s21 = rotate(s02, (uint64_t) 3); \
s02 = t0; \
}
#define KHI(s00, s01, s02, s03, s04, \
s10, s11, s12, s13, s14, \
s20, s21, s22, s23, s24, \
s30, s31, s32, s33, s34, \
s40, s41, s42, s43, s44) \
{ \
t0 = s00 ^ (~s10 & s20); \
t1 = s10 ^ (~s20 & s30); \
t2 = s20 ^ (~s30 & s40); \
t3 = s30 ^ (~s40 & s00); \
t4 = s40 ^ (~s00 & s10); \
s00 = t0; s10 = t1; s20 = t2; s30 = t3; s40 = t4; \
\
t0 = s01 ^ (~s11 & s21); \
t1 = s11 ^ (~s21 & s31); \
t2 = s21 ^ (~s31 & s41); \
t3 = s31 ^ (~s41 & s01); \
t4 = s41 ^ (~s01 & s11); \
s01 = t0; s11 = t1; s21 = t2; s31 = t3; s41 = t4; \
\
t0 = s02 ^ (~s12 & s22); \
t1 = s12 ^ (~s22 & s32); \
t2 = s22 ^ (~s32 & s42); \
t3 = s32 ^ (~s42 & s02); \
t4 = s42 ^ (~s02 & s12); \
s02 = t0; s12 = t1; s22 = t2; s32 = t3; s42 = t4; \
\
t0 = s03 ^ (~s13 & s23); \
t1 = s13 ^ (~s23 & s33); \
t2 = s23 ^ (~s33 & s43); \
t3 = s33 ^ (~s43 & s03); \
t4 = s43 ^ (~s03 & s13); \
s03 = t0; s13 = t1; s23 = t2; s33 = t3; s43 = t4; \
\
t0 = s04 ^ (~s14 & s24); \
t1 = s14 ^ (~s24 & s34); \
t2 = s24 ^ (~s34 & s44); \
t3 = s34 ^ (~s44 & s04); \
t4 = s44 ^ (~s04 & s14); \
s04 = t0; s14 = t1; s24 = t2; s34 = t3; s44 = t4; \
}
#define IOTA(s00, r) { s00 ^= r; }
const uint64_t keccakf_rndc[24] = {
0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
0x8000000000008080, 0x0000000080000001, 0x8000000080008008
};
typedef union {
uint8_t b[200];
uint64_t q[25];
uint32_t d[50];
} ethhash;
void sha3_keccakf(ethhash* const h)
{
uint64_t* const st = (uint64_t* const)&h->q;
h->d[33] ^= 0x80000000;
uint64_t t0, t1, t2, t3, t4;
for (int i = 0; i < 24; ++i) {
THETA(st[0], st[5], st[10], st[15], st[20], st[1], st[6], st[11], st[16], st[21], st[2], st[7], st[12], st[17], st[22], st[3], st[8], st[13], st[18], st[23], st[4], st[9], st[14], st[19], st[24]);
RHOPI(st[0], st[5], st[10], st[15], st[20], st[1], st[6], st[11], st[16], st[21], st[2], st[7], st[12], st[17], st[22], st[3], st[8], st[13], st[18], st[23], st[4], st[9], st[14], st[19], st[24]);
KHI(st[0], st[5], st[10], st[15], st[20], st[1], st[6], st[11], st[16], st[21], st[2], st[7], st[12], st[17], st[22], st[3], st[8], st[13], st[18], st[23], st[4], st[9], st[14], st[19], st[24]);
IOTA(st[0], keccakf_rndc[i]);
}
}
#define SHAF1(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
#define SHAF0(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define mod(x,y) ((x)-((x)/(y)*(y)))
#define shr32(x,n) ((x) >> (n))
#define rotl32(n,d) (((n) << (d)) | ((n) >> (32 - (d))))
#define rotl64(n,d) (((n) << (d)) | ((n) >> (64 - (d))))
#define rotr64(n,d) (((n) >> (d)) | ((n) << (64 - (d))))
#define S0(x) (rotl32 ((x), 25u) ^ rotl32 ((x), 14u) ^ shr32 ((x), 3u))
#define S1(x) (rotl32 ((x), 15u) ^ rotl32 ((x), 13u) ^ shr32 ((x), 10u))
#define S2(x) (rotl32 ((x), 30u) ^ rotl32 ((x), 19u) ^ rotl32 ((x), 10u))
#define S3(x) (rotl32 ((x), 26u) ^ rotl32 ((x), 21u) ^ rotl32 ((x), 7u))
#define SHR(b,x) ((x) >> (b))
#define ROTR32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
#define ROTR64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
#define REVERSE32(w,x) { \
uint32_t tmp = (w); \
tmp = (tmp >> 16) | (tmp << 16); \
(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
}
#define REVERSE64(w,x) { \
uint64_t tmp = (w); \
tmp = (tmp >> 32) | (tmp << 32); \
tmp = ((tmp & 0xff00ff00ff00ff00UL) >> 8) | \
((tmp & 0x00ff00ff00ff00ffUL) << 8); \
(x) = ((tmp & 0xffff0000ffff0000UL) >> 16) | \
((tmp & 0x0000ffff0000ffffUL) << 16); \
}
#define SHA512_S0(x) (ROTR64(28, (x)) ^ ROTR64(34, (x)) ^ ROTR64(39, (x)))
#define SHA512_S1(x) (ROTR64(14, (x)) ^ ROTR64(18, (x)) ^ ROTR64(41, (x)))
#define little_s0(x) (ROTR64( 1, (x)) ^ ROTR64( 8, (x)) ^ SHR( 7, (x)))
#define little_s1(x) (ROTR64(19, (x)) ^ ROTR64(61, (x)) ^ SHR( 6, (x)))
#define highBit(i) (0x0000000000000001ULL << (8*(i) + 7))
#define fBytes(i) (0xFFFFFFFFFFFFFFFFULL >> (8 * (8-(i))))
#define SHA256C00 0x428a2f98u
#define SHA256C01 0x71374491u
#define SHA256C02 0xb5c0fbcfu
#define SHA256C03 0xe9b5dba5u
#define SHA256C04 0x3956c25bu
#define SHA256C05 0x59f111f1u
#define SHA256C06 0x923f82a4u
#define SHA256C07 0xab1c5ed5u
#define SHA256C08 0xd807aa98u
#define SHA256C09 0x12835b01u
#define SHA256C0a 0x243185beu
#define SHA256C0b 0x550c7dc3u
#define SHA256C0c 0x72be5d74u
#define SHA256C0d 0x80deb1feu
#define SHA256C0e 0x9bdc06a7u
#define SHA256C0f 0xc19bf174u
#define SHA256C10 0xe49b69c1u
#define SHA256C11 0xefbe4786u
#define SHA256C12 0x0fc19dc6u
#define SHA256C13 0x240ca1ccu
#define SHA256C14 0x2de92c6fu
#define SHA256C15 0x4a7484aau
#define SHA256C16 0x5cb0a9dcu
#define SHA256C17 0x76f988dau
#define SHA256C18 0x983e5152u
#define SHA256C19 0xa831c66du
#define SHA256C1a 0xb00327c8u
#define SHA256C1b 0xbf597fc7u
#define SHA256C1c 0xc6e00bf3u
#define SHA256C1d 0xd5a79147u
#define SHA256C1e 0x06ca6351u
#define SHA256C1f 0x14292967u
#define SHA256C20 0x27b70a85u
#define SHA256C21 0x2e1b2138u
#define SHA256C22 0x4d2c6dfcu
#define SHA256C23 0x53380d13u
#define SHA256C24 0x650a7354u
#define SHA256C25 0x766a0abbu
#define SHA256C26 0x81c2c92eu
#define SHA256C27 0x92722c85u
#define SHA256C28 0xa2bfe8a1u
#define SHA256C29 0xa81a664bu
#define SHA256C2a 0xc24b8b70u
#define SHA256C2b 0xc76c51a3u
#define SHA256C2c 0xd192e819u
#define SHA256C2d 0xd6990624u
#define SHA256C2e 0xf40e3585u
#define SHA256C2f 0x106aa070u
#define SHA256C30 0x19a4c116u
#define SHA256C31 0x1e376c08u
#define SHA256C32 0x2748774cu
#define SHA256C33 0x34b0bcb5u
#define SHA256C34 0x391c0cb3u
#define SHA256C35 0x4ed8aa4au
#define SHA256C36 0x5b9cca4fu
#define SHA256C37 0x682e6ff3u
#define SHA256C38 0x748f82eeu
#define SHA256C39 0x78a5636fu
#define SHA256C3a 0x84c87814u
#define SHA256C3b 0x8cc70208u
#define SHA256C3c 0x90befffau
#define SHA256C3d 0xa4506cebu
#define SHA256C3e 0xbef9a3f7u
#define SHA256C3f 0xc67178f2u
const uint64_t padLong[8] = { highBit(0), highBit(1), highBit(2), highBit(3), highBit(4), highBit(5), highBit(6), highBit(7) };
const uint64_t maskLong[8] = { 0, fBytes(1), fBytes(2), fBytes(3), fBytes(4), fBytes(5), fBytes(6), fBytes(7) };
static uint32_t SWAP256(uint32_t val) {
return (rotl32(((val) & (uint32_t)0x00FF00FF), (uint32_t)24U) | rotl32(((val) & (uint32_t)0xFF00FF00), (uint32_t)8U));
}
static uint64_t SWAP512(uint64_t val) {
uint64_t tmp;
uint64_t ret;
tmp = (rotr64((uint64_t)((val) & (uint64_t)0x0000FFFF0000FFFFUL), 16) | rotl64((uint64_t)((val) & (uint64_t)0xFFFF0000FFFF0000UL), 16));
ret = (rotr64((uint64_t)((tmp) & (uint64_t)0xFF00FF00FF00FF00UL), 8) | rotl64((uint64_t)((tmp) & (uint64_t)0x00FF00FF00FF00FFUL), 8));
return ret;
}
static void md_pad_128(uint64_t* msg, const long msgLen_bytes) {
uint32_t padLongIndex, overhang;
padLongIndex = ((uint32_t)msgLen_bytes) / 8; overhang = (((uint32_t)msgLen_bytes) - padLongIndex * 8); msg[padLongIndex] &= maskLong[overhang]; msg[padLongIndex] |= padLong[overhang]; msg[padLongIndex + 1] = 0; msg[padLongIndex + 2] = 0; uint32_t i = 0;
for (i = padLongIndex + 3; i < 32; i++) {
msg[i] = 0;
}
msg[i - 2] = 0; msg[i - 1] = SWAP512(msgLen_bytes * 8); };
const static uint32_t k_sha256[64] =
{
SHA256C00, SHA256C01, SHA256C02, SHA256C03,
SHA256C04, SHA256C05, SHA256C06, SHA256C07,
SHA256C08, SHA256C09, SHA256C0a, SHA256C0b,
SHA256C0c, SHA256C0d, SHA256C0e, SHA256C0f,
SHA256C10, SHA256C11, SHA256C12, SHA256C13,
SHA256C14, SHA256C15, SHA256C16, SHA256C17,
SHA256C18, SHA256C19, SHA256C1a, SHA256C1b,
SHA256C1c, SHA256C1d, SHA256C1e, SHA256C1f,
SHA256C20, SHA256C21, SHA256C22, SHA256C23,
SHA256C24, SHA256C25, SHA256C26, SHA256C27,
SHA256C28, SHA256C29, SHA256C2a, SHA256C2b,
SHA256C2c, SHA256C2d, SHA256C2e, SHA256C2f,
SHA256C30, SHA256C31, SHA256C32, SHA256C33,
SHA256C34, SHA256C35, SHA256C36, SHA256C37,
SHA256C38, SHA256C39, SHA256C3a, SHA256C3b,
SHA256C3c, SHA256C3d, SHA256C3e, SHA256C3f,
};
const static uint64_t k_sha512[80] =
{
0x428a2f98d728ae22UL, 0x7137449123ef65cdUL, 0xb5c0fbcfec4d3b2fUL, 0xe9b5dba58189dbbcUL, 0x3956c25bf348b538UL,
0x59f111f1b605d019UL, 0x923f82a4af194f9bUL, 0xab1c5ed5da6d8118UL, 0xd807aa98a3030242UL, 0x12835b0145706fbeUL,
0x243185be4ee4b28cUL, 0x550c7dc3d5ffb4e2UL, 0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL, 0x9bdc06a725c71235UL,
0xc19bf174cf692694UL, 0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL, 0x0fc19dc68b8cd5b5UL, 0x240ca1cc77ac9c65UL,
0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL, 0x5cb0a9dcbd41fbd4UL, 0x76f988da831153b5UL, 0x983e5152ee66dfabUL,
0xa831c66d2db43210UL, 0xb00327c898fb213fUL, 0xbf597fc7beef0ee4UL, 0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL,
0x06ca6351e003826fUL, 0x142929670a0e6e70UL, 0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL, 0x4d2c6dfc5ac42aedUL,
0x53380d139d95b3dfUL, 0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL, 0x81c2c92e47edaee6UL, 0x92722c851482353bUL,
0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL, 0xc24b8b70d0f89791UL, 0xc76c51a30654be30UL, 0xd192e819d6ef5218UL,
0xd69906245565a910UL, 0xf40e35855771202aUL, 0x106aa07032bbd1b8UL, 0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL,
0x2748774cdf8eeb99UL, 0x34b0bcb5e19b48a8UL, 0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL, 0x5b9cca4f7763e373UL,
0x682e6ff3d6b2b8a3UL, 0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL, 0x84c87814a1f0ab72UL, 0x8cc702081a6439ecUL,
0x90befffa23631e28UL, 0xa4506cebde82bde9UL, 0xbef9a3f7b2c67915UL, 0xc67178f2e372532bUL, 0xca273eceea26619cUL,
0xd186b8c721c0c207UL, 0xeada7dd6cde0eb1eUL, 0xf57d4f7fee6ed178UL, 0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL,
0x113f9804bef90daeUL, 0x1b710b35131c471bUL, 0x28db77f523047d84UL, 0x32caab7b40c72493UL, 0x3c9ebe0a15c9bebcUL,
0x431d67c49c100d4cUL, 0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL, 0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL
};
#define SHA256_STEP(F0a,F1a,a,b,c,d,e,f,g,h,x,K) { h += K; h += x; h += S3 (e); h += F1a (e,f,g); d += h; h += S2 (a); h += F0a (a,b,c); }
#define SHA512_STEP(a,b,c,d,e,f,g,h,x,K) { h += K + SHA512_S1(e) + SHAF1(e, f, g) + x; d += h; h += SHA512_S0(a) + SHAF0(a, b, c);}
#define ROUND_STEP_SHA512(i) { SHA512_STEP(a, b, c, d, e, f, g, h, W[i + 0], k_sha512[i + 0]); SHA512_STEP(h, a, b, c, d, e, f, g, W[i + 1], k_sha512[i + 1]); SHA512_STEP(g, h, a, b, c, d, e, f, W[i + 2], k_sha512[i + 2]); SHA512_STEP(f, g, h, a, b, c, d, e, W[i + 3], k_sha512[i + 3]); SHA512_STEP(e, f, g, h, a, b, c, d, W[i + 4], k_sha512[i + 4]); SHA512_STEP(d, e, f, g, h, a, b, c, W[i + 5], k_sha512[i + 5]); SHA512_STEP(c, d, e, f, g, h, a, b, W[i + 6], k_sha512[i + 6]); SHA512_STEP(b, c, d, e, f, g, h, a, W[i + 7], k_sha512[i + 7]); SHA512_STEP(a, b, c, d, e, f, g, h, W[i + 8], k_sha512[i + 8]); SHA512_STEP(h, a, b, c, d, e, f, g, W[i + 9], k_sha512[i + 9]); SHA512_STEP(g, h, a, b, c, d, e, f, W[i + 10], k_sha512[i + 10]); SHA512_STEP(f, g, h, a, b, c, d, e, W[i + 11], k_sha512[i + 11]); SHA512_STEP(e, f, g, h, a, b, c, d, W[i + 12], k_sha512[i + 12]); SHA512_STEP(d, e, f, g, h, a, b, c, W[i + 13], k_sha512[i + 13]); SHA512_STEP(c, d, e, f, g, h, a, b, W[i + 14], k_sha512[i + 14]); SHA512_STEP(b, c, d, e, f, g, h, a, W[i + 15], k_sha512[i + 15]); }
#define SHA256_EXPAND(x,y,z,w) (S1 (x) + y + S0 (z) + w)
#define ROUND_STEP_SHA512_SHARED(i) { \
SHA512_STEP(a, b, c, d, e, f, g, h, W_data[i + 0], k_sha512[i + 0]); \
SHA512_STEP(h, a, b, c, d, e, f, g, W_data[i + 1], k_sha512[i + 1]); \
SHA512_STEP(g, h, a, b, c, d, e, f, W_data[i + 2], k_sha512[i + 2]); \
SHA512_STEP(f, g, h, a, b, c, d, e, W_data[i + 3], k_sha512[i + 3]); \
SHA512_STEP(e, f, g, h, a, b, c, d, W_data[i + 4], k_sha512[i + 4]); \
SHA512_STEP(d, e, f, g, h, a, b, c, W_data[i + 5], k_sha512[i + 5]); \
SHA512_STEP(c, d, e, f, g, h, a, b, W_data[i + 6], k_sha512[i + 6]); \
SHA512_STEP(b, c, d, e, f, g, h, a, W_data[i + 7], k_sha512[i + 7]); \
SHA512_STEP(a, b, c, d, e, f, g, h, W_data[i + 8], k_sha512[i + 8]); \
SHA512_STEP(h, a, b, c, d, e, f, g, W_data[i + 9], k_sha512[i + 9]); \
SHA512_STEP(g, h, a, b, c, d, e, f, W_data[i + 10], k_sha512[i + 10]); \
SHA512_STEP(f, g, h, a, b, c, d, e, W_data[i + 11], k_sha512[i + 11]); \
SHA512_STEP(e, f, g, h, a, b, c, d, W_data[i + 12], k_sha512[i + 12]); \
SHA512_STEP(d, e, f, g, h, a, b, c, W_data[i + 13], k_sha512[i + 13]); \
SHA512_STEP(c, d, e, f, g, h, a, b, W_data[i + 14], k_sha512[i + 14]); \
SHA512_STEP(b, c, d, e, f, g, h, a, W_data[i + 15], k_sha512[i + 15]);}
static void sha256_process2(const uint32_t* W, uint32_t* digest) {
uint32_t a = digest[0];
uint32_t b = digest[1];
uint32_t c = digest[2];
uint32_t d = digest[3];
uint32_t e = digest[4];
uint32_t f = digest[5];
uint32_t g = digest[6];
uint32_t h = digest[7];
uint32_t w0_t = W[0];
uint32_t w1_t = W[1];
uint32_t w2_t = W[2];
uint32_t w3_t = W[3];
uint32_t w4_t = W[4];
uint32_t w5_t = W[5];
uint32_t w6_t = W[6];
uint32_t w7_t = W[7];
uint32_t w8_t = W[8];
uint32_t w9_t = W[9];
uint32_t wa_t = W[10];
uint32_t wb_t = W[11];
uint32_t wc_t = W[12];
uint32_t wd_t = W[13];
uint32_t we_t = W[14];
uint32_t wf_t = W[15];
#define ROUND_EXPAND() { w0_t = SHA256_EXPAND (we_t, w9_t, w1_t, w0_t); w1_t = SHA256_EXPAND (wf_t, wa_t, w2_t, w1_t); w2_t = SHA256_EXPAND (w0_t, wb_t, w3_t, w2_t); w3_t = SHA256_EXPAND (w1_t, wc_t, w4_t, w3_t); w4_t = SHA256_EXPAND (w2_t, wd_t, w5_t, w4_t); w5_t = SHA256_EXPAND (w3_t, we_t, w6_t, w5_t); w6_t = SHA256_EXPAND (w4_t, wf_t, w7_t, w6_t); w7_t = SHA256_EXPAND (w5_t, w0_t, w8_t, w7_t); w8_t = SHA256_EXPAND (w6_t, w1_t, w9_t, w8_t); w9_t = SHA256_EXPAND (w7_t, w2_t, wa_t, w9_t); wa_t = SHA256_EXPAND (w8_t, w3_t, wb_t, wa_t); wb_t = SHA256_EXPAND (w9_t, w4_t, wc_t, wb_t); wc_t = SHA256_EXPAND (wa_t, w5_t, wd_t, wc_t); wd_t = SHA256_EXPAND (wb_t, w6_t, we_t, wd_t); we_t = SHA256_EXPAND (wc_t, w7_t, wf_t, we_t); wf_t = SHA256_EXPAND (wd_t, w8_t, w0_t, wf_t); }
#define ROUND_STEP(i) { SHA256_STEP (SHAF0, SHAF1, a, b, c, d, e, f, g, h, w0_t, k_sha256[i + 0]); SHA256_STEP (SHAF0, SHAF1, h, a, b, c, d, e, f, g, w1_t, k_sha256[i + 1]); SHA256_STEP (SHAF0, SHAF1, g, h, a, b, c, d, e, f, w2_t, k_sha256[i + 2]); SHA256_STEP (SHAF0, SHAF1, f, g, h, a, b, c, d, e, w3_t, k_sha256[i + 3]); SHA256_STEP (SHAF0, SHAF1, e, f, g, h, a, b, c, d, w4_t, k_sha256[i + 4]); SHA256_STEP (SHAF0, SHAF1, d, e, f, g, h, a, b, c, w5_t, k_sha256[i + 5]); SHA256_STEP (SHAF0, SHAF1, c, d, e, f, g, h, a, b, w6_t, k_sha256[i + 6]); SHA256_STEP (SHAF0, SHAF1, b, c, d, e, f, g, h, a, w7_t, k_sha256[i + 7]); SHA256_STEP (SHAF0, SHAF1, a, b, c, d, e, f, g, h, w8_t, k_sha256[i + 8]); SHA256_STEP (SHAF0, SHAF1, h, a, b, c, d, e, f, g, w9_t, k_sha256[i + 9]); SHA256_STEP (SHAF0, SHAF1, g, h, a, b, c, d, e, f, wa_t, k_sha256[i + 10]); SHA256_STEP (SHAF0, SHAF1, f, g, h, a, b, c, d, e, wb_t, k_sha256[i + 11]); SHA256_STEP (SHAF0, SHAF1, e, f, g, h, a, b, c, d, wc_t, k_sha256[i + 12]); SHA256_STEP (SHAF0, SHAF1, d, e, f, g, h, a, b, c, wd_t, k_sha256[i + 13]); SHA256_STEP (SHAF0, SHAF1, c, d, e, f, g, h, a, b, we_t, k_sha256[i + 14]); SHA256_STEP (SHAF0, SHAF1, b, c, d, e, f, g, h, a, wf_t, k_sha256[i + 15]); }
ROUND_STEP(0);
ROUND_EXPAND();
ROUND_STEP(16);
ROUND_EXPAND();
ROUND_STEP(32);
ROUND_EXPAND();
ROUND_STEP(48);
digest[0] += a;
digest[1] += b;
digest[2] += c;