This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rescue_prime.c
883 lines (731 loc) · 31.1 KB
/
rescue_prime.c
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
#include <rescue_prime.h>
#include <rescue_prime_constants.h>
cl_int bench_hash_elements(cl_context ctx, cl_command_queue cq, cl_kernel krnl,
size_t glb_sz_x, size_t glb_sz_y, size_t loc_sz_x,
size_t loc_sz_y) {
cl_int status;
const size_t in_width = 8ul;
const size_t out_width = 4ul;
const size_t in_size = glb_sz_x * glb_sz_y * in_width * sizeof(cl_ulong);
const size_t out_size = glb_sz_x * glb_sz_y * out_width * sizeof(cl_ulong);
cl_ulong *in_arr = malloc(in_size);
cl_ulong *out_arr = malloc(out_size);
random_field_elements(in_arr, in_size / sizeof(cl_ulong));
cl_mem in_buf = clCreateBuffer(ctx, CL_MEM_READ_ONLY, in_size, NULL, &status);
check(status);
cl_mem in_size_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(size_t), NULL, &status);
check(status);
cl_mem mds_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(MDS), NULL, &status);
check(status);
cl_mem ark1_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK1), NULL, &status);
check(status);
cl_mem ark2_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK2), NULL, &status);
check(status);
cl_mem out_buf =
clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, out_size, NULL, &status);
check(status);
status = clSetKernelArg(krnl, 0, sizeof(cl_mem), &in_buf);
check(status);
status = clSetKernelArg(krnl, 1, sizeof(cl_mem), &in_size_buf);
check(status);
status = clSetKernelArg(krnl, 2, sizeof(cl_mem), &mds_buf);
check(status);
status = clSetKernelArg(krnl, 3, sizeof(cl_mem), &ark1_buf);
check(status);
status = clSetKernelArg(krnl, 4, sizeof(cl_mem), &ark2_buf);
check(status);
status = clSetKernelArg(krnl, 5, sizeof(cl_mem), &out_buf);
check(status);
cl_event evt_0;
status = clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0, in_size, in_arr, 0,
NULL, &evt_0);
check(status);
cl_event evt_1;
status = clEnqueueWriteBuffer(cq, in_size_buf, CL_FALSE, 0, sizeof(size_t),
&in_width, 0, NULL, &evt_1);
check(status);
cl_event evt_2;
status = clEnqueueWriteBuffer(cq, mds_buf, CL_FALSE, 0, sizeof(MDS), MDS, 0,
NULL, &evt_2);
check(status);
cl_event evt_3;
status = clEnqueueWriteBuffer(cq, ark1_buf, CL_FALSE, 0, sizeof(ARK1), ARK1,
0, NULL, &evt_3);
check(status);
cl_event evt_4;
status = clEnqueueWriteBuffer(cq, ark2_buf, CL_FALSE, 0, sizeof(ARK2), ARK2,
0, NULL, &evt_4);
check(status);
size_t global_size[] = {glb_sz_x, glb_sz_y};
size_t local_size[] = {loc_sz_x, loc_sz_y};
cl_event evts[] = {evt_0, evt_1, evt_2, evt_3, evt_4};
cl_event evt_5;
status = clEnqueueNDRangeKernel(cq, krnl, 2, NULL, global_size, local_size, 5,
evts, &evt_5);
check(status);
cl_event evt_6;
status = clEnqueueReadBuffer(cq, out_buf, CL_FALSE, 0, out_size, out_arr, 1,
&evt_5, &evt_6);
check(status);
status = clWaitForEvents(1, &evt_6);
check(status);
cl_ulong start, end;
status = clGetEventProfilingInfo(evt_5, CL_PROFILING_COMMAND_START,
sizeof(cl_ulong), &start, NULL);
check(status);
status = clGetEventProfilingInfo(evt_5, CL_PROFILING_COMMAND_END,
sizeof(cl_ulong), &end, NULL);
check(status);
// kernel execution time in nanoseconds, obtained
// by enabling profiling in command queue
double ts = (double)(end - start);
printf("%15s\t\t%5lu x %5lu\t\t%20.2f ms\t\t%15.2f hashes/ sec\n",
"hash_elements", glb_sz_x, glb_sz_y, ts * 1e-6,
((double)(glb_sz_x * glb_sz_y) / (double)ts) * 1e9);
status = clReleaseEvent(evt_0);
check(status);
status = clReleaseEvent(evt_1);
check(status);
status = clReleaseEvent(evt_2);
check(status);
status = clReleaseEvent(evt_3);
check(status);
status = clReleaseEvent(evt_4);
check(status);
status = clReleaseEvent(evt_5);
check(status);
status = clReleaseEvent(evt_6);
check(status);
status = clReleaseMemObject(in_buf);
check(status);
status = clReleaseMemObject(in_size_buf);
check(status);
status = clReleaseMemObject(mds_buf);
check(status);
status = clReleaseMemObject(ark1_buf);
check(status);
status = clReleaseMemObject(ark2_buf);
check(status);
status = clReleaseMemObject(out_buf);
check(status);
free(in_arr);
free(out_arr);
return CL_SUCCESS;
}
cl_int bench_merge(cl_context ctx, cl_command_queue cq, cl_kernel krnl,
size_t global_size_x, size_t global_size_y,
size_t local_size_x, size_t local_size_y) {
cl_int status;
const size_t in_width = 8ul;
const size_t out_width = 4ul;
const size_t in_size =
global_size_x * global_size_y * in_width * sizeof(cl_ulong);
const size_t out_size =
global_size_x * global_size_y * out_width * sizeof(cl_ulong);
cl_ulong *in_arr = malloc(in_size);
cl_ulong *out_arr = malloc(out_size);
random_field_elements(in_arr, in_size / sizeof(cl_ulong));
cl_mem in_buf = clCreateBuffer(ctx, CL_MEM_READ_ONLY, in_size, NULL, &status);
check(status);
cl_mem mds_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(MDS), NULL, &status);
check(status);
cl_mem ark1_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK1), NULL, &status);
check(status);
cl_mem ark2_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK2), NULL, &status);
check(status);
cl_mem out_buf =
clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, out_size, NULL, &status);
check(status);
status = clSetKernelArg(krnl, 0, sizeof(cl_mem), &in_buf);
check(status);
status = clSetKernelArg(krnl, 1, sizeof(cl_mem), &mds_buf);
check(status);
status = clSetKernelArg(krnl, 2, sizeof(cl_mem), &ark1_buf);
check(status);
status = clSetKernelArg(krnl, 3, sizeof(cl_mem), &ark2_buf);
check(status);
status = clSetKernelArg(krnl, 4, sizeof(cl_mem), &out_buf);
check(status);
cl_event evt_0;
status = clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0, in_size, in_arr, 0,
NULL, &evt_0);
check(status);
cl_event evt_1;
status = clEnqueueWriteBuffer(cq, mds_buf, CL_FALSE, 0, sizeof(MDS), MDS, 0,
NULL, &evt_1);
check(status);
cl_event evt_2;
status = clEnqueueWriteBuffer(cq, ark1_buf, CL_FALSE, 0, sizeof(ARK1), ARK1,
0, NULL, &evt_2);
check(status);
cl_event evt_3;
status = clEnqueueWriteBuffer(cq, ark2_buf, CL_FALSE, 0, sizeof(ARK2), ARK2,
0, NULL, &evt_3);
check(status);
size_t global_size[] = {global_size_x, global_size_y};
size_t local_size[] = {local_size_x, local_size_y};
cl_event evts[] = {evt_0, evt_1, evt_2, evt_3};
cl_event evt_4;
status = clEnqueueNDRangeKernel(cq, krnl, 2, NULL, global_size, local_size, 4,
evts, &evt_4);
check(status);
cl_event evt_5;
status = clEnqueueReadBuffer(cq, out_buf, CL_FALSE, 0, out_size, out_arr, 1,
&evt_4, &evt_5);
check(status);
status = clWaitForEvents(1, &evt_5);
check(status);
cl_ulong start, end;
status = clGetEventProfilingInfo(evt_5, CL_PROFILING_COMMAND_START,
sizeof(cl_ulong), &start, NULL);
check(status);
status = clGetEventProfilingInfo(evt_5, CL_PROFILING_COMMAND_END,
sizeof(cl_ulong), &end, NULL);
check(status);
// kernel execution time in nanoseconds, obtained
// by enabling profiling in command queue
//
// make sure
// https://github.com/itzmeanjan/vectorized-rescue-prime/blob/54df2cd08de2e3d56c7a6e0202981c489ff0ee63/main.c#L35-L44
// stays as it's
double ts = (double)(end - start);
printf("%15s\t\t%5lu x %5lu\t\t%20.2f ms\t\t%15.2f merges/ sec\n", "merge",
global_size_x, global_size_y, ts * 1e-6,
((double)(global_size_x * global_size_y) / (double)ts) * 1e9);
status = clReleaseEvent(evt_0);
check(status);
status = clReleaseEvent(evt_1);
check(status);
status = clReleaseEvent(evt_2);
check(status);
status = clReleaseEvent(evt_3);
check(status);
status = clReleaseEvent(evt_4);
check(status);
status = clReleaseEvent(evt_5);
check(status);
status = clReleaseMemObject(in_buf);
check(status);
status = clReleaseMemObject(mds_buf);
check(status);
status = clReleaseMemObject(ark1_buf);
check(status);
status = clReleaseMemObject(ark2_buf);
check(status);
status = clReleaseMemObject(out_buf);
check(status);
free(in_arr);
free(out_arr);
return CL_SUCCESS;
}
cl_int test_apply_sbox(cl_context ctx, cl_command_queue cq, cl_kernel krnl) {
cl_int status;
uint64_t in_arr[16] = {1ull << 10, 1ull << 11, 1ull << 12, 1ull << 13,
1ull << 20, 1ull << 21, 1ull << 22, 1ull << 23,
1ull << 60, 1ull << 61, 1ull << 62, 1ull << 63,
0ull, 0ull, 0ull, 0ull};
uint64_t out_arr[16] = {0ull};
uint64_t exp_out_arr[16] = {274877906880ull,
35184372080640ull,
4503599626321920ull,
576460752169205760ull,
18446726477228539905ull,
18444492269600899073ull,
18158513693262872577ull,
18446744060824649731ull,
68719476736ull,
8796093022208ull,
1125899906842624ull,
144115188075855872ull,
0ull,
0ull,
0ull,
0ull};
cl_mem in_buf = clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(cl_ulong) * 16,
NULL, &status);
cl_mem out_buf = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, sizeof(cl_ulong) * 16,
NULL, &status);
status = clSetKernelArg(krnl, 0, sizeof(cl_mem), &in_buf);
status = clSetKernelArg(krnl, 1, sizeof(cl_mem), &out_buf);
cl_event evt_0;
status = clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0, sizeof(in_arr), in_arr,
0, NULL, &evt_0);
size_t global_size[] = {1};
size_t local_size[] = {1};
cl_event evt_1;
status = clEnqueueNDRangeKernel(cq, krnl, 1, NULL, global_size, local_size, 1,
&evt_0, &evt_1);
cl_event evt_2;
status = clEnqueueReadBuffer(cq, out_buf, CL_FALSE, 0, sizeof(out_arr),
out_arr, 1, &evt_1, &evt_2);
status = clWaitForEvents(1, &evt_2);
for (size_t i = 0; i < 16; i++) {
assert(out_arr[i] == exp_out_arr[i]);
}
clReleaseEvent(evt_0);
clReleaseEvent(evt_1);
clReleaseEvent(evt_2);
clReleaseMemObject(in_buf);
clReleaseMemObject(out_buf);
printf("passed apply_sbox tests !\n");
return status;
}
cl_int test_apply_inv_sbox(cl_context ctx, cl_command_queue cq,
cl_kernel krnl) {
cl_int status;
uint64_t in_arr[16] = {1ull << 10, 1ull << 11, 1ull << 12, 1ull << 13,
1ull << 20, 1ull << 21, 1ull << 22, 1ull << 23,
1ull << 60, 1ull << 61, 1ull << 62, 1ull << 63,
0ull, 0ull, 0ull, 0ull};
uint64_t out_arr[16] = {0ull};
uint64_t exp_out_arr[16] = {18446743794536677441ull,
536870912ull,
4503599626321920ull,
18446735273321562113ull,
18446726477228539905ul,
8ull,
288230376151711744ull,
18446744069414453249ull,
68719476736ull,
576460752169205760ull,
18445618169507741697ull,
512ull,
0ull,
0ull,
0ull,
0ull};
cl_mem in_buf = clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(cl_ulong) * 16,
NULL, &status);
cl_mem out_buf = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, sizeof(cl_ulong) * 16,
NULL, &status);
status = clSetKernelArg(krnl, 0, sizeof(cl_mem), &in_buf);
status = clSetKernelArg(krnl, 1, sizeof(cl_mem), &out_buf);
cl_event evt_0;
status = clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0, sizeof(in_arr), in_arr,
0, NULL, &evt_0);
size_t global_size[] = {1};
size_t local_size[] = {1};
cl_event evt_1;
status = clEnqueueNDRangeKernel(cq, krnl, 1, NULL, global_size, local_size, 1,
&evt_0, &evt_1);
cl_event evt_2;
status = clEnqueueReadBuffer(cq, out_buf, CL_FALSE, 0, sizeof(out_arr),
out_arr, 1, &evt_1, &evt_2);
status = clWaitForEvents(1, &evt_2);
for (size_t i = 0; i < 16; i++) {
assert(out_arr[i] % MOD == exp_out_arr[i]);
}
clReleaseEvent(evt_0);
clReleaseEvent(evt_1);
clReleaseEvent(evt_2);
clReleaseMemObject(in_buf);
clReleaseMemObject(out_buf);
printf("passed apply_inv_sbox tests !\n");
return status;
}
cl_int test_apply_rescue_permutation(cl_context ctx, cl_command_queue cq,
cl_kernel krnl) {
cl_int status;
uint64_t in_arr[16] = {0ull, 1ull, 2ull, 3ull, 4ull, 5ull, 6ull, 7ull,
8ull, 9ull, 10ull, 11ull, 0ull, 0ull, 0ull, 0ull};
uint64_t out_arr[16] = {0ull};
uint64_t exp_out_arr[16] = {10809974140050983728ull,
6938491977181280539ull,
8834525837561071698ull,
6854417192438540779ull,
4476630872663101667ull,
6292749486700362097ull,
18386622366690620454ull,
10614098972800193173ull,
7543273285584849722ull,
9490898458612615694ull,
9030271581669113292ull,
10101107035874348250ull,
0ull,
0ull,
0ull,
0ull};
cl_mem in_buf = clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(cl_ulong) * 16,
NULL, &status);
cl_mem out_buf = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, sizeof(cl_ulong) * 16,
NULL, &status);
cl_mem mds_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(MDS), NULL, &status);
cl_mem ark1_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK1), NULL, &status);
cl_mem ark2_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK2), NULL, &status);
status = clSetKernelArg(krnl, 0, sizeof(cl_mem), &in_buf);
status = clSetKernelArg(krnl, 1, sizeof(cl_mem), &out_buf);
status = clSetKernelArg(krnl, 2, sizeof(cl_mem), &mds_buf);
status = clSetKernelArg(krnl, 3, sizeof(cl_mem), &ark1_buf);
status = clSetKernelArg(krnl, 4, sizeof(cl_mem), &ark2_buf);
cl_event evt_0;
status = clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0, sizeof(in_arr), in_arr,
0, NULL, &evt_0);
cl_event evt_1;
status = clEnqueueWriteBuffer(cq, mds_buf, CL_FALSE, 0, sizeof(MDS), MDS, 0,
NULL, &evt_1);
cl_event evt_2;
status = clEnqueueWriteBuffer(cq, ark1_buf, CL_FALSE, 0, sizeof(ARK1), ARK1,
0, NULL, &evt_2);
cl_event evt_3;
status = clEnqueueWriteBuffer(cq, ark2_buf, CL_FALSE, 0, sizeof(ARK2), ARK2,
0, NULL, &evt_3);
// creating dependency in compute pipeline !
cl_event evts[] = {evt_0, evt_1, evt_2, evt_3};
size_t global_size[] = {1};
size_t local_size[] = {1};
cl_event evt_4;
status = clEnqueueNDRangeKernel(cq, krnl, 1, NULL, global_size, local_size, 4,
evts, &evt_4);
cl_event evt_5;
status = clEnqueueReadBuffer(cq, out_buf, CL_FALSE, 0, sizeof(out_arr),
out_arr, 1, &evt_4, &evt_5);
status = clWaitForEvents(1, &evt_5);
for (size_t i = 0; i < 16; i++) {
assert(out_arr[i] % MOD == exp_out_arr[i] % MOD);
}
clReleaseEvent(evt_0);
clReleaseEvent(evt_1);
clReleaseEvent(evt_2);
clReleaseEvent(evt_3);
clReleaseEvent(evt_4);
clReleaseEvent(evt_5);
clReleaseMemObject(in_buf);
clReleaseMemObject(out_buf);
clReleaseMemObject(mds_buf);
clReleaseMemObject(ark1_buf);
clReleaseMemObject(ark2_buf);
printf("passed apply_rescue_permutation tests !\n");
return status;
}
cl_int test_apply_mds(cl_context ctx, cl_command_queue cq, cl_kernel krnl) {
cl_int status;
uint64_t in_arr[16] = {0ull, 1ull, 2ull, 3ull, 4ull, 5ull, 6ull, 7ull,
8ull, 9ull, 10ull, 11ull, 0ull, 0ull, 0ull, 0ull};
uint64_t out_arr[16] = {0ull};
uint64_t exp_out_arr[16] = {8268579649362235275ull,
2236502997719307940ull,
4445585223683938180ull,
8490351819144058838ull,
17912450758129541069ull,
12381447012212465193ull,
6444916863184583255ull,
5403602327365240081ull,
7656905977925454065ull,
12880871053868334997ull,
13669293285556299269ull,
2401034710645280649ull,
0ull,
0ull,
0ull,
0ull};
cl_mem in_buf = clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(cl_ulong) * 16,
NULL, &status);
cl_mem out_buf = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, sizeof(cl_ulong) * 16,
NULL, &status);
cl_mem mds_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(MDS), NULL, &status);
status = clSetKernelArg(krnl, 0, sizeof(cl_mem), &in_buf);
status = clSetKernelArg(krnl, 1, sizeof(cl_mem), &out_buf);
status = clSetKernelArg(krnl, 2, sizeof(cl_mem), &mds_buf);
cl_event evt_0;
status = clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0, sizeof(in_arr), in_arr,
0, NULL, &evt_0);
cl_event evt_1;
status = clEnqueueWriteBuffer(cq, mds_buf, CL_FALSE, 0, sizeof(MDS), MDS, 0,
NULL, &evt_1);
// creating dependency in compute pipeline !
cl_event evts[] = {evt_0, evt_1};
size_t global_size[] = {1};
size_t local_size[] = {1};
cl_event evt_2;
status = clEnqueueNDRangeKernel(cq, krnl, 1, NULL, global_size, local_size, 2,
evts, &evt_2);
cl_event evt_3;
status = clEnqueueReadBuffer(cq, out_buf, CL_FALSE, 0, sizeof(out_arr),
out_arr, 1, &evt_2, &evt_3);
status = clWaitForEvents(1, &evt_3);
for (size_t i = 0; i < 16; i++) {
assert(out_arr[i] % MOD == exp_out_arr[i] % MOD);
}
clReleaseEvent(evt_0);
clReleaseEvent(evt_1);
clReleaseEvent(evt_2);
clReleaseEvent(evt_3);
clReleaseMemObject(in_buf);
clReleaseMemObject(out_buf);
clReleaseMemObject(mds_buf);
printf("passed apply_mds tests !\n");
return status;
}
cl_int test_reduce_sum_vec2(cl_context ctx, cl_command_queue cq,
cl_kernel krnl) {
cl_int status;
uint64_t in_arr[16] = {1ull << 10, 1ull << 11, 1ull << 12, 1ull << 13,
1ull << 20, 1ull << 21, 1ull << 22, 1ull << 23,
1ull << 60, 1ull << 61, 1ull << 62, 1ull << 63,
MOD - 1ull, 1ull << 63, 0xffffffff, MOD - 1ull};
uint64_t out_arr[8] = {0ull};
uint64_t exp_out_arr[8] = {3072ull,
12288ull,
3145728ull,
12582912ull,
3458764513820540928ull,
13835058055282163712ull,
9223372036854775807ull,
18446744073709551615ull};
cl_mem in_buf = clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(cl_ulong) * 16,
NULL, &status);
cl_mem out_buf = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, sizeof(cl_ulong) * 8,
NULL, &status);
status = clSetKernelArg(krnl, 0, sizeof(cl_mem), &in_buf);
status = clSetKernelArg(krnl, 1, sizeof(cl_mem), &out_buf);
cl_event evt_0;
status = clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0, sizeof(in_arr), in_arr,
0, NULL, &evt_0);
size_t global_size[] = {8};
size_t local_size[] = {8};
cl_event evt_1;
status = clEnqueueNDRangeKernel(cq, krnl, 1, NULL, global_size, local_size, 1,
&evt_0, &evt_1);
cl_event evt_2;
status = clEnqueueReadBuffer(cq, out_buf, CL_FALSE, 0, sizeof(out_arr),
out_arr, 1, &evt_1, &evt_2);
status = clWaitForEvents(1, &evt_2);
for (size_t i = 0; i < 8; i++) {
assert(out_arr[i] % MOD == exp_out_arr[i] % MOD);
}
clReleaseEvent(evt_0);
clReleaseEvent(evt_1);
clReleaseEvent(evt_2);
clReleaseMemObject(in_buf);
clReleaseMemObject(out_buf);
printf("passed reduce_sum_vec2 tests !\n");
return status;
}
// This function is used for computing (global_size_x x global_size_y) rescue
// prime hashes, each of input of width N, and produces same number of outputs,
// each of width 4
//
// Note when I mentioned about width of N or 4, I mean input/ output
// will have those many 64-bit prime field elements
//
// local_size_{x, y} denotes work-group size vertically/ horizontally
//
// I'm going to use this function for testing `merge` kernel
// https://github.com/itzmeanjan/vectorized-rescue-prime/blob/bf40c7e41431487633288b7f64ebd804245fd8eb/kernel.cl#L378
cl_int calculate_hash(cl_context ctx, cl_command_queue cq, cl_kernel krnl,
cl_ulong *input, size_t input_width, cl_ulong *output,
size_t global_size_x, size_t global_size_y,
size_t local_size_x, size_t local_size_y) {
cl_int status;
// input is supplied to kernel by this buffer
cl_mem in_buf = clCreateBuffer(ctx, CL_MEM_READ_ONLY,
sizeof(cl_ulong) * input_width *
global_size_x * global_size_y,
NULL, &status);
// buffer for keeping width of input to hash_elements kernel, stored in
// constant memory
cl_mem in_width_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(size_t), NULL, &status);
// Following three buffers will be storing rescue prime hash constants
cl_mem mds_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(MDS), NULL, &status);
cl_mem ark1_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK1), NULL, &status);
cl_mem ark2_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK2), NULL, &status);
// output to be placed here, after kernel completes hash computation
cl_mem out_buf = clCreateBuffer(
ctx, CL_MEM_WRITE_ONLY,
sizeof(cl_ulong) * 4 * global_size_x * global_size_y, NULL, &status);
// input being copied to device memory
cl_event evt_0;
status = clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0,
sizeof(cl_ulong) * input_width * global_size_x *
global_size_y,
input, 0, NULL, &evt_0);
// input width being copied to device memory
cl_event evt_1;
status = clEnqueueWriteBuffer(cq, in_width_buf, CL_FALSE, 0, sizeof(size_t),
&input_width, 0, NULL, &evt_1);
// scheduling rescue prime constant copying
cl_event evt_2;
status = clEnqueueWriteBuffer(cq, mds_buf, CL_FALSE, 0, sizeof(MDS), MDS, 0,
NULL, &evt_2);
cl_event evt_3;
status = clEnqueueWriteBuffer(cq, ark1_buf, CL_FALSE, 0, sizeof(ARK1), ARK1,
0, NULL, &evt_3);
cl_event evt_4;
status = clEnqueueWriteBuffer(cq, ark2_buf, CL_FALSE, 0, sizeof(ARK2), ARK2,
0, NULL, &evt_4);
// setting kernel arguments for
// https://github.com/itzmeanjan/vectorized-rescue-prime/blob/bf40c7e41431487633288b7f64ebd804245fd8eb/kernel.cl#L320
status = clSetKernelArg(krnl, 0, sizeof(cl_mem), &in_buf);
status = clSetKernelArg(krnl, 1, sizeof(cl_mem), &in_width_buf);
status = clSetKernelArg(krnl, 2, sizeof(cl_mem), &mds_buf);
status = clSetKernelArg(krnl, 3, sizeof(cl_mem), &ark1_buf);
status = clSetKernelArg(krnl, 4, sizeof(cl_mem), &ark2_buf);
status = clSetKernelArg(krnl, 5, sizeof(cl_mem), &out_buf);
// preparing for creating dependency in compute execution graph
cl_event evts[] = {evt_0, evt_1, evt_2, evt_3, evt_4};
size_t global_size[] = {global_size_x, global_size_y};
size_t local_size[] = {local_size_x, local_size_y};
// kernel being dispatched for execution on device
cl_event evt_5;
status = clEnqueueNDRangeKernel(cq, krnl, 2, NULL, global_size, local_size, 5,
evts, &evt_5);
// hash output being copied back to host
cl_event evt_6;
status =
clEnqueueReadBuffer(cq, out_buf, CL_FALSE, 0,
sizeof(cl_ulong) * 4 * global_size_x * global_size_y,
output, 1, &evt_5, &evt_6);
status = clWaitForEvents(1, &evt_6);
clReleaseEvent(evt_0);
clReleaseEvent(evt_1);
clReleaseEvent(evt_2);
clReleaseEvent(evt_3);
clReleaseEvent(evt_4);
clReleaseEvent(evt_5);
clReleaseEvent(evt_6);
clReleaseMemObject(in_buf);
clReleaseMemObject(in_width_buf);
clReleaseMemObject(mds_buf);
clReleaseMemObject(ark1_buf);
clReleaseMemObject(ark2_buf);
clReleaseMemObject(out_buf);
return status;
}
// Merges two rescue prime hashes into single one by dispatching
// https://github.com/itzmeanjan/vectorized-rescue-prime/blob/bf40c7e41431487633288b7f64ebd804245fd8eb/kernel.cl#L378
// kernel (global_size_x x global_size_y)-many times
//
// It should produce same number of rescue prime digests, each of width 4,
// while in input each was of width 8, because two input rescue prime digests to
// be merged
cl_int merge(cl_context ctx, cl_command_queue cq, cl_kernel krnl,
cl_ulong *input, cl_ulong *output, size_t global_size_x,
size_t global_size_y, size_t local_size_x, size_t local_size_y) {
cl_int status;
// input is supplied to kernel by this buffer
cl_mem in_buf = clCreateBuffer(
ctx, CL_MEM_READ_ONLY,
sizeof(cl_ulong) * 8 * global_size_x * global_size_y, NULL, &status);
// Following three buffers will be storing rescue prime hash constants
cl_mem mds_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(MDS), NULL, &status);
cl_mem ark1_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK1), NULL, &status);
cl_mem ark2_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(ARK2), NULL, &status);
// output to be placed here, after kernel completes hash computation
cl_mem out_buf = clCreateBuffer(
ctx, CL_MEM_WRITE_ONLY,
sizeof(cl_ulong) * 4 * global_size_x * global_size_y, NULL, &status);
// input being copied to device memory
cl_event evt_0;
status =
clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0,
sizeof(cl_ulong) * 8 * global_size_x * global_size_y,
input, 0, NULL, &evt_0);
// scheduling rescue prime constant copying
cl_event evt_1;
status = clEnqueueWriteBuffer(cq, mds_buf, CL_FALSE, 0, sizeof(MDS), MDS, 0,
NULL, &evt_1);
cl_event evt_2;
status = clEnqueueWriteBuffer(cq, ark1_buf, CL_FALSE, 0, sizeof(ARK1), ARK1,
0, NULL, &evt_2);
cl_event evt_3;
status = clEnqueueWriteBuffer(cq, ark2_buf, CL_FALSE, 0, sizeof(ARK2), ARK2,
0, NULL, &evt_3);
// setting kernel arguments for
// https://github.com/itzmeanjan/vectorized-rescue-prime/blob/bf40c7e41431487633288b7f64ebd804245fd8eb/kernel.cl#L320
status = clSetKernelArg(krnl, 0, sizeof(cl_mem), &in_buf);
status = clSetKernelArg(krnl, 1, sizeof(cl_mem), &mds_buf);
status = clSetKernelArg(krnl, 2, sizeof(cl_mem), &ark1_buf);
status = clSetKernelArg(krnl, 3, sizeof(cl_mem), &ark2_buf);
status = clSetKernelArg(krnl, 4, sizeof(cl_mem), &out_buf);
// preparing for creating dependency in compute execution graph
cl_event evts[] = {evt_0, evt_1, evt_2, evt_3};
size_t global_size[] = {global_size_x, global_size_y};
size_t local_size[] = {local_size_x, local_size_y};
// kernel being dispatched for execution on device
cl_event evt_4;
status = clEnqueueNDRangeKernel(cq, krnl, 2, NULL, global_size, local_size, 4,
evts, &evt_4);
// hash output being copied back to host
cl_event evt_5;
status =
clEnqueueReadBuffer(cq, out_buf, CL_FALSE, 0,
sizeof(cl_ulong) * 4 * global_size_x * global_size_y,
output, 1, &evt_4, &evt_5);
status = clWaitForEvents(1, &evt_5);
clReleaseEvent(evt_0);
clReleaseEvent(evt_1);
clReleaseEvent(evt_2);
clReleaseEvent(evt_3);
clReleaseEvent(evt_4);
clReleaseEvent(evt_5);
clReleaseMemObject(in_buf);
clReleaseMemObject(mds_buf);
clReleaseMemObject(ark1_buf);
clReleaseMemObject(ark2_buf);
clReleaseMemObject(out_buf);
return status;
}
cl_int test_merge(cl_context ctx, cl_command_queue cq, cl_kernel hash_krnl,
cl_kernel merge_krnl) {
cl_int status;
cl_ulong *in = malloc(sizeof(cl_ulong) * 16);
cl_ulong *out = malloc(sizeof(cl_ulong) * 16);
// prepare random 16 field elements
random_field_elements(in, 16);
// Hash 8 consequtive elements together, twice i.e. using two work-items
status = calculate_hash(ctx, cq, hash_krnl, in, 8, out, 1, 2, 1, 2);
// Then merge 8 consequtive elements together, such that they are
// interpreted to be two rescue prime hash digests concatenated
// one after another
//
// Do this thing twice i.e. using two work-items
status = merge(ctx, cq, merge_krnl, in, out + 8, 1, 2, 1, 2);
// it should produce 8 hash digests, each of width 4 field elements 👇
//
// out[0:4] produced from first work-item of `calculate_hash` function above
// out[4:8] produced from second work-item of `calculate_hash` function above
//
// out[8:12] produced from first work-item of `merge` function above
// out[12:16] produced from second work-item of `merge` function above
//
// This is how input and output are associated
//
// out[0:4] = hash_elements(in[0:8])
// out[4:8] = hash_elements(in[8:16])
//
// out[8:12] = merge(in[0:8])
// out[12:16] = merge(in[8:16])
//
// so followings should be passing !
assert(out[0] == out[8]);
assert(out[1] == out[9]);
assert(out[2] == out[10]);
assert(out[3] == out[11]);
assert(out[4] == out[12]);
assert(out[5] == out[13]);
assert(out[6] == out[14]);
assert(out[7] == out[15]);
printf("passed merge tests !\n");
// deallocate memory
free(in);
free(out);
return status;
}