Skip to content

Commit

Permalink
[sha256] perf updates - inlining and less scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jul 12, 2023
1 parent 20a94c4 commit d08f772
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 45 deletions.
79 changes: 36 additions & 43 deletions hash/sha256.fallback.js
Expand Up @@ -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;) {
Expand All @@ -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
Expand All @@ -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++) {
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions hash/sha256.test.js
Expand Up @@ -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')
}

/**
Expand Down

0 comments on commit d08f772

Please sign in to comment.