From 255352a0fcaf5c21374718371222a96395736adb Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 5 Mar 2024 01:45:37 +0000 Subject: [PATCH] Debug import bug --- benchmark/bug.js | 5 +++++ src/utils.ts | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 benchmark/bug.js diff --git a/benchmark/bug.js b/benchmark/bug.js new file mode 100644 index 0000000..ec67cdb --- /dev/null +++ b/benchmark/bug.js @@ -0,0 +1,5 @@ +const { scrypt } = require('../scrypt.js'); +let start = Date.now(); +scrypt('password', 'salt', { N: 2 ** 15, r: 8, p: 1, dkLen: 32 }) +let end = Date.now(); +console.log(end - start, 'ms'); \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index 03582db..8e0ada9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,7 +7,15 @@ // Makes the utils un-importable in browsers without a bundler. // Once node.js 18 is deprecated (2025-04-30), we can just drop the import. import { crypto } from '@noble/hashes/crypto'; -import { isBytes, bytes as abytes } from './_assert.js'; +import { bytes as abytes } from './_assert.js'; +// export { isBytes } from './_assert.js'; +// We can't reuse isBytes from _assert, because somehow this causes huge perf issues +export function isBytes(a: unknown): a is Uint8Array { + return ( + a instanceof Uint8Array || + (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array') + ); +} // prettier-ignore export type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array | @@ -38,8 +46,6 @@ export const byteSwap = (word: number) => // Conditionally byte swap if on a big-endian platform export const byteSwapIfBE = isLE ? (n: number) => n : (n: number) => byteSwap(n); -export { isBytes }; - // In place byte swap for Uint32Array export function byteSwap32(arr: Uint32Array) { for (let i = 0; i < arr.length; i++) {