/
mtp.cl
1181 lines (949 loc) · 28.3 KB
/
mtp.cl
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
/**
* MTP
* djm34 2017-2018
* krnlx 2018
* djm34 2019
**/
#define NVIDIA_GPU 0
#ifdef cl_nv_pragma_unroll
#define NVIDIA
#undef NVIDIA_GPU
#define NVIDIA_GPU 1
#endif
#pragma OPENCL EXTENSION cl_clang_storage_class_specifiers : enable
typedef unsigned long uint64_t;
typedef uint uint32_t;
//typedef unsigned char uint8_t;
/*
__constant uint32_t *h_MinNonces[16]; // this need to get fixed as the rest of that routine
__constant uint32_t *d_MinNonces[16];
__constant uint32_t pTarget[8];
__constant uint32_t pData[20]; // truncated data
__constant uint4 Elements[1];
uint4 * HBlock[16];
uint32_t *Header[16];
uint2 *buffer_a[16];
*/
#define ARGON2_SYNC_POINTS 4
#define argon_outlen 32
#define argon_timecost 1
#define argon_memcost 4*1024*1024 // *1024 //32*1024*2 //1024*256*1 //2Gb
#define argon_lanes 4
#define argon_threads 1
#define argon_hashlen 80
#define argon_version 19
#define argon_type 0 // argon2d
#define argon_pwdlen 80 // hash and salt lenght
#define argon_default_flags 0 // hmm not sure
#define argon_segment_length argon_memcost/(argon_lanes * ARGON2_SYNC_POINTS)
#define argon_lane_length argon_segment_length * ARGON2_SYNC_POINTS
#define TREE_LEVELS 20
#define ELEM_MAX 2048
#define gpu_thread 2
#define gpu_shared 128
#define kernel1_thread 64
#define mtp_L 64
#define TPB52 32
#define TPB30 160
#define TPB20 160
#define _HIDWORD(x) as_uint2(x).y
#define _LODWORD(x) as_uint2(x).x
#define UINT64_C(x) (x ## UL)
#define devectorize(x) as_ulong(x)
#define vectorize(x) as_uint2(x)
static __constant const uint2 blakeInit[8] =
{
( 0xf2bdc948U, 0x6a09e667U ),
( 0x84caa73bU, 0xbb67ae85U ),
( 0xfe94f82bU, 0x3c6ef372U ),
( 0x5f1d36f1U, 0xa54ff53aU ),
( 0xade682d1U, 0x510e527fU ),
( 0x2b3e6c1fU, 0x9b05688cU ),
( 0xfb41bd6bU, 0x1f83d9abU ),
( 0x137e2179U, 0x5be0cd19U )
};
__constant const uint2 blakeFinal[8] =
{
( 0xf2bdc928U, 0x6a09e667U ),
( 0x84caa73bU, 0xbb67ae85U ),
( 0xfe94f82bU, 0x3c6ef372U ),
( 0x5f1d36f1U, 0xa54ff53aU ),
( 0xade682d1U, 0x510e527fU ),
( 0x2b3e6c1fU, 0x9b05688cU ),
( 0xfb41bd6bU, 0x1f83d9abU ),
( 0x137e2179U, 0x5be0cd19U )
};
__constant const uint2 blakeIV[8] =
{
( 0xf3bcc908U, 0x6a09e667U ),
( 0x84caa73bU, 0xbb67ae85U ),
( 0xfe94f82bU, 0x3c6ef372U ),
( 0x5f1d36f1U, 0xa54ff53aU ),
( 0xade682d1U, 0x510e527fU ),
( 0x2b3e6c1fU, 0x9b05688cU ),
( 0xfb41bd6bU, 0x1f83d9abU ),
( 0x137e2179U, 0x5be0cd19U )
};
__constant const uint2 blakeInit2[8] =
{
( 0xf2bdc918U, 0x6a09e667U ),
( 0x84caa73bU, 0xbb67ae85U ),
( 0xfe94f82bU, 0x3c6ef372U ),
( 0x5f1d36f1U, 0xa54ff53aU ),
( 0xade682d1U, 0x510e527fU ),
( 0x2b3e6c1fU, 0x9b05688cU ),
( 0xfb41bd6bU, 0x1f83d9abU ),
( 0x137e2179U, 0x5be0cd19U )
};
__constant static const uchar blake2b_sigma[12][16] =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
};
#define SPH_ROTL64(x, n) rotate(as_ulong(x), (n) & 0xFFFFFFFFFFFFFFFFUL)
#define SPH_ROTR64(x, n) SPH_ROTL64(x, (64 - (n)))
#if NVIDIA_GPU == 1
static inline uint64_t ROTR64X(const uint64_t x2, const int y) {
return rotate(x2, (ulong)(64 - y));
}
#else
static inline uint64_t ROTR64X(const uint64_t x2, const int y) {
uint2 x = as_uint2(x2);
if (y < 32) return(as_ulong(amd_bitalign(x.s10, x, y)));
else return(as_ulong(amd_bitalign(x, x.s10, (y - 32))));
}
#endif
static inline uint2 ROR2(uint2 v, unsigned a) {
uint2 result;
unsigned n = 64 - a;
if (n == 32) { return (uint2)(v.y, v.x); }
if (n < 32) {
result.y = ((v.y << (n)) | (v.x >> (32 - n)));
result.x = ((v.x << (n)) | (v.y >> (32 - n)));
}
else {
result.y = ((v.x << (n - 32)) | (v.y >> (64 - n)));
result.x = ((v.y << (n - 32)) | (v.x >> (64 - n)));
}
return result;
}
static inline uint2 ror2l(uint2 v, unsigned a) {
uint2 result;
result.y = ((v.x << (32 - a)) | (v.y >> (a)));
result.x = ((v.y << (32 - a)) | (v.x >> (a)));
return result;
}
static inline uint2 ror2r(uint2 v, unsigned a) {
uint2 result;
result.y = ((v.y << (64 - a)) | (v.x >> (a - 32)));
result.x = ((v.x << (64 - a)) | (v.y >> (a - 32)));
return result;
}
static inline uint2 eorswap32(uint2 u, uint2 v)
{
uint2 result;
result.y = u.x ^ v.x;
result.x = u.y ^ v.y;
return result;
}
static inline uint64_t eorswap64(uint64_t u, uint64_t v)
{
return ROTR64X(u^v, 32);
}
#define GS(a,b,c,d,e,f) \
{ \
v[a] += v[b] + m[e]; \
v[d] = eorswap64(v[d] , v[a]); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 24); \
v[a] += v[b] + m[f]; \
v[d] = ROTR64X(v[d] ^ v[a], 16); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 63); \
}
static inline int blake2b_compress2_256(uint64_t *hash, const uint64_t *hzcash, const uint64_t block[16], const uint32_t len)
{
uint64_t m[16];
uint64_t v[16];
const uint64_t blakeIV_[8] = {
0x6a09e667f3bcc908UL,
0xbb67ae8584caa73bUL,
0x3c6ef372fe94f82bUL,
0xa54ff53a5f1d36f1UL,
0x510e527fade682d1UL,
0x9b05688c2b3e6c1fUL,
0x1f83d9abfb41bd6bUL,
0x5be0cd19137e2179UL
};
#pragma unroll
for (int i = 0; i < 16; ++i)
m[i] = block[i];
#pragma unroll
for (int i = 0; i < 8; ++i)
v[i] = hzcash[i];
/*
v[8] = devectorize(blakeIV[0]);
v[9] = devectorize(blakeIV[1]);
v[10] = devectorize(blakeIV[2]);
v[11] = devectorize(blakeIV[3]);
v[12] = devectorize(blakeIV[4]);
v[12] ^= len;
v[13] = devectorize(blakeIV[5]);
v[14] = ~devectorize(blakeIV[6]);
v[15] = devectorize(blakeIV[7]);
*/
v[8] = blakeIV_[0];
v[9] = blakeIV_[1];
v[10] = blakeIV_[2];
v[11] = blakeIV_[3];
v[12] = blakeIV_[4];
v[12] ^= len;
v[13] = blakeIV_[5];
v[14] = ~blakeIV_[6];
v[15] = blakeIV_[7];
#define G(r,i,a,b,c,d) \
{ \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+0]]); \
v[d] = eorswap64(v[d] , v[a]); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 24); \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+1]]); \
v[d] = ROTR64X(v[d] ^ v[a], 16); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 63); \
}
#define ROUND(r) \
{ \
G(r,0, 0,4,8,12); \
G(r,1, 1,5,9,13); \
G(r,2, 2,6,10,14); \
G(r,3, 3,7,11,15); \
G(r,4, 0,5,10,15); \
G(r,5, 1,6,11,12); \
G(r,6, 2,7,8,13); \
G(r,7, 3,4,9,14); \
}
ROUND(0);
ROUND(1);
ROUND(2);
ROUND(3);
ROUND(4);
ROUND(5);
ROUND(6);
ROUND(7);
ROUND(8);
ROUND(9);
ROUND(10);
ROUND(11);
#pragma unroll
for (int i = 0; i < 4; ++i)
hash[i] = hzcash[i] ^ v[i] ^ v[i + 8];
#undef G
#undef ROUND
return 0;
}
static inline int blake2b_compress2c_256(uint64_t *hash, const uint64_t *hzcash, const uint64_t block[16], const uint32_t len)
{
uint64_t m[16];
uint64_t v[16];
#pragma unroll
// for (int i = 0; i < 16; ++i)
// m[i] = block[i];
for (int i = 0; i < 4; ++i)
m[i] = block[i];
for (int i = 4; i < 16; ++i)
m[i] = 0;
#pragma unroll
for (int i = 0; i < 8; ++i)
v[i] = hzcash[i];
v[8] = devectorize(blakeIV[0]);
v[9] = devectorize(blakeIV[1]);
v[10] = devectorize(blakeIV[2]);
v[11] = devectorize(blakeIV[3]);
v[12] = devectorize(blakeIV[4]);
v[12] ^= len;
v[13] = devectorize(blakeIV[5]);
v[14] = ~devectorize(blakeIV[6]);
v[15] = devectorize(blakeIV[7]);
#define G(r,i,a,b,c,d) \
{ \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+0]]); \
v[d] = eorswap64(v[d] , v[a]); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 24); \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+1]]); \
v[d] = ROTR64X(v[d] ^ v[a], 16); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 63); \
}
#define ROUND(r) \
{ \
G(r,0, 0,4,8,12); \
G(r,1, 1,5,9,13); \
G(r,2, 2,6,10,14); \
G(r,3, 3,7,11,15); \
G(r,4, 0,5,10,15); \
G(r,5, 1,6,11,12); \
G(r,6, 2,7,8,13); \
G(r,7, 3,4,9,14); \
}
ROUND(0);
ROUND(1);
ROUND(2);
ROUND(3);
ROUND(4);
ROUND(5);
ROUND(6);
ROUND(7);
ROUND(8);
ROUND(9);
ROUND(10);
ROUND(11);
#pragma unroll
for (int i = 0; i < 4; ++i)
hash[i] = hzcash[i] ^ v[i] ^ v[i + 8];
#undef G
#undef ROUND
return 0;
}
static inline int blake2b_compress4xv2(uint64_t *hash, const uint64_t *hzcash, const uint64_t block[16], const uint32_t len, int last)
{
uint64_t m[16];
uint64_t v[16];
const uint64_t blakeIV_[8] = {
0x6a09e667f3bcc908UL,
0xbb67ae8584caa73bUL,
0x3c6ef372fe94f82bUL,
0xa54ff53a5f1d36f1UL,
0x510e527fade682d1UL,
0x9b05688c2b3e6c1fUL,
0x1f83d9abfb41bd6bUL,
0x5be0cd19137e2179UL
};
for (int i = 0; i < 16; ++i)
m[i] = block[i];
for (int i = 0; i < 8; ++i)
v[i] = hzcash[i];
v[8] = blakeIV_[0];
v[9] = blakeIV_[1];
v[10] = blakeIV_[2];
v[11] = blakeIV_[3];
v[12] = blakeIV_[4];
v[12] ^= len;
v[13] = blakeIV_[5];
v[14] = last ? ~blakeIV_[6] : blakeIV_[6];
v[15] = blakeIV_[7];
#define G(r,i,a,b,c,d) \
{ \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+0]]); \
v[d] = eorswap64(v[d] , v[a]); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 24); \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+1]]); \
v[d] = ROTR64X(v[d] ^ v[a], 16); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 63); \
}
#define ROUND(r) \
{ \
G(r,0, 0,4,8,12); \
G(r,1, 1,5,9,13); \
G(r,2, 2,6,10,14); \
G(r,3, 3,7,11,15); \
G(r,4, 0,5,10,15); \
G(r,5, 1,6,11,12); \
G(r,6, 2,7,8,13); \
G(r,7, 3,4,9,14); \
}
ROUND(0);
ROUND(1);
ROUND(2);
ROUND(3);
for (int i = 0; i < 8; ++i)
hash[i] = hzcash[i] ^ v[i] ^ v[i + 8];
#undef G
#undef ROUND
return 0;
}
static inline int blake2b_compress2b(uint64_t *hzcash, const uint64_t * __restrict__ m, const uint32_t len, int last)
{
// uint64_t m[16];
uint64_t v[16];
const uint64_t blakeIV_[8] = {
0x6a09e667f3bcc908UL,
0xbb67ae8584caa73bUL,
0x3c6ef372fe94f82bUL,
0xa54ff53a5f1d36f1UL,
0x510e527fade682d1UL,
0x9b05688c2b3e6c1fUL,
0x1f83d9abfb41bd6bUL,
0x5be0cd19137e2179UL
};
/*
#pragma unroll
for (int i = 0; i < 16; ++i)
m[i] = block[i];
*/
#pragma unroll
for (int i = 0; i < 8; ++i)
v[i] = hzcash[i];
v[8] = blakeIV_[0];
v[9] = blakeIV_[1];
v[10] = blakeIV_[2];
v[11] = blakeIV_[3];
v[12] = blakeIV_[4];
v[12] ^= len;
v[13] = blakeIV_[5];
v[14] = last ? ~blakeIV_[6] : blakeIV_[6];
v[15] = blakeIV_[7];
/*
if(!thread){
printf("0x%llxULL\n", v[12]);
}*/
#define G(r,i,a,b,c,d) \
{ \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+0]]); \
v[d] = eorswap64(v[d] , v[a]); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 24); \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+1]]); \
v[d] = ROTR64X(v[d] ^ v[a], 16); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 63); \
}
#define ROUND(r) \
{ \
G(r,0, 0,4,8,12); \
G(r,1, 1,5,9,13); \
G(r,2, 2,6,10,14); \
G(r,3, 3,7,11,15); \
G(r,4, 0,5,10,15); \
G(r,5, 1,6,11,12); \
G(r,6, 2,7,8,13); \
G(r,7, 3,4,9,14); \
}
#define H(r,i,a,b,c,d) \
{ \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+0]]); \
v[d] = eorswap64(v[d] , v[a]); \
v[c] += v[d]; \
v[b] = ROTR64X(v[b] ^ v[c], 24); \
v[a] += v[b] + (m[blake2b_sigma[r][2*i+1]]); \
v[d] = ROTR64X(v[d] ^ v[a], 16); \
v[c] += v[d]; \
}
#define ROUNDF \
{ \
G(11,0, 0,4,8,12); \
G(11,1, 1,5,9,13); \
G(11,2, 2,6,10,14); \
G(11,3, 3,7,11,15); \
if(!last){\
G(11,4, 0,5,10,15); \
G(11,5, 1,6,11,12); \
G(11,6, 2,7,8,13); \
G(11,7, 3,4,9,14); \
}else{\
H(11,4, 0,5,10,15); \
H(11,5, 1,6,11,12); \
H(11,6, 2,7,8,13); \
H(11,7, 3,4,9,14); \
}\
}
ROUND(0);
ROUND(1);
ROUND(2);
ROUND(3);
ROUND(4);
ROUND(5);
ROUND(6);
ROUND(7);
ROUND(8);
ROUND(9);
ROUND(10);
ROUND(11);
// ROUNDF;
/*
ROUND0;
ROUND1;
ROUND2;
ROUND3;
ROUND4;
ROUND5;
ROUND6;
ROUND7;
ROUND8;
ROUND9;
ROUND10;
ROUND11;
*/
for (int i = 0; i < 8; ++i)
hzcash[i] ^= v[i] ^ v[i + 8];
#undef G
#undef ROUND
return 0;
}
#if PLATFORM == OPENCL_PLATFORM_NVIDIA && COMPUTE >= 35
static unsigned lane_id()
{
unsigned ret;
asm volatile ("mov.u32 %0, %laneid;" : "=r"(ret));
return ret;
}
static unsigned warp_id()
{
// this is not equal to threadIdx.x / 32
unsigned ret;
asm volatile ("mov.u32 %0, %warpid;" : "=r"(ret));
return ret;
}
#endif
#define LEN 8
#define DIV 256
//#define FARLOAD(x) far[warp][(x) + lane*(LEN+SHR_OFF)]
#define FARSTORE(x) far[warp][lane + (x)*(LEN+SHR_OFF)]
#define FARLOAD(x) FarReg[(x)]
#define SHR_OFF 0
#define TPB_MTP 64
__attribute__((reqd_work_group_size(TPB_MTP, 1, 1)))
__kernel void mtp_yloop(__global unsigned int* pData, __global const uint4 * __restrict__ DBlock, __global const uint4 * __restrict__ DBlock2,
__global uint4 * Elements, __global uint32_t * __restrict__ SmallestNonce, uint pTarget)
{
uint32_t NonceNumber = 1; // old
uint32_t ThreadNumber = 1;
uint32_t event_thread = get_global_id(0) - get_global_offset(0); //thread / ThreadNumber;
uint32_t NonceIterator = get_global_id(0);
int lane = get_local_id(0) % DIV;
int warp = get_local_id(0) / DIV;;//warp_id();
ulong2 FarReg[8];
uint32_t farIndex;
const uint32_t half_memcost = 2 * 1024 * 1024;
const uint64_t lblakeFinal[8] =
{
0x6a09e667f2bdc928UL,
0xbb67ae8584caa73bUL,
0x3c6ef372fe94f82bUL,
0xa54ff53a5f1d36f1UL,
0x510e527fade682d1UL,
0x9b05688c2b3e6c1fUL,
0x1f83d9abfb41bd6bUL,
0x5be0cd19137e2179UL,
};
__global const ulong2 * __restrict__ GBlock = &((__global ulong2*)DBlock)[0];
__global const ulong2 * __restrict__ GBlock2 = &((__global ulong2*)DBlock2)[0];
uint8 YLocal;
uint8 YLocalPrint;
ulong8 DataChunk[2] = { 0 };
((uint8 *)DataChunk)[0] = ((__global uint8 *)pData)[0];
((uint8 *)DataChunk)[1] = ((__global uint8 *)pData)[1];
((uint4*)DataChunk)[4] = ((__global uint4*)pData)[4];
((uint4*)DataChunk)[5] = ((__global uint4*)Elements)[0];
((uint16*)DataChunk)[1].hi.s0 = NonceIterator;
blake2b_compress2_256((uint64_t*)&YLocal, lblakeFinal, (uint64_t*)DataChunk, 100);
YLocalPrint = YLocal;
bool init_blocks;
uint32_t unmatch_block;
// uint32_t localIndex;
init_blocks = false;
unmatch_block = 0;
#pragma unroll 1
for (int j = 1; j <= mtp_L; j++)
{
// localIndex = YLocal.s0%(argon_memcost);
// localIndex = YLocal.s0 & 0x3FFFFF;
// uint64_t farIndex[8];
#pragma unroll
for (int t = 0; t<2; t++) {
ulong2 *D = (ulong2*)&YLocal;
FARLOAD(t + 6) = D[t];
}
farIndex = YLocal.s0 & 0x3FFFFF;
ulong8 DataChunk[2];
uint32_t len = 0;
uint16 DataTmp; uint2 * blake_init = (uint2*)&DataTmp;
for (int i = 0; i<8; i++)blake_init[i] = as_uint2(lblakeFinal[i]);
// uint8 part;
#pragma unroll 1
for (int i = 0; i < 9; i++) {
int last = (i == 8);
#pragma unroll
for (int t = 0; t<2; t++) {
ulong2 *D = (ulong2*)&YLocal;
D[t] = FARLOAD(t + 6);
}
barrier(CLK_LOCAL_MEM_FENCE);
len += last ? 32 : 128;
//if(!last)
{
#pragma unroll
for (int t = 0; t<8; t++) {
__global ulong2 *farP = (farIndex<half_memcost)? (__global ulong2*)&GBlock[farIndex * 64 + 0 + 8 * i + 0]
: (__global ulong2*)&GBlock2[(farIndex - half_memcost) * 64 + 0 + 8 * i + 0];
FarReg[t] = (last) ? (ulong2)(0, 0) : farP[t];
}
}
#pragma unroll
for (int t = 0; t<6; t++) {
ulong2 *D = (ulong2*)DataChunk;
D[t + 2] = (FARLOAD(t));
}
((uint16*)DataChunk)[0].lo = YLocal;
// uint16 DataTmp2;
blake2b_compress2b(/*(uint64_t*)&DataTmp2,*/ (uint64_t*)&DataTmp, (uint64_t*)DataChunk, len, last);
// DataTmp = DataTmp2;
}
YLocal = DataTmp.lo;
}
if (YLocal.s7 <= pTarget)
{
SmallestNonce[atomic_inc(SmallestNonce + 0xFF)] = NonceIterator;
}
}
static inline int blake2b_compress4x(uint2 *hash, const uint2 *hzcash, const uint2 block[16], const uint32_t len, int last)
{
uint2 m[16];
uint2 v[16];
const uint64_t blakeIV_[8] = {
0x6a09e667f3bcc908UL,
0xbb67ae8584caa73bUL,
0x3c6ef372fe94f82bUL,
0xa54ff53a5f1d36f1UL,
0x510e527fade682d1UL,
0x9b05688c2b3e6c1fUL,
0x1f83d9abfb41bd6bUL,
0x5be0cd19137e2179UL
};
for (int i = 0; i < 16; ++i)
m[i] = block[i];
for (int i = 0; i < 8; ++i)
v[i] = hzcash[i];
uint64_t xv = last ? (uint64_t)-1 : 0;
uint2 xv2 = as_uint2(xv);
v[8] = as_uint2(blakeIV_[0]);
v[9] = as_uint2(blakeIV_[1]);
v[10] = as_uint2(blakeIV_[2]);
v[11] = as_uint2(blakeIV_[3]);
v[12] = as_uint2(blakeIV_[4]);
v[12].x ^= len;
v[13] = as_uint2(blakeIV_[5]);
v[14] = last? as_uint2(~blakeIV_[6]) : as_uint2(blakeIV_[6]);
v[15] = as_uint2(blakeIV_[7]);
// uint64_t *d = (uint64_t*)v;
/*
#define G(r,i,a,b,c,d) \
{ \
v[a] += v[b] + m[blake2b_sigma[r][2*i+0]]; \
v[d] = eorswap32(v[d] , v[a]); \
v[c] += v[d]; \
v[b] = ROR2(v[b] ^ v[c], 24); \
v[a] += v[b] + m[blake2b_sigma[r][2*i+1]]; \
v[d] = ROR2(v[d] ^ v[a],16); \
v[c] += v[d]; \
v[b] = ROR2(v[b] ^ v[c], 63); \
}
*/
#define G(r,i,a,b,c,d) \
{ \
v[a] += v[b] + m[blake2b_sigma[r][2*i+0]]; \
v[d] = ROR2(v[d] ^ v[a],32); \
v[c] += v[d]; \
v[b] = ROR2(v[b] ^ v[c], 24); \
v[a] += v[b] + m[blake2b_sigma[r][2*i+1]]; \
v[d] = ROR2(v[d] ^ v[a],16); \
v[c] += v[d]; \
v[b] = ROR2(v[b] ^ v[c], 63); \
}
#define ROUND(r) \
{ \
G(r,0, 0,4,8,12); \
G(r,1, 1,5,9,13); \
G(r,2, 2,6,10,14); \
G(r,3, 3,7,11,15); \
G(r,4, 0,5,10,15); \
G(r,5, 1,6,11,12); \
G(r,6, 2,7,8,13); \
G(r,7, 3,4,9,14); \
}
ROUND(0);
ROUND(1);
ROUND(2);
ROUND(3);
for (int i = 0; i < 8; ++i)
hash[i] = hzcash[i] ^ v[i] ^ v[i + 8];
#undef G
#undef ROUND
return 0;
}
static inline uint32_t index_alpha(const uint32_t passs, const uint32_t slice, const uint32_t index,
uint32_t pseudo_rand,
int same_lane, const uint32_t ss, const uint32_t ss1) {
uint32_t reference_area_size;
uint64_t relative_position;
uint32_t start_position, absolute_position;
uint32_t lane_length = 1048576;
uint32_t segment_length = 262144;
uint32_t lanes = 4;
if (0 == passs) {
if (0 == slice) {
reference_area_size = index - 1;
} else {
if (same_lane) {
reference_area_size = ss + index - 1;
} else {
reference_area_size = ss - ((index == 0) ? 1 : 0);
}
}
} else {
if (same_lane) {
reference_area_size = lane_length - segment_length + index - 1;
}
else {
reference_area_size = lane_length - segment_length - ((index == 0) ? 1 : 0);
}
}
relative_position = pseudo_rand;
relative_position = (uint)((relative_position * relative_position) >> 32);
relative_position = reference_area_size - 1 - (uint)((reference_area_size * relative_position) >> 32);
start_position = 0;
if (0 != passs)
start_position = (slice == ARGON2_SYNC_POINTS - 1)? 0: (ss1);
absolute_position = (start_position + relative_position) & 0xFFFFF;
return absolute_position;
}
struct mem_blk {
uint64_t v[128];
};
/*__device__ __forceinline__*/ // void copy_block(__local struct mem_blk *dst, __local const struct mem_blk *src)
/*__device__ __forceinline__*/ uint64_t fBlaMka(uint64_t x, uint64_t y) {
// const uint64_t m = 0xFFFFFFFFUL;
// const uint64_t xy = ((uint64_t)_LODWORD(x) * (uint64_t)_LODWORD(y));
const uint64_t xy = (ulong)((uint)(x & 0xFFFFFFFFUL)) * (ulong)((uint)(y & 0xFFFFFFFFUL));
return x + y + 2 * xy;
}
static inline void fill_block_withIndex(__global const struct mem_blk *prev_block,__global const struct mem_blk *ref_block,
__global struct mem_blk *next_block, int with_xor, uint32_t block_header[8], uint32_t index) {
__local struct mem_blk blockR;
__local struct mem_blk block_tmp;
int tid = get_local_id(0);
uint32_t TheIndex[2] = { 0,index };
uint2 TheIndex2;
TheIndex2.x = 0;
TheIndex2.y = index;
unsigned i;
#define GBLOCK(a, b, c, d) \
{ \
a = fBlaMka(a, b); \
d = ROTR64X(d ^ a, 32); \
c = fBlaMka(c, d); \
b = ROTR64X(b ^ c, 24); \
a = fBlaMka(a, b); \
d = ROTR64X(d ^ a, 16); \
c = fBlaMka(c, d); \
b = ROTR64X(b ^ c, 63); \
}
// copy_block(blockR, ref_block);
blockR.v[tid] = ref_block->v[tid];
blockR.v[tid + 32] = ref_block->v[tid + 32];
blockR.v[tid + 64] = ref_block->v[tid + 64];
blockR.v[tid + 96] = ref_block->v[tid + 96];
// xor_block(blockR, prev_block);
blockR.v[tid] ^= prev_block->v[tid];
blockR.v[tid + 32] ^= prev_block->v[tid + 32];
blockR.v[tid + 64] ^= prev_block->v[tid + 64];
blockR.v[tid + 96] ^= prev_block->v[tid + 96];
// copy_block(block_tmp, blockR);
block_tmp.v[tid] = blockR.v[tid];
block_tmp.v[tid + 32] = blockR.v[tid + 32];
block_tmp.v[tid + 64] = blockR.v[tid + 64];
block_tmp.v[tid + 96] = blockR.v[tid + 96];
barrier(CLK_LOCAL_MEM_FENCE);
if (!tid)
blockR.v[14] = as_ulong(TheIndex2);
__local uint32_t *bl = (__local uint32_t*)&blockR.v[16];
if (!tid)
for (int i = 0; i<8; i++)
bl[i] = block_header[i];
barrier(CLK_LOCAL_MEM_FENCE);
{
int i = tid;
int y = (tid >> 2) << 4;
int x = tid & 3;
GBLOCK(blockR.v[y + x], blockR.v[y + 4 + x], blockR.v[y + 8 + x], blockR.v[y + 12 + x]);
GBLOCK(blockR.v[y + x], blockR.v[y + 4 + ((1 + x) & 3)], blockR.v[y + 8 + ((2 + x) & 3)], blockR.v[y + 12 + ((3 + x) & 3)]);
}
barrier(CLK_LOCAL_MEM_FENCE);
{
int i = tid;
int y = (tid >> 2) << 1;
int x = tid & 3;
int a = ((x) >> 1) * 16;
int b = x & 1;
int a1 = (((x + 1) & 3) >> 1) * 16;
int b1 = (x + 1) & 1;
int a2 = (((x + 2) & 3) >> 1) * 16;
int b2 = (x + 2) & 1;
int a3 = (((x + 3) & 3) >> 1) * 16;
int b3 = (x + 3) & 1;
GBLOCK(blockR.v[y + b + a], blockR.v[y + 32 + b + a], blockR.v[y + 64 + b + a], blockR.v[y + 96 + b + a]);
GBLOCK(blockR.v[y + b + a], blockR.v[y + 32 + b1 + a1], blockR.v[y + 64 + b2 + a2], blockR.v[y + 96 + a3 + b3]);
}
barrier(CLK_LOCAL_MEM_FENCE);
// xor_copy_block(next_block, &block_tmp, &blockR);
next_block->v[tid] = block_tmp.v[tid] ^ blockR.v[tid];
next_block->v[tid + 32] = block_tmp.v[tid + 32] ^ blockR.v[tid + 32];
next_block->v[tid + 64] = block_tmp.v[tid + 64] ^ blockR.v[tid + 64];
next_block->v[tid + 96] = block_tmp.v[tid + 96] ^ blockR.v[tid + 96];
#undef GBLOCK
}
//template <const uint32_t slice>
//__global__ __launch_bounds__(128, 1)
__attribute__((reqd_work_group_size(32, 1, 1)))
__kernel void mtp_i(__global uint4 * DBlock, __global uint4 * DBlock2, __global uint32_t *block_header, uint32_t slice) {
uint32_t prev_offset, curr_offset;