-
Notifications
You must be signed in to change notification settings - Fork 188
/
input.cl
834 lines (802 loc) · 25.8 KB
/
input.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
#include "param.h"
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
/*
** Assuming NR_ROWS_LOG == 16, the hash table slots have this layout (length in
** bytes in parens):
**
** round 0, table 0: cnt(4) i(4) pad(0) Xi(23.0) pad(1)
** round 1, table 1: cnt(4) i(4) pad(0.5) Xi(20.5) pad(3)
** round 2, table 0: cnt(4) i(4) i(4) pad(0) Xi(18.0) pad(2)
** round 3, table 1: cnt(4) i(4) i(4) pad(0.5) Xi(15.5) pad(4)
** round 4, table 0: cnt(4) i(4) i(4) i(4) pad(0) Xi(13.0) pad(3)
** round 5, table 1: cnt(4) i(4) i(4) i(4) pad(0.5) Xi(10.5) pad(5)
** round 6, table 0: cnt(4) i(4) i(4) i(4) i(4) pad(0) Xi( 8.0) pad(4)
** round 7, table 1: cnt(4) i(4) i(4) i(4) i(4) pad(0.5) Xi( 5.5) pad(6)
** round 8, table 0: cnt(4) i(4) i(4) i(4) i(4) i(4) pad(0) Xi( 3.0) pad(5)
**
** If the first byte of Xi is 0xAB then:
** - on even rounds, 'A' is part of the colliding PREFIX, 'B' is part of Xi
** - on odd rounds, 'A' and 'B' are both part of the colliding PREFIX, but
** 'A' is considered redundant padding as it was used to compute the row #
**
** - cnt is an atomic counter keeping track of the number of used slots.
** it is used in the first slot only; subsequent slots replace it with
** 4 padding bytes
** - i encodes either the 21-bit input value (round 0) or a reference to two
** inputs from the previous round
**
** Formula for Xi length and pad length above:
** > for i in range(9):
** > xi=(200-20*i-NR_ROWS_LOG)/8.; ci=8+4*((i)/2); print xi,32-ci-xi
**
** Note that the fractional .5-byte/4-bit padding following Xi for odd rounds
** is the 4 most significant bits of the last byte of Xi.
*/
__constant ulong blake_iv[] =
{
0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
0x510e527fade682d1, 0x9b05688c2b3e6c1f,
0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
};
/*
** Reset counters in hash table.
*/
__kernel
void kernel_init_ht(__global char *ht, __global uint *rowCounters)
{
rowCounters[get_global_id(0)] = 0;
}
/*
** If xi0,xi1,xi2,xi3 are stored consecutively in little endian then they
** represent (hex notation, group of 5 hex digits are a group of PREFIX bits):
** aa aa ab bb bb cc cc cd dd... [round 0]
** --------------------
** ...ab bb bb cc cc cd dd... [odd round]
** --------------
** ...cc cc cd dd... [next even round]
** -----
** Bytes underlined are going to be stored in the slot. Preceding bytes
** (and possibly part of the underlined bytes, depending on NR_ROWS_LOG) are
** used to compute the row number.
**
** Round 0: xi0,xi1,xi2,xi3 is a 25-byte Xi (xi3: only the low byte matter)
** Round 1: xi0,xi1,xi2 is a 23-byte Xi (incl. the colliding PREFIX nibble)
** TODO: update lines below with padding nibbles
** Round 2: xi0,xi1,xi2 is a 20-byte Xi (xi2: only the low 4 bytes matter)
** Round 3: xi0,xi1,xi2 is a 17.5-byte Xi (xi2: only the low 1.5 bytes matter)
** Round 4: xi0,xi1 is a 15-byte Xi (xi1: only the low 7 bytes matter)
** Round 5: xi0,xi1 is a 12.5-byte Xi (xi1: only the low 4.5 bytes matter)
** Round 6: xi0,xi1 is a 10-byte Xi (xi1: only the low 2 bytes matter)
** Round 7: xi0 is a 7.5-byte Xi (xi0: only the low 7.5 bytes matter)
** Round 8: xi0 is a 5-byte Xi (xi0: only the low 5 bytes matter)
**
** Return 0 if successfully stored, or 1 if the row overflowed.
*/
uint ht_store(uint round, __global char *ht, uint i,
ulong xi0, ulong xi1, ulong xi2, ulong xi3, __global uint *rowCounters)
{
uint row;
__global char *p;
uint cnt;
#if NR_ROWS_LOG == 16
if (!(round % 2))
row = (xi0 & 0xffff);
else
// if we have in hex: "ab cd ef..." (little endian xi0) then this
// formula computes the row as 0xdebc. it skips the 'a' nibble as it
// is part of the PREFIX. The Xi will be stored starting with "ef...";
// 'e' will be considered padding and 'f' is part of the current PREFIX
row = ((xi0 & 0xf00) << 4) | ((xi0 & 0xf00000) >> 12) |
((xi0 & 0xf) << 4) | ((xi0 & 0xf000) >> 12);
#elif NR_ROWS_LOG == 18
if (!(round % 2))
row = (xi0 & 0xffff) | ((xi0 & 0xc00000) >> 6);
else
row = ((xi0 & 0xc0000) >> 2) |
((xi0 & 0xf00) << 4) | ((xi0 & 0xf00000) >> 12) |
((xi0 & 0xf) << 4) | ((xi0 & 0xf000) >> 12);
#elif NR_ROWS_LOG == 19
if (!(round % 2))
row = (xi0 & 0xffff) | ((xi0 & 0xe00000) >> 5);
else
row = ((xi0 & 0xe0000) >> 1) |
((xi0 & 0xf00) << 4) | ((xi0 & 0xf00000) >> 12) |
((xi0 & 0xf) << 4) | ((xi0 & 0xf000) >> 12);
#elif NR_ROWS_LOG == 20
if (!(round % 2))
row = (xi0 & 0xffff) | ((xi0 & 0xf00000) >> 4);
else
row = ((xi0 & 0xf0000) >> 0) |
((xi0 & 0xf00) << 4) | ((xi0 & 0xf00000) >> 12) |
((xi0 & 0xf) << 4) | ((xi0 & 0xf000) >> 12);
#else
#error "unsupported NR_ROWS_LOG"
#endif
xi0 = (xi0 >> 16) | (xi1 << (64 - 16));
xi1 = (xi1 >> 16) | (xi2 << (64 - 16));
xi2 = (xi2 >> 16) | (xi3 << (64 - 16));
p = ht + row * NR_SLOTS * SLOT_LEN;
uint rowIdx = row/ROWS_PER_UINT;
uint rowOffset = BITS_PER_ROW*(row%ROWS_PER_UINT);
uint xcnt = atomic_add(rowCounters + rowIdx, 1 << rowOffset);
xcnt = (xcnt >> rowOffset) & ROW_MASK;
cnt = xcnt;
if (cnt >= NR_SLOTS)
{
// avoid overflows
atomic_sub(rowCounters + rowIdx, 1 << rowOffset);
return 1;
}
p += cnt * SLOT_LEN + xi_offset_for_round(round);
// store "i" (always 4 bytes before Xi)
*(__global uint *)(p - 4) = i;
if (round == 0 || round == 1)
{
// store 24 bytes
*(__global ulong *)(p + 0) = xi0;
*(__global ulong *)(p + 8) = xi1;
*(__global ulong *)(p + 16) = xi2;
}
else if (round == 2)
{
// store 20 bytes
*(__global uint *)(p + 0) = xi0;
*(__global ulong *)(p + 4) = (xi0 >> 32) | (xi1 << 32);
*(__global ulong *)(p + 12) = (xi1 >> 32) | (xi2 << 32);
}
else if (round == 3)
{
// store 16 bytes
*(__global uint *)(p + 0) = xi0;
*(__global ulong *)(p + 4) = (xi0 >> 32) | (xi1 << 32);
*(__global uint *)(p + 12) = (xi1 >> 32);
}
else if (round == 4)
{
// store 16 bytes
*(__global ulong *)(p + 0) = xi0;
*(__global ulong *)(p + 8) = xi1;
}
else if (round == 5)
{
// store 12 bytes
*(__global ulong *)(p + 0) = xi0;
*(__global uint *)(p + 8) = xi1;
}
else if (round == 6 || round == 7)
{
// store 8 bytes
*(__global uint *)(p + 0) = xi0;
*(__global uint *)(p + 4) = (xi0 >> 32);
}
else if (round == 8)
{
// store 4 bytes
*(__global uint *)(p + 0) = xi0;
}
return 0;
}
#define mix(va, vb, vc, vd, x, y) \
va = (va + vb + x); \
vd = rotate((vd ^ va), (ulong)64 - 32); \
vc = (vc + vd); \
vb = rotate((vb ^ vc), (ulong)64 - 24); \
va = (va + vb + y); \
vd = rotate((vd ^ va), (ulong)64 - 16); \
vc = (vc + vd); \
vb = rotate((vb ^ vc), (ulong)64 - 63);
/*
** Execute round 0 (blake).
**
** Note: making the work group size less than or equal to the wavefront size
** allows the OpenCL compiler to remove the barrier() calls, see "2.2 Local
** Memory (LDS) Optimization 2-10" in:
** http://developer.amd.com/tools-and-sdks/opencl-zone/amd-accelerated-parallel-processing-app-sdk/opencl-optimization-guide/
*/
__kernel __attribute__((reqd_work_group_size(64, 1, 1)))
void kernel_round0(__global ulong *blake_state, __global char *ht,
__global uint *rowCounters, __global uint *debug)
{
uint tid = get_global_id(0);
ulong v[16];
uint inputs_per_thread = NR_INPUTS / get_global_size(0);
uint input = tid * inputs_per_thread;
uint input_end = (tid + 1) * inputs_per_thread;
uint dropped = 0;
while (input < input_end)
{
// shift "i" to occupy the high 32 bits of the second ulong word in the
// message block
ulong word1 = (ulong)input << 32;
// init vector v
v[0] = blake_state[0];
v[1] = blake_state[1];
v[2] = blake_state[2];
v[3] = blake_state[3];
v[4] = blake_state[4];
v[5] = blake_state[5];
v[6] = blake_state[6];
v[7] = blake_state[7];
v[8] = blake_iv[0];
v[9] = blake_iv[1];
v[10] = blake_iv[2];
v[11] = blake_iv[3];
v[12] = blake_iv[4];
v[13] = blake_iv[5];
v[14] = blake_iv[6];
v[15] = blake_iv[7];
// mix in length of data
v[12] ^= ZCASH_BLOCK_HEADER_LEN + 4 /* length of "i" */;
// last block
v[14] ^= (ulong)-1;
// round 1
mix(v[0], v[4], v[8], v[12], 0, word1);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 2
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], word1, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 3
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, word1);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 4
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, word1);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 5
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, word1);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 6
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], word1, 0);
// round 7
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], word1, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 8
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, word1);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 9
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], word1, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 10
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], word1, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 11
mix(v[0], v[4], v[8], v[12], 0, word1);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], 0, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// round 12
mix(v[0], v[4], v[8], v[12], 0, 0);
mix(v[1], v[5], v[9], v[13], 0, 0);
mix(v[2], v[6], v[10], v[14], 0, 0);
mix(v[3], v[7], v[11], v[15], 0, 0);
mix(v[0], v[5], v[10], v[15], word1, 0);
mix(v[1], v[6], v[11], v[12], 0, 0);
mix(v[2], v[7], v[8], v[13], 0, 0);
mix(v[3], v[4], v[9], v[14], 0, 0);
// compress v into the blake state; this produces the 50-byte hash
// (two Xi values)
ulong h[7];
h[0] = blake_state[0] ^ v[0] ^ v[8];
h[1] = blake_state[1] ^ v[1] ^ v[9];
h[2] = blake_state[2] ^ v[2] ^ v[10];
h[3] = blake_state[3] ^ v[3] ^ v[11];
h[4] = blake_state[4] ^ v[4] ^ v[12];
h[5] = blake_state[5] ^ v[5] ^ v[13];
h[6] = (blake_state[6] ^ v[6] ^ v[14]) & 0xffff;
// store the two Xi values in the hash table
#if ZCASH_HASH_LEN == 50
dropped += ht_store(0, ht, input * 2,
h[0],
h[1],
h[2],
h[3], rowCounters);
dropped += ht_store(0, ht, input * 2 + 1,
(h[3] >> 8) | (h[4] << (64 - 8)),
(h[4] >> 8) | (h[5] << (64 - 8)),
(h[5] >> 8) | (h[6] << (64 - 8)),
(h[6] >> 8), rowCounters);
#else
#error "unsupported ZCASH_HASH_LEN"
#endif
input++;
}
#ifdef ENABLE_DEBUG
debug[tid * 2] = 0;
debug[tid * 2 + 1] = dropped;
#endif
}
#if NR_ROWS_LOG <= 16 && NR_SLOTS <= (1 << 8)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 16) | ((slot1 & 0xff) << 8) | (slot0 & 0xff))
#define DECODE_ROW(REF) (REF >> 16)
#define DECODE_SLOT1(REF) ((REF >> 8) & 0xff)
#define DECODE_SLOT0(REF) (REF & 0xff)
#elif NR_ROWS_LOG == 18 && NR_SLOTS <= (1 << 7)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 14) | ((slot1 & 0x7f) << 7) | (slot0 & 0x7f))
#define DECODE_ROW(REF) (REF >> 14)
#define DECODE_SLOT1(REF) ((REF >> 7) & 0x7f)
#define DECODE_SLOT0(REF) (REF & 0x7f)
#elif NR_ROWS_LOG == 19 && NR_SLOTS <= (1 << 6)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 13) | ((slot1 & 0x3f) << 6) | (slot0 & 0x3f)) /* 1 spare bit */
#define DECODE_ROW(REF) (REF >> 13)
#define DECODE_SLOT1(REF) ((REF >> 6) & 0x3f)
#define DECODE_SLOT0(REF) (REF & 0x3f)
#elif NR_ROWS_LOG == 20 && NR_SLOTS <= (1 << 6)
#define ENCODE_INPUTS(row, slot0, slot1) \
((row << 12) | ((slot1 & 0x3f) << 6) | (slot0 & 0x3f))
#define DECODE_ROW(REF) (REF >> 12)
#define DECODE_SLOT1(REF) ((REF >> 6) & 0x3f)
#define DECODE_SLOT0(REF) (REF & 0x3f)
#else
#error "unsupported NR_ROWS_LOG"
#endif
/*
** Access a half-aligned long, that is a long aligned on a 4-byte boundary.
*/
ulong half_aligned_long(__global ulong *p, uint offset)
{
return
(((ulong)*(__global uint *)((__global char *)p + offset + 0)) << 0) |
(((ulong)*(__global uint *)((__global char *)p + offset + 4)) << 32);
}
/*
** Access a well-aligned int.
*/
uint well_aligned_int(__global ulong *_p, uint offset)
{
__global char *p = (__global char *)_p;
return *(__global uint *)(p + offset);
}
/*
** XOR a pair of Xi values computed at "round - 1" and store the result in the
** hash table being built for "round". Note that when building the table for
** even rounds we need to skip 1 padding byte present in the "round - 1" table
** (the "0xAB" byte mentioned in the description at the top of this file.) But
** also note we can't load data directly past this byte because this would
** cause an unaligned memory access which is undefined per the OpenCL spec.
**
** Return 0 if successfully stored, or 1 if the row overflowed.
*/
uint xor_and_store(uint round, __global char *ht_dst, uint row,
uint slot_a, uint slot_b, __global ulong *a, __global ulong *b,
__global uint *rowCounters)
{
ulong xi0, xi1, xi2;
#if NR_ROWS_LOG >= 16 && NR_ROWS_LOG <= 20
// Note: for NR_ROWS_LOG == 20, for odd rounds, we could optimize by not
// storing the byte containing bits from the previous PREFIX block for
if (round == 1 || round == 2)
{
// xor 24 bytes
xi0 = *(a++) ^ *(b++);
xi1 = *(a++) ^ *(b++);
xi2 = *a ^ *b;
if (round == 2)
{
// skip padding byte
xi0 = (xi0 >> 8) | (xi1 << (64 - 8));
xi1 = (xi1 >> 8) | (xi2 << (64 - 8));
xi2 = (xi2 >> 8);
}
}
else if (round == 3)
{
// xor 20 bytes
xi0 = half_aligned_long(a, 0) ^ half_aligned_long(b, 0);
xi1 = half_aligned_long(a, 8) ^ half_aligned_long(b, 8);
xi2 = well_aligned_int(a, 16) ^ well_aligned_int(b, 16);
}
else if (round == 4 || round == 5)
{
// xor 16 bytes
xi0 = half_aligned_long(a, 0) ^ half_aligned_long(b, 0);
xi1 = half_aligned_long(a, 8) ^ half_aligned_long(b, 8);
xi2 = 0;
if (round == 4)
{
// skip padding byte
xi0 = (xi0 >> 8) | (xi1 << (64 - 8));
xi1 = (xi1 >> 8);
}
}
else if (round == 6)
{
// xor 12 bytes
xi0 = *a++ ^ *b++;
xi1 = *(__global uint *)a ^ *(__global uint *)b;
xi2 = 0;
if (round == 6)
{
// skip padding byte
xi0 = (xi0 >> 8) | (xi1 << (64 - 8));
xi1 = (xi1 >> 8);
}
}
else if (round == 7 || round == 8)
{
// xor 8 bytes
xi0 = half_aligned_long(a, 0) ^ half_aligned_long(b, 0);
xi1 = 0;
xi2 = 0;
if (round == 8)
{
// skip padding byte
xi0 = (xi0 >> 8);
}
}
// invalid solutions (which start happenning in round 5) have duplicate
// inputs and xor to zero, so discard them
if (!xi0 && !xi1)
return 0;
#else
#error "unsupported NR_ROWS_LOG"
#endif
return ht_store(round, ht_dst, ENCODE_INPUTS(row, slot_a, slot_b),
xi0, xi1, xi2, 0, rowCounters);
}
/*
** Execute one Equihash round. Read from ht_src, XOR colliding pairs of Xi,
** store them in ht_dst.
*/
void equihash_round(uint round,
__global char *ht_src,
__global char *ht_dst,
__global uint *debug,
__local uchar *first_words_data,
__local uint *collisionsData,
__local uint *collisionsNum,
__global uint *rowCountersSrc,
__global uint *rowCountersDst)
{
uint tid = get_global_id(0);
uint tlid = get_local_id(0);
__global char *p;
uint cnt;
__local uchar *first_words = &first_words_data[(NR_SLOTS+2)*tlid];
uchar mask;
uint i, j;
// NR_SLOTS is already oversized (by a factor of OVERHEAD), but we want to
// make it even larger
uint n;
uint dropped_coll = 0;
uint dropped_stor = 0;
__global ulong *a, *b;
uint xi_offset;
// read first words of Xi from the previous (round - 1) hash table
xi_offset = xi_offset_for_round(round - 1);
// the mask is also computed to read data from the previous round
#if NR_ROWS_LOG == 16
mask = ((!(round % 2)) ? 0x0f : 0xf0);
#elif NR_ROWS_LOG == 18
mask = ((!(round % 2)) ? 0x03 : 0x30);
#elif NR_ROWS_LOG == 19
mask = ((!(round % 2)) ? 0x01 : 0x10);
#elif NR_ROWS_LOG == 20
mask = 0; /* we can vastly simplify the code below */
#else
#error "unsupported NR_ROWS_LOG"
#endif
uint thCollNum = 0;
*collisionsNum = 0;
barrier(CLK_LOCAL_MEM_FENCE);
p = (ht_src + tid * NR_SLOTS * SLOT_LEN);
uint rowIdx = tid/ROWS_PER_UINT;
uint rowOffset = BITS_PER_ROW*(tid%ROWS_PER_UINT);
cnt = (rowCountersSrc[rowIdx] >> rowOffset) & ROW_MASK;
cnt = min(cnt, (uint)NR_SLOTS); // handle possible overflow in prev. round
if (!cnt)
// no elements in row, no collisions
goto part2;
p += xi_offset;
for (i = 0; i < cnt; i++, p += SLOT_LEN)
first_words[i] = (*(__global uchar *)p) & mask;
// find collisions
for (i = 0; i < cnt-1 && thCollNum < COLL_DATA_SIZE_PER_TH; i++)
{
uchar data_i = first_words[i];
uint collision = (tid << 10) | (i << 5) | (i + 1);
for (j = i+1; (j+4) < cnt;)
{
{
uint isColl = ((data_i == first_words[j]) ? 1 : 0);
if (isColl)
{
thCollNum++;
uint index = atomic_inc(collisionsNum);
collisionsData[index] = collision;
}
collision++;
j++;
}
{
uint isColl = ((data_i == first_words[j]) ? 1 : 0);
if (isColl)
{
thCollNum++;
uint index = atomic_inc(collisionsNum);
collisionsData[index] = collision;
}
collision++;
j++;
}
{
uint isColl = ((data_i == first_words[j]) ? 1 : 0);
if (isColl)
{
thCollNum++;
uint index = atomic_inc(collisionsNum);
collisionsData[index] = collision;
}
collision++;
j++;
}
{
uint isColl = ((data_i == first_words[j]) ? 1 : 0);
if (isColl)
{
thCollNum++;
uint index = atomic_inc(collisionsNum);
collisionsData[index] = collision;
}
collision++;
j++;
}
}
for (; j < cnt; j++)
{
uint isColl = ((data_i == first_words[j]) ? 1 : 0);
if (isColl)
{
thCollNum++;
uint index = atomic_inc(collisionsNum);
collisionsData[index] = collision;
}
collision++;
}
}
part2:
barrier(CLK_LOCAL_MEM_FENCE);
uint totalCollisions = *collisionsNum;
for (uint index = tlid; index < totalCollisions; index += get_local_size(0))
{
uint collision = collisionsData[index];
uint collisionThreadId = collision >> 10;
uint i = (collision >> 5) & 0x1F;
uint j = collision & 0x1F;
__global uchar *ptr = ht_src + collisionThreadId * NR_SLOTS * SLOT_LEN +
xi_offset;
a = (__global ulong *)(ptr + i * SLOT_LEN);
b = (__global ulong *)(ptr + j * SLOT_LEN);
dropped_stor += xor_and_store(round, ht_dst, collisionThreadId, i, j,
a, b, rowCountersDst);
}
#ifdef ENABLE_DEBUG
debug[tid * 2] = dropped_coll;
debug[tid * 2 + 1] = dropped_stor;
#endif
}
/*
** This defines kernel_round1, kernel_round2, ..., kernel_round7.
*/
#define KERNEL_ROUND(N) \
__kernel __attribute__((reqd_work_group_size(64, 1, 1))) \
void kernel_round ## N(__global char *ht_src, __global char *ht_dst, \
__global uint *rowCountersSrc, __global uint *rowCountersDst, \
__global uint *debug) \
{ \
__local uchar first_words_data[(NR_SLOTS+2)*64]; \
__local uint collisionsData[COLL_DATA_SIZE_PER_TH * 64]; \
__local uint collisionsNum; \
equihash_round(N, ht_src, ht_dst, debug, first_words_data, collisionsData, \
&collisionsNum, rowCountersSrc, rowCountersDst); \
}
KERNEL_ROUND(1)
KERNEL_ROUND(2)
KERNEL_ROUND(3)
KERNEL_ROUND(4)
KERNEL_ROUND(5)
KERNEL_ROUND(6)
KERNEL_ROUND(7)
// kernel_round8 takes an extra argument, "sols"
__kernel __attribute__((reqd_work_group_size(64, 1, 1)))
void kernel_round8(__global char *ht_src, __global char *ht_dst,
__global uint *rowCountersSrc, __global uint *rowCountersDst,
__global uint *debug, __global sols_t *sols)
{
uint tid = get_global_id(0);
__local uchar first_words_data[(NR_SLOTS+2)*64];
__local uint collisionsData[COLL_DATA_SIZE_PER_TH * 64];
__local uint collisionsNum;
equihash_round(8, ht_src, ht_dst, debug, first_words_data, collisionsData,
&collisionsNum, rowCountersSrc, rowCountersDst);
if (!tid)
sols->nr = sols->likely_invalids = 0;
}
uint expand_ref(__global char *ht, uint xi_offset, uint row, uint slot)
{
return *(__global uint *)(ht + row * NR_SLOTS * SLOT_LEN +
slot * SLOT_LEN + xi_offset - 4);
}
/*
** Expand references to inputs. Return 1 if so far the solution appears valid,
** or 0 otherwise (an invalid solution would be a solution with duplicate
** inputs, which can be detected at the last step: round == 0).
*/
uint expand_refs(uint *ins, uint nr_inputs, __global char **htabs,
uint round)
{
__global char *ht = htabs[round % 2];
uint i = nr_inputs - 1;
uint j = nr_inputs * 2 - 1;
uint xi_offset = xi_offset_for_round(round);
int dup_to_watch = -1;
do
{
ins[j] = expand_ref(ht, xi_offset,
DECODE_ROW(ins[i]), DECODE_SLOT1(ins[i]));
ins[j - 1] = expand_ref(ht, xi_offset,
DECODE_ROW(ins[i]), DECODE_SLOT0(ins[i]));
if (!round)
{
if (dup_to_watch == -1)
dup_to_watch = ins[j];
else if (ins[j] == dup_to_watch || ins[j - 1] == dup_to_watch)
return 0;
}
if (!i)
break ;
i--;
j -= 2;
}
while (1);
return 1;
}
/*
** Verify if a potential solution is in fact valid.
*/
void potential_sol(__global char **htabs, __global sols_t *sols,
uint ref0, uint ref1)
{
uint nr_values;
uint values_tmp[(1 << PARAM_K)];
uint sol_i;
uint i;
nr_values = 0;
values_tmp[nr_values++] = ref0;
values_tmp[nr_values++] = ref1;
uint round = PARAM_K - 1;
do
{
round--;
if (!expand_refs(values_tmp, nr_values, htabs, round))
return ;
nr_values *= 2;
}
while (round > 0);
// solution appears valid, copy it to sols
sol_i = atomic_inc(&sols->nr);
if (sol_i >= MAX_SOLS)
return ;
for (i = 0; i < (1 << PARAM_K); i++)
sols->values[sol_i][i] = values_tmp[i];
sols->valid[sol_i] = 1;
}
/*
** Scan the hash tables to find Equihash solutions.
*/
__kernel __attribute__((reqd_work_group_size(64, 1, 1)))
void kernel_sols(__global char *ht0, __global char *ht1, __global sols_t *sols,
__global uint *rowCountersSrc, __global uint *rowCountersDst)
{
uint tid = get_global_id(0);
__global char *htabs[2] = { ht0, ht1 };
__global char *hcounters[2] = { rowCountersSrc, rowCountersDst };
uint ht_i = (PARAM_K - 1) % 2; // table filled at last round
uint cnt;
uint xi_offset = xi_offset_for_round(PARAM_K - 1);
uint i, j;
__global char *a, *b;
uint ref_i, ref_j;
// it's ok for the collisions array to be so small, as if it fills up
// the potential solutions are likely invalid (many duplicate inputs)
ulong collisions;
uint coll;
#if NR_ROWS_LOG >= 16 && NR_ROWS_LOG <= 20
// in the final hash table, we are looking for a match on both the bits
// part of the previous PREFIX colliding bits, and the last PREFIX bits.
uint mask = 0xffffff;
#else
#error "unsupported NR_ROWS_LOG"
#endif
a = htabs[ht_i] + tid * NR_SLOTS * SLOT_LEN;
uint rowIdx = tid/ROWS_PER_UINT;
uint rowOffset = BITS_PER_ROW*(tid%ROWS_PER_UINT);
cnt = (rowCountersSrc[rowIdx] >> rowOffset) & ROW_MASK;
cnt = min(cnt, (uint)NR_SLOTS); // handle possible overflow in last round
coll = 0;
a += xi_offset;
for (i = 0; i < cnt; i++, a += SLOT_LEN)
{
uint a_data = ((*(__global uint *)a) & mask);
ref_i = *(__global uint *)(a - 4);
for (j = i + 1, b = a + SLOT_LEN; j < cnt; j++, b += SLOT_LEN)
{
if (a_data == ((*(__global uint *)b) & mask))
{
ref_j = *(__global uint *)(b - 4);
collisions = ((ulong)ref_i << 32) | ref_j;
goto exit1;
}
}
}
return;
exit1:
potential_sol(htabs, sols, collisions >> 32, collisions & 0xffffffff);
}