From f1c55d099beb923f63235b9ba51279f5203f067e Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 17 Oct 2026 01:00:10 -0700 Subject: [PATCH] Set private key bytes into WASM input buffer via byte-level copying only. --- src/lib/wasm.ts | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/lib/wasm.ts b/src/lib/wasm.ts index e9f8ae1..50d23d2 100644 --- a/src/lib/wasm.ts +++ b/src/lib/wasm.ts @@ -3,8 +3,8 @@ //@ts-expect-error import nano25519_wasm from '../../build/nano25519.wasm' -import { Nano25519MutexError, Nano25519TypeError, abort, errorCodes, } from './errors' import { Bytes, Words, byteLength, isLE, words, } from './bytes' +import { Nano25519MutexError, Nano25519TypeError, abort, errorCodes, } from './errors' type Exports = WebAssembly.Instance['exports'] & { clearMemory: () => void @@ -163,7 +163,19 @@ function setInputMsg (message: Bytes): void { } } -function setInputPrvShift (b: Bytes): void { +/** + * Copies the bytes of a private key into the static private key input buffer of + * the WASM module. Bytes are packed in little-endian order to align with the + * WASM spec. The entire input is written in one call rather than looped over an + * intermediate array because allocating costs more than the copy itself. + * + * This function **specifically** only operates on individual bytes and is not + * optimized to use 32-bit words because reading words can create numbers stored + * separately on the heap and can remain after zeroing the key, while reading + * bytes produces small integers that never get their own heap objects and are + * generally indistinguishable from other unrelated bytes. + */ +function setInputPrv (b: Bytes): void { exports.setInputPrv( (b[0] ?? 0) | ((b[1] ?? 0) << 8) | ((b[2] ?? 0) << 16) | ((b[3] ?? 0) << 24), (b[4] ?? 0) | ((b[5] ?? 0) << 8) | ((b[6] ?? 0) << 16) | ((b[7] ?? 0) << 24), @@ -176,33 +188,6 @@ function setInputPrvShift (b: Bytes): void { ) } -function setInputPrvWords (w: Words): void { - exports.setInputPrv( - w[0], - w[1], - w[2], - w[3], - w[4], - w[5], - w[6], - w[7] - ) -} - -const setInputPrvEndian = isLE ? setInputPrvWords : setInputPrvShift - -/** - * Copies the bytes of a private key into the static private key input buffer of - * the WASM module. Bytes are packed in little-endian order to align with the - * WASM spec. The entire input is written in one call rather than looped over an - * intermediate array because allocating costs more than the copy itself. - */ -function setInputPrv (prv: Bytes): void { - const p = setInputView(prv) - //@ts-expect-error - setInputPrvEndian(p) -} - function setInputPubShift (b: Bytes): void { exports.setInputPub( b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24), -- 2.52.0