• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     #define BLAKE3_USE_NEON 1
55   #else
56     #define BLAKE3_USE_NEON 0
57   #endif
58 #endif
59 
60 #if defined(IS_X86)
61 #define MAX_SIMD_DEGREE 16
62 #elif BLAKE3_USE_NEON == 1
63 #define MAX_SIMD_DEGREE 4
64 #else
65 #define MAX_SIMD_DEGREE 1
66 #endif
67 
68 // There are some places where we want a static size that's equal to the
69 // MAX_SIMD_DEGREE, but also at least 2.
70 #define MAX_SIMD_DEGREE_OR_2 (MAX_SIMD_DEGREE > 2 ? MAX_SIMD_DEGREE : 2)
71 
72 static const uint32_t IV[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL,
73                                0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL,
74                                0x1F83D9ABUL, 0x5BE0CD19UL};
75 
76 static const uint8_t MSG_SCHEDULE[7][16] = {
77     {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
78     {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8},
79     {3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1},
80     {10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6},
81     {12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4},
82     {9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7},
83     {11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13},
84 };
85 
86 /* Find index of the highest set bit */
87 /* x is assumed to be nonzero.       */
highest_one(uint64_t x)88 static unsigned int highest_one(uint64_t x) {
89 #if defined(__GNUC__) || defined(__clang__)
90   return 63 ^ __builtin_clzll(x);
91 #elif defined(_MSC_VER) && defined(IS_X86_64)
92   unsigned long index;
93   _BitScanReverse64(&index, x);
94   return index;
95 #elif defined(_MSC_VER) && defined(IS_X86_32)
96   if(x >> 32) {
97     unsigned long index;
98     _BitScanReverse(&index, (unsigned long)(x >> 32));
99     return 32 + index;
100   } else {
101     unsigned long index;
102     _BitScanReverse(&index, (unsigned long)x);
103     return index;
104   }
105 #else
106   unsigned int c = 0;
107   if(x & 0xffffffff00000000ULL) { x >>= 32; c += 32; }
108   if(x & 0x00000000ffff0000ULL) { x >>= 16; c += 16; }
109   if(x & 0x000000000000ff00ULL) { x >>=  8; c +=  8; }
110   if(x & 0x00000000000000f0ULL) { x >>=  4; c +=  4; }
111   if(x & 0x000000000000000cULL) { x >>=  2; c +=  2; }
112   if(x & 0x0000000000000002ULL) {           c +=  1; }
113   return c;
114 #endif
115 }
116 
117 // Count the number of 1 bits.
popcnt(uint64_t x)118 INLINE unsigned int popcnt(uint64_t x) {
119 #if defined(__GNUC__) || defined(__clang__)
120   return __builtin_popcountll(x);
121 #else
122   unsigned int count = 0;
123   while (x != 0) {
124     count += 1;
125     x &= x - 1;
126   }
127   return count;
128 #endif
129 }
130 
131 // Largest power of two less than or equal to x. As a special case, returns 1
132 // when x is 0.
round_down_to_power_of_2(uint64_t x)133 INLINE uint64_t round_down_to_power_of_2(uint64_t x) {
134   return 1ULL << highest_one(x | 1);
135 }
136 
counter_low(uint64_t counter)137 INLINE uint32_t counter_low(uint64_t counter) { return (uint32_t)counter; }
138 
counter_high(uint64_t counter)139 INLINE uint32_t counter_high(uint64_t counter) {
140   return (uint32_t)(counter >> 32);
141 }
142 
load32(const void * src)143 INLINE uint32_t load32(const void *src) {
144   const uint8_t *p = (const uint8_t *)src;
145   return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |
146          ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);
147 }
148 
load_key_words(const uint8_t key[BLAKE3_KEY_LEN],uint32_t key_words[8])149 INLINE void load_key_words(const uint8_t key[BLAKE3_KEY_LEN],
150                            uint32_t key_words[8]) {
151   key_words[0] = load32(&key[0 * 4]);
152   key_words[1] = load32(&key[1 * 4]);
153   key_words[2] = load32(&key[2 * 4]);
154   key_words[3] = load32(&key[3 * 4]);
155   key_words[4] = load32(&key[4 * 4]);
156   key_words[5] = load32(&key[5 * 4]);
157   key_words[6] = load32(&key[6 * 4]);
158   key_words[7] = load32(&key[7 * 4]);
159 }
160 
store32(void * dst,uint32_t w)161 INLINE void store32(void *dst, uint32_t w) {
162   uint8_t *p = (uint8_t *)dst;
163   p[0] = (uint8_t)(w >> 0);
164   p[1] = (uint8_t)(w >> 8);
165   p[2] = (uint8_t)(w >> 16);
166   p[3] = (uint8_t)(w >> 24);
167 }
168 
store_cv_words(uint8_t bytes_out[32],uint32_t cv_words[8])169 INLINE void store_cv_words(uint8_t bytes_out[32], uint32_t cv_words[8]) {
170   store32(&bytes_out[0 * 4], cv_words[0]);
171   store32(&bytes_out[1 * 4], cv_words[1]);
172   store32(&bytes_out[2 * 4], cv_words[2]);
173   store32(&bytes_out[3 * 4], cv_words[3]);
174   store32(&bytes_out[4 * 4], cv_words[4]);
175   store32(&bytes_out[5 * 4], cv_words[5]);
176   store32(&bytes_out[6 * 4], cv_words[6]);
177   store32(&bytes_out[7 * 4], cv_words[7]);
178 }
179 
180 void blake3_compress_in_place(uint32_t cv[8],
181                               const uint8_t block[BLAKE3_BLOCK_LEN],
182                               uint8_t block_len, uint64_t counter,
183                               uint8_t flags);
184 
185 void blake3_compress_xof(const uint32_t cv[8],
186                          const uint8_t block[BLAKE3_BLOCK_LEN],
187                          uint8_t block_len, uint64_t counter, uint8_t flags,
188                          uint8_t out[64]);
189 
190 void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs,
191                       size_t blocks, const uint32_t key[8], uint64_t counter,
192                       bool increment_counter, uint8_t flags,
193                       uint8_t flags_start, uint8_t flags_end, uint8_t *out);
194 
195 size_t blake3_simd_degree(void);
196 
197 
198 // Declarations for implementation-specific functions.
199 void blake3_compress_in_place_portable(uint32_t cv[8],
200                                        const uint8_t block[BLAKE3_BLOCK_LEN],
201                                        uint8_t block_len, uint64_t counter,
202                                        uint8_t flags);
203 
204 void blake3_compress_xof_portable(const uint32_t cv[8],
205                                   const uint8_t block[BLAKE3_BLOCK_LEN],
206                                   uint8_t block_len, uint64_t counter,
207                                   uint8_t flags, uint8_t out[64]);
208 
209 void blake3_hash_many_portable(const uint8_t *const *inputs, size_t num_inputs,
210                                size_t blocks, const uint32_t key[8],
211                                uint64_t counter, bool increment_counter,
212                                uint8_t flags, uint8_t flags_start,
213                                uint8_t flags_end, uint8_t *out);
214 
215 #if defined(IS_X86)
216 #if !defined(BLAKE3_NO_SSE2)
217 void blake3_compress_in_place_sse2(uint32_t cv[8],
218                                    const uint8_t block[BLAKE3_BLOCK_LEN],
219                                    uint8_t block_len, uint64_t counter,
220                                    uint8_t flags);
221 void blake3_compress_xof_sse2(const uint32_t cv[8],
222                               const uint8_t block[BLAKE3_BLOCK_LEN],
223                               uint8_t block_len, uint64_t counter,
224                               uint8_t flags, uint8_t out[64]);
225 void blake3_hash_many_sse2(const uint8_t *const *inputs, size_t num_inputs,
226                            size_t blocks, const uint32_t key[8],
227                            uint64_t counter, bool increment_counter,
228                            uint8_t flags, uint8_t flags_start,
229                            uint8_t flags_end, uint8_t *out);
230 #endif
231 #if !defined(BLAKE3_NO_SSE41)
232 void blake3_compress_in_place_sse41(uint32_t cv[8],
233                                     const uint8_t block[BLAKE3_BLOCK_LEN],
234                                     uint8_t block_len, uint64_t counter,
235                                     uint8_t flags);
236 void blake3_compress_xof_sse41(const uint32_t cv[8],
237                                const uint8_t block[BLAKE3_BLOCK_LEN],
238                                uint8_t block_len, uint64_t counter,
239                                uint8_t flags, uint8_t out[64]);
240 void blake3_hash_many_sse41(const uint8_t *const *inputs, size_t num_inputs,
241                             size_t blocks, const uint32_t key[8],
242                             uint64_t counter, bool increment_counter,
243                             uint8_t flags, uint8_t flags_start,
244                             uint8_t flags_end, uint8_t *out);
245 #endif
246 #if !defined(BLAKE3_NO_AVX2)
247 void blake3_hash_many_avx2(const uint8_t *const *inputs, size_t num_inputs,
248                            size_t blocks, const uint32_t key[8],
249                            uint64_t counter, bool increment_counter,
250                            uint8_t flags, uint8_t flags_start,
251                            uint8_t flags_end, uint8_t *out);
252 #endif
253 #if !defined(BLAKE3_NO_AVX512)
254 void blake3_compress_in_place_avx512(uint32_t cv[8],
255                                      const uint8_t block[BLAKE3_BLOCK_LEN],
256                                      uint8_t block_len, uint64_t counter,
257                                      uint8_t flags);
258 
259 void blake3_compress_xof_avx512(const uint32_t cv[8],
260                                 const uint8_t block[BLAKE3_BLOCK_LEN],
261                                 uint8_t block_len, uint64_t counter,
262                                 uint8_t flags, uint8_t out[64]);
263 
264 void blake3_hash_many_avx512(const uint8_t *const *inputs, size_t num_inputs,
265                              size_t blocks, const uint32_t key[8],
266                              uint64_t counter, bool increment_counter,
267                              uint8_t flags, uint8_t flags_start,
268                              uint8_t flags_end, uint8_t *out);
269 #endif
270 #endif
271 
272 #if BLAKE3_USE_NEON == 1
273 void blake3_hash_many_neon(const uint8_t *const *inputs, size_t num_inputs,
274                            size_t blocks, const uint32_t key[8],
275                            uint64_t counter, bool increment_counter,
276                            uint8_t flags, uint8_t flags_start,
277                            uint8_t flags_end, uint8_t *out);
278 #endif
279 
280 
281 #endif /* BLAKE3_IMPL_H */
282