]> git.codecow.com Git - nano25519.git/commitdiff
Deprecate excessive primordials and simplify others.
authorChris Duncan <chris@codecow.com>
Thu, 15 Oct 2026 20:10:45 +0000 (13:10 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 15 Oct 2026 20:10:45 +0000 (13:10 -0700)
src/lib/derive.ts
src/lib/primordials.ts
src/lib/sign.ts
src/lib/verify.ts
src/lib/wasm.ts

index c09cbeade44ceb8aa61d27fc77b92ef54e303a26..60252a4ea51b7b681b49c989281effbb850df5e9 100644 (file)
@@ -1,12 +1,13 @@
 //! 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)) {
index c089504ee98289bae97dd37a8ef495b1a5527bf8..a62a80109fa8c3887a8045a7fd130a3036e09e3a 100644 (file)
@@ -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<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 {
@@ -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<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.
@@ -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
        }
index e77ff939999e755cc1c0c72508b7e5a5fad71ba9..9a73affd10035ab2a0d9ea21aaea4ba6285d5e60 100644 (file)
@@ -1,13 +1,14 @@
 //! 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)) {
index 80a0c80f4fafb48cf1fe85f5c9e983fa9e411c2b..4ce692eb9ea6d13f91aaa9ea3ed2fc3a8d2d8b47 100644 (file)
@@ -1,13 +1,14 @@
 //! 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()
@@ -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 {
index b09cfddc02f01f1e8d560e2cfc69650e870d0621..3f0273d7d1b460bc13e3f3b34638dbaf7611aefa 100644 (file)
@@ -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,