blake3.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.blake3 = exports.BLAKE3 = void 0;
  4. /**
  5. * Blake3 fast hash is Blake2 with reduced security (round count). Can also be used as MAC & KDF.
  6. *
  7. * It is advertised as "the fastest cryptographic hash". However, it isn't true in JS.
  8. * Why is this so slow? While it should be 6x faster than blake2b, perf diff is only 20%:
  9. *
  10. * * There is only 30% reduction in number of rounds from blake2s
  11. * * Speed-up comes from tree structure, which is parallelized using SIMD & threading.
  12. * These features are not present in JS, so we only get overhead from trees.
  13. * * Parallelization only happens on 1024-byte chunks: there is no benefit for small inputs.
  14. * * It is still possible to make it faster using: a) loop unrolling b) web workers c) wasm
  15. * @module
  16. */
  17. const _md_ts_1 = require("./_md.js");
  18. const _u64_ts_1 = require("./_u64.js");
  19. const blake2_ts_1 = require("./blake2.js");
  20. // prettier-ignore
  21. const utils_ts_1 = require("./utils.js");
  22. // Flag bitset
  23. const B3_Flags = {
  24. CHUNK_START: 0b1,
  25. CHUNK_END: 0b10,
  26. PARENT: 0b100,
  27. ROOT: 0b1000,
  28. KEYED_HASH: 0b10000,
  29. DERIVE_KEY_CONTEXT: 0b100000,
  30. DERIVE_KEY_MATERIAL: 0b1000000,
  31. };
  32. const B3_IV = _md_ts_1.SHA256_IV.slice();
  33. const B3_SIGMA = /* @__PURE__ */ (() => {
  34. const Id = Array.from({ length: 16 }, (_, i) => i);
  35. const permute = (arr) => [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8].map((i) => arr[i]);
  36. const res = [];
  37. for (let i = 0, v = Id; i < 7; i++, v = permute(v))
  38. res.push(...v);
  39. return Uint8Array.from(res);
  40. })();
  41. /** Blake3 hash. Can be used as MAC and KDF. */
  42. class BLAKE3 extends blake2_ts_1.BLAKE2 {
  43. constructor(opts = {}, flags = 0) {
  44. super(64, opts.dkLen === undefined ? 32 : opts.dkLen);
  45. this.chunkPos = 0; // Position of current block in chunk
  46. this.chunksDone = 0; // How many chunks we already have
  47. this.flags = 0 | 0;
  48. this.stack = [];
  49. // Output
  50. this.posOut = 0;
  51. this.bufferOut32 = new Uint32Array(16);
  52. this.chunkOut = 0; // index of output chunk
  53. this.enableXOF = true;
  54. const { key, context } = opts;
  55. const hasContext = context !== undefined;
  56. if (key !== undefined) {
  57. if (hasContext)
  58. throw new Error('Only "key" or "context" can be specified at same time');
  59. const k = (0, utils_ts_1.toBytes)(key).slice();
  60. (0, utils_ts_1.abytes)(k, 32);
  61. this.IV = (0, utils_ts_1.u32)(k);
  62. (0, utils_ts_1.swap32IfBE)(this.IV);
  63. this.flags = flags | B3_Flags.KEYED_HASH;
  64. }
  65. else if (hasContext) {
  66. const ctx = (0, utils_ts_1.toBytes)(context);
  67. const contextKey = new BLAKE3({ dkLen: 32 }, B3_Flags.DERIVE_KEY_CONTEXT)
  68. .update(ctx)
  69. .digest();
  70. this.IV = (0, utils_ts_1.u32)(contextKey);
  71. (0, utils_ts_1.swap32IfBE)(this.IV);
  72. this.flags = flags | B3_Flags.DERIVE_KEY_MATERIAL;
  73. }
  74. else {
  75. this.IV = B3_IV.slice();
  76. this.flags = flags;
  77. }
  78. this.state = this.IV.slice();
  79. this.bufferOut = (0, utils_ts_1.u8)(this.bufferOut32);
  80. }
  81. // Unused
  82. get() {
  83. return [];
  84. }
  85. set() { }
  86. b2Compress(counter, flags, buf, bufPos = 0) {
  87. const { state: s, pos } = this;
  88. const { h, l } = (0, _u64_ts_1.fromBig)(BigInt(counter), true);
  89. // prettier-ignore
  90. const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = (0, blake2_ts_1.compress)(B3_SIGMA, bufPos, buf, 7, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], B3_IV[0], B3_IV[1], B3_IV[2], B3_IV[3], h, l, pos, flags);
  91. s[0] = v0 ^ v8;
  92. s[1] = v1 ^ v9;
  93. s[2] = v2 ^ v10;
  94. s[3] = v3 ^ v11;
  95. s[4] = v4 ^ v12;
  96. s[5] = v5 ^ v13;
  97. s[6] = v6 ^ v14;
  98. s[7] = v7 ^ v15;
  99. }
  100. compress(buf, bufPos = 0, isLast = false) {
  101. // Compress last block
  102. let flags = this.flags;
  103. if (!this.chunkPos)
  104. flags |= B3_Flags.CHUNK_START;
  105. if (this.chunkPos === 15 || isLast)
  106. flags |= B3_Flags.CHUNK_END;
  107. if (!isLast)
  108. this.pos = this.blockLen;
  109. this.b2Compress(this.chunksDone, flags, buf, bufPos);
  110. this.chunkPos += 1;
  111. // If current block is last in chunk (16 blocks), then compress chunks
  112. if (this.chunkPos === 16 || isLast) {
  113. let chunk = this.state;
  114. this.state = this.IV.slice();
  115. // If not the last one, compress only when there are trailing zeros in chunk counter
  116. // chunks used as binary tree where current stack is path. Zero means current leaf is finished and can be compressed.
  117. // 1 (001) - leaf not finished (just push current chunk to stack)
  118. // 2 (010) - leaf finished at depth=1 (merge with last elm on stack and push back)
  119. // 3 (011) - last leaf not finished
  120. // 4 (100) - leafs finished at depth=1 and depth=2
  121. for (let last, chunks = this.chunksDone + 1; isLast || !(chunks & 1); chunks >>= 1) {
  122. if (!(last = this.stack.pop()))
  123. break;
  124. this.buffer32.set(last, 0);
  125. this.buffer32.set(chunk, 8);
  126. this.pos = this.blockLen;
  127. this.b2Compress(0, this.flags | B3_Flags.PARENT, this.buffer32, 0);
  128. chunk = this.state;
  129. this.state = this.IV.slice();
  130. }
  131. this.chunksDone++;
  132. this.chunkPos = 0;
  133. this.stack.push(chunk);
  134. }
  135. this.pos = 0;
  136. }
  137. _cloneInto(to) {
  138. to = super._cloneInto(to);
  139. const { IV, flags, state, chunkPos, posOut, chunkOut, stack, chunksDone } = this;
  140. to.state.set(state.slice());
  141. to.stack = stack.map((i) => Uint32Array.from(i));
  142. to.IV.set(IV);
  143. to.flags = flags;
  144. to.chunkPos = chunkPos;
  145. to.chunksDone = chunksDone;
  146. to.posOut = posOut;
  147. to.chunkOut = chunkOut;
  148. to.enableXOF = this.enableXOF;
  149. to.bufferOut32.set(this.bufferOut32);
  150. return to;
  151. }
  152. destroy() {
  153. this.destroyed = true;
  154. (0, utils_ts_1.clean)(this.state, this.buffer32, this.IV, this.bufferOut32);
  155. (0, utils_ts_1.clean)(...this.stack);
  156. }
  157. // Same as b2Compress, but doesn't modify state and returns 16 u32 array (instead of 8)
  158. b2CompressOut() {
  159. const { state: s, pos, flags, buffer32, bufferOut32: out32 } = this;
  160. const { h, l } = (0, _u64_ts_1.fromBig)(BigInt(this.chunkOut++));
  161. (0, utils_ts_1.swap32IfBE)(buffer32);
  162. // prettier-ignore
  163. const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = (0, blake2_ts_1.compress)(B3_SIGMA, 0, buffer32, 7, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], B3_IV[0], B3_IV[1], B3_IV[2], B3_IV[3], l, h, pos, flags);
  164. out32[0] = v0 ^ v8;
  165. out32[1] = v1 ^ v9;
  166. out32[2] = v2 ^ v10;
  167. out32[3] = v3 ^ v11;
  168. out32[4] = v4 ^ v12;
  169. out32[5] = v5 ^ v13;
  170. out32[6] = v6 ^ v14;
  171. out32[7] = v7 ^ v15;
  172. out32[8] = s[0] ^ v8;
  173. out32[9] = s[1] ^ v9;
  174. out32[10] = s[2] ^ v10;
  175. out32[11] = s[3] ^ v11;
  176. out32[12] = s[4] ^ v12;
  177. out32[13] = s[5] ^ v13;
  178. out32[14] = s[6] ^ v14;
  179. out32[15] = s[7] ^ v15;
  180. (0, utils_ts_1.swap32IfBE)(buffer32);
  181. (0, utils_ts_1.swap32IfBE)(out32);
  182. this.posOut = 0;
  183. }
  184. finish() {
  185. if (this.finished)
  186. return;
  187. this.finished = true;
  188. // Padding
  189. (0, utils_ts_1.clean)(this.buffer.subarray(this.pos));
  190. // Process last chunk
  191. let flags = this.flags | B3_Flags.ROOT;
  192. if (this.stack.length) {
  193. flags |= B3_Flags.PARENT;
  194. (0, utils_ts_1.swap32IfBE)(this.buffer32);
  195. this.compress(this.buffer32, 0, true);
  196. (0, utils_ts_1.swap32IfBE)(this.buffer32);
  197. this.chunksDone = 0;
  198. this.pos = this.blockLen;
  199. }
  200. else {
  201. flags |= (!this.chunkPos ? B3_Flags.CHUNK_START : 0) | B3_Flags.CHUNK_END;
  202. }
  203. this.flags = flags;
  204. this.b2CompressOut();
  205. }
  206. writeInto(out) {
  207. (0, utils_ts_1.aexists)(this, false);
  208. (0, utils_ts_1.abytes)(out);
  209. this.finish();
  210. const { blockLen, bufferOut } = this;
  211. for (let pos = 0, len = out.length; pos < len;) {
  212. if (this.posOut >= blockLen)
  213. this.b2CompressOut();
  214. const take = Math.min(blockLen - this.posOut, len - pos);
  215. out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
  216. this.posOut += take;
  217. pos += take;
  218. }
  219. return out;
  220. }
  221. xofInto(out) {
  222. if (!this.enableXOF)
  223. throw new Error('XOF is not possible after digest call');
  224. return this.writeInto(out);
  225. }
  226. xof(bytes) {
  227. (0, utils_ts_1.anumber)(bytes);
  228. return this.xofInto(new Uint8Array(bytes));
  229. }
  230. digestInto(out) {
  231. (0, utils_ts_1.aoutput)(out, this);
  232. if (this.finished)
  233. throw new Error('digest() was already called');
  234. this.enableXOF = false;
  235. this.writeInto(out);
  236. this.destroy();
  237. return out;
  238. }
  239. digest() {
  240. return this.digestInto(new Uint8Array(this.outputLen));
  241. }
  242. }
  243. exports.BLAKE3 = BLAKE3;
  244. /**
  245. * BLAKE3 hash function. Can be used as MAC and KDF.
  246. * @param msg - message that would be hashed
  247. * @param opts - `dkLen` for output length, `key` for MAC mode, `context` for KDF mode
  248. * @example
  249. * const data = new Uint8Array(32);
  250. * const hash = blake3(data);
  251. * const mac = blake3(data, { key: new Uint8Array(32) });
  252. * const kdf = blake3(data, { context: 'application name' });
  253. */
  254. exports.blake3 = (0, utils_ts_1.createXOFer)((opts) => new BLAKE3(opts));
  255. //# sourceMappingURL=blake3.js.map