From 9175e579e973a4cec35924ab6f89e1ad4c17877c Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 16 Oct 2026 07:42:04 -0700 Subject: [PATCH] Restore captured byte allocation and object & array accessors. Validate length of batch block input. --- src/lib/bytes.ts | 20 +++++++++++++++----- src/lib/derive.ts | 6 +++--- src/lib/sign.ts | 8 ++++---- src/lib/verify.ts | 25 +++++++++++++++---------- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/lib/bytes.ts b/src/lib/bytes.ts index 2a431ef..835ca1b 100644 --- a/src/lib/bytes.ts +++ b/src/lib/bytes.ts @@ -9,9 +9,10 @@ import { Nano25519RangeError, Nano25519TypeError, } from './errors' * that code patching a prototype afterwards cannot change what is validated, * copied, scrubbed or decoded. The `%TypedArray%.prototype` getters also read * internal slots, which a caller's subclass cannot override, so use these - * helpers on every typed array instead of its own properties. Global - * constructors and static functions are looked up at call time; replacing them, - * or running before this module, is out of scope. + * helpers on every typed array instead of its own properties. Use the captured + * `allocate()` and `words()` instead of `Uint8Array` and `Uint32Array`. Other + * global constructors and static functions are looked up at time-of-call, and + * capturing them is out of scope. */ /** Type aliases for legibility. */ @@ -54,6 +55,15 @@ function isArrayBuffer (a: unknown): a is ArrayBuffer { return false } +/** Captured version of `new Uint8Array(length)` with argument type-checking. */ +export function allocate (byteLength: number): Bytes { + const length = byteLength + if (typeof length !== 'number') { + throw new Nano25519TypeError('Invalid byte length') + } + return new Bytes(length) +} + /** Captured version of `Uint8Array.prototype.byteLength`. */ export function byteLength (bytes: Uint8Array): number { return getByteLength(bytes) @@ -175,7 +185,7 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n if (value.length < (byteLengthMin << 1) || value.length > (byteLengthMax << 1)) { throw new Nano25519TypeError(`Invalid hexadecimal length ${value.length} for ${name}`) } - if (typeof out === 'undefined') out = new Bytes(value.length >> 1) + if (typeof out === 'undefined') out = allocate(value.length >> 1) hexToBytes(out, value) return out } @@ -188,7 +198,7 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n if (byteLength > byteLengthMax) { throw new Nano25519TypeError(`${name} must be no more than ${byteLengthMax} bytes`) } - if (typeof out === 'undefined') out = new Bytes(byteLength) + if (typeof out === 'undefined') out = allocate(byteLength) copy(out, value) return out } diff --git a/src/lib/derive.ts b/src/lib/derive.ts index 57862a2..4dc5179 100644 --- a/src/lib/derive.ts +++ b/src/lib/derive.ts @@ -1,13 +1,13 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './bytes' +import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './bytes' import { Nano25519TypeError, } from './errors' import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm' const { KEY_BYTELENGTH, } = constants -const privateKey = new Uint8Array(KEY_BYTELENGTH) -const publicKey = new Uint8Array(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)) { diff --git a/src/lib/sign.ts b/src/lib/sign.ts index 0de6bb7..6ad1a83 100644 --- a/src/lib/sign.ts +++ b/src/lib/sign.ts @@ -1,14 +1,14 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './bytes' +import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './bytes' import { Nano25519TypeError, } from './errors' import { Mutex, constants, getOutput, setInput, sign as wasm_sign, } from './wasm' const { KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, SIGNATURE_BYTELENGTH, } = constants -const privateKey = new Uint8Array(KEY_BYTELENGTH) -const publicKey = new Uint8Array(KEY_BYTELENGTH) -const signature = new Uint8Array(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)) { diff --git a/src/lib/verify.ts b/src/lib/verify.ts index a97918e..6fec007 100644 --- a/src/lib/verify.ts +++ b/src/lib/verify.ts @@ -1,14 +1,16 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { byteLength, copy, normalize, } from './bytes' +import { allocate, byteLength, copy, normalize, } from './bytes' import { Nano25519RangeError, Nano25519TypeError, } from './errors' 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 = new Uint8Array(BLOCKHASH_BYTELENGTH) -const publicKey = new Uint8Array(KEY_BYTELENGTH) -const signature = new Uint8Array(SIGNATURE_BYTELENGTH) +const { create, defineProperty, freeze } = Object +const { isArray } = Array +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() @@ -28,8 +30,11 @@ export function verify (sig: unknown, msg: unknown, pub: unknown): boolean { } export function verify_blocks (pub: unknown, data: unknown): readonly boolean[] { - const blocks: unknown[] = Array.isArray(data) ? data : [data] - const count = blocks.length + const blocks: unknown[] = isArray(data) ? data : [data] + const count: unknown = blocks.length + if (typeof count !== 'number') { + throw new Nano25519TypeError('Invalid blocks length', { cause: data }) + } if (count < 1) { throw new Nano25519RangeError('Must pass at least one block', { cause: data }) } @@ -41,7 +46,7 @@ export function verify_blocks (pub: unknown, data: unknown): readonly boolean[] try { normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey) setInput('pub', publicKey) - const message = new Uint8Array(count * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH)) + const message = allocate(count * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH)) for (let b = 0; b < count; b++) { const block = blocks[b] if (block == null || typeof block !== 'object') { @@ -62,14 +67,14 @@ export function verify_blocks (pub: unknown, data: unknown): readonly boolean[] setInput('msg', message) wasm_verify_blocks(count) for (let i = 0; i < count; i++) { - const descriptor = Object.create(null) + const descriptor = create(null) descriptor.value = getOutput('verify', i) === 1 descriptor.writable = true descriptor.enumerable = true descriptor.configurable = true - Object.defineProperty(verified, i, descriptor) + defineProperty(verified, i, descriptor) } - return Object.freeze(verified) + return freeze(verified) } finally { Mutex.unlock() } -- 2.52.0