]> git.codecow.com Git - nano25519.git/commitdiff
Refactor entry point buffer design to allow constants for byte arrays under module...
authorChris Duncan <chris@codecow.com>
Sat, 10 Oct 2026 20:17:50 +0000 (13:17 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 10 Oct 2026 20:17:50 +0000 (13:17 -0700)
src/lib/derive.ts
src/lib/primordials.ts
src/lib/sign.ts
src/lib/verify.ts

index dbe7d6897112d69c146b2b8102f334f6ccd80779..0c90b3456fc7984e305366bccd54f0048a7af7c9 100644 (file)
@@ -5,8 +5,8 @@ import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from
 import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm'
 
 const { KEY_BYTELENGTH, } = constants
-let privateKey = allocate(KEY_BYTELENGTH)
-let publicKey = allocate(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)) {
@@ -14,7 +14,7 @@ export function derive (prv: unknown, out?: unknown): string | void {
        }
        Mutex.lock()
        try {
-               privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv)
+               normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv, privateKey)
                setInput('prv', privateKey)
                wasm_derive()
                for (let i = 0; i < KEY_BYTELENGTH; i++) {
index 78bb4938fde1f371c46b22a653502bc40d843c1f..90cc92ab6891392ac1078b60e38ca92e909fbc51 100644 (file)
@@ -194,25 +194,30 @@ export function bytesToHex (bytes: Uint8Array): string {
 /**
  * Decodes hex string to Bytes.
  */
-function hexToBytes (hex: string): Bytes {
+function hexToBytes (out: Bytes, hex: string): void {
        const length = hex.length
        if (length & 1) {
                throw new TypeError(`Invalid hexadecimal length ${length}`)
        }
-       const bytes = new Bytes(length >> 1)
+       const byteLength = getByteLength(out)
+       if (byteLength !== (length >> 1)) {
+               throw new TypeError(`Invalid output byte length ${byteLength}`)
+       }
        let d = 0
        for (let i = 0, b = 0; i < length; i += 2, b++) {
                const hi = nibble(charCodeAt(hex, i))
                const lo = nibble(charCodeAt(hex, i + 1))
                d |= hi | lo
-               bytes[b] = (hi << 4) | lo
+               out[b] = (hi << 4) | lo
        }
        if (d >> 4) {
-               fill(bytes, 0)
+               fill(out, 0)
                throw new TypeError('Invalid hexadecimal character')
        }
-       return bytes
 }
+
+export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown): Bytes
+export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown, out: Bytes): Bytes
 /**
  * Accepts an unknown input; checks that it is a representation of bytes using a
  * hex string or Uint8Array<ArrayBuffer>; and validates that the byte length of
@@ -224,12 +229,14 @@ function hexToBytes (hex: string): Bytes {
  * @param {unknown} value User input to validate
  * @returns User input bytes copied to a new Uint8Array for internal use.
  */
-export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown): Bytes {
+export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown, out?: Bytes): Bytes {
        if (typeof value === 'string') {
                if (value.length < (byteLengthMin << 1) || value.length > (byteLengthMax << 1)) {
                        throw new TypeError(`Invalid hexadecimal length ${value.length} for ${name}`)
                }
-               return hexToBytes(value)
+               if (typeof out === 'undefined') out = allocate(value.length >> 1)
+               hexToBytes(out, value)
+               return out
        }
 
        if (isBytes(value)) {
@@ -240,8 +247,9 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n
                if (byteLength > byteLengthMax) {
                        throw new TypeError(`${name} must be no more than ${byteLengthMax} bytes`)
                }
-               const bytes = copy(value)
-               return bytes
+               if (typeof out === 'undefined') out = allocate(byteLength)
+               copy(out, value)
+               return out
        }
 
        throw new TypeError(`${name} must be a hex string or Uint8Array<ArrayBuffer>`)
index 1bc7c16b87569f88ec2548f0c0e343d4282a829c..5f638f41fa953ea3802e9307370902f68d8cd2fd 100644 (file)
@@ -5,9 +5,9 @@ import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from
 import { Mutex, constants, getOutput, setInput, sign as wasm_sign, } from './wasm'
 
 const { KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, SIGNATURE_BYTELENGTH, } = constants
-let privateKey = allocate(KEY_BYTELENGTH)
-let publicKey = allocate(KEY_BYTELENGTH)
-let signature = allocate(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)) {
@@ -16,8 +16,8 @@ export function sign (msg: unknown, prv: unknown, pub: unknown, out?: unknown):
        Mutex.lock()
        try {
                const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg)
-               privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv)
-               publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub)
+               normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv, privateKey)
+               normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey)
                setInput('msg', message)
                setInput('prv', privateKey)
                setInput('pub', publicKey)
index f7e89a3a6294fce2d76e33dc2746cdf8f8ece3f5..df551d6d3bf0c694083a00e89ecc0c4689de515d 100644 (file)
@@ -5,13 +5,16 @@ import { allocate, byteLength, copy, isArray, normalize, slot } from './primordi
 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)
 
 export function verify (sig: unknown, msg: unknown, pub: unknown): boolean {
        Mutex.lock()
        try {
                const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg)
-               const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub)
-               const signature = normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, sig)
+               normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey)
+               normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, sig, signature)
                setInput('msg', message)
                setInput('pub', publicKey)
                setInput('sig', signature)
@@ -35,7 +38,7 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] {
        const verified: boolean[] = []
        Mutex.lock()
        try {
-               const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub)
+               normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey)
                setInput('pub', publicKey)
                const message = allocate(count * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH))
                for (let b = 0; b < count; b++) {
@@ -49,8 +52,8 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] {
                        if (!('hash' in block)) {
                                throw new TypeError('Block hash is required to verify', { cause: block })
                        }
-                       const signature = normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, block.signature)
-                       const hash = normalize('hash', BLOCKHASH_BYTELENGTH, BLOCKHASH_BYTELENGTH, block.hash)
+                       normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, block.signature, signature)
+                       normalize('hash', BLOCKHASH_BYTELENGTH, BLOCKHASH_BYTELENGTH, block.hash, hash)
                        const offset = b * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH)
                        copy(message, signature, offset)
                        copy(message, hash, offset + SIGNATURE_BYTELENGTH)