//@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
}
}
-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),
)
}
-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),