Skip to content

Commit 59fda2c

Browse files
authored
Merge pull request #122 from ChALkeR/chalker/perf/scrypt/0
perf: tiny improvement in scrypt
2 parents 30f8780 + 90dfe17 commit 59fda2c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/scrypt.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ export function scrypt(password: KDFInput, salt: KDFInput, opts: ScryptOpts): Ui
198198
blockMixCb();
199199
for (let i = 0; i < N; i++) {
200200
// First u32 of the last 64-byte block (u32 is LE)
201-
const j = B32[Pi + blockSize32 - 16] % N; // j = Integrify(X) % iterations
201+
// & (N - 1) is % N as N is a power of 2, N & (N - 1) = 0 is checked above; >>> 0 for unsigned, input fits in u32
202+
const j = (B32[Pi + blockSize32 - 16] & (N - 1)) >>> 0; // j = Integrify(X) % iterations
202203
for (let k = 0; k < blockSize32; k++) tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k]; // tmp = B ^ V[j]
203204
BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
204205
blockMixCb();
@@ -236,7 +237,8 @@ export async function scryptAsync(
236237
blockMixCb();
237238
await asyncLoop(N, asyncTick, () => {
238239
// First u32 of the last 64-byte block (u32 is LE)
239-
const j = B32[Pi + blockSize32 - 16] % N; // j = Integrify(X) % iterations
240+
// & (N - 1) is % N as N is a power of 2, N & (N - 1) = 0 is checked above; >>> 0 for unsigned, input fits in u32
241+
const j = (B32[Pi + blockSize32 - 16] & (N - 1)) >>> 0; // j = Integrify(X) % iterations
240242
for (let k = 0; k < blockSize32; k++) tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k]; // tmp = B ^ V[j]
241243
BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
242244
blockMixCb();

0 commit comments

Comments
 (0)