* 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. */
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)
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
}
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
}
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! 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)) {
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! 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)) {
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! 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()
}
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 })
}
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') {
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()
}