|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/** |
| 4 | + * FP_BLAKE3 - BLAKE3 Cryptographic Hash using FP_ASM_LIB primitives |
| 5 | + * |
| 6 | + * BLAKE3 is a cryptographic hash function that is: |
| 7 | + * - Fast: ~3x faster than SHA-256 on modern CPUs |
| 8 | + * - Secure: Based on ChaCha permutation, resistant to length extension |
| 9 | + * - Parallelizable: Tree structure enables multi-threaded hashing |
| 10 | + * - Versatile: Supports hashing, keyed hashing, and key derivation |
| 11 | + * |
| 12 | + * This implementation uses FP_ASM_LIB's AVX2-optimized u32 primitives |
| 13 | + * for the core mixing operations. |
| 14 | + * |
| 15 | + * Output: 256-bit (32-byte) hash |
| 16 | + * Block size: 64 bytes |
| 17 | + */ |
| 18 | + |
| 19 | +#include <stdint.h> |
| 20 | +#include <stddef.h> |
| 21 | + |
| 22 | +#ifdef __cplusplus |
| 23 | +extern "C" { |
| 24 | +#endif |
| 25 | + |
| 26 | +/* ============================================================================ |
| 27 | + * Constants |
| 28 | + * ============================================================================ */ |
| 29 | + |
| 30 | +#define FP_BLAKE3_OUT_LEN 32 /* Default output length (256 bits) */ |
| 31 | +#define FP_BLAKE3_KEY_LEN 32 /* Key length for keyed hashing */ |
| 32 | +#define FP_BLAKE3_BLOCK_LEN 64 /* Block size in bytes */ |
| 33 | +#define FP_BLAKE3_CHUNK_LEN 1024 /* Chunk size for tree hashing */ |
| 34 | + |
| 35 | +/* ============================================================================ |
| 36 | + * Types |
| 37 | + * ============================================================================ */ |
| 38 | + |
| 39 | +/** |
| 40 | + * BLAKE3 hasher state |
| 41 | + * |
| 42 | + * Maintains incremental hashing state for streaming input. |
| 43 | + */ |
| 44 | +typedef struct { |
| 45 | + uint32_t cv[8]; /* Chaining value (current state) */ |
| 46 | + uint64_t chunk_counter; /* Number of chunks processed */ |
| 47 | + uint8_t buf[FP_BLAKE3_BLOCK_LEN]; /* Partial block buffer */ |
| 48 | + uint8_t buf_len; /* Bytes in buffer */ |
| 49 | + uint8_t blocks_compressed; /* Blocks compressed in current chunk */ |
| 50 | + uint8_t flags; /* Domain separation flags */ |
| 51 | + uint8_t key[FP_BLAKE3_KEY_LEN]; /* Key (for keyed mode) */ |
| 52 | +} FpBlake3Hasher; |
| 53 | + |
| 54 | +/** |
| 55 | + * BLAKE3 output structure |
| 56 | + */ |
| 57 | +typedef struct { |
| 58 | + uint8_t hash[FP_BLAKE3_OUT_LEN]; |
| 59 | +} FpBlake3Hash; |
| 60 | + |
| 61 | +/* ============================================================================ |
| 62 | + * Simple API (One-shot hashing) |
| 63 | + * ============================================================================ */ |
| 64 | + |
| 65 | +/** |
| 66 | + * Hash data in one call |
| 67 | + * |
| 68 | + * @param input Input data to hash |
| 69 | + * @param len Length of input in bytes |
| 70 | + * @param output Output buffer (32 bytes) |
| 71 | + */ |
| 72 | +void fp_blake3_hash(const uint8_t* input, size_t len, uint8_t* output); |
| 73 | + |
| 74 | +/** |
| 75 | + * Hash data with key (MAC) |
| 76 | + * |
| 77 | + * @param key 32-byte key |
| 78 | + * @param input Input data |
| 79 | + * @param len Length of input |
| 80 | + * @param output Output buffer (32 bytes) |
| 81 | + */ |
| 82 | +void fp_blake3_hash_keyed(const uint8_t* key, const uint8_t* input, |
| 83 | + size_t len, uint8_t* output); |
| 84 | + |
| 85 | +/** |
| 86 | + * Derive a key from context and key material |
| 87 | + * |
| 88 | + * @param context Context string (domain separator) |
| 89 | + * @param context_len Length of context |
| 90 | + * @param key_material Input key material |
| 91 | + * @param km_len Length of key material |
| 92 | + * @param output Output buffer (32 bytes) |
| 93 | + */ |
| 94 | +void fp_blake3_derive_key(const char* context, size_t context_len, |
| 95 | + const uint8_t* key_material, size_t km_len, |
| 96 | + uint8_t* output); |
| 97 | + |
| 98 | +/* ============================================================================ |
| 99 | + * Incremental API (Streaming hashing) |
| 100 | + * ============================================================================ */ |
| 101 | + |
| 102 | +/** |
| 103 | + * Initialize hasher for incremental hashing |
| 104 | + * |
| 105 | + * @param hasher Hasher state to initialize |
| 106 | + */ |
| 107 | +void fp_blake3_hasher_init(FpBlake3Hasher* hasher); |
| 108 | + |
| 109 | +/** |
| 110 | + * Initialize hasher for keyed hashing |
| 111 | + * |
| 112 | + * @param hasher Hasher state to initialize |
| 113 | + * @param key 32-byte key |
| 114 | + */ |
| 115 | +void fp_blake3_hasher_init_keyed(FpBlake3Hasher* hasher, const uint8_t* key); |
| 116 | + |
| 117 | +/** |
| 118 | + * Add input data to hasher |
| 119 | + * |
| 120 | + * @param hasher Hasher state |
| 121 | + * @param input Input data |
| 122 | + * @param len Length of input |
| 123 | + */ |
| 124 | +void fp_blake3_hasher_update(FpBlake3Hasher* hasher, const uint8_t* input, size_t len); |
| 125 | + |
| 126 | +/** |
| 127 | + * Finalize hash and produce output |
| 128 | + * |
| 129 | + * @param hasher Hasher state |
| 130 | + * @param output Output buffer (32 bytes) |
| 131 | + */ |
| 132 | +void fp_blake3_hasher_finalize(const FpBlake3Hasher* hasher, uint8_t* output); |
| 133 | + |
| 134 | +/** |
| 135 | + * Finalize with extended output (XOF mode) |
| 136 | + * |
| 137 | + * @param hasher Hasher state |
| 138 | + * @param output Output buffer |
| 139 | + * @param output_len Desired output length (can be > 32 bytes) |
| 140 | + */ |
| 141 | +void fp_blake3_hasher_finalize_xof(const FpBlake3Hasher* hasher, |
| 142 | + uint8_t* output, size_t output_len); |
| 143 | + |
| 144 | +/* ============================================================================ |
| 145 | + * Low-level API (For advanced use) |
| 146 | + * ============================================================================ */ |
| 147 | + |
| 148 | +/** |
| 149 | + * BLAKE3 compression function (single block) |
| 150 | + * |
| 151 | + * This is the core primitive that transforms the state. |
| 152 | + * Uses AVX2-optimized operations from FP_ASM_LIB. |
| 153 | + * |
| 154 | + * @param state 8 x u32 state (modified in-place) |
| 155 | + * @param block 64-byte input block |
| 156 | + * @param block_len Actual length (for padding) |
| 157 | + * @param counter Block counter |
| 158 | + * @param flags Domain flags |
| 159 | + */ |
| 160 | +void fp_blake3_compress(uint32_t state[8], const uint8_t block[64], |
| 161 | + uint8_t block_len, uint64_t counter, uint8_t flags); |
| 162 | + |
| 163 | +/** |
| 164 | + * BLAKE3 G mixing function (quarter-round) |
| 165 | + * |
| 166 | + * Core mixing operation, optimized with AVX2. |
| 167 | + * Operates on 4 words at positions a, b, c, d with messages mx, my. |
| 168 | + * |
| 169 | + * @param state 16 x u32 working state |
| 170 | + * @param a,b,c,d Indices into state |
| 171 | + * @param mx, my Message words |
| 172 | + */ |
| 173 | +void fp_blake3_g(uint32_t* state, int a, int b, int c, int d, |
| 174 | + uint32_t mx, uint32_t my); |
| 175 | + |
| 176 | +/** |
| 177 | + * BLAKE3 round function |
| 178 | + * |
| 179 | + * Applies 8 G functions in specific pattern. |
| 180 | + * |
| 181 | + * @param state 16 x u32 working state |
| 182 | + * @param msg Message schedule (16 x u32) |
| 183 | + */ |
| 184 | +void fp_blake3_round(uint32_t* state, const uint32_t* msg); |
| 185 | + |
| 186 | +/* ============================================================================ |
| 187 | + * SIMD-Optimized Batch Operations |
| 188 | + * ============================================================================ */ |
| 189 | + |
| 190 | +/** |
| 191 | + * Hash multiple inputs in parallel (AVX2 optimized) |
| 192 | + * |
| 193 | + * Hashes up to 8 independent inputs simultaneously using SIMD. |
| 194 | + * Significant speedup for batch processing. |
| 195 | + * |
| 196 | + * @param inputs Array of input pointers |
| 197 | + * @param lengths Array of input lengths |
| 198 | + * @param outputs Array of output buffers (32 bytes each) |
| 199 | + * @param count Number of inputs (1-8) |
| 200 | + */ |
| 201 | +void fp_blake3_hash_batch(const uint8_t** inputs, const size_t* lengths, |
| 202 | + uint8_t** outputs, size_t count); |
| 203 | + |
| 204 | +/* ============================================================================ |
| 205 | + * AVX2-Accelerated Functions |
| 206 | + * ============================================================================ */ |
| 207 | + |
| 208 | +/** |
| 209 | + * AVX2-accelerated hash (3-5x faster than scalar) |
| 210 | + */ |
| 211 | +void fp_blake3_hash_avx2(const uint8_t* input, size_t len, uint8_t* output); |
| 212 | + |
| 213 | +/** |
| 214 | + * Maximum performance hash - all operations in registers |
| 215 | + */ |
| 216 | +void fp_blake3_hash_fast(const uint8_t* input, size_t len, uint8_t* output); |
| 217 | + |
| 218 | +/** |
| 219 | + * Official BLAKE3 implementation (3-5x faster than SHA-256) |
| 220 | + */ |
| 221 | +void fp_blake3_hash_official(const uint8_t* input, size_t len, uint8_t* output); |
| 222 | +void fp_blake3_hash_keyed_official(const uint8_t* key, const uint8_t* input, |
| 223 | + size_t len, uint8_t* output); |
| 224 | +void fp_blake3_derive_key_official(const char* context, size_t context_len, |
| 225 | + const uint8_t* key_material, size_t km_len, |
| 226 | + uint8_t* output); |
| 227 | + |
| 228 | +/** |
| 229 | + * AVX2-accelerated keyed hash |
| 230 | + */ |
| 231 | +void fp_blake3_hash_keyed_avx2(const uint8_t* key, const uint8_t* input, |
| 232 | + size_t len, uint8_t* output); |
| 233 | + |
| 234 | +/* ============================================================================ |
| 235 | + * Utility Functions |
| 236 | + * ============================================================================ */ |
| 237 | + |
| 238 | +/** |
| 239 | + * Convert hash to hexadecimal string |
| 240 | + * |
| 241 | + * @param hash 32-byte hash |
| 242 | + * @param hex Output buffer (65 bytes: 64 hex chars + null) |
| 243 | + */ |
| 244 | +void fp_blake3_to_hex(const uint8_t* hash, char* hex); |
| 245 | + |
| 246 | +/** |
| 247 | + * Compare two hashes in constant time |
| 248 | + * |
| 249 | + * @param a First hash (32 bytes) |
| 250 | + * @param b Second hash (32 bytes) |
| 251 | + * @return 0 if equal, non-zero otherwise |
| 252 | + */ |
| 253 | +int fp_blake3_compare(const uint8_t* a, const uint8_t* b); |
| 254 | + |
| 255 | +#ifdef __cplusplus |
| 256 | +} |
| 257 | +#endif |
0 commit comments