From 045548f7bec7e41d80796da507348983917462f0 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 24 Aug 2026 15:26:47 -0700 Subject: [PATCH] Hoist buffer pointers to only reach into wasm once per buffer instead of every call site. Export buffer sizes for consistency when clearing. --- src/assembly/index.ts | 6 +++--- src/lib/nano25519.ts | 42 ++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/assembly/index.ts b/src/assembly/index.ts index 443e0b6..343ad17 100644 --- a/src/assembly/index.ts +++ b/src/assembly/index.ts @@ -10,9 +10,9 @@ const PUBLICKEY_BYTES: i32 = 32 const SIGNATURE_BYTES: i32 = 64 // Static I/O buffers -const INPUT_BUFFER_BYTES: i32 = SIGNATURE_BYTES + PUBLICKEY_BYTES -const MESSAGE_BUFFER_BYTES: i32 = 32768 -const OUTPUT_BUFFER_BYTES: i32 = SIGNATURE_BYTES +export const INPUT_BUFFER_BYTES: i32 = SIGNATURE_BYTES + PUBLICKEY_BYTES +export const MESSAGE_BUFFER_BYTES: i32 = 32768 +export const OUTPUT_BUFFER_BYTES: i32 = SIGNATURE_BYTES const INPUT_BUFFER = new StaticArray(INPUT_BUFFER_BYTES) const MESSAGE_BUFFER = new StaticArray(MESSAGE_BUFFER_BYTES) const OUTPUT_BUFFER = new StaticArray(OUTPUT_BUFFER_BYTES) diff --git a/src/lib/nano25519.ts b/src/lib/nano25519.ts index cdd0642..a5e1b21 100644 --- a/src/lib/nano25519.ts +++ b/src/lib/nano25519.ts @@ -12,6 +12,9 @@ type Exports = { getInputPointer: () => number getMessagePointer: () => number getOutputPointer: () => number + INPUT_BUFFER_BYTES: WebAssembly.Global + MESSAGE_BUFFER_BYTES: WebAssembly.Global + OUTPUT_BUFFER_BYTES: WebAssembly.Global memory: WebAssembly.Memory } } @@ -42,6 +45,12 @@ const { exports } = new WebAssembly.Instance(module, { } } }) as Exports +const IN_LEN = exports.INPUT_BUFFER_BYTES.value +const MSG_LEN = exports.MESSAGE_BUFFER_BYTES.value +const OUT_LEN = exports.OUTPUT_BUFFER_BYTES.value +const IN_PTR = exports.getInputPointer() +const MSG_PTR = exports.getMessagePointer() +const OUT_PTR = exports.getOutputPointer() export function derive (k: unknown, out?: unknown): string | Uint8Array | void { if (typeof out !== 'undefined' && !(isBytes(out) && out.byteLength === 32)) { @@ -52,15 +61,13 @@ export function derive (k: unknown, out?: unknown): string | Uint8Array { -- 2.52.0