blake3.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import { bytes, exists, number, output } from './_assert.js';
  2. import { fromBig } from './_u64.js';
  3. import { BLAKE } from './_blake.js';
  4. import { compress, B2S_IV } from './blake2s.js';
  5. import {
  6. Input,
  7. u8,
  8. u32,
  9. toBytes,
  10. HashXOF,
  11. wrapXOFConstructorWithOpts,
  12. isLE,
  13. byteSwap32,
  14. } from './utils.js';
  15. // Blake3 is single-option Blake2 with reduced security (round count).
  16. // Flag bitset
  17. const enum B3_Flags {
  18. CHUNK_START = 1 << 0,
  19. CHUNK_END = 1 << 1,
  20. PARENT = 1 << 2,
  21. ROOT = 1 << 3,
  22. KEYED_HASH = 1 << 4,
  23. DERIVE_KEY_CONTEXT = 1 << 5,
  24. DERIVE_KEY_MATERIAL = 1 << 6,
  25. }
  26. const SIGMA: Uint8Array = /* @__PURE__ */ (() => {
  27. const Id = Array.from({ length: 16 }, (_, i) => i);
  28. const permute = (arr: number[]) =>
  29. [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8].map((i) => arr[i]);
  30. const res: number[] = [];
  31. for (let i = 0, v = Id; i < 7; i++, v = permute(v)) res.push(...v);
  32. return Uint8Array.from(res);
  33. })();
  34. // - key: is 256-bit key
  35. // - context: string should be hardcoded, globally unique, and application - specific.
  36. // A good default format for the context string is "[application] [commit timestamp] [purpose]"
  37. // - Only one of 'key' (keyed mode) or 'context' (derive key mode) can be used at same time
  38. export type Blake3Opts = { dkLen?: number; key?: Input; context?: Input };
  39. // Why is this so slow? It should be 6x faster than blake2b.
  40. // - There is only 30% reduction in number of rounds from blake2s
  41. // - This function uses tree mode to achive parallelisation via SIMD and threading,
  42. // however in JS we don't have threads and SIMD, so we get only overhead from tree structure
  43. // - It is possible to speed it up via Web Workers, hovewer it will make code singnificantly more
  44. // complicated, which we are trying to avoid, since this library is intended to be used
  45. // for cryptographic purposes. Also, parallelization happens only on chunk level (1024 bytes),
  46. // which won't really benefit small inputs.
  47. class BLAKE3 extends BLAKE<BLAKE3> implements HashXOF<BLAKE3> {
  48. private IV: Uint32Array;
  49. private flags = 0 | 0;
  50. private state: Uint32Array;
  51. private chunkPos = 0; // Position of current block in chunk
  52. private chunksDone = 0; // How many chunks we already have
  53. private stack: Uint32Array[] = [];
  54. // Output
  55. private posOut = 0;
  56. private bufferOut32 = new Uint32Array(16);
  57. private bufferOut: Uint8Array;
  58. private chunkOut = 0; // index of output chunk
  59. private enableXOF = true;
  60. constructor(opts: Blake3Opts = {}, flags = 0) {
  61. super(64, opts.dkLen === undefined ? 32 : opts.dkLen, {}, Number.MAX_SAFE_INTEGER, 0, 0);
  62. this.outputLen = opts.dkLen === undefined ? 32 : opts.dkLen;
  63. number(this.outputLen);
  64. if (opts.key !== undefined && opts.context !== undefined)
  65. throw new Error('Blake3: only key or context can be specified at same time');
  66. else if (opts.key !== undefined) {
  67. const key = toBytes(opts.key).slice();
  68. if (key.length !== 32) throw new Error('Blake3: key should be 32 byte');
  69. this.IV = u32(key);
  70. if (!isLE) byteSwap32(this.IV);
  71. this.flags = flags | B3_Flags.KEYED_HASH;
  72. } else if (opts.context !== undefined) {
  73. const context_key = new BLAKE3({ dkLen: 32 }, B3_Flags.DERIVE_KEY_CONTEXT)
  74. .update(opts.context)
  75. .digest();
  76. this.IV = u32(context_key);
  77. if (!isLE) byteSwap32(this.IV);
  78. this.flags = flags | B3_Flags.DERIVE_KEY_MATERIAL;
  79. } else {
  80. this.IV = B2S_IV.slice();
  81. this.flags = flags;
  82. }
  83. this.state = this.IV.slice();
  84. this.bufferOut = u8(this.bufferOut32);
  85. }
  86. // Unused
  87. protected get() {
  88. return [];
  89. }
  90. protected set() {}
  91. private b2Compress(counter: number, flags: number, buf: Uint32Array, bufPos: number = 0) {
  92. const { state: s, pos } = this;
  93. const { h, l } = fromBig(BigInt(counter), true);
  94. // prettier-ignore
  95. const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } =
  96. compress(
  97. SIGMA, bufPos, buf, 7,
  98. s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
  99. B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], h, l, pos, flags
  100. );
  101. s[0] = v0 ^ v8;
  102. s[1] = v1 ^ v9;
  103. s[2] = v2 ^ v10;
  104. s[3] = v3 ^ v11;
  105. s[4] = v4 ^ v12;
  106. s[5] = v5 ^ v13;
  107. s[6] = v6 ^ v14;
  108. s[7] = v7 ^ v15;
  109. }
  110. protected compress(buf: Uint32Array, bufPos: number = 0, isLast: boolean = false) {
  111. // Compress last block
  112. let flags = this.flags;
  113. if (!this.chunkPos) flags |= B3_Flags.CHUNK_START;
  114. if (this.chunkPos === 15 || isLast) flags |= B3_Flags.CHUNK_END;
  115. if (!isLast) this.pos = this.blockLen;
  116. this.b2Compress(this.chunksDone, flags, buf, bufPos);
  117. this.chunkPos += 1;
  118. // If current block is last in chunk (16 blocks), then compress chunks
  119. if (this.chunkPos === 16 || isLast) {
  120. let chunk = this.state;
  121. this.state = this.IV.slice();
  122. // If not the last one, compress only when there are trailing zeros in chunk counter
  123. // chunks used as binary tree where current stack is path. Zero means current leaf is finished and can be compressed.
  124. // 1 (001) - leaf not finished (just push current chunk to stack)
  125. // 2 (010) - leaf finished at depth=1 (merge with last elm on stack and push back)
  126. // 3 (011) - last leaf not finished
  127. // 4 (100) - leafs finished at depth=1 and depth=2
  128. for (let last, chunks = this.chunksDone + 1; isLast || !(chunks & 1); chunks >>= 1) {
  129. if (!(last = this.stack.pop())) break;
  130. this.buffer32.set(last, 0);
  131. this.buffer32.set(chunk, 8);
  132. this.pos = this.blockLen;
  133. this.b2Compress(0, this.flags | B3_Flags.PARENT, this.buffer32, 0);
  134. chunk = this.state;
  135. this.state = this.IV.slice();
  136. }
  137. this.chunksDone++;
  138. this.chunkPos = 0;
  139. this.stack.push(chunk);
  140. }
  141. this.pos = 0;
  142. }
  143. _cloneInto(to?: BLAKE3): BLAKE3 {
  144. to = super._cloneInto(to) as BLAKE3;
  145. const { IV, flags, state, chunkPos, posOut, chunkOut, stack, chunksDone } = this;
  146. to.state.set(state.slice());
  147. to.stack = stack.map((i) => Uint32Array.from(i));
  148. to.IV.set(IV);
  149. to.flags = flags;
  150. to.chunkPos = chunkPos;
  151. to.chunksDone = chunksDone;
  152. to.posOut = posOut;
  153. to.chunkOut = chunkOut;
  154. to.enableXOF = this.enableXOF;
  155. to.bufferOut32.set(this.bufferOut32);
  156. return to;
  157. }
  158. destroy() {
  159. this.destroyed = true;
  160. this.state.fill(0);
  161. this.buffer32.fill(0);
  162. this.IV.fill(0);
  163. this.bufferOut32.fill(0);
  164. for (let i of this.stack) i.fill(0);
  165. }
  166. // Same as b2Compress, but doesn't modify state and returns 16 u32 array (instead of 8)
  167. private b2CompressOut() {
  168. const { state: s, pos, flags, buffer32, bufferOut32: out32 } = this;
  169. const { h, l } = fromBig(BigInt(this.chunkOut++));
  170. if (!isLE) byteSwap32(buffer32);
  171. // prettier-ignore
  172. const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } =
  173. compress(
  174. SIGMA, 0, buffer32, 7,
  175. s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
  176. B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], l, h, pos, flags
  177. );
  178. out32[0] = v0 ^ v8;
  179. out32[1] = v1 ^ v9;
  180. out32[2] = v2 ^ v10;
  181. out32[3] = v3 ^ v11;
  182. out32[4] = v4 ^ v12;
  183. out32[5] = v5 ^ v13;
  184. out32[6] = v6 ^ v14;
  185. out32[7] = v7 ^ v15;
  186. out32[8] = s[0] ^ v8;
  187. out32[9] = s[1] ^ v9;
  188. out32[10] = s[2] ^ v10;
  189. out32[11] = s[3] ^ v11;
  190. out32[12] = s[4] ^ v12;
  191. out32[13] = s[5] ^ v13;
  192. out32[14] = s[6] ^ v14;
  193. out32[15] = s[7] ^ v15;
  194. if (!isLE) {
  195. byteSwap32(buffer32);
  196. byteSwap32(out32);
  197. }
  198. this.posOut = 0;
  199. }
  200. protected finish() {
  201. if (this.finished) return;
  202. this.finished = true;
  203. // Padding
  204. this.buffer.fill(0, this.pos);
  205. // Process last chunk
  206. let flags = this.flags | B3_Flags.ROOT;
  207. if (this.stack.length) {
  208. flags |= B3_Flags.PARENT;
  209. if (!isLE) byteSwap32(this.buffer32);
  210. this.compress(this.buffer32, 0, true);
  211. if (!isLE) byteSwap32(this.buffer32);
  212. this.chunksDone = 0;
  213. this.pos = this.blockLen;
  214. } else {
  215. flags |= (!this.chunkPos ? B3_Flags.CHUNK_START : 0) | B3_Flags.CHUNK_END;
  216. }
  217. this.flags = flags;
  218. this.b2CompressOut();
  219. }
  220. private writeInto(out: Uint8Array) {
  221. exists(this, false);
  222. bytes(out);
  223. this.finish();
  224. const { blockLen, bufferOut } = this;
  225. for (let pos = 0, len = out.length; pos < len; ) {
  226. if (this.posOut >= blockLen) this.b2CompressOut();
  227. const take = Math.min(blockLen - this.posOut, len - pos);
  228. out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
  229. this.posOut += take;
  230. pos += take;
  231. }
  232. return out;
  233. }
  234. xofInto(out: Uint8Array): Uint8Array {
  235. if (!this.enableXOF) throw new Error('XOF is not possible after digest call');
  236. return this.writeInto(out);
  237. }
  238. xof(bytes: number): Uint8Array {
  239. number(bytes);
  240. return this.xofInto(new Uint8Array(bytes));
  241. }
  242. digestInto(out: Uint8Array) {
  243. output(out, this);
  244. if (this.finished) throw new Error('digest() was already called');
  245. this.enableXOF = false;
  246. this.writeInto(out);
  247. this.destroy();
  248. return out;
  249. }
  250. digest() {
  251. return this.digestInto(new Uint8Array(this.outputLen));
  252. }
  253. }
  254. /**
  255. * BLAKE3 hash function.
  256. * @param msg - message that would be hashed
  257. * @param opts - dkLen, key, context
  258. */
  259. export const blake3 = /* @__PURE__ */ wrapXOFConstructorWithOpts<BLAKE3, Blake3Opts>(
  260. (opts) => new BLAKE3(opts)
  261. );