-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathHlpBlake3Dispatch.pas
More file actions
746 lines (695 loc) · 24.8 KB
/
HlpBlake3Dispatch.pas
File metadata and controls
746 lines (695 loc) · 24.8 KB
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
unit HlpBlake3Dispatch;
{$I ..\Include\HashLib.inc}
interface
type
TBlake3CompressProc = procedure(AState, AMsg, ACV, ACounterFlags: Pointer);
// Hash N complete chunks in parallel.
// AInput: pointer to N * 1024 bytes of contiguous input
// AKey: pointer to 8 x UInt32 key/IV
// AOut: pointer to N * 8 x UInt32 output chaining values
// ANumChunks: number of chunks to hash
// ACounter: starting chunk counter
// AFlags: base flags (chunk start/end handled internally)
TBlake3HashManyProc = procedure(AInput, AKey, AOut: Pointer;
ANumChunks: Int32; ACounter: UInt64; AFlags: UInt32);
var
Blake3_Compress: TBlake3CompressProc;
Blake3_HashMany: TBlake3HashManyProc;
Blake3_ParallelDegree: Int32;
implementation
uses
HlpBits,
HlpCpuFeatures,
HlpSimdLevels;
const
Blake3IV: array [0 .. 3] of UInt32 = (
UInt32($6A09E667), UInt32($BB67AE85),
UInt32($3C6EF372), UInt32($A54FF53A)
);
FlagChunkStart = UInt32(1 shl 0);
FlagChunkEnd = UInt32(1 shl 1);
// =============================================================================
// Scalar fallback implementation (fully unrolled, 7 rounds, inlined G)
// =============================================================================
procedure Blake3_Compress_Scalar(AState, AMsg, ACV, ACounterFlags: Pointer);
var
LV0, LV1, LV2, LV3, LV4, LV5, LV6, LV7: UInt32;
LV8, LV9, LV10, LV11, LV12, LV13, LV14, LV15: UInt32;
LPMsg, LPCV, LPCounterFlags, LPState: PCardinal;
begin
LPMsg := PCardinal(AMsg);
LPCV := PCardinal(ACV);
LPCounterFlags := PCardinal(ACounterFlags);
LPState := PCardinal(AState);
// Initialize state from chaining value
LV0 := LPCV[0];
LV1 := LPCV[1];
LV2 := LPCV[2];
LV3 := LPCV[3];
LV4 := LPCV[4];
LV5 := LPCV[5];
LV6 := LPCV[6];
LV7 := LPCV[7];
// Initialize counter half from IV and counter/flags
LV8 := Blake3IV[0];
LV9 := Blake3IV[1];
LV10 := Blake3IV[2];
LV11 := Blake3IV[3];
LV12 := LPCounterFlags[0];
LV13 := LPCounterFlags[1];
LV14 := LPCounterFlags[2];
LV15 := LPCounterFlags[3];
// Round 0
LV0 := LV0 + LV4 + LPMsg[0];
LV12 := TBits.RotateRight32(LV12 xor LV0, 16);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 12);
LV0 := LV0 + LV4 + LPMsg[1];
LV12 := TBits.RotateRight32(LV12 xor LV0, 8);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 7);
LV1 := LV1 + LV5 + LPMsg[2];
LV13 := TBits.RotateRight32(LV13 xor LV1, 16);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 12);
LV1 := LV1 + LV5 + LPMsg[3];
LV13 := TBits.RotateRight32(LV13 xor LV1, 8);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 7);
LV2 := LV2 + LV6 + LPMsg[4];
LV14 := TBits.RotateRight32(LV14 xor LV2, 16);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 12);
LV2 := LV2 + LV6 + LPMsg[5];
LV14 := TBits.RotateRight32(LV14 xor LV2, 8);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 7);
LV3 := LV3 + LV7 + LPMsg[6];
LV15 := TBits.RotateRight32(LV15 xor LV3, 16);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 12);
LV3 := LV3 + LV7 + LPMsg[7];
LV15 := TBits.RotateRight32(LV15 xor LV3, 8);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 7);
LV0 := LV0 + LV5 + LPMsg[8];
LV15 := TBits.RotateRight32(LV15 xor LV0, 16);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 12);
LV0 := LV0 + LV5 + LPMsg[9];
LV15 := TBits.RotateRight32(LV15 xor LV0, 8);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 7);
LV1 := LV1 + LV6 + LPMsg[10];
LV12 := TBits.RotateRight32(LV12 xor LV1, 16);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 12);
LV1 := LV1 + LV6 + LPMsg[11];
LV12 := TBits.RotateRight32(LV12 xor LV1, 8);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 7);
LV2 := LV2 + LV7 + LPMsg[12];
LV13 := TBits.RotateRight32(LV13 xor LV2, 16);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 12);
LV2 := LV2 + LV7 + LPMsg[13];
LV13 := TBits.RotateRight32(LV13 xor LV2, 8);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 7);
LV3 := LV3 + LV4 + LPMsg[14];
LV14 := TBits.RotateRight32(LV14 xor LV3, 16);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 12);
LV3 := LV3 + LV4 + LPMsg[15];
LV14 := TBits.RotateRight32(LV14 xor LV3, 8);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 7);
// Round 1
LV0 := LV0 + LV4 + LPMsg[2];
LV12 := TBits.RotateRight32(LV12 xor LV0, 16);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 12);
LV0 := LV0 + LV4 + LPMsg[6];
LV12 := TBits.RotateRight32(LV12 xor LV0, 8);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 7);
LV1 := LV1 + LV5 + LPMsg[3];
LV13 := TBits.RotateRight32(LV13 xor LV1, 16);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 12);
LV1 := LV1 + LV5 + LPMsg[10];
LV13 := TBits.RotateRight32(LV13 xor LV1, 8);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 7);
LV2 := LV2 + LV6 + LPMsg[7];
LV14 := TBits.RotateRight32(LV14 xor LV2, 16);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 12);
LV2 := LV2 + LV6 + LPMsg[0];
LV14 := TBits.RotateRight32(LV14 xor LV2, 8);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 7);
LV3 := LV3 + LV7 + LPMsg[4];
LV15 := TBits.RotateRight32(LV15 xor LV3, 16);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 12);
LV3 := LV3 + LV7 + LPMsg[13];
LV15 := TBits.RotateRight32(LV15 xor LV3, 8);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 7);
LV0 := LV0 + LV5 + LPMsg[1];
LV15 := TBits.RotateRight32(LV15 xor LV0, 16);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 12);
LV0 := LV0 + LV5 + LPMsg[11];
LV15 := TBits.RotateRight32(LV15 xor LV0, 8);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 7);
LV1 := LV1 + LV6 + LPMsg[12];
LV12 := TBits.RotateRight32(LV12 xor LV1, 16);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 12);
LV1 := LV1 + LV6 + LPMsg[5];
LV12 := TBits.RotateRight32(LV12 xor LV1, 8);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 7);
LV2 := LV2 + LV7 + LPMsg[9];
LV13 := TBits.RotateRight32(LV13 xor LV2, 16);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 12);
LV2 := LV2 + LV7 + LPMsg[14];
LV13 := TBits.RotateRight32(LV13 xor LV2, 8);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 7);
LV3 := LV3 + LV4 + LPMsg[15];
LV14 := TBits.RotateRight32(LV14 xor LV3, 16);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 12);
LV3 := LV3 + LV4 + LPMsg[8];
LV14 := TBits.RotateRight32(LV14 xor LV3, 8);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 7);
// Round 2
LV0 := LV0 + LV4 + LPMsg[3];
LV12 := TBits.RotateRight32(LV12 xor LV0, 16);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 12);
LV0 := LV0 + LV4 + LPMsg[4];
LV12 := TBits.RotateRight32(LV12 xor LV0, 8);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 7);
LV1 := LV1 + LV5 + LPMsg[10];
LV13 := TBits.RotateRight32(LV13 xor LV1, 16);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 12);
LV1 := LV1 + LV5 + LPMsg[12];
LV13 := TBits.RotateRight32(LV13 xor LV1, 8);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 7);
LV2 := LV2 + LV6 + LPMsg[13];
LV14 := TBits.RotateRight32(LV14 xor LV2, 16);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 12);
LV2 := LV2 + LV6 + LPMsg[2];
LV14 := TBits.RotateRight32(LV14 xor LV2, 8);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 7);
LV3 := LV3 + LV7 + LPMsg[7];
LV15 := TBits.RotateRight32(LV15 xor LV3, 16);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 12);
LV3 := LV3 + LV7 + LPMsg[14];
LV15 := TBits.RotateRight32(LV15 xor LV3, 8);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 7);
LV0 := LV0 + LV5 + LPMsg[6];
LV15 := TBits.RotateRight32(LV15 xor LV0, 16);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 12);
LV0 := LV0 + LV5 + LPMsg[5];
LV15 := TBits.RotateRight32(LV15 xor LV0, 8);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 7);
LV1 := LV1 + LV6 + LPMsg[9];
LV12 := TBits.RotateRight32(LV12 xor LV1, 16);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 12);
LV1 := LV1 + LV6 + LPMsg[0];
LV12 := TBits.RotateRight32(LV12 xor LV1, 8);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 7);
LV2 := LV2 + LV7 + LPMsg[11];
LV13 := TBits.RotateRight32(LV13 xor LV2, 16);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 12);
LV2 := LV2 + LV7 + LPMsg[15];
LV13 := TBits.RotateRight32(LV13 xor LV2, 8);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 7);
LV3 := LV3 + LV4 + LPMsg[8];
LV14 := TBits.RotateRight32(LV14 xor LV3, 16);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 12);
LV3 := LV3 + LV4 + LPMsg[1];
LV14 := TBits.RotateRight32(LV14 xor LV3, 8);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 7);
// Round 3
LV0 := LV0 + LV4 + LPMsg[10];
LV12 := TBits.RotateRight32(LV12 xor LV0, 16);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 12);
LV0 := LV0 + LV4 + LPMsg[7];
LV12 := TBits.RotateRight32(LV12 xor LV0, 8);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 7);
LV1 := LV1 + LV5 + LPMsg[12];
LV13 := TBits.RotateRight32(LV13 xor LV1, 16);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 12);
LV1 := LV1 + LV5 + LPMsg[9];
LV13 := TBits.RotateRight32(LV13 xor LV1, 8);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 7);
LV2 := LV2 + LV6 + LPMsg[14];
LV14 := TBits.RotateRight32(LV14 xor LV2, 16);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 12);
LV2 := LV2 + LV6 + LPMsg[3];
LV14 := TBits.RotateRight32(LV14 xor LV2, 8);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 7);
LV3 := LV3 + LV7 + LPMsg[13];
LV15 := TBits.RotateRight32(LV15 xor LV3, 16);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 12);
LV3 := LV3 + LV7 + LPMsg[15];
LV15 := TBits.RotateRight32(LV15 xor LV3, 8);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 7);
LV0 := LV0 + LV5 + LPMsg[4];
LV15 := TBits.RotateRight32(LV15 xor LV0, 16);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 12);
LV0 := LV0 + LV5 + LPMsg[0];
LV15 := TBits.RotateRight32(LV15 xor LV0, 8);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 7);
LV1 := LV1 + LV6 + LPMsg[11];
LV12 := TBits.RotateRight32(LV12 xor LV1, 16);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 12);
LV1 := LV1 + LV6 + LPMsg[2];
LV12 := TBits.RotateRight32(LV12 xor LV1, 8);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 7);
LV2 := LV2 + LV7 + LPMsg[5];
LV13 := TBits.RotateRight32(LV13 xor LV2, 16);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 12);
LV2 := LV2 + LV7 + LPMsg[8];
LV13 := TBits.RotateRight32(LV13 xor LV2, 8);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 7);
LV3 := LV3 + LV4 + LPMsg[1];
LV14 := TBits.RotateRight32(LV14 xor LV3, 16);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 12);
LV3 := LV3 + LV4 + LPMsg[6];
LV14 := TBits.RotateRight32(LV14 xor LV3, 8);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 7);
// Round 4
LV0 := LV0 + LV4 + LPMsg[12];
LV12 := TBits.RotateRight32(LV12 xor LV0, 16);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 12);
LV0 := LV0 + LV4 + LPMsg[13];
LV12 := TBits.RotateRight32(LV12 xor LV0, 8);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 7);
LV1 := LV1 + LV5 + LPMsg[9];
LV13 := TBits.RotateRight32(LV13 xor LV1, 16);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 12);
LV1 := LV1 + LV5 + LPMsg[11];
LV13 := TBits.RotateRight32(LV13 xor LV1, 8);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 7);
LV2 := LV2 + LV6 + LPMsg[15];
LV14 := TBits.RotateRight32(LV14 xor LV2, 16);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 12);
LV2 := LV2 + LV6 + LPMsg[10];
LV14 := TBits.RotateRight32(LV14 xor LV2, 8);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 7);
LV3 := LV3 + LV7 + LPMsg[14];
LV15 := TBits.RotateRight32(LV15 xor LV3, 16);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 12);
LV3 := LV3 + LV7 + LPMsg[8];
LV15 := TBits.RotateRight32(LV15 xor LV3, 8);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 7);
LV0 := LV0 + LV5 + LPMsg[7];
LV15 := TBits.RotateRight32(LV15 xor LV0, 16);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 12);
LV0 := LV0 + LV5 + LPMsg[2];
LV15 := TBits.RotateRight32(LV15 xor LV0, 8);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 7);
LV1 := LV1 + LV6 + LPMsg[5];
LV12 := TBits.RotateRight32(LV12 xor LV1, 16);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 12);
LV1 := LV1 + LV6 + LPMsg[3];
LV12 := TBits.RotateRight32(LV12 xor LV1, 8);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 7);
LV2 := LV2 + LV7 + LPMsg[0];
LV13 := TBits.RotateRight32(LV13 xor LV2, 16);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 12);
LV2 := LV2 + LV7 + LPMsg[1];
LV13 := TBits.RotateRight32(LV13 xor LV2, 8);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 7);
LV3 := LV3 + LV4 + LPMsg[6];
LV14 := TBits.RotateRight32(LV14 xor LV3, 16);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 12);
LV3 := LV3 + LV4 + LPMsg[4];
LV14 := TBits.RotateRight32(LV14 xor LV3, 8);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 7);
// Round 5
LV0 := LV0 + LV4 + LPMsg[9];
LV12 := TBits.RotateRight32(LV12 xor LV0, 16);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 12);
LV0 := LV0 + LV4 + LPMsg[14];
LV12 := TBits.RotateRight32(LV12 xor LV0, 8);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 7);
LV1 := LV1 + LV5 + LPMsg[11];
LV13 := TBits.RotateRight32(LV13 xor LV1, 16);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 12);
LV1 := LV1 + LV5 + LPMsg[5];
LV13 := TBits.RotateRight32(LV13 xor LV1, 8);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 7);
LV2 := LV2 + LV6 + LPMsg[8];
LV14 := TBits.RotateRight32(LV14 xor LV2, 16);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 12);
LV2 := LV2 + LV6 + LPMsg[12];
LV14 := TBits.RotateRight32(LV14 xor LV2, 8);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 7);
LV3 := LV3 + LV7 + LPMsg[15];
LV15 := TBits.RotateRight32(LV15 xor LV3, 16);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 12);
LV3 := LV3 + LV7 + LPMsg[1];
LV15 := TBits.RotateRight32(LV15 xor LV3, 8);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 7);
LV0 := LV0 + LV5 + LPMsg[13];
LV15 := TBits.RotateRight32(LV15 xor LV0, 16);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 12);
LV0 := LV0 + LV5 + LPMsg[3];
LV15 := TBits.RotateRight32(LV15 xor LV0, 8);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 7);
LV1 := LV1 + LV6 + LPMsg[0];
LV12 := TBits.RotateRight32(LV12 xor LV1, 16);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 12);
LV1 := LV1 + LV6 + LPMsg[10];
LV12 := TBits.RotateRight32(LV12 xor LV1, 8);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 7);
LV2 := LV2 + LV7 + LPMsg[2];
LV13 := TBits.RotateRight32(LV13 xor LV2, 16);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 12);
LV2 := LV2 + LV7 + LPMsg[6];
LV13 := TBits.RotateRight32(LV13 xor LV2, 8);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 7);
LV3 := LV3 + LV4 + LPMsg[4];
LV14 := TBits.RotateRight32(LV14 xor LV3, 16);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 12);
LV3 := LV3 + LV4 + LPMsg[7];
LV14 := TBits.RotateRight32(LV14 xor LV3, 8);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 7);
// Round 6
LV0 := LV0 + LV4 + LPMsg[11];
LV12 := TBits.RotateRight32(LV12 xor LV0, 16);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 12);
LV0 := LV0 + LV4 + LPMsg[15];
LV12 := TBits.RotateRight32(LV12 xor LV0, 8);
LV8 := LV8 + LV12;
LV4 := TBits.RotateRight32(LV4 xor LV8, 7);
LV1 := LV1 + LV5 + LPMsg[5];
LV13 := TBits.RotateRight32(LV13 xor LV1, 16);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 12);
LV1 := LV1 + LV5 + LPMsg[0];
LV13 := TBits.RotateRight32(LV13 xor LV1, 8);
LV9 := LV9 + LV13;
LV5 := TBits.RotateRight32(LV5 xor LV9, 7);
LV2 := LV2 + LV6 + LPMsg[1];
LV14 := TBits.RotateRight32(LV14 xor LV2, 16);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 12);
LV2 := LV2 + LV6 + LPMsg[9];
LV14 := TBits.RotateRight32(LV14 xor LV2, 8);
LV10 := LV10 + LV14;
LV6 := TBits.RotateRight32(LV6 xor LV10, 7);
LV3 := LV3 + LV7 + LPMsg[8];
LV15 := TBits.RotateRight32(LV15 xor LV3, 16);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 12);
LV3 := LV3 + LV7 + LPMsg[6];
LV15 := TBits.RotateRight32(LV15 xor LV3, 8);
LV11 := LV11 + LV15;
LV7 := TBits.RotateRight32(LV7 xor LV11, 7);
LV0 := LV0 + LV5 + LPMsg[14];
LV15 := TBits.RotateRight32(LV15 xor LV0, 16);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 12);
LV0 := LV0 + LV5 + LPMsg[10];
LV15 := TBits.RotateRight32(LV15 xor LV0, 8);
LV10 := LV10 + LV15;
LV5 := TBits.RotateRight32(LV5 xor LV10, 7);
LV1 := LV1 + LV6 + LPMsg[2];
LV12 := TBits.RotateRight32(LV12 xor LV1, 16);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 12);
LV1 := LV1 + LV6 + LPMsg[12];
LV12 := TBits.RotateRight32(LV12 xor LV1, 8);
LV11 := LV11 + LV12;
LV6 := TBits.RotateRight32(LV6 xor LV11, 7);
LV2 := LV2 + LV7 + LPMsg[3];
LV13 := TBits.RotateRight32(LV13 xor LV2, 16);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 12);
LV2 := LV2 + LV7 + LPMsg[4];
LV13 := TBits.RotateRight32(LV13 xor LV2, 8);
LV8 := LV8 + LV13;
LV7 := TBits.RotateRight32(LV7 xor LV8, 7);
LV3 := LV3 + LV4 + LPMsg[7];
LV14 := TBits.RotateRight32(LV14 xor LV3, 16);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 12);
LV3 := LV3 + LV4 + LPMsg[13];
LV14 := TBits.RotateRight32(LV14 xor LV3, 8);
LV9 := LV9 + LV14;
LV4 := TBits.RotateRight32(LV4 xor LV9, 7);
// Finalization: XOR top and bottom halves
LPState[0] := LV0 xor LV8;
LPState[1] := LV1 xor LV9;
LPState[2] := LV2 xor LV10;
LPState[3] := LV3 xor LV11;
LPState[4] := LV4 xor LV12;
LPState[5] := LV5 xor LV13;
LPState[6] := LV6 xor LV14;
LPState[7] := LV7 xor LV15;
LPState[8] := LV8 xor LPCV[0];
LPState[9] := LV9 xor LPCV[1];
LPState[10] := LV10 xor LPCV[2];
LPState[11] := LV11 xor LPCV[3];
LPState[12] := LV12 xor LPCV[4];
LPState[13] := LV13 xor LPCV[5];
LPState[14] := LV14 xor LPCV[6];
LPState[15] := LV15 xor LPCV[7];
end;
// =============================================================================
// Scalar HashMany fallback (sequential, no parallelism)
// =============================================================================
procedure Blake3_HashMany_Scalar(AInput, AKey, AOut: Pointer;
ANumChunks: Int32; ACounter: UInt64; AFlags: UInt32);
var
LChunk, LBlock: Int32;
LCV: array [0 .. 7] of UInt32;
LBlockWords: array [0 .. 15] of UInt32;
LState: array [0 .. 15] of UInt32;
LCounterFlags: array [0 .. 3] of UInt32;
LBlockFlags: UInt32;
LPInput, LPOut: PByte;
begin
LPInput := PByte(AInput);
LPOut := PByte(AOut);
for LChunk := 0 to ANumChunks - 1 do
begin
// Initialize CV from key/IV
System.Move(AKey^, LCV[0], 8 * System.SizeOf(UInt32));
// Process 16 blocks per chunk
for LBlock := 0 to 15 do
begin
// Convert block bytes to words (little-endian, which is native on x86)
System.Move(LPInput^, LBlockWords[0], 64);
// Set flags for this block
LBlockFlags := AFlags;
if LBlock = 0 then
LBlockFlags := LBlockFlags or FlagChunkStart;
if LBlock = 15 then
LBlockFlags := LBlockFlags or FlagChunkEnd;
LCounterFlags[0] := UInt32(ACounter);
LCounterFlags[1] := UInt32(ACounter shr 32);
LCounterFlags[2] := 64; // BlockLen = 64
LCounterFlags[3] := LBlockFlags;
Blake3_Compress(@LState[0], @LBlockWords[0], @LCV[0], @LCounterFlags[0]);
// Extract chaining value (first 8 words of output)
if LBlock < 15 then
System.Move(LState[0], LCV[0], 8 * System.SizeOf(UInt32));
System.Inc(LPInput, 64);
end;
// Write final chaining value for this chunk
System.Move(LState[0], LPOut^, 8 * System.SizeOf(UInt32));
System.Inc(LPOut, 8 * System.SizeOf(UInt32));
System.Inc(ACounter);
end;
end;
// =============================================================================
// SIMD implementations: SSE2 (IA-32); SSE2 / SSSE3 / AVX2 (x86-64)
// =============================================================================
{$IFDEF HASHLIB_I386_ASM}
procedure Blake3_Compress_Sse2(AState, AMsg, ACV, ACounterFlags: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_i386.inc}
{$I ..\Include\Simd\Blake3\Blake3CompressSse2_i386.inc}
end;
procedure Blake3_Hash4_Sse2(AInput, AKey, AOut: Pointer;
ANumChunks: Int32; ACounter: UInt64; AFlags: UInt32);
{$I ..\Include\Simd\Common\SimdProc6Begin_i386.inc}
{$I ..\Include\Simd\Blake3\Blake3Hash4Sse2_i386.inc}
end;
{$ENDIF HASHLIB_I386_ASM}
{$IFDEF HASHLIB_X86_64_ASM}
procedure Blake3_Compress_Sse2(AState, AMsg, ACV, ACounterFlags: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_x86_64.inc}
{$I ..\Include\Simd\Blake3\Blake3CompressSse2_x86_64.inc}
end;
procedure Blake3_Compress_Avx2(AState, AMsg, ACV, ACounterFlags: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_x86_64.inc}
{$I ..\Include\Simd\Blake3\Blake3CompressAvx2_x86_64.inc}
end;
procedure Blake3_Hash4_Sse2(AInput, AKey, AOut: Pointer;
ANumChunks: Int32; ACounter: UInt64; AFlags: UInt32);
{$I ..\Include\Simd\Common\SimdProc6Begin_x86_64.inc}
{$I ..\Include\Simd\Blake3\Blake3Hash4Sse2_x86_64.inc}
end;
procedure Blake3_Hash8_Avx2(AInput, AKey, AOut: Pointer;
ANumChunks: Int32; ACounter: UInt64; AFlags: UInt32);
{$I ..\Include\Simd\Common\SimdProc6Begin_x86_64.inc}
{$I ..\Include\Simd\Blake3\Blake3Hash8Avx2_x86_64.inc}
end;
{$ENDIF HASHLIB_X86_64_ASM}
{$IFDEF HASHLIB_X86_SIMD}
// SSE2 hash_many: hash4 -> delegate remainder to scalar hash_many
procedure Blake3_HashMany_Sse2(AInput, AKey, AOut: Pointer;
ANumChunks: Int32; ACounter: UInt64; AFlags: UInt32);
var
LPInput, LPOut: PByte;
begin
LPInput := PByte(AInput);
LPOut := PByte(AOut);
while ANumChunks >= 4 do
begin
Blake3_Hash4_Sse2(LPInput, AKey, LPOut, 4, ACounter, AFlags);
System.Inc(LPInput, 4 * 1024);
System.Inc(LPOut, 4 * 32);
System.Inc(ACounter, 4);
System.Dec(ANumChunks, 4);
end;
if ANumChunks > 0 then
Blake3_HashMany_Scalar(LPInput, AKey, LPOut, ANumChunks, ACounter, AFlags);
end;
{$ENDIF HASHLIB_X86_SIMD}
{$IFDEF HASHLIB_X86_64_ASM}
// AVX2 hash_many: hash8 -> delegate remainder to SSE2 hash_many (x64 only; after shared Sse2)
procedure Blake3_HashMany_Avx2(AInput, AKey, AOut: Pointer;
ANumChunks: Int32; ACounter: UInt64; AFlags: UInt32);
var
LPInput, LPOut: PByte;
begin
LPInput := PByte(AInput);
LPOut := PByte(AOut);
while ANumChunks >= 8 do
begin
Blake3_Hash8_Avx2(LPInput, AKey, LPOut, 8, ACounter, AFlags);
System.Inc(LPInput, 8 * 1024);
System.Inc(LPOut, 8 * 32);
System.Inc(ACounter, 8);
System.Dec(ANumChunks, 8);
end;
if ANumChunks > 0 then
Blake3_HashMany_Sse2(LPInput, AKey, LPOut, ANumChunks, ACounter, AFlags);
end;
{$ENDIF HASHLIB_X86_64_ASM}
// =============================================================================
// Dispatch initialization
// =============================================================================
procedure InitDispatch();
begin
Blake3_Compress := @Blake3_Compress_Scalar;
Blake3_HashMany := @Blake3_HashMany_Scalar;
Blake3_ParallelDegree := 1;
{$IFDEF HASHLIB_I386_ASM}
case TCpuFeatures.X86.GetSimdLevel() of
TX86SimdLevel.SSE2, TX86SimdLevel.SSSE3:
begin
Blake3_Compress := @Blake3_Compress_Sse2;
Blake3_HashMany := @Blake3_HashMany_Sse2;
Blake3_ParallelDegree := 4;
end;
end;
{$ENDIF}
{$IFDEF HASHLIB_X86_64_ASM}
case TCpuFeatures.X86.GetSimdLevel() of
TX86SimdLevel.AVX2:
begin
Blake3_Compress := @Blake3_Compress_Avx2;
Blake3_HashMany := @Blake3_HashMany_Avx2;
Blake3_ParallelDegree := 8;
end;
TX86SimdLevel.SSE2, TX86SimdLevel.SSSE3:
begin
Blake3_Compress := @Blake3_Compress_Sse2;
Blake3_HashMany := @Blake3_HashMany_Sse2;
Blake3_ParallelDegree := 4;
end;
end;
{$ENDIF}
end;
initialization
InitDispatch();
end.