From d08f7722cc9aacad6ae897b6b006c8be0c191e1c Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 12 Jul 2023 15:44:25 +0200 Subject: [PATCH] [sha256] perf updates - inlining and less scopes --- hash/sha256.fallback.js | 79 +++++++++++++++++++---------------------- hash/sha256.test.js | 4 +-- 2 files changed, 38 insertions(+), 45 deletions(-) diff --git a/hash/sha256.fallback.js b/hash/sha256.fallback.js index b72acb5..d08c520 100644 --- a/hash/sha256.fallback.js +++ b/hash/sha256.fallback.js @@ -77,6 +77,40 @@ export const hash = data => { const H = new Uint32Array(HINIT) // "Message schedule" - a working variable const W = new Uint32Array(64) + const updateHash = () => { + for (let t = 16; t < 64; t++) { + W[t] = sigma1to256(W[t - 2]) + W[t - 7] + sigma0to256(W[t - 15]) + W[t - 16] + } + let a = H[0] + let b = H[1] + let c = H[2] + let d = H[3] + let e = H[4] + let f = H[5] + let g = H[6] + let h = H[7] + // Step 3 + for (let tt = 0, T1, T2; tt < 64; tt++) { + T1 = (h + sum1to256(e) + ((e & f) ^ (~e & g)) + K[tt] + W[tt]) >>> 0 + T2 = (sum0to256(a) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0 + h = g + g = f + f = e + e = (d + T1) >>> 0 + d = c + c = b + b = a + a = (T1 + T2) >>> 0 + } + H[0] += a + H[1] += b + H[2] += c + H[3] += d + H[4] += e + H[5] += f + H[6] += g + H[7] += h + } let i = 0 let isPaddedWith1 = false for (; i + 56 <= data.length;) { @@ -94,7 +128,7 @@ export const hash = data => { } W[j] |= binary.BIT8 << ((3 - (i % 4)) * 8) } - updateHash(H, W, K) + updateHash() } // write rest of the data, including the padding (using msb endiannes) let j = 0 @@ -112,7 +146,7 @@ export const hash = data => { // @todo test that this works correctly W[14] = math.round(data.byteLength / binary.BIT30) W[15] = data.byteLength * 8 - updateHash(H, W, K) + updateHash() // correct H endianness and return a Uint8Array view const dv = new Uint8Array(H.buffer) for (let i = 0; i < H.length; i++) { @@ -123,44 +157,3 @@ export const hash = data => { } return dv } - -/** - * @param {Uint32Array} H - @todo since this is manipulated, it should be lower case - * @param {Uint32Array} W - * @param {Uint32Array} K - */ -const updateHash = (H, W, K) => { - for (let t = 16; t < 64; t++) { - W[t] = sigma1to256(W[t - 2]) + W[t - 7] + sigma0to256(W[t - 15]) + W[t - 16] - } - // Step 2 - let a = H[0] - let b = H[1] - let c = H[2] - let d = H[3] - let e = H[4] - let f = H[5] - let g = H[6] - let h = H[7] - // Step 3 - for (let t = 0; t < 64; t++) { - const T1 = (h + sum1to256(e) + ((e & f) ^ (~e & g)) + K[t] + W[t]) >>> 0 - const T2 = (sum0to256(a) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0 - h = g - g = f - f = e - e = (d + T1) >>> 0 - d = c - c = b - b = a - a = (T1 + T2) >>> 0 - } - H[0] += a - H[1] += b - H[2] += c - H[3] += d - H[4] += e - H[5] += f - H[6] += g - H[7] += h -} diff --git a/hash/sha256.test.js b/hash/sha256.test.js index 034eb2f..89427e6 100644 --- a/hash/sha256.test.js +++ b/hash/sha256.test.js @@ -28,9 +28,9 @@ export const testSha256Basics = async _tc => { t.assert(resWebcryptoHex === result) } - await test(string.encodeUtf8('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'), '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1') - await test(string.encodeUtf8('abc'), 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') await test('', 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855') + await test(string.encodeUtf8('abc'), 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') + await test(string.encodeUtf8('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'), '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1') } /**