]> git.codecow.com Git - nano25519.git/commitdiff
Set private key bytes into WASM input buffer via byte-level copying only.
authorChris Duncan <chris@codecow.com>
Sat, 17 Oct 2026 08:00:10 +0000 (01:00 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 17 Oct 2026 08:00:10 +0000 (01:00 -0700)
src/lib/wasm.ts

index e9f8ae1218a987bc7fadb9b9eb535ec4b48879c9..50d23d214fab8ddabea7f147562fd336c9c7bc05 100644 (file)
@@ -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),