kenqtade's picture
Use ken-q org namespace for project Spaces
0865492
Raw
History Blame Contribute Delete
3.67 kB
/**
* BLAKE3 Utility Functions
*
* Optimized for little-endian systems (most user-facing systems).
* BLAKE3 is little-endian friendly - on little-endian systems we can
* create Uint32Array views directly over input buffers.
*/
/**
* Detect system endianness at module load time.
* On little-endian systems, the byte 0x01 will be at index 0.
*/
export declare const IS_LITTLE_ENDIAN: boolean;
/**
* Read 16 little-endian 32-bit words from a byte array into a Uint32Array.
* This is only needed on big-endian systems.
*
* @param input - Source byte array
* @param offset - Starting byte offset in input
* @param words - Destination Uint32Array (must have at least 16 elements)
*/
export declare function readLittleEndianWordsFull(input: Uint8Array, offset: number, words: Uint32Array): void;
/**
* Read N little-endian 32-bit words from a byte array.
* Handles partial reads (for final blocks).
*
* @param input - Source byte array
* @param offset - Starting byte offset
* @param words - Destination Uint32Array
* @param wordCount - Number of words to read
*/
export declare function readLittleEndianWords(input: Uint8Array, offset: number, words: Uint32Array, wordCount: number): void;
/**
* Read a partial block with zero padding.
* Used for the final block when input length is not a multiple of 64.
*
* @param input - Source byte array
* @param offset - Starting byte offset
* @param length - Number of bytes to read (< 64)
* @param words - Destination Uint32Array (must have 16 elements)
*/
export declare function readLittleEndianWordsPartial(input: Uint8Array, offset: number, length: number, words: Uint32Array): void;
/**
* Write 8 little-endian 32-bit words to a byte array.
*
* @param words - Source Uint32Array
* @param wordOffset - Starting word offset in source
* @param output - Destination byte array
* @param byteOffset - Starting byte offset in destination
*/
export declare function writeLittleEndianWords(words: Uint32Array, wordOffset: number, output: Uint8Array, byteOffset: number): void;
/**
* Write N bytes from 32-bit words to output.
* Used for variable-length output (XOF mode).
*
* @param words - Source Uint32Array
* @param wordOffset - Starting word offset
* @param output - Destination byte array
* @param byteOffset - Starting byte offset in destination
* @param byteCount - Number of bytes to write
*/
export declare function writeLittleEndianBytesPartial(words: Uint32Array, wordOffset: number, output: Uint8Array, byteOffset: number, byteCount: number): void;
/**
* Encode a UTF-8 string to Uint8Array.
* Used for derive_key context strings.
*/
export declare function encodeUTF8(str: string): Uint8Array;
/**
* Count trailing zero bits in a 32-bit number using De Bruijn multiplication.
* This is O(1) and branchless for non-zero inputs.
*
* For Merkle tree merge: ctz32(chunkCounter) tells us how many merges to do.
*/
export declare function ctz32(n: number): number;
/**
* Count trailing zero bits in a 64-bit number.
* Used to determine how many parent nodes to compute after adding a chunk.
*
* Note: JavaScript bitwise ops work on 32-bit signed integers,
* so we need to handle 64-bit numbers carefully.
*/
export declare function countTrailingZeros(n: number): number;
/**
* Create a Uint32Array view of a Uint8Array.
* Only works correctly on little-endian systems when the offset is 4-byte aligned.
*
* @param arr - Source byte array
* @param byteOffset - Starting byte offset (must be 4-byte aligned)
* @param wordLength - Number of 32-bit words
*/
export declare function uint32View(arr: Uint8Array, byteOffset: number, wordLength: number): Uint32Array;