xref: /aosp_15_r20/external/mesa3d/src/util/blake3/blake3_impl.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 #ifndef BLAKE3_IMPL_H
2 #define BLAKE3_IMPL_H
3 
4 #include <assert.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <string.h>
9 
10 #include "blake3.h"
11 
12 // internal flags
13 enum blake3_flags {
14   CHUNK_START         = 1 << 0,
15   CHUNK_END           = 1 << 1,
16   PARENT              = 1 << 2,
17   ROOT                = 1 << 3,
18   KEYED_HASH          = 1 << 4,
19   DERIVE_KEY_CONTEXT  = 1 << 5,
20   DERIVE_KEY_MATERIAL = 1 << 6,
21 };
22 
23 // This C implementation tries to support recent versions of GCC, Clang, and
24 // MSVC.
25 #if defined(_MSC_VER)
26 #define INLINE static __forceinline
27 #else
28 #define INLINE static inline __attribute__((always_inline))
29 #endif
30 
31 #if defined(__x86_64__) || defined(_M_X64) && !defined(_M_ARM64EC)
32 #define IS_X86
33 #define IS_X86_64
34 #endif
35 
36 #if defined(__i386__) || defined(_M_IX86)
37 #define IS_X86
38 #define IS_X86_32
39 #endif
40 
41 #if defined(__aarch64__) || defined(_M_ARM64)|| defined(_M_ARM64EC)
42 #define IS_AARCH64
43 #endif
44 
45 #if defined(IS_X86)
46 #if defined(_MSC_VER)
47 #include <intrin.h>
48 #endif
49 #endif
50 
51 #if !defined(BLAKE3_USE_NEON)
52   // If BLAKE3_USE_NEON not manually set, autodetect based on AArch64ness
53   #if defined(IS_AARCH64)
54     #if defined(__ARM_BIG_ENDIAN)
55       #define BLAKE3_USE_NEON 0
56     #else
57       #define BLAKE3_USE_NEON 1
58     #endif
59   #else
60     #define BLAKE3_USE_NEON 0
61   #endif
62 #endif
63 
64 #if defined(IS_X86)
65 #define MAX_SIMD_DEGREE 16
66 #elif BLAKE3_USE_NEON == 1
67 #define MAX_SIMD_DEGREE 4
68 #else
69 #define MAX_SIMD_DEGREE 1
70 #endif
71 
72 // There are some places where we want a static size that's equal to the
73 // MAX_SIMD_DEGREE, but also at least 2.
74 #define MAX_SIMD_DEGREE_OR_2 (MAX_SIMD_DEGREE > 2 ? MAX_SIMD_DEGREE : 2)
75 
76 static const uint32_t IV[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL,
77                                0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL,
78                                0x1F83D9ABUL, 0x5BE0CD19UL};
79 
80 static const uint8_t MSG_SCHEDULE[7][16] = {
81     {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
82     {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8},
83     {3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1},
84     {10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6},
85     {12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4},
86     {9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7},
87     {11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13},
88 };
89 
90 /* Find index of the highest set bit */
91 /* x is assumed to be nonzero.       */
highest_one(uint64_t x)92 static unsigned int highest_one(uint64_t x) {
93 #if defined(__GNUC__) || defined(__clang__)
94   return 63 ^ (unsigned int)__builtin_clzll(x);
95 #elif defined(_MSC_VER) && defined(IS_X86_64)
96   unsigned long index;
97   _BitScanReverse64(&index, x);
98   return index;
99 #elif defined(_MSC_VER) && defined(IS_X86_32)
100   if(x >> 32) {
101     unsigned long index;
102     _BitScanReverse(&index, (unsigned long)(x >> 32));
103     return 32 + index;
104   } else {
105     unsigned long index;
106     _BitScanReverse(&index, (unsigned long)x);
107     return index;
108   }
109 #else
110   unsigned int c = 0;
111   if(x & 0xffffffff00000000ULL) { x >>= 32; c += 32; }
112   if(x & 0x00000000ffff0000ULL) { x >>= 16; c += 16; }
113   if(x & 0x000000000000ff00ULL) { x >>=  8; c +=  8; }
114   if(x & 0x00000000000000f0ULL) { x >>=  4; c +=  4; }
115   if(x & 0x000000000000000cULL) { x >>=  2; c +=  2; }
116   if(x & 0x0000000000000002ULL) {           c +=  1; }
117   return c;
118 #endif
119 }
120 
121 // Count the number of 1 bits.
popcnt(uint64_t x)122 INLINE unsigned int popcnt(uint64_t x) {
123 #if defined(__GNUC__) || defined(__clang__)
124   return (unsigned int)__builtin_popcountll(x);
125 #else
126   unsigned int count = 0;
127   while (x != 0) {
128     count += 1;
129     x &= x - 1;
130   }
131   return count;
132 #endif
133 }
134 
135 // Largest power of two less than or equal to x. As a special case, returns 1
136 // when x is 0.
round_down_to_power_of_2(uint64_t x)137 INLINE uint64_t round_down_to_power_of_2(uint64_t x) {
138   return 1ULL << highest_one(x | 1);
139 }
140 
counter_low(uint64_t counter)141 INLINE uint32_t counter_low(uint64_t counter) { return (uint32_t)counter; }
142 
counter_high(uint64_t counter)143 INLINE uint32_t counter_high(uint64_t counter) {
144   return (uint32_t)(counter >> 32);
145 }
146 
load32(const void * src)147 INLINE uint32_t load32(const void *src) {
148   const uint8_t *p = (const uint8_t *)src;
149   return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |
150          ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);
151 }
152 
load_key_words(const uint8_t key[BLAKE3_KEY_LEN],uint32_t key_words[8])153 INLINE void load_key_words(const uint8_t key[BLAKE3_KEY_LEN],
154                            uint32_t key_words[8]) {
155   key_words[0] = load32(&key[0 * 4]);
156   key_words[1] = load32(&key[1 * 4]);
157   key_words[2] = load32(&key[2 * 4]);
158   key_words[3] = load32(&key[3 * 4]);
159   key_words[4] = load32(&key[4 * 4]);
160   key_words[5] = load32(&key[5 * 4]);
161   key_words[6] = load32(&key[6 * 4]);
162   key_words[7] = load32(&key[7 * 4]);
163 }
164 
store32(void * dst,uint32_t w)165 INLINE void store32(void *dst, uint32_t w) {
166   uint8_t *p = (uint8_t *)dst;
167   p[0] = (uint8_t)(w >> 0);
168   p[1] = (uint8_t)(w >> 8);
169   p[2] = (uint8_t)(w >> 16);
170   p[3] = (uint8_t)(w >> 24);
171 }
172 
store_cv_words(uint8_t bytes_out[32],uint32_t cv_words[8])173 INLINE void store_cv_words(uint8_t bytes_out[32], uint32_t cv_words[8]) {
174   store32(&bytes_out[0 * 4], cv_words[0]);
175   store32(&bytes_out[1 * 4], cv_words[1]);
176   store32(&bytes_out[2 * 4], cv_words[2]);
177   store32(&bytes_out[3 * 4], cv_words[3]);
178   store32(&bytes_out[4 * 4], cv_words[4]);
179   store32(&bytes_out[5 * 4], cv_words[5]);
180   store32(&bytes_out[6 * 4], cv_words[6]);
181   store32(&bytes_out[7 * 4], cv_words[7]);
182 }
183 
184 void blake3_compress_in_place(uint32_t cv[8],
185                               const uint8_t block[BLAKE3_BLOCK_LEN],
186                               uint8_t block_len, uint64_t counter,
187                               uint8_t flags);
188 
189 void blake3_compress_xof(const uint32_t cv[8],
190                          const uint8_t block[BLAKE3_BLOCK_LEN],
191                          uint8_t block_len, uint64_t counter, uint8_t flags,
192                          uint8_t out[64]);
193 
194 void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs,
195                       size_t blocks, const uint32_t key[8], uint64_t counter,
196                       bool increment_counter, uint8_t flags,
197                       uint8_t flags_start, uint8_t flags_end, uint8_t *out);
198 
199 size_t blake3_simd_degree(void);
200 
201 
202 // Declarations for implementation-specific functions.
203 void blake3_compress_in_place_portable(uint32_t cv[8],
204                                        const uint8_t block[BLAKE3_BLOCK_LEN],
205                                        uint8_t block_len, uint64_t counter,
206                                        uint8_t flags);
207 
208 void blake3_compress_xof_portable(const uint32_t cv[8],
209                                   const uint8_t block[BLAKE3_BLOCK_LEN],
210                                   uint8_t block_len, uint64_t counter,
211                                   uint8_t flags, uint8_t out[64]);
212 
213 void blake3_hash_many_portable(const uint8_t *const *inputs, size_t num_inputs,
214                                size_t blocks, const uint32_t key[8],
215                                uint64_t counter, bool increment_counter,
216                                uint8_t flags, uint8_t flags_start,
217                                uint8_t flags_end, uint8_t *out);
218 
219 #if defined(IS_X86)
220 #if !defined(BLAKE3_NO_SSE2)
221 void blake3_compress_in_place_sse2(uint32_t cv[8],
222                                    const uint8_t block[BLAKE3_BLOCK_LEN],
223                                    uint8_t block_len, uint64_t counter,
224                                    uint8_t flags);
225 void blake3_compress_xof_sse2(const uint32_t cv[8],
226                               const uint8_t block[BLAKE3_BLOCK_LEN],
227                               uint8_t block_len, uint64_t counter,
228                               uint8_t flags, uint8_t out[64]);
229 void blake3_hash_many_sse2(const uint8_t *const *inputs, size_t num_inputs,
230                            size_t blocks, const uint32_t key[8],
231                            uint64_t counter, bool increment_counter,
232                            uint8_t flags, uint8_t flags_start,
233                            uint8_t flags_end, uint8_t *out);
234 #endif
235 #if !defined(BLAKE3_NO_SSE41)
236 void blake3_compress_in_place_sse41(uint32_t cv[8],
237                                     const uint8_t block[BLAKE3_BLOCK_LEN],
238                                     uint8_t block_len, uint64_t counter,
239                                     uint8_t flags);
240 void blake3_compress_xof_sse41(const uint32_t cv[8],
241                                const uint8_t block[BLAKE3_BLOCK_LEN],
242                                uint8_t block_len, uint64_t counter,
243                                uint8_t flags, uint8_t out[64]);
244 void blake3_hash_many_sse41(const uint8_t *const *inputs, size_t num_inputs,
245                             size_t blocks, const uint32_t key[8],
246                             uint64_t counter, bool increment_counter,
247                             uint8_t flags, uint8_t flags_start,
248                             uint8_t flags_end, uint8_t *out);
249 #endif
250 #if !defined(BLAKE3_NO_AVX2)
251 void blake3_hash_many_avx2(const uint8_t *const *inputs, size_t num_inputs,
252                            size_t blocks, const uint32_t key[8],
253                            uint64_t counter, bool increment_counter,
254                            uint8_t flags, uint8_t flags_start,
255                            uint8_t flags_end, uint8_t *out);
256 #endif
257 #if !defined(BLAKE3_NO_AVX512)
258 void blake3_compress_in_place_avx512(uint32_t cv[8],
259                                      const uint8_t block[BLAKE3_BLOCK_LEN],
260                                      uint8_t block_len, uint64_t counter,
261                                      uint8_t flags);
262 
263 void blake3_compress_xof_avx512(const uint32_t cv[8],
264                                 const uint8_t block[BLAKE3_BLOCK_LEN],
265                                 uint8_t block_len, uint64_t counter,
266                                 uint8_t flags, uint8_t out[64]);
267 
268 void blake3_hash_many_avx512(const uint8_t *const *inputs, size_t num_inputs,
269                              size_t blocks, const uint32_t key[8],
270                              uint64_t counter, bool increment_counter,
271                              uint8_t flags, uint8_t flags_start,
272                              uint8_t flags_end, uint8_t *out);
273 #endif
274 #endif
275 
276 #if BLAKE3_USE_NEON == 1
277 void blake3_hash_many_neon(const uint8_t *const *inputs, size_t num_inputs,
278                            size_t blocks, const uint32_t key[8],
279                            uint64_t counter, bool increment_counter,
280                            uint8_t flags, uint8_t flags_start,
281                            uint8_t flags_end, uint8_t *out);
282 #endif
283 
284 
285 #endif /* BLAKE3_IMPL_H */
286