Path: blob/main/contrib/llvm-project/llvm/lib/Support/BLAKE3/blake3_impl.h
35269 views
#ifndef BLAKE3_IMPL_H1#define BLAKE3_IMPL_H23#include <assert.h>4#include <stdbool.h>5#include <stddef.h>6#include <stdint.h>7#include <string.h>89#include "llvm-c/blake3.h"10// For \p LLVM_LIBRARY_VISIBILITY11#include "llvm/Support/Compiler.h"1213#include "llvm_blake3_prefix.h"1415// internal flags16enum blake3_flags {17CHUNK_START = 1 << 0,18CHUNK_END = 1 << 1,19PARENT = 1 << 2,20ROOT = 1 << 3,21KEYED_HASH = 1 << 4,22DERIVE_KEY_CONTEXT = 1 << 5,23DERIVE_KEY_MATERIAL = 1 << 6,24};2526// This C implementation tries to support recent versions of GCC, Clang, and27// MSVC.28#if defined(_MSC_VER)29#define INLINE static __forceinline30#else31#define INLINE static inline __attribute__((always_inline))32#endif3334#if defined(__x86_64__) || defined(_M_X64)35#define IS_X8636#define IS_X86_6437#endif3839#if defined(__i386__) || defined(_M_IX86)40#define IS_X8641#define IS_X86_3242#endif4344#if defined(__aarch64__) || defined(_M_ARM64)45#define IS_AARCH6446#endif4748#if defined(IS_X86)49#if defined(_MSC_VER)50#include <intrin.h>51#endif52#include <immintrin.h>53#endif5455#if !defined(BLAKE3_USE_NEON)56// If BLAKE3_USE_NEON not manually set, autodetect based on57// AArch64ness and endianness.58#if defined(IS_AARCH64) && !defined(__ARM_BIG_ENDIAN)59#define BLAKE3_USE_NEON 160#else61#define BLAKE3_USE_NEON 062#endif63#endif6465#if defined(IS_X86)66#define MAX_SIMD_DEGREE 1667#elif BLAKE3_USE_NEON == 168#define MAX_SIMD_DEGREE 469#else70#define MAX_SIMD_DEGREE 171#endif7273// There are some places where we want a static size that's equal to the74// MAX_SIMD_DEGREE, but also at least 2.75#define MAX_SIMD_DEGREE_OR_2 (MAX_SIMD_DEGREE > 2 ? MAX_SIMD_DEGREE : 2)7677static const uint32_t IV[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL,780xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL,790x1F83D9ABUL, 0x5BE0CD19UL};8081static const uint8_t MSG_SCHEDULE[7][16] = {82{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},83{2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8},84{3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1},85{10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6},86{12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4},87{9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7},88{11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13},89};9091/* Find index of the highest set bit */92/* x is assumed to be nonzero. */93static unsigned int highest_one(uint64_t x) {94#if defined(__GNUC__) || defined(__clang__)95return 63 ^ __builtin_clzll(x);96#elif defined(_MSC_VER) && defined(IS_X86_64)97unsigned long index;98_BitScanReverse64(&index, x);99return index;100#elif defined(_MSC_VER) && defined(IS_X86_32)101if(x >> 32) {102unsigned long index;103_BitScanReverse(&index, (unsigned long)(x >> 32));104return 32 + index;105} else {106unsigned long index;107_BitScanReverse(&index, (unsigned long)x);108return index;109}110#else111unsigned int c = 0;112if(x & 0xffffffff00000000ULL) { x >>= 32; c += 32; }113if(x & 0x00000000ffff0000ULL) { x >>= 16; c += 16; }114if(x & 0x000000000000ff00ULL) { x >>= 8; c += 8; }115if(x & 0x00000000000000f0ULL) { x >>= 4; c += 4; }116if(x & 0x000000000000000cULL) { x >>= 2; c += 2; }117if(x & 0x0000000000000002ULL) { c += 1; }118return c;119#endif120}121122// Count the number of 1 bits.123INLINE unsigned int popcnt(uint64_t x) {124#if defined(__GNUC__) || defined(__clang__)125return __builtin_popcountll(x);126#else127unsigned int count = 0;128while (x != 0) {129count += 1;130x &= x - 1;131}132return count;133#endif134}135136// Largest power of two less than or equal to x. As a special case, returns 1137// when x is 0.138INLINE uint64_t round_down_to_power_of_2(uint64_t x) {139return 1ULL << highest_one(x | 1);140}141142INLINE uint32_t counter_low(uint64_t counter) { return (uint32_t)counter; }143144INLINE uint32_t counter_high(uint64_t counter) {145return (uint32_t)(counter >> 32);146}147148INLINE uint32_t load32(const void *src) {149const uint8_t *p = (const uint8_t *)src;150return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |151((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);152}153154INLINE void load_key_words(const uint8_t key[BLAKE3_KEY_LEN],155uint32_t key_words[8]) {156key_words[0] = load32(&key[0 * 4]);157key_words[1] = load32(&key[1 * 4]);158key_words[2] = load32(&key[2 * 4]);159key_words[3] = load32(&key[3 * 4]);160key_words[4] = load32(&key[4 * 4]);161key_words[5] = load32(&key[5 * 4]);162key_words[6] = load32(&key[6 * 4]);163key_words[7] = load32(&key[7 * 4]);164}165166INLINE void store32(void *dst, uint32_t w) {167uint8_t *p = (uint8_t *)dst;168p[0] = (uint8_t)(w >> 0);169p[1] = (uint8_t)(w >> 8);170p[2] = (uint8_t)(w >> 16);171p[3] = (uint8_t)(w >> 24);172}173174INLINE void store_cv_words(uint8_t bytes_out[32], uint32_t cv_words[8]) {175store32(&bytes_out[0 * 4], cv_words[0]);176store32(&bytes_out[1 * 4], cv_words[1]);177store32(&bytes_out[2 * 4], cv_words[2]);178store32(&bytes_out[3 * 4], cv_words[3]);179store32(&bytes_out[4 * 4], cv_words[4]);180store32(&bytes_out[5 * 4], cv_words[5]);181store32(&bytes_out[6 * 4], cv_words[6]);182store32(&bytes_out[7 * 4], cv_words[7]);183}184185LLVM_LIBRARY_VISIBILITY186void blake3_compress_in_place(uint32_t cv[8],187const uint8_t block[BLAKE3_BLOCK_LEN],188uint8_t block_len, uint64_t counter,189uint8_t flags);190191LLVM_LIBRARY_VISIBILITY192void blake3_compress_xof(const uint32_t cv[8],193const uint8_t block[BLAKE3_BLOCK_LEN],194uint8_t block_len, uint64_t counter, uint8_t flags,195uint8_t out[64]);196197LLVM_LIBRARY_VISIBILITY198void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs,199size_t blocks, const uint32_t key[8], uint64_t counter,200bool increment_counter, uint8_t flags,201uint8_t flags_start, uint8_t flags_end, uint8_t *out);202203LLVM_LIBRARY_VISIBILITY204size_t blake3_simd_degree(void);205206207// Declarations for implementation-specific functions.208LLVM_LIBRARY_VISIBILITY209void blake3_compress_in_place_portable(uint32_t cv[8],210const uint8_t block[BLAKE3_BLOCK_LEN],211uint8_t block_len, uint64_t counter,212uint8_t flags);213214LLVM_LIBRARY_VISIBILITY215void blake3_compress_xof_portable(const uint32_t cv[8],216const uint8_t block[BLAKE3_BLOCK_LEN],217uint8_t block_len, uint64_t counter,218uint8_t flags, uint8_t out[64]);219220LLVM_LIBRARY_VISIBILITY221void blake3_hash_many_portable(const uint8_t *const *inputs, size_t num_inputs,222size_t blocks, const uint32_t key[8],223uint64_t counter, bool increment_counter,224uint8_t flags, uint8_t flags_start,225uint8_t flags_end, uint8_t *out);226227#if defined(IS_X86)228#if !defined(BLAKE3_NO_SSE2)229LLVM_LIBRARY_VISIBILITY230void blake3_compress_in_place_sse2(uint32_t cv[8],231const uint8_t block[BLAKE3_BLOCK_LEN],232uint8_t block_len, uint64_t counter,233uint8_t flags);234LLVM_LIBRARY_VISIBILITY235void blake3_compress_xof_sse2(const uint32_t cv[8],236const uint8_t block[BLAKE3_BLOCK_LEN],237uint8_t block_len, uint64_t counter,238uint8_t flags, uint8_t out[64]);239LLVM_LIBRARY_VISIBILITY240void blake3_hash_many_sse2(const uint8_t *const *inputs, size_t num_inputs,241size_t blocks, const uint32_t key[8],242uint64_t counter, bool increment_counter,243uint8_t flags, uint8_t flags_start,244uint8_t flags_end, uint8_t *out);245#endif246#if !defined(BLAKE3_NO_SSE41)247LLVM_LIBRARY_VISIBILITY248void blake3_compress_in_place_sse41(uint32_t cv[8],249const uint8_t block[BLAKE3_BLOCK_LEN],250uint8_t block_len, uint64_t counter,251uint8_t flags);252LLVM_LIBRARY_VISIBILITY253void blake3_compress_xof_sse41(const uint32_t cv[8],254const uint8_t block[BLAKE3_BLOCK_LEN],255uint8_t block_len, uint64_t counter,256uint8_t flags, uint8_t out[64]);257LLVM_LIBRARY_VISIBILITY258void blake3_hash_many_sse41(const uint8_t *const *inputs, size_t num_inputs,259size_t blocks, const uint32_t key[8],260uint64_t counter, bool increment_counter,261uint8_t flags, uint8_t flags_start,262uint8_t flags_end, uint8_t *out);263#endif264#if !defined(BLAKE3_NO_AVX2)265LLVM_LIBRARY_VISIBILITY266void blake3_hash_many_avx2(const uint8_t *const *inputs, size_t num_inputs,267size_t blocks, const uint32_t key[8],268uint64_t counter, bool increment_counter,269uint8_t flags, uint8_t flags_start,270uint8_t flags_end, uint8_t *out);271#endif272#if !defined(BLAKE3_NO_AVX512)273LLVM_LIBRARY_VISIBILITY274void blake3_compress_in_place_avx512(uint32_t cv[8],275const uint8_t block[BLAKE3_BLOCK_LEN],276uint8_t block_len, uint64_t counter,277uint8_t flags);278279LLVM_LIBRARY_VISIBILITY280void blake3_compress_xof_avx512(const uint32_t cv[8],281const uint8_t block[BLAKE3_BLOCK_LEN],282uint8_t block_len, uint64_t counter,283uint8_t flags, uint8_t out[64]);284285LLVM_LIBRARY_VISIBILITY286void blake3_hash_many_avx512(const uint8_t *const *inputs, size_t num_inputs,287size_t blocks, const uint32_t key[8],288uint64_t counter, bool increment_counter,289uint8_t flags, uint8_t flags_start,290uint8_t flags_end, uint8_t *out);291#endif292#endif293294#if BLAKE3_USE_NEON == 1295LLVM_LIBRARY_VISIBILITY296void blake3_hash_many_neon(const uint8_t *const *inputs, size_t num_inputs,297size_t blocks, const uint32_t key[8],298uint64_t counter, bool increment_counter,299uint8_t flags, uint8_t flags_start,300uint8_t flags_end, uint8_t *out);301#endif302303304#endif /* BLAKE3_IMPL_H */305306307