blake1.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.blake512 = exports.blake384 = exports.blake256 = exports.blake224 = exports.BLAKE512 = exports.BLAKE384 = exports.BLAKE256 = exports.BLAKE224 = void 0;
  4. /**
  5. * Blake1 legacy hash function, one of SHA3 proposals.
  6. * Rarely used. Check out blake2 or blake3 instead.
  7. * https://www.aumasson.jp/blake/blake.pdf
  8. *
  9. * In the best case, there are 0 allocations.
  10. *
  11. * Differences from blake2:
  12. *
  13. * - BE instead of LE
  14. * - Paddings, similar to MD5, RIPEMD, SHA1, SHA2, but:
  15. * - length flag is located before actual length
  16. * - padding block is compressed differently (no lengths)
  17. * Instead of msg[sigma[k]], we have `msg[sigma[k]] ^ constants[sigma[k-1]]`
  18. * (-1 for g1, g2 without -1)
  19. * - Salt is XOR-ed into constants instead of state
  20. * - Salt is XOR-ed with output in `compress`
  21. * - Additional rows (+64 bytes) in SIGMA for new rounds
  22. * - Different round count:
  23. * - 14 / 10 rounds in blake256 / blake2s
  24. * - 16 / 12 rounds in blake512 / blake2b
  25. * - blake512: G1b: rotr 24 -> 25, G2b: rotr 63 -> 11
  26. * @module
  27. */
  28. const _blake_ts_1 = require("./_blake.js");
  29. const _md_ts_1 = require("./_md.js");
  30. const u64 = require("./_u64.js");
  31. // prettier-ignore
  32. const utils_ts_1 = require("./utils.js");
  33. // Empty zero-filled salt
  34. const EMPTY_SALT = /* @__PURE__ */ new Uint32Array(8);
  35. class BLAKE1 extends utils_ts_1.Hash {
  36. constructor(blockLen, outputLen, lengthFlag, counterLen, saltLen, constants, opts = {}) {
  37. super();
  38. this.finished = false;
  39. this.length = 0;
  40. this.pos = 0;
  41. this.destroyed = false;
  42. const { salt } = opts;
  43. this.blockLen = blockLen;
  44. this.outputLen = outputLen;
  45. this.lengthFlag = lengthFlag;
  46. this.counterLen = counterLen;
  47. this.buffer = new Uint8Array(blockLen);
  48. this.view = (0, utils_ts_1.createView)(this.buffer);
  49. if (salt) {
  50. let slt = salt;
  51. slt = (0, utils_ts_1.toBytes)(slt);
  52. (0, utils_ts_1.abytes)(slt);
  53. if (slt.length !== 4 * saltLen)
  54. throw new Error('wrong salt length');
  55. const salt32 = (this.salt = new Uint32Array(saltLen));
  56. const sv = (0, utils_ts_1.createView)(slt);
  57. this.constants = constants.slice();
  58. for (let i = 0, offset = 0; i < salt32.length; i++, offset += 4) {
  59. salt32[i] = sv.getUint32(offset, false);
  60. this.constants[i] ^= salt32[i];
  61. }
  62. }
  63. else {
  64. this.salt = EMPTY_SALT;
  65. this.constants = constants;
  66. }
  67. }
  68. update(data) {
  69. (0, utils_ts_1.aexists)(this);
  70. data = (0, utils_ts_1.toBytes)(data);
  71. (0, utils_ts_1.abytes)(data);
  72. // From _md, but update length before each compress
  73. const { view, buffer, blockLen } = this;
  74. const len = data.length;
  75. let dataView;
  76. for (let pos = 0; pos < len;) {
  77. const take = Math.min(blockLen - this.pos, len - pos);
  78. // Fast path: we have at least one block in input, cast it to view and process
  79. if (take === blockLen) {
  80. if (!dataView)
  81. dataView = (0, utils_ts_1.createView)(data);
  82. for (; blockLen <= len - pos; pos += blockLen) {
  83. this.length += blockLen;
  84. this.compress(dataView, pos);
  85. }
  86. continue;
  87. }
  88. buffer.set(data.subarray(pos, pos + take), this.pos);
  89. this.pos += take;
  90. pos += take;
  91. if (this.pos === blockLen) {
  92. this.length += blockLen;
  93. this.compress(view, 0, true);
  94. this.pos = 0;
  95. }
  96. }
  97. return this;
  98. }
  99. destroy() {
  100. this.destroyed = true;
  101. if (this.salt !== EMPTY_SALT) {
  102. (0, utils_ts_1.clean)(this.salt, this.constants);
  103. }
  104. }
  105. _cloneInto(to) {
  106. to || (to = new this.constructor());
  107. to.set(...this.get());
  108. const { buffer, length, finished, destroyed, constants, salt, pos } = this;
  109. to.buffer.set(buffer);
  110. to.constants = constants.slice();
  111. to.destroyed = destroyed;
  112. to.finished = finished;
  113. to.length = length;
  114. to.pos = pos;
  115. to.salt = salt.slice();
  116. return to;
  117. }
  118. clone() {
  119. return this._cloneInto();
  120. }
  121. digestInto(out) {
  122. (0, utils_ts_1.aexists)(this);
  123. (0, utils_ts_1.aoutput)(out, this);
  124. this.finished = true;
  125. // Padding
  126. const { buffer, blockLen, counterLen, lengthFlag, view } = this;
  127. (0, utils_ts_1.clean)(buffer.subarray(this.pos)); // clean buf
  128. const counter = BigInt((this.length + this.pos) * 8);
  129. const counterPos = blockLen - counterLen - 1;
  130. buffer[this.pos] |= 128; // End block flag
  131. this.length += this.pos; // add unwritten length
  132. // Not enough in buffer for length: write what we have.
  133. if (this.pos > counterPos) {
  134. this.compress(view, 0);
  135. (0, utils_ts_1.clean)(buffer);
  136. this.pos = 0;
  137. }
  138. // Difference with md: here we have lengthFlag!
  139. buffer[counterPos] |= lengthFlag; // Length flag
  140. // We always set 8 byte length flag. Because length will overflow significantly sooner.
  141. (0, _md_ts_1.setBigUint64)(view, blockLen - 8, counter, false);
  142. this.compress(view, 0, this.pos !== 0); // don't add length if length is not empty block?
  143. // Write output
  144. (0, utils_ts_1.clean)(buffer);
  145. const v = (0, utils_ts_1.createView)(out);
  146. const state = this.get();
  147. for (let i = 0; i < this.outputLen / 4; ++i)
  148. v.setUint32(i * 4, state[i]);
  149. }
  150. digest() {
  151. const { buffer, outputLen } = this;
  152. this.digestInto(buffer);
  153. const res = buffer.slice(0, outputLen);
  154. this.destroy();
  155. return res;
  156. }
  157. }
  158. // Constants
  159. const B64C = /* @__PURE__ */ Uint32Array.from([
  160. 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
  161. 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
  162. 0x9216d5d9, 0x8979fb1b, 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
  163. 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69,
  164. ]);
  165. // first half of C512
  166. const B32C = B64C.slice(0, 16);
  167. const B256_IV = _md_ts_1.SHA256_IV.slice();
  168. const B224_IV = _md_ts_1.SHA224_IV.slice();
  169. const B384_IV = _md_ts_1.SHA384_IV.slice();
  170. const B512_IV = _md_ts_1.SHA512_IV.slice();
  171. function generateTBL256() {
  172. const TBL = [];
  173. for (let i = 0, j = 0; i < 14; i++, j += 16) {
  174. for (let offset = 1; offset < 16; offset += 2) {
  175. TBL.push(B32C[_blake_ts_1.BSIGMA[j + offset]]);
  176. TBL.push(B32C[_blake_ts_1.BSIGMA[j + offset - 1]]);
  177. }
  178. }
  179. return new Uint32Array(TBL);
  180. }
  181. const TBL256 = /* @__PURE__ */ generateTBL256(); // C256[SIGMA[X]] precompute
  182. // Reusable temporary buffer
  183. const BLAKE256_W = /* @__PURE__ */ new Uint32Array(16);
  184. class Blake1_32 extends BLAKE1 {
  185. constructor(outputLen, IV, lengthFlag, opts = {}) {
  186. super(64, outputLen, lengthFlag, 8, 4, B32C, opts);
  187. this.v0 = IV[0] | 0;
  188. this.v1 = IV[1] | 0;
  189. this.v2 = IV[2] | 0;
  190. this.v3 = IV[3] | 0;
  191. this.v4 = IV[4] | 0;
  192. this.v5 = IV[5] | 0;
  193. this.v6 = IV[6] | 0;
  194. this.v7 = IV[7] | 0;
  195. }
  196. get() {
  197. const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
  198. return [v0, v1, v2, v3, v4, v5, v6, v7];
  199. }
  200. // prettier-ignore
  201. set(v0, v1, v2, v3, v4, v5, v6, v7) {
  202. this.v0 = v0 | 0;
  203. this.v1 = v1 | 0;
  204. this.v2 = v2 | 0;
  205. this.v3 = v3 | 0;
  206. this.v4 = v4 | 0;
  207. this.v5 = v5 | 0;
  208. this.v6 = v6 | 0;
  209. this.v7 = v7 | 0;
  210. }
  211. destroy() {
  212. super.destroy();
  213. this.set(0, 0, 0, 0, 0, 0, 0, 0);
  214. }
  215. compress(view, offset, withLength = true) {
  216. for (let i = 0; i < 16; i++, offset += 4)
  217. BLAKE256_W[i] = view.getUint32(offset, false);
  218. // NOTE: we cannot re-use compress from blake2s, since there is additional xor over u256[SIGMA[e]]
  219. let v00 = this.v0 | 0;
  220. let v01 = this.v1 | 0;
  221. let v02 = this.v2 | 0;
  222. let v03 = this.v3 | 0;
  223. let v04 = this.v4 | 0;
  224. let v05 = this.v5 | 0;
  225. let v06 = this.v6 | 0;
  226. let v07 = this.v7 | 0;
  227. let v08 = this.constants[0] | 0;
  228. let v09 = this.constants[1] | 0;
  229. let v10 = this.constants[2] | 0;
  230. let v11 = this.constants[3] | 0;
  231. const { h, l } = u64.fromBig(BigInt(withLength ? this.length * 8 : 0));
  232. let v12 = (this.constants[4] ^ l) >>> 0;
  233. let v13 = (this.constants[5] ^ l) >>> 0;
  234. let v14 = (this.constants[6] ^ h) >>> 0;
  235. let v15 = (this.constants[7] ^ h) >>> 0;
  236. // prettier-ignore
  237. for (let i = 0, k = 0, j = 0; i < 14; i++) {
  238. ({ a: v00, b: v04, c: v08, d: v12 } = (0, _blake_ts_1.G1s)(v00, v04, v08, v12, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  239. ({ a: v00, b: v04, c: v08, d: v12 } = (0, _blake_ts_1.G2s)(v00, v04, v08, v12, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  240. ({ a: v01, b: v05, c: v09, d: v13 } = (0, _blake_ts_1.G1s)(v01, v05, v09, v13, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  241. ({ a: v01, b: v05, c: v09, d: v13 } = (0, _blake_ts_1.G2s)(v01, v05, v09, v13, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  242. ({ a: v02, b: v06, c: v10, d: v14 } = (0, _blake_ts_1.G1s)(v02, v06, v10, v14, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  243. ({ a: v02, b: v06, c: v10, d: v14 } = (0, _blake_ts_1.G2s)(v02, v06, v10, v14, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  244. ({ a: v03, b: v07, c: v11, d: v15 } = (0, _blake_ts_1.G1s)(v03, v07, v11, v15, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  245. ({ a: v03, b: v07, c: v11, d: v15 } = (0, _blake_ts_1.G2s)(v03, v07, v11, v15, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  246. ({ a: v00, b: v05, c: v10, d: v15 } = (0, _blake_ts_1.G1s)(v00, v05, v10, v15, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  247. ({ a: v00, b: v05, c: v10, d: v15 } = (0, _blake_ts_1.G2s)(v00, v05, v10, v15, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  248. ({ a: v01, b: v06, c: v11, d: v12 } = (0, _blake_ts_1.G1s)(v01, v06, v11, v12, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  249. ({ a: v01, b: v06, c: v11, d: v12 } = (0, _blake_ts_1.G2s)(v01, v06, v11, v12, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  250. ({ a: v02, b: v07, c: v08, d: v13 } = (0, _blake_ts_1.G1s)(v02, v07, v08, v13, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  251. ({ a: v02, b: v07, c: v08, d: v13 } = (0, _blake_ts_1.G2s)(v02, v07, v08, v13, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  252. ({ a: v03, b: v04, c: v09, d: v14 } = (0, _blake_ts_1.G1s)(v03, v04, v09, v14, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  253. ({ a: v03, b: v04, c: v09, d: v14 } = (0, _blake_ts_1.G2s)(v03, v04, v09, v14, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
  254. }
  255. this.v0 = (this.v0 ^ v00 ^ v08 ^ this.salt[0]) >>> 0;
  256. this.v1 = (this.v1 ^ v01 ^ v09 ^ this.salt[1]) >>> 0;
  257. this.v2 = (this.v2 ^ v02 ^ v10 ^ this.salt[2]) >>> 0;
  258. this.v3 = (this.v3 ^ v03 ^ v11 ^ this.salt[3]) >>> 0;
  259. this.v4 = (this.v4 ^ v04 ^ v12 ^ this.salt[0]) >>> 0;
  260. this.v5 = (this.v5 ^ v05 ^ v13 ^ this.salt[1]) >>> 0;
  261. this.v6 = (this.v6 ^ v06 ^ v14 ^ this.salt[2]) >>> 0;
  262. this.v7 = (this.v7 ^ v07 ^ v15 ^ this.salt[3]) >>> 0;
  263. (0, utils_ts_1.clean)(BLAKE256_W);
  264. }
  265. }
  266. const BBUF = /* @__PURE__ */ new Uint32Array(32);
  267. const BLAKE512_W = /* @__PURE__ */ new Uint32Array(32);
  268. function generateTBL512() {
  269. const TBL = [];
  270. for (let r = 0, k = 0; r < 16; r++, k += 16) {
  271. for (let offset = 1; offset < 16; offset += 2) {
  272. TBL.push(B64C[_blake_ts_1.BSIGMA[k + offset] * 2 + 0]);
  273. TBL.push(B64C[_blake_ts_1.BSIGMA[k + offset] * 2 + 1]);
  274. TBL.push(B64C[_blake_ts_1.BSIGMA[k + offset - 1] * 2 + 0]);
  275. TBL.push(B64C[_blake_ts_1.BSIGMA[k + offset - 1] * 2 + 1]);
  276. }
  277. }
  278. return new Uint32Array(TBL);
  279. }
  280. const TBL512 = /* @__PURE__ */ generateTBL512(); // C512[SIGMA[X]] precompute
  281. // Mixing function G splitted in two halfs
  282. function G1b(a, b, c, d, msg, k) {
  283. const Xpos = 2 * _blake_ts_1.BSIGMA[k];
  284. const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
  285. let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
  286. let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
  287. let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
  288. let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
  289. // v[a] = (v[a] + v[b] + x) | 0;
  290. let ll = u64.add3L(Al, Bl, Xl);
  291. Ah = u64.add3H(ll, Ah, Bh, Xh) >>> 0;
  292. Al = (ll | 0) >>> 0;
  293. // v[d] = rotr(v[d] ^ v[a], 32)
  294. ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
  295. ({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
  296. // v[c] = (v[c] + v[d]) | 0;
  297. ({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
  298. // v[b] = rotr(v[b] ^ v[c], 25)
  299. ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
  300. ({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 25), Bl: u64.rotrSL(Bh, Bl, 25) });
  301. (BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah);
  302. (BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh);
  303. (BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch);
  304. (BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh);
  305. }
  306. function G2b(a, b, c, d, msg, k) {
  307. const Xpos = 2 * _blake_ts_1.BSIGMA[k];
  308. const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
  309. let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
  310. let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
  311. let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
  312. let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
  313. // v[a] = (v[a] + v[b] + x) | 0;
  314. let ll = u64.add3L(Al, Bl, Xl);
  315. Ah = u64.add3H(ll, Ah, Bh, Xh);
  316. Al = ll | 0;
  317. // v[d] = rotr(v[d] ^ v[a], 16)
  318. ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
  319. ({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
  320. // v[c] = (v[c] + v[d]) | 0;
  321. ({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
  322. // v[b] = rotr(v[b] ^ v[c], 11)
  323. ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
  324. ({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 11), Bl: u64.rotrSL(Bh, Bl, 11) });
  325. (BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah);
  326. (BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh);
  327. (BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch);
  328. (BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh);
  329. }
  330. class Blake1_64 extends BLAKE1 {
  331. constructor(outputLen, IV, lengthFlag, opts = {}) {
  332. super(128, outputLen, lengthFlag, 16, 8, B64C, opts);
  333. this.v0l = IV[0] | 0;
  334. this.v0h = IV[1] | 0;
  335. this.v1l = IV[2] | 0;
  336. this.v1h = IV[3] | 0;
  337. this.v2l = IV[4] | 0;
  338. this.v2h = IV[5] | 0;
  339. this.v3l = IV[6] | 0;
  340. this.v3h = IV[7] | 0;
  341. this.v4l = IV[8] | 0;
  342. this.v4h = IV[9] | 0;
  343. this.v5l = IV[10] | 0;
  344. this.v5h = IV[11] | 0;
  345. this.v6l = IV[12] | 0;
  346. this.v6h = IV[13] | 0;
  347. this.v7l = IV[14] | 0;
  348. this.v7h = IV[15] | 0;
  349. }
  350. // prettier-ignore
  351. get() {
  352. let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
  353. return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
  354. }
  355. // prettier-ignore
  356. set(v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h) {
  357. this.v0l = v0l | 0;
  358. this.v0h = v0h | 0;
  359. this.v1l = v1l | 0;
  360. this.v1h = v1h | 0;
  361. this.v2l = v2l | 0;
  362. this.v2h = v2h | 0;
  363. this.v3l = v3l | 0;
  364. this.v3h = v3h | 0;
  365. this.v4l = v4l | 0;
  366. this.v4h = v4h | 0;
  367. this.v5l = v5l | 0;
  368. this.v5h = v5h | 0;
  369. this.v6l = v6l | 0;
  370. this.v6h = v6h | 0;
  371. this.v7l = v7l | 0;
  372. this.v7h = v7h | 0;
  373. }
  374. destroy() {
  375. super.destroy();
  376. this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  377. }
  378. compress(view, offset, withLength = true) {
  379. for (let i = 0; i < 32; i++, offset += 4)
  380. BLAKE512_W[i] = view.getUint32(offset, false);
  381. this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
  382. BBUF.set(this.constants.subarray(0, 16), 16);
  383. if (withLength) {
  384. const { h, l } = u64.fromBig(BigInt(this.length * 8));
  385. BBUF[24] = (BBUF[24] ^ h) >>> 0;
  386. BBUF[25] = (BBUF[25] ^ l) >>> 0;
  387. BBUF[26] = (BBUF[26] ^ h) >>> 0;
  388. BBUF[27] = (BBUF[27] ^ l) >>> 0;
  389. }
  390. for (let i = 0, k = 0; i < 16; i++) {
  391. G1b(0, 4, 8, 12, BLAKE512_W, k++);
  392. G2b(0, 4, 8, 12, BLAKE512_W, k++);
  393. G1b(1, 5, 9, 13, BLAKE512_W, k++);
  394. G2b(1, 5, 9, 13, BLAKE512_W, k++);
  395. G1b(2, 6, 10, 14, BLAKE512_W, k++);
  396. G2b(2, 6, 10, 14, BLAKE512_W, k++);
  397. G1b(3, 7, 11, 15, BLAKE512_W, k++);
  398. G2b(3, 7, 11, 15, BLAKE512_W, k++);
  399. G1b(0, 5, 10, 15, BLAKE512_W, k++);
  400. G2b(0, 5, 10, 15, BLAKE512_W, k++);
  401. G1b(1, 6, 11, 12, BLAKE512_W, k++);
  402. G2b(1, 6, 11, 12, BLAKE512_W, k++);
  403. G1b(2, 7, 8, 13, BLAKE512_W, k++);
  404. G2b(2, 7, 8, 13, BLAKE512_W, k++);
  405. G1b(3, 4, 9, 14, BLAKE512_W, k++);
  406. G2b(3, 4, 9, 14, BLAKE512_W, k++);
  407. }
  408. this.v0l ^= BBUF[0] ^ BBUF[16] ^ this.salt[0];
  409. this.v0h ^= BBUF[1] ^ BBUF[17] ^ this.salt[1];
  410. this.v1l ^= BBUF[2] ^ BBUF[18] ^ this.salt[2];
  411. this.v1h ^= BBUF[3] ^ BBUF[19] ^ this.salt[3];
  412. this.v2l ^= BBUF[4] ^ BBUF[20] ^ this.salt[4];
  413. this.v2h ^= BBUF[5] ^ BBUF[21] ^ this.salt[5];
  414. this.v3l ^= BBUF[6] ^ BBUF[22] ^ this.salt[6];
  415. this.v3h ^= BBUF[7] ^ BBUF[23] ^ this.salt[7];
  416. this.v4l ^= BBUF[8] ^ BBUF[24] ^ this.salt[0];
  417. this.v4h ^= BBUF[9] ^ BBUF[25] ^ this.salt[1];
  418. this.v5l ^= BBUF[10] ^ BBUF[26] ^ this.salt[2];
  419. this.v5h ^= BBUF[11] ^ BBUF[27] ^ this.salt[3];
  420. this.v6l ^= BBUF[12] ^ BBUF[28] ^ this.salt[4];
  421. this.v6h ^= BBUF[13] ^ BBUF[29] ^ this.salt[5];
  422. this.v7l ^= BBUF[14] ^ BBUF[30] ^ this.salt[6];
  423. this.v7h ^= BBUF[15] ^ BBUF[31] ^ this.salt[7];
  424. (0, utils_ts_1.clean)(BBUF, BLAKE512_W);
  425. }
  426. }
  427. class BLAKE224 extends Blake1_32 {
  428. constructor(opts = {}) {
  429. super(28, B224_IV, 0, opts);
  430. }
  431. }
  432. exports.BLAKE224 = BLAKE224;
  433. class BLAKE256 extends Blake1_32 {
  434. constructor(opts = {}) {
  435. super(32, B256_IV, 1, opts);
  436. }
  437. }
  438. exports.BLAKE256 = BLAKE256;
  439. class BLAKE384 extends Blake1_64 {
  440. constructor(opts = {}) {
  441. super(48, B384_IV, 0, opts);
  442. }
  443. }
  444. exports.BLAKE384 = BLAKE384;
  445. class BLAKE512 extends Blake1_64 {
  446. constructor(opts = {}) {
  447. super(64, B512_IV, 1, opts);
  448. }
  449. }
  450. exports.BLAKE512 = BLAKE512;
  451. /** blake1-224 hash function */
  452. exports.blake224 = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE224(opts));
  453. /** blake1-256 hash function */
  454. exports.blake256 = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE256(opts));
  455. /** blake1-384 hash function */
  456. exports.blake384 = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE384(opts));
  457. /** blake1-512 hash function */
  458. exports.blake512 = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE512(opts));
  459. //# sourceMappingURL=blake1.js.map