From e3bba6a0da3a0de14b11431259cedc503a911ea9 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 14 Aug 2026 16:29:17 -0700 Subject: [PATCH] Avoid writing seed to output buffer of derive since the private key bytes are discarded by JS caller anyway. --- src/assembly/index.ts | 13 +++++-------- src/lib/nano25519.ts | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/assembly/index.ts b/src/assembly/index.ts index 51e1b6d..da1d0ee 100644 --- a/src/assembly/index.ts +++ b/src/assembly/index.ts @@ -36,8 +36,7 @@ function crypto_derive (sk: StaticArray, seed: StaticArray): void { clamp(sk) ge_scalarmult_base_tobytes(pk, sk) - memory.copy(changetype(sk), changetype(seed), PRIVATEKEY_BYTES) - memory.copy(changetype(sk) + 32, changetype(pk), PUBLICKEY_BYTES) + memory.copy(changetype(sk), changetype(pk), PUBLICKEY_BYTES) } const crypto_sign_az = new StaticArray(64) @@ -137,16 +136,14 @@ export function getMessagePointer (): usize { } /** - * Derives a Nano public key from a private key and writes it to the static - * output buffer. This mirrors the functionality of - * `nacl.sign.keyPair.fromSeed()`, returning both private and public keys as a - * concatenated `secretKey` property. + * Uses a private key to derive a Nano public key which is then written to the + * static output buffer. */ export function derive (): void { - const sk = changetype>(OUTPUT_BUFFER) + const pk = changetype>(OUTPUT_BUFFER) const seed = changetype>(INPUT_BUFFER) memory.fill(OUTPUT_BUFFER, 0, SECRETKEY_BYTES) - crypto_derive(sk, seed) + crypto_derive(pk, seed) memory.fill(INPUT_BUFFER, 0, PRIVATEKEY_BYTES) } diff --git a/src/lib/nano25519.ts b/src/lib/nano25519.ts index da2def5..ca2fe32 100644 --- a/src/lib/nano25519.ts +++ b/src/lib/nano25519.ts @@ -85,7 +85,7 @@ export const nano25519_init = (bytes: number[]): { derive: typeof derive, sign: const outPtr = exports.getOutputPointer() buffer = new DataView(exports.memory.buffer) for (let i = 0; i < 32; i++) { - out[i] = buffer.getUint8(outPtr + i + 32) + out[i] = buffer.getUint8(outPtr + i) } clear(buffer) if (typeof k === 'string') { -- 2.52.0