blake3.js 9.2 KB

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