From: Chris Duncan Date: Sat, 10 Oct 2026 07:41:53 +0000 (-0700) Subject: Capture array constructor to control index access on block verification results. X-Git-Tag: v2.0.0~1^2~39 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=0eb1bff8249988526aef4c60fd8b34929fc106c3;p=nano25519.git Capture array constructor to control index access on block verification results. --- diff --git a/src/lib/primordials.ts b/src/lib/primordials.ts index fcf43f7..37b8da1 100644 --- a/src/lib/primordials.ts +++ b/src/lib/primordials.ts @@ -39,15 +39,18 @@ * during its own evaluation will read captures that are still unassigned. */ -/** Type alias for legibility. */ +/** Type aliases for legibility. */ +export type List = Array export type Bytes = Uint8Array export type Words = Uint32Array -/** `TypedArray` constructors, bound before anything can replace the globals. */ +/** Constructors bound before anything can replace the globals. */ +const List = Array const Bytes = Uint8Array const Words = Uint32Array const TypedArray = Object.getPrototypeOf(Bytes.prototype) +const arrayFill = Function.prototype.call.bind(List.prototype.fill) const bytesSet = Function.prototype.call.bind(Bytes.prototype.set) const bytesFill = Function.prototype.call.bind(Bytes.prototype.fill) @@ -84,9 +87,6 @@ export function allocate (length: number): Bytes { return new Bytes(length) } -/** Captured version of `Array.push(value)`. */ -export const arrayPush = Function.prototype.call.bind(Array.prototype.push) - /** Captured version of `Uint8Array.prototype.byteLength`. */ export function byteLength (bytes: Uint8Array): number { return getByteLength(bytes) @@ -129,6 +129,11 @@ export function isBytes (a: unknown): a is Bytes { /** Checks system endianness. Returns true if little-endian, else false. */ export const isLE = new Bytes(getBuffer(new Words([1])))[0] === 1 +/** Creates a non-sparse array of the specified length */ +export function list (length: number): List { + return arrayFill(new List(length), undefined) +} + /** * Captured version of `Uint32Array` reusing input offset and length. * Enables reading multiple bytes in a single load which is more efficient than diff --git a/src/lib/verify.ts b/src/lib/verify.ts index c416de8..ca513be 100644 --- a/src/lib/verify.ts +++ b/src/lib/verify.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { allocate, byteLength, copy, isArray, normalize, arrayPush } from './primordials' +import { allocate, byteLength, copy, isArray, list, normalize, } from './primordials' 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 @@ -32,7 +32,7 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { if (MAX_VERIFY_BLOCKS < count) { throw new RangeError(`Bulk Nano block verification must be no more than ${MAX_VERIFY_BLOCKS} blocks`, { cause: count }) } - const verified: boolean[] = [] + const verified: boolean[] = list(count) Mutex.lock() try { const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub) @@ -58,7 +58,7 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { setInput('msg', message) wasm_verify_blocks(count) for (let i = 0; i < count; i++) { - arrayPush(verified, getOutput('verify', i) === 1) + verified[i] = getOutput('verify', i) === 1 } return verified } finally {