-
Notifications
You must be signed in to change notification settings - Fork 0
/
MuseAir.hpp
1321 lines (1174 loc) · 81 KB
/
MuseAir.hpp
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
#if !defined( NON_CRYPTOGRAPHIC_HASH_MUSE_AIR_HPP )
#define NON_CRYPTOGRAPHIC_HASH_MUSE_AIR_HPP
#include <cstdint>
#include <cassert>
#include <immintrin.h>
#include <array>
#include <algorithm>
#include <utility>
#include <vector>
#include <functional>
#if defined( _MSC_VER )
#define FORCE_INLINE __forceinline
#define NEVER_INLINE __declspec( noinline )
#elif defined( __GNUC__ ) || defined( __clang__ )
#define FORCE_INLINE __attribute__( ( always_inline ) ) inline
#define NEVER_INLINE __attribute__( ( noinline ) )
#else
#define FORCE_INLINE inline
#define NEVER_INLINE
#endif
#if defined(__GNUC__) || defined(__clang__)
#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#elif defined(_MSC_VER)
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif
// 定义一个名为State的类型,它是包含6个uint64_t元素的数组,用于表示哈希算法的状态。
// Define a type named State, which is an array of 6 uint64_t elements representing the state of the hash algorithm.
using State = std::array<uint64_t, 6>;
// 定义一个常量数组DEFAULT_SECRET,包含6个uint64_t元素,作为哈希算法的默认密钥。
// DEFAULT_SECRET is a constant array containing 6 uint64_t elements, serving as the default secret key for the hash algorithm.
constexpr std::array<uint64_t, 6> DEFAULT_SECRET =
{
UINT64_C( 0x5ae31e589c56e17a ),
UINT64_C( 0x96d7bb04e64f6da9 ),
UINT64_C( 0x7ab1006b26f9eb64 ),
UINT64_C( 0x21233394220b8457 ),
UINT64_C( 0x047cb9557c9f3b43 ),
UINT64_C( 0xd24f2590c0bcee28 ),
};
// 定义一个常量INIT_RING_PREV,用于初始化环的前一个值,作为哈希过程中的初始参数。
// INIT_RING_PREV is a constant used to initialize the previous value of the ring, serving as an initial parameter in the hashing process.
constexpr uint64_t INIT_RING_PREV = UINT64_C( 0x33ea8f71bb6016d8 );
// 定义一个内联函数seg,用于计算给定数字乘以8后的值,常用于字节数转换。
// Define an inline function seg that calculates the given number multiplied by 8, commonly used for byte conversions.
constexpr size_t seg( size_t n )
{
return n * 8;
}
// 定义一个结构模板Values2,包含两个类型为Type的成员变量first和second,用于存储两组相关值。
// Define a struct template Values2 with two member variables of type Type, first and second, used to store two related values.
template <typename Type>
struct Values2
{
Type first;
Type second;
};
// 定义一个结构模板Values3,包含三个类型为Type的成员变量first、second和third,用于存储三组相关值。
// Define a struct template Values3 with three member variables of type Type, first, second, and third, used to store three related values.
template <typename Type>
struct Values3
{
Type first;
Type second;
Type third;
};
// 辅助函数,用于交换字节顺序(用于小端/大端转换)
// Helper function to swap byte order (for little-endian/big-endian conversion)
FORCE_INLINE uint64_t swap_uint64( uint64_t value )
{
// 按位操作,将64位的值转换成相反的字节顺序
// Bitwise operations to convert the 64-bit value to the opposite byte order
return ( ( value >> 56 ) & 0x00000000000000FF ) | ( ( value >> 40 ) & 0x000000000000FF00 ) | ( ( value >> 24 ) & 0x0000000000FF0000 ) | ( ( value >> 8 ) & 0x00000000FF000000 ) | ( ( value << 8 ) & 0x000000FF00000000 ) | ( ( value << 24 ) & 0x0000FF0000000000 ) | ( ( value << 40 ) & 0x00FF000000000000 ) | ( ( value << 56 ) & 0xFF00000000000000 );
}
// 辅助函数,用于交换32位整数的字节顺序(用于小端/大端转换)
// Helper function to swap the byte order of a 32-bit integer (for little-endian/big-endian conversion)
FORCE_INLINE uint32_t swap_uint32( uint32_t value )
{
return ( ( value >> 24 ) & 0x000000FF ) | ( ( value >> 8 ) & 0x0000FF00 ) | ( ( value << 8 ) & 0x00FF0000 ) | ( ( value << 24 ) & 0xFF000000 );
}
// 判断系统是否为小端模式的辅助函数
// Helper function to determine if the system is little-endian
FORCE_INLINE bool is_little_endian()
{
uint16_t value = 0x1;
uint8_t* byte = reinterpret_cast<uint8_t*>( &value );
return byte[ 0 ] == 0x1;
}
// 读取64位无符号整数的模板函数,支持字节顺序的自动调整
// Template function to read a 64-bit unsigned integer, supporting automatic byte order adjustment
template <bool ByteSwap>
FORCE_INLINE uint64_t read_u64( const uint8_t* p )
{
uint64_t value;
std::memcpy( &value, p, sizeof( value ) );
if constexpr ( ByteSwap )
{
return swap_uint64( value );
}
else
{
return value;
}
}
// 读取32位无符号整数的模板函数,支持字节顺序的自动调整
// Template function to read a 32-bit unsigned integer, supporting automatic byte order adjustment
template <bool ByteSwap>
FORCE_INLINE uint64_t read_u32( const uint8_t* p )
{
uint32_t value;
std::memcpy( &value, p, sizeof( value ) );
if constexpr ( ByteSwap )
{
return swap_uint64( value ); // 错误:应为swap_uint32。Error: should be swap_uint32.
}
else
{
return value;
}
}
// 写入64位无符号整数的模板函数,支持字节顺序的自动调整
// Template function to write a 64-bit unsigned integer, supporting automatic byte order adjustment
template <bool ByteSwap>
FORCE_INLINE void write_u64( uint8_t* p, uint64_t value )
{
if constexpr ( ByteSwap )
{
value = swap_uint64( value );
}
std::memcpy( p, &value, sizeof( value ) );
}
/**
* @brief Reads and processes a short sequence of bytes, converting it into two 64-bit values.
*
* This function reads a byte array and interprets the data based on the provided length.
* If the length is 4 or more, it combines two 32-bit values into two 64-bit values.
* For shorter lengths, it packs bytes into a 64-bit value, handling both little-endian and big-endian formats.
*
* @tparam ByteSwap A boolean indicating whether to swap byte order (true for swapping, false for no swap).
* @param bytes The input byte array to read from.
* @param length The length of the byte array.
* @param values A reference to a Values2<uint64_t> structure where the output values are stored.
*/
template <bool ByteSwap>
FORCE_INLINE void read_short( const uint8_t* bytes, const size_t length, Values2<uint64_t>& values )
{
auto& [ i, j ] = values;
// 当长度>=4时,读取两个32位的值,并将它们合并成两个64位的值
// When length >= 4, read two 32-bit values and combine them into two 64-bit values
if ( length >= 4 )
{
int offset = ( length & 24 ) >> ( length >> 3 ); // 对length的值进行位操作来计算偏移量 (length >= 8 ? 4 : 0)
i = ( read_u32<ByteSwap>( bytes ) << 32 ) | read_u32<ByteSwap>( bytes + length - 4 );
j = ( read_u32<ByteSwap>( bytes + offset ) << 32 ) | read_u32<ByteSwap>( bytes + length - 4 - offset );
}
// 对于小于4字节的长度,按字节顺序处理并打包为64位整数
// For lengths less than 4 bytes, handle and pack the bytes into a 64-bit integer
else if ( length > 0 )
{
// MSB <-> LSB
// [0] [0] [0] for len == 1 (0b01)
// [0] [1] [1] for len == 2 (0b10)
// [0] [1] [2] for len == 3 (0b11)
i = ( ( uint64_t )bytes[ 0 ] << 48 ) | ( ( uint64_t )bytes[ length >> 1 ] << 24 ) | ( uint64_t )bytes[ length - 1 ];
j = 0;
}
// 如果长度为0,将i和j初始化为0
// If the length is 0, initialize i and j to 0
else
{
i = 0;
j = 0;
}
}
/**
* @brief Multiplies two 64-bit integers and stores the result as a 128-bit value.
*
* This function performs a 64-bit multiplication and stores the result in two 64-bit parts,
* effectively providing a 128-bit result. It leverages platform-specific intrinsics if available,
* otherwise, it falls back to manual multiplication.
*
* @param x The first 64-bit integer operand.
* @param y The second 64-bit integer operand.
* @param result A reference to a Values2<uint64_t> structure where the 128-bit result is stored.
*/
FORCE_INLINE void multiple64_128bit( uint64_t x, uint64_t y, Values2<uint64_t>& result )
{
auto& [ left, right ] = result;
#if defined( __SIZEOF_INT128__ )
// 使用128位整数类型进行乘法运算(GCC/Clang支持)
// Use 128-bit integer type for multiplication (supported by GCC/Clang)
__uint128_t product = static_cast<__uint128_t>( x ) * static_cast<__uint128_t>( y );
left = static_cast<uint64_t>( product );
right = static_cast<uint64_t>( product >> 64 );
#elif defined( _M_X64 ) || defined( __x86_64__ )
// 使用MSVC的 _umul128 函数进行64位乘法,并得到128位结果
// Use MSVC's _umul128 function for 64-bit multiplication, yielding a 128-bit result
left = _umul128( x, y, &right );
#elif defined( __aarch64__ )
// 在ARM架构上使用__umulh进行64位乘法
// Use __umulh for 64-bit multiplication on ARM architecture
left = x * y;
right = __umulh( x, y );
#elif defined( __POWERPC64__ )
// 在PowerPC64平台上使用内建的__builtin_mulll_overflow进行乘法
// Use __builtin_mulll_overflow for multiplication on PowerPC64 platform
unsigned __int128 product = static_cast<unsigned __int128>( x ) * static_cast<unsigned __int128>( y );
left = static_cast<uint64_t>( product );
right = static_cast<uint64_t>( product >> 64 );
#else
// 如果没有128位整数支持,使用分割乘法方法手动计算128位结果
// If 128-bit integer support is unavailable, manually compute the 128-bit result using split multiplication
uint64_t x_high = x >> 32;
uint64_t x_low = x & 0xFFFFFFFF;
uint64_t y_high = y >> 32;
uint64_t y_low = y & 0xFFFFFFFF;
uint64_t high_high = x_high * y_high;
uint64_t high_low = x_high * y_low;
uint64_t low_high = x_low * y_high;
uint64_t low_low = x_low * y_low;
uint64_t cross = ( high_low & 0xFFFFFFFF ) + ( low_high & 0xFFFFFFFF ) + ( low_low >> 32 );
right = high_high + ( high_low >> 32 ) + ( low_high >> 32 ) + ( cross >> 32 );
left = ( cross << 32 ) | ( low_low & 0xFFFFFFFF );
#endif
}
/**
* @brief Applies a simple bitwise transformation to three 64-bit values.
*
* This function performs a bitwise transformation on the provided three 64-bit values
* stored in the Values3 structure. The transformation is based on a series of XOR and
* AND NOT operations, manipulating the bits to generate a pseudo-randomized output.
*
* @param data A reference to a Values3<uint64_t> structure containing the three 64-bit values.
*/
FORCE_INLINE void chixx( Values3<uint64_t>& data )
{
auto& [ t, u, v ] = data;
// XOR transformation to mix bits of t, u, and v.
// 使用异或变换来混合t、u和v的位。
t ^= ( ~u & v );
u ^= ( ~v & t );
v ^= ( ~t & u );
}
/**
* @brief Applies a 6-layer fractional function to two 64-bit values, with an optional BlindFast mode.
*
* This function processes two 64-bit input values through a fractional function (frac) with 6 layers.
* It either XORs the input with the state or directly manipulates the state based on the BlindFast mode.
*
* @tparam BlindFast A boolean indicating whether to apply the BlindFast optimization.
* @param output A reference to a Values2<uint64_t> structure where the output values are stored.
* @param input A constant reference to a Values2<uint64_t> structure containing the input values.
*/
template <bool BlindFast>
FORCE_INLINE void frac_6( Values2<uint64_t>& output, const Values2<uint64_t>& input )
{
auto& [ p, q ] = input;
auto& [ state_p, state_q ] = output;
Values2<uint64_t> result { 0, 0 };
if constexpr ( !BlindFast )
{
// Update state by XORing with input values p and q
// 通过与输入值p和q进行异或来更新状态
state_p ^= p;
state_q ^= q;
// Multiply state_p and state_q and store the result in result
// 对state_p和state_q进行乘法运算,并将结果存储在result中
multiple64_128bit( state_p, state_q, result );
// XOR the result back into the state
// 将结果再次与状态进行异或运算
state_p ^= result.first;
state_q ^= result.second;
}
else
{
// Directly XOR input with state and then multiply
// 直接将输入与状态异或后再进行乘法运算
multiple64_128bit( state_p ^ p, state_q ^ q, result );
// Update state with the multiplication result
// 使用乘法结果更新状态
state_p = result.first;
state_q = result.second;
}
}
/**
* @brief Applies a 3-layer fractional function to a single 64-bit input, with an optional BlindFast mode.
*
* This function processes a single 64-bit input value through a fractional function (frac) with 3 layers.
* It either XORs the input with the state or directly manipulates the state based on the BlindFast mode.
*
* @tparam BlindFast A boolean indicating whether to apply the BlindFast optimization.
* @param output A reference to a Values2<uint64_t> structure where the output values are stored.
* @param input A 64-bit integer input to be processed.
*/
template <bool BlindFast>
FORCE_INLINE void frac_3( Values2<uint64_t>& output, const uint64_t input )
{
auto& [ state_p, state_q ] = output;
Values2<uint64_t> result { 0, 0 };
if constexpr ( !BlindFast )
{
// XOR the input with state_q to update the state
// 将输入与state_q进行异或以更新状态
state_q ^= input;
// Multiply state_p and state_q and store the result in result
// 对state_p和state_q进行乘法运算,并将结果存储在result中
multiple64_128bit( state_p, state_q, result );
// XOR the result back into the state
// 将结果再次与状态进行异或运算
state_p ^= result.first;
state_q ^= result.second;
}
else
{
// Directly XOR input with state_q and then multiply
// 直接将输入与state_q异或后再进行乘法运算
multiple64_128bit( state_p, state_q ^ input, result );
// Update state with the multiplication result
// 使用乘法结果更新状态
state_p = result.first;
state_q = result.second;
}
}
/**
* @brief Processes a single layer of the 'tower' function, transforming the state and input data.
*
* This function processes a specified layer (layer 0) of a multi-layered 'tower' function.
* It reads bytes from the input, processes them, and combines the result with the current state.
* The transformation includes optional byte-swapping and handles different input lengths accordingly.
*
* @tparam ByteSwap A boolean indicating whether to swap byte order (true for swapping, false for no swap).
* @param state The current state array of 6 elements, each a 64-bit integer.
* @param bytes A pointer to the input byte array.
* @param offset The offset within the byte array to start reading from.
* @param length The length of the byte array segment to process.
* @param result A reference to a Values3<uint64_t> structure where the output values are stored.
*/
template <bool ByteSwap>
FORCE_INLINE void tower_layer_0( State state, const uint8_t* bytes, size_t offset, size_t length, Values3<uint64_t>& result)
{
auto& [a, b, c] = result;
// Check if the offset is within the first two segments
// 检查偏移量是否在前两个分段内
if ( offset <= seg( 2 ) )
{
Values2<uint64_t> data { 0, 0 };
auto& [ i, j ] = data;
// Read a short sequence of bytes with optional byte swapping
// 读取一个短字节序列,支持字节顺序交换
read_short<ByteSwap>( bytes, offset, data );
// Assign the read values to the result variables
// 将读取的值分配给结果变量
a = i;
b = j;
c = 0;
}
else
{
// Read full 64-bit values from the byte array
// 从字节数组中读取完整的64位值
a = read_u64<ByteSwap>( bytes );
b = read_u64<ByteSwap>( bytes + seg( 1 ) );
c = read_u64<ByteSwap>( bytes + offset - seg( 1 ) );
}
// If the length is large enough, apply additional transformations
// 如果长度足够大,应用额外的转换
if ( length >= seg( 3 ) )
{
Values3<uint64_t> temp1 { state[ 0 ], state[ 2 ], state[ 4 ] };
Values3<uint64_t> temp2 { state[ 1 ], state[ 3 ], state[ 5 ] };
// Apply the chi transformation to both temp variables
// 对temp1和temp2应用chi变换
chixx( temp1 );
chixx( temp2 );
// Update the state with the transformed values
// 使用转换后的值更新状态
state[ 0 ] = temp1.first;
state[ 2 ] = temp1.second;
state[ 4 ] = temp1.third;
state[ 1 ] = temp2.first;
state[ 3 ] = temp2.second;
state[ 5 ] = temp2.third;
// XOR the state with the result variables
// 将状态与结果变量进行异或
a ^= state[ 0 ] + state[ 1 ];
b ^= state[ 2 ] + state[ 3 ];
c ^= state[ 4 ] + state[ 5 ];
}
else
{
// Apply a simpler XOR transformation for shorter lengths
// 对较短长度的数据应用简单的异或变换
a ^= state[ 0 ];
b ^= state[ 1 ];
c ^= state[ 2 ];
}
}
/**
* @brief Rotates the bits of a 64-bit unsigned integer to the left.
*
* This function takes a 64-bit unsigned integer and rotates its bits to the left by a specified number of positions.
* The rotation is circular, meaning the bits shifted out on the left are reintroduced on the right.
*
* @param value The 64-bit unsigned integer to rotate.
* @param shift The number of positions to rotate the bits.
* @return The result of the left bitwise rotation.
*/
FORCE_INLINE uint64_t rotate_left(uint64_t value, int shift)
{
// Perform left rotation by shifting the bits left and then ORing with the shifted bits from the right
// 通过将位左移,然后将从右侧移出的位与操作结合,执行左旋转
return (value << shift) | (value >> (64 - shift));
}
/**
* @brief Rotates the bits of a 64-bit unsigned integer to the right.
*
* This function takes a 64-bit unsigned integer and rotates its bits to the right by a specified number of positions.
* The rotation is circular, meaning the bits shifted out on the right are reintroduced on the left.
*
* @param value The 64-bit unsigned integer to rotate.
* @param shift The number of positions to rotate the bits.
* @return The result of the right bitwise rotation.
*/
FORCE_INLINE uint64_t rotate_right(uint64_t value, int shift)
{
// Perform right rotation by shifting the bits right and then ORing with the shifted bits from the left
// 通过将位右移,然后将从左侧移出的位与操作结合,执行右旋转
return (value >> shift) | (value << (64 - shift));
}
template <bool BlindFast>
class MuseAir
{
public:
// 计算64位的MuseAir散列值
template <bool ByteSwap>
inline void hash( const void* bytes, const size_t length, const uint64_t seed, void* result )
{
Values2<uint64_t> values2 {0, 0};
if ( LIKELY( length <= 16 ) )
{
// 更可能会执行的分支
tower_short<ByteSwap>( ( const uint8_t* )bytes, length, seed, values2 );
epi_short( values2 );
}
else
{
Values3<uint64_t> values3 {0, 0, 0};
tower_long<ByteSwap>( ( const uint8_t* )bytes, length, seed, values3 );
epi_long( values3 );
values2.first = values3.first;
values2.second = values3.second;
}
if ( is_little_endian() )
{
write_u64<false>( ( uint8_t* )result, values2.first );
}
else
{
write_u64<true>( ( uint8_t* )result, values2.first );
}
}
// 计算128位的MuseAir散列值
template <bool ByteSwap>
inline void hash_128( const void* bytes, const size_t length, const uint64_t seed, void* result )
{
Values3<uint64_t> values3 {0, 0, 0};
if ( LIKELY( length <= 16 ) )
{
// 更可能会执行的分支
Values2<uint64_t> values {0, 0};
tower_short<ByteSwap>( ( const uint8_t* )bytes, length, seed, values );
epi_short_128( values );
values3.first = values.first;
values3.second = values.second;
values3.third = 0; // 如果在这个路径下不需要第三个值
}
else
{
tower_long<ByteSwap>( ( const uint8_t* )bytes, length, seed, values3 );
epi_long_128( values3 );
}
if ( is_little_endian() )
{
write_u64<false>( ( uint8_t* )result, values3.first );
write_u64<false>( ( uint8_t* )result + 8, values3.second );
}
else
{
write_u64<true>( ( uint8_t* )result, values3.first );
write_u64<true>( ( uint8_t* )result + 8, values3.second );
}
}
private:
/**
* @brief Processes a specific layer (x) of the 'tower' function, performing bitwise operations and multiplications.
*
* This function handles the x-th layer of a multi-layered 'tower' function. It includes rotations, XOR operations,
* and multiple 64-bit to 128-bit multiplications. The logic is optimized for performance based on the input size
* and the platform's specific characteristics.
*
* @param total_length The total length of the input data, used for determining rotation amounts.
* @param data A reference to a Values3<uint64_t> structure where the input and output values are stored.
*/
FORCE_INLINE void tower_layer_x( const size_t total_length, Values3<uint64_t>& data )
{
// First of all, `tower_long` must not be inlined, otherwise it will slow down for all input sizes.
// If this function is in `tower_loong`, it will always be ~1 GiB/s faster for bulk.
// This function, if placed in `epi_loong_*`, is ~2 cyc slower for keys smaller than 16-bytes, and ~3 cyc faster for keys larger than 16-bytes.
// Currently, the biggest draw of MuseAir is the speed of bulk processing, so this function should be in `tower_loong`.
// These features may be machine-specific or related to cache performance. But I think, regardless, figuring out how to make `tower_short` capable of handling longer keys is the best solution.
// 首先,`tower_long`不得内联,否则对于所有大小的输入都会变慢。
// 这个函数如果放在`tower_loong`里,那么对于 bulk 而言总是能够提速 ~1 GiB/s。
// 这个函数如果放在`epi_loong_*`里,那么对小于 16-bytes 的 key 而言会慢 ~2 cyc,对大于 16-bytes 的 key 而言会快 ~3 cyc。
// 目前 MuseAir 最大的亮点是对 bulk 的处理速度,所以这个函数应该放在`tower_loong`里。
// 这些特性可能是机器特定的,或与缓存性能相关。但我想,不论如何,想办法让`tower_short`能够处理更长的 key 才是最好的解决方案。
auto& [ i, j, k ] = data;
// Calculate rotation based on the total length, mod 64 (x AND 0b111111)
// 根据总长度计算旋转量,取模64
size_t rotate = total_length & 63;
// Apply the chi transformation to the data
// 对数据应用chi变换
chixx( data );
// Perform bitwise rotations on i and j
// 对i和j执行按位旋转
i = rotate_left( i, rotate );
j = rotate_right( j, rotate );
// XOR k with the total length
// 将k与总长度进行异或
k ^= total_length;
Values2<uint64_t> data0 { 0, 0 };
Values2<uint64_t> data1 { 0, 0 };
Values2<uint64_t> data2 { 0, 0 };
if constexpr ( !BlindFast )
{
// Perform 64x64 to 128-bit multiplications with secrets and XOR the results
// 使用密钥执行64x64到128位的乘法,并将结果进行异或
multiple64_128bit( i ^ DEFAULT_SECRET[ 3 ], j, data0 );
multiple64_128bit( j ^ DEFAULT_SECRET[ 4 ], k, data1 );
multiple64_128bit( k ^ DEFAULT_SECRET[ 5 ], i, data2 );
auto& [low0, high0] = data0;
auto& [low1, high1] = data1;
auto& [low2, high2] = data2;
// XOR the multiplication results into i, j, and k
// 将乘法结果与i, j, k进行异或
i ^= low0 ^ high2;
j ^= low1 ^ high0;
k ^= low2 ^ high1;
}
else
{
// Perform 64x64 to 128-bit multiplications without secrets and assign the results directly
// 不使用密钥执行64x64到128位的乘法,并直接分配结果
multiple64_128bit( i, j, data0 );
multiple64_128bit( j, k, data1 );
multiple64_128bit( k, i, data2 );
auto& [low0, high0] = data0;
auto& [low1, high1] = data1;
auto& [low2, high2] = data2;
// Assign the multiplication results directly to i, j, and k
// 将乘法结果直接分配给i, j, k
i = low0 ^ high2;
j = low1 ^ high0;
k = low2 ^ high1;
}
}
/**
* @brief Processes the third layer of the 'tower' function, applying transformations to specific pairs of the state.
*
* This function processes the third layer of a multi-layered 'tower' function. It reads input bytes, applies the `frac_3`
* function to specific pairs of the state array, and updates the state with the results.
*
* @tparam ByteSwap A boolean indicating whether to swap byte order (true for swapping, false for no swap).
* @param state A reference to the current state array, consisting of six 64-bit integers.
* @param bytes A pointer to the input byte array.
*/
template <bool ByteSwap>
FORCE_INLINE void tower_layer_3( State& state, const uint8_t* bytes )
{
Values2<uint64_t> output { 0, 0 };
// Process the first pair (state[0] and state[3])
// 处理第一对(state[0] 和 state[3])
output = { state[ 0 ], state[ 3 ] };
frac_3<BlindFast>( output, read_u64<ByteSwap>( bytes + seg( 0 ) ) );
state[ 0 ] = output.first;
state[ 3 ] = output.second;
// Process the second pair (state[1] and state[4])
// 处理第二对(state[1] 和 state[4])
output = { state[ 1 ], state[ 4 ] };
frac_3<BlindFast>( output, read_u64<ByteSwap>( bytes + seg( 1 ) ) );
state[ 1 ] = output.first;
state[ 4 ] = output.second;
// Process the third pair (state[2] and state[5])
// 处理第三对(state[2] 和 state[5])
output = { state[ 2 ], state[ 5 ] };
frac_3<BlindFast>( output, read_u64<ByteSwap>( bytes + seg( 2 ) ) );
state[ 2 ] = output.first;
state[ 5 ] = output.second;
}
/**
* @brief Processes the sixth layer of the 'tower' function, applying transformations to specific pairs of the state.
*
* This function processes the sixth layer of a multi-layered 'tower' function. It reads input bytes, applies the `frac_6`
* function to specific pairs of the state array, and updates the state with the results.
*
* @tparam ByteSwap A boolean indicating whether to swap byte order (true for swapping, false for no swap).
* @param state A reference to the current state array, consisting of six 64-bit integers.
* @param bytes A pointer to the input byte array.
*/
template <bool ByteSwap>
FORCE_INLINE void tower_layer_6( State& state, const uint8_t* bytes )
{
Values2<uint64_t> input { 0, 0 }, output { 0, 0 };
// Process the first pair (state[0] and state[1])
// 处理第一对(state[0] 和 state[1])
input = { read_u64<ByteSwap>( bytes + seg( 0 ) ), read_u64<ByteSwap>( bytes + seg( 1 ) ) };
output = { state[ 0 ], state[ 1 ] };
frac_6<BlindFast>( output, input );
state[ 0 ] = output.first;
state[ 1 ] = output.second;
// Process the second pair (state[2] and state[3])
// 处理第二对(state[2] 和 state[3]
input = { read_u64<ByteSwap>( bytes + seg( 2 ) ), read_u64<ByteSwap>( bytes + seg( 3 ) ) };
output = { state[ 2 ], state[ 3 ] };
frac_6<BlindFast>( output, input );
state[ 2 ] = output.first;
state[ 3 ] = output.second;
// Process the third pair (state[4] and state[5])
// 处理第三对(state[4] 和 state[5])
input = { read_u64<ByteSwap>( bytes + seg( 4 ) ), read_u64<ByteSwap>( bytes + seg( 5 ) ) };
output = { state[ 4 ], state[ 5 ] };
frac_6<BlindFast>( output, input );
state[ 4 ] = output.first;
state[ 5 ] = output.second;
}
/**
* @brief Processes the twelfth layer of the 'tower' function, applying multiple 64x64 to 128-bit multiplications
* and updating the state with the results.
*
* This function is a critical component of the MuseAir hashing algorithm. It processes the twelfth layer
* by reading input bytes, performing multiple 64-bit multiplications, and updating the internal state accordingly.
* The function supports an optional `BlindFast` mode that optimizes for speed by simplifying the state updates.
*
* @tparam ByteSwap A boolean indicating whether to swap byte order (true for swapping, false for no swap).
* @param state A reference to the current state array, consisting of six 64-bit integers.
* @param bytes A pointer to the input byte array.
* @param ring_prev A reference to the previous ring value, used in the state update.
*/
template <bool ByteSwap>
NEVER_INLINE void tower_layer_12(State& state, const uint8_t* bytes, uint64_t& ring_prev )
{
Values2<uint64_t> result {0, 0};
if constexpr ( !BlindFast )
{
// If BlindFast mode is not enabled, apply full processing including modular additions
// 如果未启用BlindFast模式,则应用包括模加操作在内的完整处理
// First pair (state[0] and state[1])
// 处理第一对(state[0] 和 state[1])
state[ 0 ] ^= read_u64<ByteSwap>( bytes );
state[ 1 ] ^= read_u64<ByteSwap>( bytes + 8 );
multiple64_128bit( state[ 0 ], state[ 1 ], result );
auto [low0, high0] = result;
state[ 0 ] += ( ring_prev ^ high0 );
// Update state with ring accumulator and high0
// 使用环形累加器和high0更新状态
// Second pair (state[1] and state[2])
// 处理第二对(state[1] 和 state[2])
state[ 1 ] ^= read_u64<ByteSwap>( bytes + 16 );
state[ 2 ] ^= read_u64<ByteSwap>( bytes + 24 );
multiple64_128bit( state[ 1 ], state[ 2 ], result );
auto [low1, high1] = result;
state[ 1 ] += ( low0 ^ high1 );
// Update state with low0 and high1
// 使用low0和high1更新状态
// Third pair (state[2] and state[3])
// 处理第三对(state[2] 和 state[3])
state[ 2 ] ^= read_u64<ByteSwap>( bytes + 32 );
state[ 3 ] ^= read_u64<ByteSwap>( bytes + 40 );
multiple64_128bit( state[ 2 ], state[ 3 ], result );
auto [low2, high2] = result;
state[ 2 ] += ( low1 ^ high2 );
// Update state with low1 and high2
// 使用low1和high2更新状态
// Fourth pair (state[3] and state[4])
// 处理第四对(state[3] 和 state[4])
state[ 3 ] ^= read_u64<ByteSwap>( bytes + 48 );
state[ 4 ] ^= read_u64<ByteSwap>( bytes + 56 );
multiple64_128bit( state[ 3 ], state[ 4 ], result );
auto [low3, high3] = result;
state[ 3 ] += ( low2 ^ high3 );
// Update state with low2 and high3
// 使用low2和high3更新状态
// Fifth pair (state[4] and state[5])
// 处理第五对(state[4] 和 state[5])
state[ 4 ] ^= read_u64<ByteSwap>( bytes + 64 );
state[ 5 ] ^= read_u64<ByteSwap>( bytes + 72 );
multiple64_128bit( state[ 4 ], state[ 5 ], result );
auto [low4, high4] = result;
state[ 4 ] += ( low3 ^ high4 );
// Update state with low3 and high4
// 使用low3和high4更新状态
// Final pair (state[5] and state[0])
// 处理最后一对(state[5] 和 state[0])
state[ 5 ] ^= read_u64<ByteSwap>( bytes + 80 );
state[ 0 ] ^= read_u64<ByteSwap>( bytes + 88 );
multiple64_128bit( state[ 5 ], state[ 0 ], result );
auto [low5, high5] = result;
state[ 5 ] += ( low4 ^ high5 );
// Update state with low4 and high5
// 使用low4和high5更新状态
ring_prev = low5;
// Update the ring accumulator with low5
// 使用low5更新环形累加器
}
else
{
// Apply the BlindFast mode optimizations by directly setting the state without modular additions
// 应用BlindFast模式优化,直接设置状态而不进行模加操作
auto& [low, high] = result;
state[ 0 ] ^= read_u64<ByteSwap>( bytes );
state[ 1 ] ^= read_u64<ByteSwap>( bytes + 8 );
multiple64_128bit( state[ 0 ], state[ 1 ], result );
auto [low0, high0] = result;
state[ 0 ] = ( ring_prev ^ high0 );
// Directly set state with ring accumulator and high0
// 直接使用环形累加器和high0设置状态
state[ 1 ] ^= read_u64<ByteSwap>( bytes + 16 );
state[ 2 ] ^= read_u64<ByteSwap>( bytes + 24 );
multiple64_128bit( state[ 1 ], state[ 2 ], result );
auto [low1, high1] = result;
state[ 1 ] = ( low0 ^ high1 );
// Directly set state with low0 and high1
// 直接使用low0和high1设置状态
state[ 2 ] ^= read_u64<ByteSwap>( bytes + 32 );
state[ 3 ] ^= read_u64<ByteSwap>( bytes + 40 );
multiple64_128bit( state[ 2 ], state[ 3 ], result );
auto [low2, high2] = result;
state[ 2 ] = ( low1 ^ high2 );
// Directly set state with low1 and high2
// 直接使用low1和high2设置状态
state[ 3 ] ^= read_u64<ByteSwap>( bytes + 48 );
state[ 4 ] ^= read_u64<ByteSwap>( bytes + 56 );
multiple64_128bit( state[ 3 ], state[ 4 ], result );
auto [low3, high3] = result;
state[ 3 ] = ( low2 ^ high3 );
// Directly set state with low2 and high3
// 直接使用low2和high3设置状态
state[ 4 ] ^= read_u64<ByteSwap>( bytes + 64 );
state[ 5 ] ^= read_u64<ByteSwap>( bytes + 72 );
multiple64_128bit( state[ 4 ], state[ 5 ], result );
auto [low4, high4] = result;
state[ 4 ] = ( low3 ^ high4 );
// Directly set state with low3 and high4
// 直接使用low3和high4设置状态
state[ 5 ] ^= read_u64<ByteSwap>( bytes + 80 );
state[ 0 ] ^= read_u64<ByteSwap>( bytes + 88 );
multiple64_128bit( state[ 5 ], state[ 0 ], result );
auto [low5, high5] = result;
state[ 5 ] = ( low4 ^ high5 );
// Directly set state with low4 and high5
// 直接使用low4和high5设置状态
ring_prev = low5;
// Update the ring accumulator with low5
// 使用low5更新环形累加器
}
}
/**
* @brief Processes a long input using multiple layers of the 'tower' function in the MuseAir algorithm.
*
* This function handles large inputs by iteratively processing the data through several layers of the 'tower' function,
* optimized for high performance. It uses a combination of XOR operations, modular additions, and multiplications
* to ensure thorough mixing of the input data, updating the state and producing a robust hash output.
*
* @tparam ByteSwap A boolean indicating whether to swap byte order (true for swapping, false for no swap).
* @param bytes A pointer to the input byte array.
* @param length The length of the input data in bytes.
* @param seed A 64-bit seed value used to initialize the state.
* @param data A reference to a Values3<uint64_t> structure where the processed output is stored.
*/
template <bool ByteSwap>
NEVER_INLINE void tower_long( const uint8_t* bytes, const size_t length, const uint64_t seed, Values3<uint64_t>& data )
{
auto& [i, j, k] = data;
const uint8_t* p = bytes;
size_t q = length;
// Initialize the state with the seed and default secret values
// 使用种子和默认密钥值初始化状态
State state =
{
DEFAULT_SECRET[ 0 ] + seed,
DEFAULT_SECRET[ 1 ] - seed,
DEFAULT_SECRET[ 2 ] ^ seed,
DEFAULT_SECRET[ 3 ],
DEFAULT_SECRET[ 4 ],
DEFAULT_SECRET[ 5 ]
};
if ( q >= seg( 12 ) )
{
// Initialize the state with the seed and default secret values
// 使用种子和默认密钥值初始化状态
state[ 3 ] += seed;
state[ 4 ] -= seed;
state[ 5 ] ^= seed;
uint64_t ring_prev = INIT_RING_PREV;
do
{
// Process the input data through the 12-layer tower function
// 通过12层塔函数处理输入数据
tower_layer_12<ByteSwap>( state, p, ring_prev );
p += seg( 12 );
q -= seg( 12 );
} while ( LIKELY( q >= seg( 12 ) ) );
// XOR the last ring value into the state for final mixing
// 将最后的环形值异或到状态中以进行最终混合
// Note:If you replace heteroscedasticity with modulo addition, the performance drops by about 1.7% (p < 0.05)
// 注意:如果将异或替换为模加法,性能将下降约1.7% (p < 0.05)
state[ 0 ] ^= ring_prev;
}
if ( q >= seg( 6 ) )
{
// Process any remaining data through the 6-layer tower function
// 通过6层塔函数处理剩余数据
tower_layer_6<ByteSwap>( state, p );
p += seg( 6 );
q -= seg( 6 );
}
if ( q >= seg( 3 ) )
{
// Process any remaining data through the 3-layer tower function
// 通过3层塔函数处理剩余数据
tower_layer_3<ByteSwap>( state, p );
p += seg( 3 );
q -= seg( 3 );
}
// Final processing of any leftover data
// 对剩余数据进行最终处理
tower_layer_0<ByteSwap>( state, p, q, length, data );
tower_layer_x( length, data );
}
/**
* @brief Processes a short input using the 'tower' function optimized for small data.
*
* This function handles small inputs by applying a series of operations designed for
* quick processing while maintaining the integrity of the output. It combines XOR
* operations, length, and seed values with the input data, using the `frac` function
* to update the state.
*
* @tparam ByteSwap A boolean indicating whether to swap byte order (true for swapping, false for no swap).
* @param bytes A pointer to the input byte array.
* @param len The length of the input data in bytes.
* @param seed A 64-bit seed value used in processing.
* @param data A reference to a Values2<uint64_t> structure where the processed output is stored.
*/
template <bool ByteSwap>
FORCE_INLINE void tower_short( const uint8_t* bytes, const size_t len, const uint64_t seed, Values2<uint64_t>& data )
{
// Read and process a short sequence of bytes
// 读取并处理一个短字节序列
read_short<ByteSwap>( bytes, len, data );
Values2<uint64_t> result {0, 0};
// Perform 64x64 to 128-bit multiplication with seed and length mixed with secret values
// 使用种子和长度与密钥值混合进行64x64到128位的乘法运算
multiple64_128bit( seed ^ DEFAULT_SECRET[ 0 ], len ^ DEFAULT_SECRET[ 1 ], result );
auto& [low, high] = result;
auto& [a, b] = data;
// XOR the results back into the data for final output
// 将结果与数据进行异或操作以生成最终输出
a ^= low ^ len;
b ^= high ^ seed;
}
static FORCE_INLINE void epi_short( Values2<uint64_t>& data )
{
auto& [a, b] = data;
Values2<uint64_t> result {0, 0};
a ^= DEFAULT_SECRET[ 2 ];
b ^= DEFAULT_SECRET[ 3 ];
multiple64_128bit(a, b, result);
auto& [low, high] = result;
a ^= low ^ DEFAULT_SECRET[ 4 ];
b ^= high ^ DEFAULT_SECRET[ 5 ];
multiple64_128bit(a, b, result );
a ^= b ^ low ^ high;
}
static FORCE_INLINE void epi_short_128( Values2<uint64_t>& values )
{
auto& [ i, j ] = values;
Values2<uint64_t> result0 {0, 0};
Values2<uint64_t> result1 {0, 0};
if constexpr ( !BlindFast )
{
multiple64_128bit( i ^ DEFAULT_SECRET[ 2 ], j, result0 );
multiple64_128bit( i, j ^ DEFAULT_SECRET[ 3 ], result1 );
auto& [lo0 , hi0] = result0;
auto& [lo1 , hi1] = result1;
i ^= lo0 ^ hi1;