xref: /aosp_15_r20/external/lzma/C/Blake2.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 /* Blake2.h -- BLAKE2sp Hash
2 2024-01-17 : Igor Pavlov : Public domain */
3 
4 #ifndef ZIP7_INC_BLAKE2_H
5 #define ZIP7_INC_BLAKE2_H
6 
7 #include "7zTypes.h"
8 
9 #if 0
10 #include "Compiler.h"
11 #include "CpuArch.h"
12 #if defined(MY_CPU_X86_OR_AMD64)
13 #if defined(__SSE2__) \
14     || defined(_MSC_VER) && _MSC_VER > 1200 \
15     || defined(Z7_GCC_VERSION) && (Z7_GCC_VERSION >= 30300) \
16     || defined(__clang__) \
17     || defined(__INTEL_COMPILER)
18 #include <emmintrin.h> // SSE2
19 #endif
20 
21 #if defined(__AVX2__) \
22     || defined(Z7_GCC_VERSION) && (Z7_GCC_VERSION >= 40900) \
23     || defined(Z7_APPLE_CLANG_VERSION) && (Z7_APPLE_CLANG_VERSION >= 40600) \
24     || defined(Z7_LLVM_CLANG_VERSION) && (Z7_LLVM_CLANG_VERSION >= 30100) \
25     || defined(Z7_MSC_VER_ORIGINAL) && (Z7_MSC_VER_ORIGINAL >= 1800) \
26     || defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1400)
27 #include <immintrin.h>
28 #if defined(__clang__)
29 #include <avxintrin.h>
30 #include <avx2intrin.h>
31 #endif
32 #endif // avx2
33 #endif // MY_CPU_X86_OR_AMD64
34 #endif // 0
35 
36 EXTERN_C_BEGIN
37 
38 #define Z7_BLAKE2S_BLOCK_SIZE         64
39 #define Z7_BLAKE2S_DIGEST_SIZE        32
40 #define Z7_BLAKE2SP_PARALLEL_DEGREE   8
41 #define Z7_BLAKE2SP_NUM_STRUCT_WORDS  16
42 
43 #if 1 || defined(Z7_BLAKE2SP_USE_FUNCTIONS)
44 typedef void (Z7_FASTCALL *Z7_BLAKE2SP_FUNC_COMPRESS)(UInt32 *states, const Byte *data, const Byte *end);
45 typedef void (Z7_FASTCALL *Z7_BLAKE2SP_FUNC_INIT)(UInt32 *states);
46 #endif
47 
48 // it's required that CBlake2sp is aligned for 32-bytes,
49 // because the code can use unaligned access with sse and avx256.
50 // but 64-bytes alignment can be better.
51 MY_ALIGN(64)
52 typedef struct
53 {
54   union
55   {
56 #if 0
57 #if defined(MY_CPU_X86_OR_AMD64)
58 #if defined(__SSE2__) \
59     || defined(_MSC_VER) && _MSC_VER > 1200 \
60     || defined(Z7_GCC_VERSION) && (Z7_GCC_VERSION >= 30300) \
61     || defined(__clang__) \
62     || defined(__INTEL_COMPILER)
63     __m128i _pad_align_128bit[4];
64 #endif // sse2
65 #if defined(__AVX2__) \
66     || defined(Z7_GCC_VERSION) && (Z7_GCC_VERSION >= 40900) \
67     || defined(Z7_APPLE_CLANG_VERSION) && (Z7_APPLE_CLANG_VERSION >= 40600) \
68     || defined(Z7_LLVM_CLANG_VERSION) && (Z7_LLVM_CLANG_VERSION >= 30100) \
69     || defined(Z7_MSC_VER_ORIGINAL) && (Z7_MSC_VER_ORIGINAL >= 1800) \
70     || defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1400)
71     __m256i _pad_align_256bit[2];
72 #endif // avx2
73 #endif // x86
74 #endif // 0
75 
76     void * _pad_align_ptr[8];
77     UInt32 _pad_align_32bit[16];
78     struct
79     {
80       unsigned cycPos;
81       unsigned _pad_unused;
82 #if 1 || defined(Z7_BLAKE2SP_USE_FUNCTIONS)
83       Z7_BLAKE2SP_FUNC_COMPRESS func_Compress_Fast;
84       Z7_BLAKE2SP_FUNC_COMPRESS func_Compress_Single;
85       Z7_BLAKE2SP_FUNC_INIT func_Init;
86       Z7_BLAKE2SP_FUNC_INIT func_Final;
87 #endif
88     } header;
89   } u;
90   // MY_ALIGN(64)
91   UInt32 states[Z7_BLAKE2SP_PARALLEL_DEGREE * Z7_BLAKE2SP_NUM_STRUCT_WORDS];
92   // MY_ALIGN(64)
93   UInt32  buf32[Z7_BLAKE2SP_PARALLEL_DEGREE * Z7_BLAKE2SP_NUM_STRUCT_WORDS * 2];
94 } CBlake2sp;
95 
96 BoolInt Blake2sp_SetFunction(CBlake2sp *p, unsigned algo);
97 void Blake2sp_Init(CBlake2sp *p);
98 void Blake2sp_InitState(CBlake2sp *p);
99 void Blake2sp_Update(CBlake2sp *p, const Byte *data, size_t size);
100 void Blake2sp_Final(CBlake2sp *p, Byte *digest);
101 void z7_Black2sp_Prepare(void);
102 
103 EXTERN_C_END
104 
105 #endif
106