//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! 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)) {
* 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<ArrayBuffer>
export type Words = Uint32Array<ArrayBuffer>
-/** 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 {
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)`. */
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
}
/** 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<T> (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.
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)
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
}
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
}
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! 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)) {
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! 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()
}
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 })
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') {
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 {
//@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
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,