|
| 1 | +// Pure JavaScript implementation of BLAKE3 compression (single-chunk, truncated to 8 words) |
| 2 | +// Ported from pow/src/blake3.rs |
| 3 | + |
| 4 | +export const IV = [ |
| 5 | + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
| 6 | + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, |
| 7 | +]; |
| 8 | + |
| 9 | +export const FLAG_CHUNK_START = 0x01; |
| 10 | +export const FLAG_CHUNK_END = 0x02; |
| 11 | +export const FLAG_ROOT = 0x08; |
| 12 | + |
| 13 | +const PERMUTATION = [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8]; |
| 14 | + |
| 15 | +// Precompute message schedule (7 rounds) |
| 16 | +const MESSAGE_SCHEDULE = (() => { |
| 17 | + const out = []; |
| 18 | + let ix = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; |
| 19 | + out.push(ix.slice()); |
| 20 | + for (let i = 1; i < 7; i++) { |
| 21 | + const newIx = new Array(16); |
| 22 | + for (let j = 0; j < 16; j++) newIx[j] = ix[PERMUTATION[j]]; |
| 23 | + ix = newIx; |
| 24 | + out.push(newIx.slice()); |
| 25 | + } |
| 26 | + return out; |
| 27 | +})(); |
| 28 | + |
| 29 | +function g(state, a, b, c, d, mx, my) { |
| 30 | + state[a] = (state[a] + state[b] + mx) | 0; |
| 31 | + state[d] = ror32(state[d] ^ state[a], 16); |
| 32 | + state[c] = (state[c] + state[d]) | 0; |
| 33 | + state[b] = ror32(state[b] ^ state[c], 12); |
| 34 | + state[a] = (state[a] + state[b] + my) | 0; |
| 35 | + state[d] = ror32(state[d] ^ state[a], 8); |
| 36 | + state[c] = (state[c] + state[d]) | 0; |
| 37 | + state[b] = ror32(state[b] ^ state[c], 7); |
| 38 | +} |
| 39 | + |
| 40 | +function ror32(v, n) { |
| 41 | + return (v >>> n) | (v << (32 - n)); |
| 42 | +} |
| 43 | + |
| 44 | +function roundFixed(state, m, round) { |
| 45 | + const s = MESSAGE_SCHEDULE[round]; |
| 46 | + // Mix columns |
| 47 | + g(state, 0, 4, 8, 12, m[s[0]], m[s[1]]); |
| 48 | + g(state, 1, 5, 9, 13, m[s[2]], m[s[3]]); |
| 49 | + g(state, 2, 6, 10, 14, m[s[4]], m[s[5]]); |
| 50 | + g(state, 3, 7, 11, 15, m[s[6]], m[s[7]]); |
| 51 | + // Mix diagonals |
| 52 | + g(state, 0, 5, 10, 15, m[s[8]], m[s[9]]); |
| 53 | + g(state, 1, 6, 11, 12, m[s[10]], m[s[11]]); |
| 54 | + g(state, 2, 7, 8, 13, m[s[12]], m[s[13]]); |
| 55 | + g(state, 3, 4, 9, 14, m[s[14]], m[s[15]]); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Truncated BLAKE3 compression function (returns first 8 words). |
| 60 | + * @param {number[]} chainingValue - 8 u32 words |
| 61 | + * @param {number[]} blockWords - 16 u32 words |
| 62 | + * @param {number} counter - 64-bit counter (as Number, only low 32 bits used here) |
| 63 | + * @param {number} blockLen - block length in bytes |
| 64 | + * @param {number} flags - BLAKE3 flags |
| 65 | + * @returns {number[]} 8 u32 words |
| 66 | + */ |
| 67 | +export function compress8(chainingValue, blockWords, counter, blockLen, flags) { |
| 68 | + const counterLow = counter | 0; |
| 69 | + const counterHigh = 0; // counter fits in 32 bits for our use case |
| 70 | + const state = [ |
| 71 | + chainingValue[0], chainingValue[1], chainingValue[2], chainingValue[3], |
| 72 | + chainingValue[4], chainingValue[5], chainingValue[6], chainingValue[7], |
| 73 | + IV[0], IV[1], IV[2], IV[3], |
| 74 | + counterLow, counterHigh, blockLen, flags, |
| 75 | + ]; |
| 76 | + |
| 77 | + for (let i = 0; i < 7; i++) { |
| 78 | + roundFixed(state, blockWords, i); |
| 79 | + } |
| 80 | + |
| 81 | + for (let i = 0; i < 8; i++) { |
| 82 | + state[i] ^= state[i + 8]; |
| 83 | + } |
| 84 | + |
| 85 | + return state.slice(0, 8); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Hash arbitrary data using BLAKE3 (single-chunk, up to 1024 bytes). |
| 90 | + * Returns 32-byte Uint8Array. |
| 91 | + * @param {Uint8Array} data |
| 92 | + * @returns {Uint8Array} |
| 93 | + */ |
| 94 | +export function blake3Hash(data) { |
| 95 | + let chainingValue = IV.slice(); |
| 96 | + |
| 97 | + // Process 64-byte blocks (at least 1 block even for empty input) |
| 98 | + const numBlocks = Math.max(1, Math.ceil(data.length / 64)); |
| 99 | + for (let blockIdx = 0; blockIdx < numBlocks; blockIdx++) { |
| 100 | + const offset = blockIdx * 64; |
| 101 | + const remaining = Math.max(0, data.length - offset); |
| 102 | + const thisBlockLen = Math.min(64, remaining); |
| 103 | + |
| 104 | + // Convert block bytes to 16 u32 words (LE) |
| 105 | + const block = new Array(16).fill(0); |
| 106 | + for (let i = 0; i < thisBlockLen; i++) { |
| 107 | + block[i >> 2] |= data[offset + i] << ((i & 3) * 8); |
| 108 | + } |
| 109 | + |
| 110 | + let flags = 0; |
| 111 | + if (blockIdx === 0) flags |= FLAG_CHUNK_START; |
| 112 | + if (blockIdx === numBlocks - 1) flags |= FLAG_CHUNK_END | FLAG_ROOT; |
| 113 | + |
| 114 | + chainingValue = compress8(chainingValue, block, 0, thisBlockLen, flags); |
| 115 | + } |
| 116 | + |
| 117 | + // Convert u32 words to bytes (LE) |
| 118 | + const out = new Uint8Array(32); |
| 119 | + for (let i = 0; i < 8; i++) { |
| 120 | + out[i * 4] = chainingValue[i] & 0xff; |
| 121 | + out[i * 4 + 1] = (chainingValue[i] >>> 8) & 0xff; |
| 122 | + out[i * 4 + 2] = (chainingValue[i] >>> 16) & 0xff; |
| 123 | + out[i * 4 + 3] = (chainingValue[i] >>> 24) & 0xff; |
| 124 | + } |
| 125 | + return out; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Encode u32[8] hash to hex string (LE byte order, matching Rust encode_hex_le). |
| 130 | + * @param {number[]} hash - 8 u32 words |
| 131 | + * @returns {string} |
| 132 | + */ |
| 133 | +export function encodeHexLE(hash) { |
| 134 | + const hex = '0123456789abcdef'; |
| 135 | + let out = ''; |
| 136 | + for (let w = 0; w < 8; w++) { |
| 137 | + for (let i = 0; i < 4; i++) { |
| 138 | + const b = (hash[w] >>> (i * 8)) & 0xff; |
| 139 | + out += hex[b >> 4]; |
| 140 | + out += hex[b & 0xf]; |
| 141 | + } |
| 142 | + } |
| 143 | + return out; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Compute the difficulty mask (matching Rust compute_mask_cerberus). |
| 148 | + * @param {number} difficulty |
| 149 | + * @returns {number} |
| 150 | + */ |
| 151 | +export function computeMask(difficulty) { |
| 152 | + if (difficulty === 16) return ~0; |
| 153 | + // !(!0u32 >> (difficulty * 2)).swap_bytes() |
| 154 | + const shifted = (~0 >>> (difficulty * 2)) | 0; |
| 155 | + const swapped = byteSwap32(shifted); |
| 156 | + return (~swapped) | 0; |
| 157 | +} |
| 158 | + |
| 159 | +function byteSwap32(v) { |
| 160 | + return ( |
| 161 | + ((v & 0xff) << 24) | |
| 162 | + (((v >>> 8) & 0xff) << 16) | |
| 163 | + (((v >>> 16) & 0xff) << 8) | |
| 164 | + ((v >>> 24) & 0xff) |
| 165 | + ) | 0; |
| 166 | +} |
0 commit comments