From: Chris Duncan Date: Thu, 15 Oct 2026 20:10:45 +0000 (-0700) Subject: Deprecate excessive primordials and simplify others. X-Git-Tag: v2.0.0~1^2~20 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=509cf27f1689159eb1183b5671eb6c59a671cadf;p=nano25519.git Deprecate excessive primordials and simplify others. --- diff --git a/src/lib/derive.ts b/src/lib/derive.ts index c09cbea..60252a4 100644 --- a/src/lib/derive.ts +++ b/src/lib/derive.ts @@ -1,12 +1,13 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { Nano25519TypeError, allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './primordials' +import { byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './primordials' +import { Nano25519TypeError, } from './errors' import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm' const { KEY_BYTELENGTH, } = constants -const privateKey = allocate(KEY_BYTELENGTH) -const publicKey = allocate(KEY_BYTELENGTH) +const privateKey = new Uint8Array(KEY_BYTELENGTH) +const publicKey = new Uint8Array(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/primordials.ts b/src/lib/primordials.ts index c089504..a62a801 100644 --- a/src/lib/primordials.ts +++ b/src/lib/primordials.ts @@ -40,21 +40,16 @@ import { Nano25519RangeError, Nano25519TypeError, } from './errors' * bundler will order that dependency first, and any call it makes into here * during its own evaluation will read captures that are still unassigned. */ -const objectCreate = Object.create -export const objectDefineProperty = Object.defineProperty -export const objectFreeze = Object.freeze /** Type aliases for legibility. */ export type Bytes = Uint8Array export type Words = Uint32Array -/** Constructors bound before anything can replace the globals. */ -const Bytes = Uint8Array -const Words = Uint32Array -const TypedArray = Object.getPrototypeOf(Bytes.prototype) +/** Constructor bound before anything can replace the global. */ +const TypedArray = Object.getPrototypeOf(Uint8Array.prototype) -const bytesSet = Function.prototype.call.bind(Bytes.prototype.set) -const bytesFill = Function.prototype.call.bind(Bytes.prototype.fill) +const bytesSet = Function.prototype.call.bind(Uint8Array.prototype.set) +const bytesFill = Function.prototype.call.bind(Uint8Array.prototype.fill) /** An accessor's getter, taking the object to read as its argument. */ function getter (target: object, key: PropertyKey): (self: unknown) => any { @@ -85,32 +80,19 @@ function isArrayBuffer (a: unknown): a is ArrayBuffer { return false } -/** Captured version of `new Uint8Array(byteLength)`. */ -export function allocate (length: number): Bytes { - if (typeof length !== 'number') throw 'Invalid byte length for Uint8Array constructor' - return new Bytes(length) -} - /** Captured version of `Uint8Array.prototype.byteLength`. */ export function byteLength (bytes: Uint8Array): number { return getByteLength(bytes) } -/** - * A copy of `src` in a fresh buffer. Unlike `slice()` this never consults - * `Symbol.species`, so it cannot hand control to a caller-supplied subclass, - * and it sizes the copy from an internal slot rather than a spoofable `length`. - */ -export function copy (src: Uint8Array): Bytes +/** Captured version of `Uint8Array.prototype.byteLength`. */ +export function byteOffset (bytes: Uint8Array): number { + return getByteOffset(bytes) +} + /** `target.set(source, offset)` through the captured method. */ -export function copy (dst: Uint8Array, src: Uint8Array, offset?: number): void -export function copy (a: Uint8Array, b?: Uint8Array, o?: number): void | Bytes { - if (b) { - return bytesSet(a, b, o) - } - const t = allocate(getByteLength(a)) - bytesSet(t, a) - return t +export function copy (dst: Uint8Array, src: Uint8Array, offset?: number): void { + return bytesSet(dst, src, offset) } /** Captured version of `target.fill(value, start, end)`. */ @@ -118,9 +100,6 @@ export function fill (target: Uint8Array, value: number, start?: number, end?: n bytesFill(target, value, start, end) } -/** Captured version of `Array.isArray(value)`. */ -export const isArray = Array.isArray - /** * True only for a genuine `Uint8Array` backed by a non-shared `ArrayBuffer`. * Reads internal slot `[[TypedArrayName]]`, so a subclass overriding @@ -131,17 +110,7 @@ export function isBytes (a: unknown): a is Bytes { } /** True on little-endian system which aligns with WASM spec. */ -export const isLE = allocate(getBuffer(new Words([1])))[0] === 1 - -/** Sets `array[index] = value`, avoiding prototypes and getters/setters. */ -export function slot (array: T[], index: number, value: T): void { - const descriptor = objectCreate(null) - descriptor.value = value - descriptor.writable = true - descriptor.enumerable = true - descriptor.configurable = true - objectDefineProperty(array, index, descriptor) -} +export const isLE = new Uint8Array(getBuffer(new Uint32Array([1])))[0] === 1 /** * Captured version of `Uint32Array` reusing input offset and length. @@ -160,7 +129,7 @@ export function words (bytes: Bytes): Words { if (offset !== 0) { throw new Nano25519RangeError('Unexpected byte offset', { cause: offset }) } - return new Words(getBuffer(bytes) as ArrayBuffer, offset, getByteLength(bytes) >> 2) + return new Uint32Array(getBuffer(bytes) as ArrayBuffer, offset, getByteLength(bytes) >> 2) } const charCodeAt = Function.prototype.call.bind(String.prototype.charCodeAt) @@ -239,7 +208,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 = allocate(value.length >> 1) + if (typeof out === 'undefined') out = new Uint8Array(value.length >> 1) hexToBytes(out, value) return out } @@ -252,7 +221,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 = allocate(byteLength) + if (typeof out === 'undefined') out = new Uint8Array(byteLength) copy(out, value) return out } diff --git a/src/lib/sign.ts b/src/lib/sign.ts index e77ff93..9a73aff 100644 --- a/src/lib/sign.ts +++ b/src/lib/sign.ts @@ -1,13 +1,14 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { Nano25519TypeError, allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './primordials' +import { byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './primordials' +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 = allocate(KEY_BYTELENGTH) -const publicKey = allocate(KEY_BYTELENGTH) -const signature = allocate(SIGNATURE_BYTELENGTH) +const privateKey = new Uint8Array(KEY_BYTELENGTH) +const publicKey = new Uint8Array(KEY_BYTELENGTH) +const signature = new Uint8Array(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 80a0c80..4ce692e 100644 --- a/src/lib/verify.ts +++ b/src/lib/verify.ts @@ -1,13 +1,14 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { Nano25519RangeError, Nano25519TypeError, allocate, byteLength, copy, isArray, normalize, slot, } from './primordials' +import { byteLength, copy, normalize, } from './primordials' +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 = allocate(BLOCKHASH_BYTELENGTH) -const publicKey = allocate(KEY_BYTELENGTH) -const signature = allocate(SIGNATURE_BYTELENGTH) +const hash = new Uint8Array(BLOCKHASH_BYTELENGTH) +const publicKey = new Uint8Array(KEY_BYTELENGTH) +const signature = new Uint8Array(SIGNATURE_BYTELENGTH) export function verify (sig: unknown, msg: unknown, pub: unknown): boolean { Mutex.lock() @@ -27,7 +28,7 @@ export function verify (sig: unknown, msg: unknown, pub: unknown): boolean { } export function verify_blocks (pub: unknown, data: unknown): boolean[] { - const blocks: unknown[] = isArray(data) ? data : [data] + const blocks: unknown[] = Array.isArray(data) ? data : [data] const count = blocks.length if (count < 1) { throw new Nano25519RangeError('Must pass at least one block', { cause: data }) @@ -40,7 +41,7 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { try { normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey) setInput('pub', publicKey) - const message = allocate(count * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH)) + const message = new Uint8Array(count * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH)) for (let b = 0; b < count; b++) { const block = blocks[b] if (block == null || typeof block !== 'object') { @@ -61,7 +62,12 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { setInput('msg', message) wasm_verify_blocks(count) for (let i = 0; i < count; i++) { - slot(verified, i, getOutput('verify', i) === 1) + const descriptor = Object.create(null) + descriptor.value = getOutput('verify', i) === 1 + descriptor.writable = false + descriptor.enumerable = true + descriptor.configurable = false + Object.defineProperty(verified, i, descriptor) } return verified } finally { diff --git a/src/lib/wasm.ts b/src/lib/wasm.ts index b09cfdd..3f0273d 100644 --- a/src/lib/wasm.ts +++ b/src/lib/wasm.ts @@ -3,8 +3,8 @@ //@ts-expect-error import nano25519_wasm from '../../build/nano25519.wasm' -import { abort, errorCodes, Nano25519MutexError, Nano25519TypeError, } from './errors' -import { Bytes, Words, byteLength, isLE, objectFreeze, words, } from './primordials' +import { Nano25519MutexError, Nano25519TypeError, abort, errorCodes, } from './errors' +import { Bytes, Words, byteLength, isLE, words, } from './primordials' type Exports = WebAssembly.Instance['exports'] & { clearMemory: () => void @@ -299,7 +299,7 @@ function setInputSig (sig: Bytes): void { setInputSigEndian(s) } -export const constants = objectFreeze({ +export const constants = Object.freeze({ BLOCKHASH_BYTELENGTH: exports.BLOCKHASH_BYTELENGTH.value, KEY_BYTELENGTH: exports.KEY_BYTELENGTH.value, SIGNATURE_BYTELENGTH: exports.SIGNATURE_BYTELENGTH.value,