From: Chris Duncan Date: Sat, 10 Oct 2026 20:17:50 +0000 (-0700) Subject: Refactor entry point buffer design to allow constants for byte arrays under module... X-Git-Tag: v2.0.0~1^2~30 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=cbf4426edc6d1a888e56c50358dde3bc3ec5a11a;p=nano25519.git Refactor entry point buffer design to allow constants for byte arrays under module control while allowing variable size messages. --- diff --git a/src/lib/derive.ts b/src/lib/derive.ts index dbe7d68..0c90b34 100644 --- a/src/lib/derive.ts +++ b/src/lib/derive.ts @@ -5,8 +5,8 @@ import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm' const { KEY_BYTELENGTH, } = constants -let privateKey = allocate(KEY_BYTELENGTH) -let publicKey = allocate(KEY_BYTELENGTH) +const privateKey = allocate(KEY_BYTELENGTH) +const publicKey = allocate(KEY_BYTELENGTH) export function derive (prv: unknown, out?: unknown): string | void { if (typeof out !== 'undefined' && !(isBytes(out) && byteLength(out) === KEY_BYTELENGTH)) { @@ -14,7 +14,7 @@ export function derive (prv: unknown, out?: unknown): string | void { } Mutex.lock() try { - privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv) + normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv, privateKey) setInput('prv', privateKey) wasm_derive() for (let i = 0; i < KEY_BYTELENGTH; i++) { diff --git a/src/lib/primordials.ts b/src/lib/primordials.ts index 78bb493..90cc92a 100644 --- a/src/lib/primordials.ts +++ b/src/lib/primordials.ts @@ -194,25 +194,30 @@ export function bytesToHex (bytes: Uint8Array): string { /** * Decodes hex string to Bytes. */ -function hexToBytes (hex: string): Bytes { +function hexToBytes (out: Bytes, hex: string): void { const length = hex.length if (length & 1) { throw new TypeError(`Invalid hexadecimal length ${length}`) } - const bytes = new Bytes(length >> 1) + const byteLength = getByteLength(out) + if (byteLength !== (length >> 1)) { + throw new TypeError(`Invalid output byte length ${byteLength}`) + } let d = 0 for (let i = 0, b = 0; i < length; i += 2, b++) { const hi = nibble(charCodeAt(hex, i)) const lo = nibble(charCodeAt(hex, i + 1)) d |= hi | lo - bytes[b] = (hi << 4) | lo + out[b] = (hi << 4) | lo } if (d >> 4) { - fill(bytes, 0) + fill(out, 0) throw new TypeError('Invalid hexadecimal character') } - return bytes } + +export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown): Bytes +export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown, out: Bytes): Bytes /** * Accepts an unknown input; checks that it is a representation of bytes using a * hex string or Uint8Array; and validates that the byte length of @@ -224,12 +229,14 @@ function hexToBytes (hex: string): Bytes { * @param {unknown} value User input to validate * @returns User input bytes copied to a new Uint8Array for internal use. */ -export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown): Bytes { +export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown, out?: Bytes): Bytes { if (typeof value === 'string') { if (value.length < (byteLengthMin << 1) || value.length > (byteLengthMax << 1)) { throw new TypeError(`Invalid hexadecimal length ${value.length} for ${name}`) } - return hexToBytes(value) + if (typeof out === 'undefined') out = allocate(value.length >> 1) + hexToBytes(out, value) + return out } if (isBytes(value)) { @@ -240,8 +247,9 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n if (byteLength > byteLengthMax) { throw new TypeError(`${name} must be no more than ${byteLengthMax} bytes`) } - const bytes = copy(value) - return bytes + if (typeof out === 'undefined') out = allocate(byteLength) + copy(out, value) + return out } throw new TypeError(`${name} must be a hex string or Uint8Array`) diff --git a/src/lib/sign.ts b/src/lib/sign.ts index 1bc7c16..5f638f4 100644 --- a/src/lib/sign.ts +++ b/src/lib/sign.ts @@ -5,9 +5,9 @@ import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from import { Mutex, constants, getOutput, setInput, sign as wasm_sign, } from './wasm' const { KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, SIGNATURE_BYTELENGTH, } = constants -let privateKey = allocate(KEY_BYTELENGTH) -let publicKey = allocate(KEY_BYTELENGTH) -let signature = allocate(SIGNATURE_BYTELENGTH) +const privateKey = allocate(KEY_BYTELENGTH) +const publicKey = allocate(KEY_BYTELENGTH) +const signature = allocate(SIGNATURE_BYTELENGTH) export function sign (msg: unknown, prv: unknown, pub: unknown, out?: unknown): string | void { if (typeof out !== 'undefined' && !(isBytes(out) && byteLength(out) === SIGNATURE_BYTELENGTH)) { @@ -16,8 +16,8 @@ export function sign (msg: unknown, prv: unknown, pub: unknown, out?: unknown): Mutex.lock() try { const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg) - privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv) - publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub) + normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv, privateKey) + normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey) setInput('msg', message) setInput('prv', privateKey) setInput('pub', publicKey) diff --git a/src/lib/verify.ts b/src/lib/verify.ts index f7e89a3..df551d6 100644 --- a/src/lib/verify.ts +++ b/src/lib/verify.ts @@ -5,13 +5,16 @@ import { allocate, byteLength, copy, isArray, normalize, slot } from './primordi import { Mutex, constants, getOutput, setInput, verify as wasm_verify, verify_blocks as wasm_verify_blocks, } from './wasm' const { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, SIGNATURE_BYTELENGTH, } = constants +const hash = allocate(BLOCKHASH_BYTELENGTH) +const publicKey = allocate(KEY_BYTELENGTH) +const signature = allocate(SIGNATURE_BYTELENGTH) export function verify (sig: unknown, msg: unknown, pub: unknown): boolean { Mutex.lock() try { const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg) - const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub) - const signature = normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, sig) + normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey) + normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, sig, signature) setInput('msg', message) setInput('pub', publicKey) setInput('sig', signature) @@ -35,7 +38,7 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { const verified: boolean[] = [] Mutex.lock() try { - const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub) + normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey) setInput('pub', publicKey) const message = allocate(count * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH)) for (let b = 0; b < count; b++) { @@ -49,8 +52,8 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { if (!('hash' in block)) { throw new TypeError('Block hash is required to verify', { cause: block }) } - const signature = normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, block.signature) - const hash = normalize('hash', BLOCKHASH_BYTELENGTH, BLOCKHASH_BYTELENGTH, block.hash) + normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, block.signature, signature) + normalize('hash', BLOCKHASH_BYTELENGTH, BLOCKHASH_BYTELENGTH, block.hash, hash) const offset = b * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH) copy(message, signature, offset) copy(message, hash, offset + SIGNATURE_BYTELENGTH)