]> git.codecow.com Git - libnemo.git/commitdiff
Simplify index calculations with addition and bitwise ops instead of multiplication.
authorChris Duncan <chris@zoso.dev>
Sun, 15 Feb 2026 06:59:58 +0000 (22:59 -0800)
committerChris Duncan <chris@zoso.dev>
Sun, 15 Feb 2026 06:59:58 +0000 (22:59 -0800)
src/lib/crypto/blake2b.ts

index 871b0c29e50dd1b0cb9f8fcaeb228f13a29df1e4..71253f981064f636141d956b51a96f2b7c61004d 100644 (file)
@@ -77,13 +77,13 @@ export class Blake2b {
        }
 
        #G (r: number, i: number, a: number, b: number, c: number, d: number): void {
-               this.#v[a] += this.#v[b] + this.#m[(this.constructor as typeof Blake2b).SIGMA[r][2 * i + 0]]
+               this.#v[a] += this.#v[b] + this.#m[(this.constructor as typeof Blake2b).SIGMA[r][i + i]]
                this.#v[d] ^= this.#v[a]
                this.#v[d] = (this.#v[d] >> 32n) | (this.#v[d] << 32n)
                this.#v[c] += this.#v[d]
                this.#v[b] ^= this.#v[c]
                this.#v[b] = (this.#v[b] >> 24n) | (this.#v[b] << 40n)
-               this.#v[a] += this.#v[b] + this.#m[(this.constructor as typeof Blake2b).SIGMA[r][2 * i + 1]]
+               this.#v[a] += this.#v[b] + this.#m[(this.constructor as typeof Blake2b).SIGMA[r][i + i + 1]]
                this.#v[d] ^= this.#v[a]
                this.#v[d] = (this.#v[d] >> 16n) | (this.#v[d] << 48n)
                this.#v[c] += this.#v[d]
@@ -130,7 +130,7 @@ export class Blake2b {
                // copy input buffer to message block
                const buf = new DataView(this.#b.buffer)
                for (let i = 0; i < 16; i++) {
-                       this.#m[i] = buf.getBigUint64(i * 8, true)
+                       this.#m[i] = buf.getBigUint64(i << 3, true)
                }
 
                // twelve rounds of mixing
@@ -266,7 +266,7 @@ export class Blake2b {
 
                // initialize hash state
                for (let i = 0; i < 8; i++) {
-                       this.#h[i] = B.IV[i] ^ this.#parameter_view.getBigUint64(i * 8, true)
+                       this.#h[i] = B.IV[i] ^ this.#parameter_view.getBigUint64(i << 3, true)
                }
 
                // key the hash, if applicable