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
1561 lines (1310 loc) · 50.6 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
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
#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));
// kernel execution time in nanoseconds, obtained
// by enabling profiling in command queue
cl_ulong ts;
status = hash_elements(ctx,
cq,
krnl,
in_arr,
in_width,
out_arr,
glb_sz_x,
glb_sz_y,
loc_sz_x,
loc_sz_y,
&ts);
check(status);
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,
(double)ts * 1e-6,
((double)(glb_sz_x * glb_sz_y) / (double)ts) * 1e9);
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));
// 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
cl_ulong ts;
status = merge(ctx,
cq,
krnl,
in_arr,
out_arr,
global_size_x,
global_size_y,
local_size_x,
local_size_y,
&ts);
check(status);
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,
(double)ts * 1e-6,
((double)(global_size_x * global_size_y) / (double)ts) * 1e9);
free(in_arr);
free(out_arr);
return CL_SUCCESS;
}
cl_int
bench_build_merkle_nodes(cl_context ctx,
cl_command_queue cq,
cl_kernel merge_krnl,
cl_kernel tip_krnl,
size_t global_size,
size_t local_size,
const size_t dev_mem_base_addr_align)
{
cl_int status;
const size_t io_width = 4;
const size_t in_size = global_size * io_width * sizeof(cl_ulong);
const size_t out_size = global_size * io_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_ulong ts;
status = build_merkle_nodes(ctx,
cq,
merge_krnl,
tip_krnl,
in_arr,
out_arr,
global_size,
local_size,
&ts,
dev_mem_base_addr_align);
check(status);
printf("%15s\t\t%10lu leaves\t\t%20.2f ms\n",
"merklize",
global_size,
(double)ts * 1e-6);
free(in_arr);
free(out_arr);
return CL_SUCCESS;
}
cl_int
build_merkle_nodes(cl_context ctx,
cl_command_queue cq,
cl_kernel merge_krnl,
cl_kernel tip_krnl,
cl_ulong* in,
cl_ulong* out,
const size_t leave_count,
const size_t wg_size,
cl_ulong* ts,
const size_t dev_mem_base_addr_align)
{
// leaf count of merkle tree should be power of 2
assert((leave_count & (leave_count - 1ul)) == 0);
// intermediate nodes of tree, living just above leaves,
// those can be computed in parallel
//
// to be specific
// https://github.com/novifinancial/winterfell/blob/377e916c47fab3d9fa173b2f6123c7b713ffce03/crypto/src/merkle/mod.rs#L326-L329
// section
assert((leave_count >> 1) >= wg_size);
// input/ output both are 4-field element wide
// rescue prime hash digests, stored in consequtive
// memory locations
const size_t io_width = 4ul;
// total input size in bytes
const size_t in_size = io_width * leave_count * sizeof(cl_ulong);
// total output size in bytes
const size_t out_size = io_width * leave_count * sizeof(cl_ulong);
// used for checking/ keeping track of result of execution of opencl functions
cl_int status;
// following three buffers are unchanged during execution of this function,
// so they are required to be copied to device one time
//
// --- one time set up buffer(s) ---
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);
// --- one time set up buffer(s) ---
// -- copy one time set up buffer(s) ---
//
// enqueued at very beginning of compute dependency graph set up
// also notice, these are not dependent on any other events
// but a lot of next kernel dispatches are going to be dependent
// on these three events below
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);
// -- copy one time set up buffer(s) ---
// input needs to be copied only once, it'll also be used during very first
// kernel dispatch, just only one time
cl_mem in_buf = clCreateBuffer(ctx, CL_MEM_READ_ONLY, in_size, NULL, &status);
check(status);
// copying input early to device
cl_event evt_0;
status =
clEnqueueWriteBuffer(cq, in_buf, CL_FALSE, 0, in_size, in, 0, NULL, &evt_0);
check(status);
// whole output buffer, allocated on device memory, this buffer will be
// sub-divided multiple times, in following section, and used in further
// kernel dispatches
cl_mem out_buf =
clCreateBuffer(ctx, CL_MEM_READ_WRITE, out_size, NULL, &status);
check(status);
// marking region of output buffer, which is to be used as subbuffer
// and written to during next kernel dispatch
cl_buffer_region sub_buf_reg;
sub_buf_reg.origin = (leave_count >> 1) * io_width * sizeof(cl_ulong);
sub_buf_reg.size = (leave_count >> 1) * io_width * sizeof(cl_ulong);
// this is the subbuffer where intermediate nodes just above
// leaves are written into ( computed in next kernel dispatch )
cl_mem out_sub_buf_abv_leaves =
clCreateSubBuffer(out_buf,
CL_MEM_WRITE_ONLY,
CL_BUFFER_CREATE_TYPE_REGION,
&sub_buf_reg,
&status);
check(status);
// setting up kernel arguments i.e. preparing for dispatch
status = clSetKernelArg(merge_krnl, 0, sizeof(cl_mem), &in_buf);
check(status);
status = clSetKernelArg(merge_krnl, 1, sizeof(cl_mem), &mds_buf);
check(status);
status = clSetKernelArg(merge_krnl, 2, sizeof(cl_mem), &ark1_buf);
check(status);
status = clSetKernelArg(merge_krnl, 3, sizeof(cl_mem), &ark2_buf);
check(status);
status =
clSetKernelArg(merge_krnl, 4, sizeof(cl_mem), &out_sub_buf_abv_leaves);
check(status);
// work-item count: leave_count >> 1
size_t global_size_0[] = { 1, leave_count >> 1 };
// work-group size is certainly compatible with total work size
// check
// https://github.com/itzmeanjan/vectorized-rescue-prime/blob/614500dd1f271e4f8badf1305c8077e2532eb510/rescue_prime.c#L173
// this assertion ensures that
size_t local_size_0[] = { 1, wg_size };
cl_event evts_0[] = { evt_0, evt_1, evt_2, evt_3 };
// final kernel dispatch, computing merkle tree intermediate nodes, which are
// living just above provided leaves ( as an input )
cl_event evt_4;
status = clEnqueueNDRangeKernel(
cq, merge_krnl, 2, NULL, global_size_0, local_size_0, 4, evts_0, &evt_4);
check(status);
// intermediate nodes in tip of tree, to be computed sequentially
//
// this needs to be done because sub bufferring of `out_buf` can't be done
const size_t subtree_count = (dev_mem_base_addr_align >> 5);
// this is one exceptional case and majorly this code block is executed only
// when running test cases ( involving merkle tree construction)
//
// this happens due to small tree size, but large `memory base address
// alignment` requirement in Nvidia GPUs ( 512 bytes on Tesla v100 )
//
// On CPUs I've seen alignment requirement to be 128 bytes, so even with
// small tree (say with 16 leaves) it works fine --- this block is skipped !
//
// and in test cases I use leave_count = 16
if (!((leave_count >> 1) >= (subtree_count << 1))) {
cl_buffer_region sub_buf_reg_0;
sub_buf_reg_0.origin = 0;
sub_buf_reg_0.size = leave_count * io_width * sizeof(cl_ulong);
cl_mem in_out_sub_buf = clCreateSubBuffer(out_buf,
CL_MEM_READ_WRITE,
CL_BUFFER_CREATE_TYPE_REGION,
&sub_buf_reg_0,
&status);
check(status);
cl_mem subtree_count_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(size_t), NULL, &status);
check(status);
const size_t subtree_count_ = leave_count >> 1;
cl_event evt_5;
status = clEnqueueWriteBuffer(cq,
subtree_count_buf,
CL_FALSE,
0,
sizeof(size_t),
&subtree_count_,
0,
NULL,
&evt_5);
check(status);
status = clSetKernelArg(tip_krnl, 0, sizeof(cl_mem), &in_out_sub_buf);
check(status);
status = clSetKernelArg(tip_krnl, 1, sizeof(cl_mem), &subtree_count_buf);
check(status);
status = clSetKernelArg(tip_krnl, 2, sizeof(cl_mem), &mds_buf);
check(status);
status = clSetKernelArg(tip_krnl, 3, sizeof(cl_mem), &ark1_buf);
check(status);
status = clSetKernelArg(tip_krnl, 4, sizeof(cl_mem), &ark2_buf);
check(status);
size_t global_size_2[] = { 1 };
size_t local_size_2[] = { 1 };
cl_event evts_1[] = { evt_4, evt_5 };
cl_event evt_6;
// this kernel operates on input sequentially (only single work-item does
// something useful), due to complicated memory access patterns and also sub
// buffer creation is prohibited when device memory base address alignment
// requirement is not properly satisfied
status = clEnqueueNDRangeKernel(
cq, tip_krnl, 1, NULL, global_size_2, local_size_2, 2, evts_1, &evt_6);
check(status);
// reading output from device
// output is all computed intermediate nodes on merkle tree
cl_event evt_7;
status = clEnqueueReadBuffer(
cq, out_buf, CL_FALSE, 0, out_size, out, 1, &evt_6, &evt_7);
check(status);
status = clWaitForEvents(1, &evt_7);
check(status);
// compute how much time spent in actual execution of dispatched kernels
//
// when this code path is chosen for execution only two kernels are
// dispatched so I'm measuring their execution time summation
if (ts != NULL) {
cl_ulong start, end;
*ts = 0; // zerod before accumulation, just to be safe
status = clGetEventProfilingInfo(
evt_4, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
check(status);
status = clGetEventProfilingInfo(
evt_4, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
check(status);
*ts += (end - start);
status = clGetEventProfilingInfo(
evt_6, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
check(status);
status = clGetEventProfilingInfo(
evt_6, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
check(status);
*ts += (end - start);
}
// deallocate all opencl related resources which were acquired
// during execution of this function
clReleaseEvent(evt_0);
check(status);
clReleaseEvent(evt_1);
check(status);
clReleaseEvent(evt_2);
check(status);
clReleaseEvent(evt_3);
check(status);
clReleaseEvent(evt_4);
check(status);
clReleaseEvent(evt_5);
check(status);
clReleaseEvent(evt_6);
check(status);
clReleaseEvent(evt_7);
check(status);
clReleaseMemObject(subtree_count_buf);
check(status);
clReleaseMemObject(in_out_sub_buf);
check(status);
clReleaseMemObject(out_sub_buf_abv_leaves);
check(status);
clReleaseMemObject(in_buf);
check(status);
clReleaseMemObject(out_buf);
check(status);
clReleaseMemObject(mds_buf);
check(status);
clReleaseMemObject(ark1_buf);
check(status);
clReleaseMemObject(ark2_buf);
check(status);
// code path ended, return back to caller
return status;
}
// data parallel intermediate node compute stages, where n-th round
// depends on completion of (n-1)-th
const size_t rounds =
(size_t)log2((double)((leave_count >> 1) / subtree_count));
// pre allocating enough memory so that all opencl resources can be
// managed and destroyed later
cl_event* evts_1 = malloc(sizeof(cl_event) * rounds);
cl_mem* rd_sub_bufs = malloc(sizeof(cl_mem) * rounds);
cl_mem* wr_sub_bufs = malloc(sizeof(cl_mem) * rounds);
for (size_t i = leave_count >> 1, idx = 0;
(i >> 1) * io_width * sizeof(cl_ulong) >= dev_mem_base_addr_align;
i >>= 1, idx++) {
// this is the region of output buffer which is to be used for
// reading input from, for next compute dispatch
cl_buffer_region sub_buf_reg_0;
sub_buf_reg_0.origin = i * io_width * sizeof(cl_ulong);
sub_buf_reg_0.size = i * io_width * sizeof(cl_ulong);
cl_mem in_sub_buf = clCreateSubBuffer(out_buf,
CL_MEM_READ_ONLY,
CL_BUFFER_CREATE_TYPE_REGION,
&sub_buf_reg_0,
&status);
check(status);
rd_sub_bufs[idx] = in_sub_buf;
// and this region of output buffer is written to, computed intermediate
// nodes are kept here, to be used during next round of dispatch for reading
// input
cl_buffer_region sub_buf_reg_1;
sub_buf_reg_1.origin = (i >> 1) * io_width * sizeof(cl_ulong);
sub_buf_reg_1.size = (i >> 1) * io_width * sizeof(cl_ulong);
cl_mem out_sub_buf = clCreateSubBuffer(out_buf,
CL_MEM_WRITE_ONLY,
CL_BUFFER_CREATE_TYPE_REGION,
&sub_buf_reg_1,
&status);
check(status);
wr_sub_bufs[idx] = out_sub_buf;
status = clSetKernelArg(merge_krnl, 0, sizeof(cl_mem), &in_sub_buf);
check(status);
status = clSetKernelArg(merge_krnl, 1, sizeof(cl_mem), &mds_buf);
check(status);
status = clSetKernelArg(merge_krnl, 2, sizeof(cl_mem), &ark1_buf);
check(status);
status = clSetKernelArg(merge_krnl, 3, sizeof(cl_mem), &ark2_buf);
check(status);
status = clSetKernelArg(merge_krnl, 4, sizeof(cl_mem), &out_sub_buf);
check(status);
// `i >> 1`-many work-items to compute same number of intermediate nodes
// in parallel, with out any dependendency ( speaking of dependency in this
// dispatch )
size_t global_size_1[] = { 1, i >> 1 };
// make sure work-group size is compatible with work-size in this iteration
size_t local_size_1[] = { 1, (i >> 1) >= wg_size ? wg_size : (i >> 1) };
cl_event evt_;
status = clEnqueueNDRangeKernel(cq,
merge_krnl,
2,
NULL,
global_size_1,
local_size_1,
1,
idx == 0 ? &evt_4 : evts_1 + (idx - 1),
&evt_);
check(status);
// kernel dispatch event to be used for computing how much time spent during
// kernel execution; also this will be used for setting up compute
// dependency graph (on device, by runtime itself) so that no data race ever
// happens
evts_1[idx] = evt_;
}
cl_ulong ts_ = 0;
// this block of code is executed sequentially on device due to
// alignment requirement of device memory, which is why sub buffers
// couldn't be created from output buffer
if (subtree_count > 1) {
cl_buffer_region sub_buf_reg_0;
sub_buf_reg_0.origin = 0;
sub_buf_reg_0.size = (subtree_count << 1) * io_width * sizeof(cl_ulong);
cl_mem in_out_sub_buf = clCreateSubBuffer(out_buf,
CL_MEM_READ_WRITE,
CL_BUFFER_CREATE_TYPE_REGION,
&sub_buf_reg_0,
&status);
check(status);
cl_mem subtree_count_buf =
clCreateBuffer(ctx, CL_MEM_READ_ONLY, sizeof(size_t), NULL, &status);
check(status);
cl_event evt_5;
status = clEnqueueWriteBuffer(cq,
subtree_count_buf,
CL_FALSE,
0,
sizeof(size_t),
&subtree_count,
0,
NULL,
&evt_5);
check(status);
status = clSetKernelArg(tip_krnl, 0, sizeof(cl_mem), &in_out_sub_buf);
check(status);
status = clSetKernelArg(tip_krnl, 1, sizeof(cl_mem), &subtree_count_buf);
check(status);
status = clSetKernelArg(tip_krnl, 2, sizeof(cl_mem), &mds_buf);
check(status);
status = clSetKernelArg(tip_krnl, 3, sizeof(cl_mem), &ark1_buf);
check(status);
status = clSetKernelArg(tip_krnl, 4, sizeof(cl_mem), &ark2_buf);
check(status);
size_t global_size_2[] = { 1 };
size_t local_size_2[] = { 1 };
cl_event evts_2[] = { *(evts_1 + (rounds - 1)), evt_5 };
cl_event evt_6;
status = clEnqueueNDRangeKernel(
cq, tip_krnl, 1, NULL, global_size_2, local_size_2, 2, evts_2, &evt_6);
check(status);
// just output being copied back to host memory
//
// the final enqueuing !
cl_event evt_7;
status = clEnqueueReadBuffer(
cq, out_buf, CL_FALSE, 0, out_size, out, 1, &evt_6, &evt_7);
check(status);
status = clWaitForEvents(1, &evt_7);
check(status);
// calcukate how much time spent executing kernel dispatched
// which is run sequentially
cl_ulong start, end;
status = clGetEventProfilingInfo(
evt_6, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
check(status);
status = clGetEventProfilingInfo(
evt_6, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
check(status);
ts_ += (end - start);
status = clReleaseEvent(evt_5);
check(status);
status = clReleaseEvent(evt_6);
check(status);
status = clReleaseEvent(evt_7);
check(status);
status = clReleaseMemObject(subtree_count_buf);
check(status);
status = clReleaseMemObject(in_out_sub_buf);
check(status);
} else {
// just output being copied back to host memory
cl_event evt_5;
status = clEnqueueReadBuffer(cq,
out_buf,
CL_FALSE,
0,
out_size,
out,
1,
evts_1 + (rounds - 1),
&evt_5);
check(status);
status = clWaitForEvents(1, &evt_5);
check(status);
status = clReleaseEvent(evt_5);
check(status);
}
// compute total execution time of all three kernels
// which are dispatched for computing intermediate nodes of merkle tree
if (ts != NULL) {
cl_ulong start, end;
*ts = 0; // zerod before accumulation, just to be safe
status = clGetEventProfilingInfo(
evt_4, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
check(status);
status = clGetEventProfilingInfo(
evt_4, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
check(status);
*ts += (end - start);
for (size_t i = 0; i < rounds; i++) {
status = clGetEventProfilingInfo(*(evts_1 + i),
CL_PROFILING_COMMAND_START,
sizeof(cl_ulong),
&start,
NULL);
check(status);
status = clGetEventProfilingInfo(
*(evts_1 + i), CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
check(status);
*ts += (end - start);
}
*ts += ts_;
}
// releasing all opencl resources
clReleaseEvent(evt_0);
check(status);
clReleaseEvent(evt_1);
check(status);
clReleaseEvent(evt_2);
check(status);
clReleaseEvent(evt_3);
check(status);
clReleaseEvent(evt_4);
check(status);
for (size_t i = 0; i < rounds; i++) {
clReleaseEvent(*(evts_1 + i));
check(status);
clReleaseMemObject(*(rd_sub_bufs + i));
check(status);
clReleaseMemObject(*(wr_sub_bufs + i));
check(status);
}
clReleaseMemObject(out_sub_buf_abv_leaves);
check(status);
clReleaseMemObject(in_buf);
check(status);
clReleaseMemObject(out_buf);
check(status);
clReleaseMemObject(mds_buf);
check(status);
clReleaseMemObject(ark1_buf);
check(status);
clReleaseMemObject(ark2_buf);
check(status);
// releasing all host memory allocations
free(evts_1);
free(rd_sub_bufs);
free(wr_sub_bufs);
return status;
}
cl_int
test_build_merkle_nodes(cl_context ctx,
cl_command_queue cq,
cl_kernel merge_krnl,
cl_kernel tip_kernel,
const size_t dev_mem_base_addr_align)
{
cl_int status;
// leave count of merkle tree
const size_t N = 16;
// because each rescue prime digest consists of 4 field elements
const size_t io_width = 4;
// in terms of bytes
const size_t io_size = N * io_width * sizeof(cl_long);
// to be randomly generated and interpreted such that N-many rescue prime
// hash digests are concatenated one after another
cl_ulong* in = malloc(io_size);
// output to be computed by function `build_merkle_tree`, this is what I'm
// testing
cl_ulong* out_0 = malloc(io_size);
// this output it going to be computed manually by invoking `merge` function
// step by step on a pair of merkle tree nodes
cl_ulong* out_1 = malloc(io_size);
// randomly generated N * 4-many prime field elements
// to be interpreted as N-many rescue prime hash digests
random_field_elements(in, io_size / sizeof(cl_ulong));
// compute merkle tree intermediate nodes, to be asserted in next few steps
status = build_merkle_nodes(ctx,
cq,
merge_krnl,
tip_kernel,
in,
out_0,
N,
1,
NULL,
dev_mem_base_addr_align);
check(status);
// Assume A = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
// to be a set of merkle tree leaves, each of width 4-prime field elements
//
// so A must have 16 * 4 = 64 prime field elements
//
// B = [0; 16], result array for storing all intermediate nodes of tree
// such that
// - B[0] == may be some random value, but of no interest
// - B[1] == root
// - B[2], B[3] == children of B[1]
// - B[4], B[5] == children of B[2]
// - B[6], B[7] == children of B[3]
// ...
//
// B[15] = merge(A[14], A[15])
// B[14] = merge(A[12], A[13])
// ...
// B[8] = merge(A[0], A[1])
//
// For N-many leaves N/2 -many intermediate nodes are computed in this step
for (size_t j = 1; j <= (N >> 1); j++) {
status = merge(ctx,
cq,
merge_krnl,
in + (N - 2 * j) * io_width,
out_1 + (N - j) * io_width,
1,
1,
1,
1,
NULL);
for (size_t i = 0; i < io_width; i++) {
assert(*(out_1 + (N - j) * io_width + i) ==
*(out_0 + (N - j) * io_width + i));
}
}
// As soon as level above leaves are computed, I can move to next level of
// tree
//
// In this step I'll compute N/4 -many intermediate nodes, living just above
// previous level of nodes
//
// B[7] = merge(B[14], B[15])
// B[6] = merge(B[12], B[13])
// B[5] = merge(B[10], B[11])
// B[4] = merge(B[8], B[9])
for (size_t j = 1; j <= (N >> 2); j++) {
status = merge(ctx,
cq,
merge_krnl,
out_1 + (N - 2 * j) * io_width,
out_1 + ((N >> 1) - j) * io_width,
1,
1,
1,
1,
NULL);
for (size_t i = 0; i < io_width; i++) {
assert(*(out_1 + ((N >> 1) - j) * io_width + i) ==
*(out_0 + ((N >> 1) - j) * io_width + i));
}
}
// In next level I'll compute N/8 -many intermediates
//
// B[3] = merge(B[6], B[7])
// B[2] = merge(B[4], B[5])
for (size_t j = 1; j <= (N >> 3); j++) {
status = merge(ctx,
cq,
merge_krnl,
out_1 + ((N >> 1) - 2 * j) * io_width,
out_1 + ((N >> 2) - j) * io_width,
1,
1,
1,
1,
NULL);
for (size_t i = 0; i < io_width; i++) {
assert(*(out_1 + ((N >> 2) - j) * io_width + i) ==
*(out_0 + ((N >> 2) - j) * io_width + i));
}
}
// And this is the root of merkle tree !
//
// Only one node to be computed
//
// B[1] = merge(B[2], B[3])
for (size_t j = 1; j <= (N >> 4); j++) {
status = merge(ctx,
cq,
merge_krnl,
out_1 + ((N >> 2) - 2 * j) * io_width,
out_1 + ((N >> 3) - j) * io_width,
1,
1,
1,
1,
NULL);
for (size_t i = 0; i < io_width; i++) {
assert(*(out_1 + ((N >> 3) - j) * io_width + i) ==
*(out_0 + ((N >> 3) - j) * io_width + i));
}
}
// deallocate resources
free(in);
free(out_0);
free(out_1);
printf("passed build_merkle_nodes tests !\n");
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);