From 9bcde04d1324974e7f5f6c72896a47c425491b22 Mon Sep 17 00:00:00 2001 From: Zahin Mohammad Date: Wed, 26 Jul 2023 08:54:36 -0400 Subject: [PATCH] test(blake2b): fix tests for node 18 TICKET: WP-338 --- modules/blake2b/index.js | 74 +++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/modules/blake2b/index.js b/modules/blake2b/index.js index 77103a0749..2777222d8b 100644 --- a/modules/blake2b/index.js +++ b/modules/blake2b/index.js @@ -1,12 +1,23 @@ -var assert = require('nanoassert'); -var b2wasm = require('@bitgo/blake2b-wasm'); +const assert = require('nanoassert'); +const b2wasm = require('@bitgo/blake2b-wasm'); + +const BYTES_MIN = (module.exports.BYTES_MIN = 16); +const BYTES_MAX = (module.exports.BYTES_MAX = 64); +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const BYTES = (module.exports.BYTES = 32); +const KEYBYTES_MIN = (module.exports.KEYBYTES_MIN = 16); +const KEYBYTES_MAX = (module.exports.KEYBYTES_MAX = 64); +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const KEYBYTES = (module.exports.KEYBYTES = 32); +const SALTBYTES = (module.exports.SALTBYTES = 16); +const PERSONALBYTES = (module.exports.PERSONALBYTES = 16); // 64-bit unsigned addition // Sets v[a,a+1] += v[b,b+1] // v should be a Uint32Array function ADD64AA(v, a, b) { - var o0 = v[a] + v[b]; - var o1 = v[a + 1] + v[b + 1]; + const o0 = v[a] + v[b]; + let o1 = v[a + 1] + v[b + 1]; if (o0 >= 0x100000000) { o1++; } @@ -18,11 +29,11 @@ function ADD64AA(v, a, b) { // Sets v[a,a+1] += b // b0 is the low 32 bits of b, b1 represents the high 32 bits function ADD64AC(v, a, b0, b1) { - var o0 = v[a] + b0; + let o0 = v[a] + b0; if (b0 < 0) { o0 += 0x100000000; } - var o1 = v[a + 1] + b1; + let o1 = v[a + 1] + b1; if (o0 >= 0x100000000) { o1++; } @@ -38,17 +49,17 @@ function B2B_GET32(arr, i) { // G Mixing function // The ROTRs are inlined for speed function B2B_G(a, b, c, d, ix, iy) { - var x0 = m[ix]; - var x1 = m[ix + 1]; - var y0 = m[iy]; - var y1 = m[iy + 1]; + const x0 = m[ix]; + const x1 = m[ix + 1]; + const y0 = m[iy]; + const y1 = m[iy + 1]; ADD64AA(v, a, b); // v[a,a+1] += v[b,b+1] ... in JS we must store a uint64 as two uint32s ADD64AC(v, a, x0, x1); // v[a, a+1] += x ... x0 is the low 32 bits of x, x1 is the high 32 bits // v[d,d+1] = (v[d,d+1] xor v[a,a+1]) rotated to the right by 32 bits - var xor0 = v[d] ^ v[a]; - var xor1 = v[d + 1] ^ v[a + 1]; + let xor0 = v[d] ^ v[a]; + let xor1 = v[d + 1] ^ v[a + 1]; v[d] = xor1; v[d + 1] = xor0; @@ -79,12 +90,12 @@ function B2B_G(a, b, c, d, ix, iy) { } // Initialization Vector -var BLAKE2B_IV32 = new Uint32Array([ +const BLAKE2B_IV32 = new Uint32Array([ 0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a, 0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19, ]); -var SIGMA8 = [ +const SIGMA8 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3, 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4, 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8, 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13, 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9, 12, 5, 1, 15, 14, 13, 4, 10, 0, @@ -96,7 +107,7 @@ var SIGMA8 = [ // These are offsets into a uint64 buffer. // Multiply them all by 2 to make them offsets into a uint32 buffer, // because this is JavaScript and we don't have uint64s -var SIGMA82 = new Uint8Array( +const SIGMA82 = new Uint8Array( SIGMA8.map(function (x) { return x * 2; }) @@ -104,12 +115,12 @@ var SIGMA82 = new Uint8Array( // Compression function. 'last' flag indicates last block. // Note we're representing 16 uint64s as 32 uint32s -var v = new Uint32Array(32); -var m = new Uint32Array(32); +const v = new Uint32Array(32); +const m = new Uint32Array(32); function blake2bCompress(ctx, last) { - var i = 0; + let i = 0; - // init work variables + // init work letiables for (i = 0; i < 16; i++) { v[i] = ctx.h[i]; v[i + 16] = BLAKE2B_IV32[i]; @@ -149,7 +160,7 @@ function blake2bCompress(ctx, last) { } // reusable parameter_block -var parameter_block = new Uint8Array([ +const parameter_block = new Uint8Array([ 0, 0, 0, @@ -239,7 +250,7 @@ function Blake2b(outlen, key, salt, personal) { if (personal) parameter_block.set(personal, 48); // initialize hash state - for (var i = 0; i < 16; i++) { + for (let i = 0; i < 16; i++) { this.h[i] = BLAKE2B_IV32[i] ^ B2B_GET32(parameter_block, i * 4); } @@ -258,7 +269,7 @@ Blake2b.prototype.update = function (input) { }; Blake2b.prototype.digest = function (out) { - var buf = !out || out === 'binary' || out === 'hex' ? new Uint8Array(this.outlen) : out; + const buf = !out || out === 'binary' || out === 'hex' ? new Uint8Array(this.outlen) : out; assert(buf instanceof Uint8Array, 'out must be "binary", "hex", Uint8Array, or Buffer'); assert(buf.length >= this.outlen, 'out must have at least outlen bytes of space'); blake2bFinal(this, buf); @@ -277,7 +288,7 @@ Blake2b.ready = function (cb) { // Updates a BLAKE2b streaming hash // Requires hash context and Uint8Array (byte array) function blake2bUpdate(ctx, input) { - for (var i = 0; i < input.length; i++) { + for (let i = 0; i < input.length; i++) { if (ctx.c === 128) { // buffer full ? ctx.t += ctx.c; // add counters @@ -299,15 +310,15 @@ function blake2bFinal(ctx, out) { } blake2bCompress(ctx, true); // final block flag = 1 - for (var i = 0; i < ctx.outlen; i++) { + for (let i = 0; i < ctx.outlen; i++) { out[i] = ctx.h[i >> 2] >> (8 * (i & 3)); } return out; } function hexSlice(buf) { - var str = ''; - for (var i = 0; i < buf.length; i++) str += toHex(buf[i]); + let str = ''; + for (let i = 0; i < buf.length; i++) str += toHex(buf[i]); return str; } @@ -316,7 +327,7 @@ function toHex(n) { return n.toString(16); } -var Proto = Blake2b; +const Proto = Blake2b; module.exports = function createHash(outlen, key, salt, personal, noAssert) { if (noAssert !== true) { @@ -353,15 +364,6 @@ module.exports.ready = function (cb) { module.exports.WASM_SUPPORTED = b2wasm.SUPPORTED; module.exports.WASM_LOADED = false; -var BYTES_MIN = (module.exports.BYTES_MIN = 16); -var BYTES_MAX = (module.exports.BYTES_MAX = 64); -var BYTES = (module.exports.BYTES = 32); -var KEYBYTES_MIN = (module.exports.KEYBYTES_MIN = 16); -var KEYBYTES_MAX = (module.exports.KEYBYTES_MAX = 64); -var KEYBYTES = (module.exports.KEYBYTES = 32); -var SALTBYTES = (module.exports.SALTBYTES = 16); -var PERSONALBYTES = (module.exports.PERSONALBYTES = 16); - b2wasm.ready(function (err) { if (!err) { module.exports.WASM_LOADED = true;