From bae65f0243f3e34bc114c952437713499c6f0fc3 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 3 Dec 2024 14:47:46 -0800 Subject: [PATCH] Use faster for loop instead of reduce function. Add typings to NanoNaCl. --- src/lib/nano-nacl.ts | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/lib/nano-nacl.ts b/src/lib/nano-nacl.ts index 5338b20..70dc671 100644 --- a/src/lib/nano-nacl.ts +++ b/src/lib/nano-nacl.ts @@ -707,7 +707,7 @@ const n = () => { return 0 } - function crypto_sign_open (m: Uint8Array, sm: Uint8Array, n: number, pk: Uint8Array) { + function crypto_sign_open (m: Uint8Array, sm: Uint8Array, n: number, pk: Uint8Array): number { var t = new Uint8Array(32), h = new Uint8Array(64) var p = [gf(), gf(), gf(), gf()], q = [gf(), gf(), gf(), gf()] @@ -736,37 +736,39 @@ const n = () => { return n } - var crypto_sign_BYTES = 64, - crypto_sign_PUBLICKEYBYTES = 32, - crypto_sign_SECRETKEYBYTES = 64, - crypto_sign_SEEDBYTES = 32 + const crypto_sign_BYTES = 64 + const crypto_sign_PUBLICKEYBYTES = 32 + const crypto_sign_SECRETKEYBYTES = 64 + const crypto_sign_SEEDBYTES = 32 /* High-level API */ - function checkArrayTypes (...args: Uint8Array[]) { - for (var i = 0; i < args.length; i++) { + function checkArrayTypes (...args: Uint8Array[]): void { + for (let i = 0; i < args.length; i++) { if (!(args[i] instanceof Uint8Array)) throw new TypeError(`expected Uint8Array; received ${args[i].constructor?.name ?? typeof args[i]}`) } } - function parseHex (hex: string) { + function parseHex (hex: string): Uint8Array { if (hex.length % 2 === 1) hex = `0${hex}` const arr = hex.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16)) return Uint8Array.from(arr) } - function hexify (buf: Uint8Array) { - return buf.reduce((curr, next) => { + function hexify (buf: Uint8Array): string { + let str = '' + for (let i = 0; i < buf.length; i++) { if (typeof next !== 'number') throw new TypeError(`expected number to convert to hex; received ${typeof next}`) if (next < 0 || next > 255) throw new RangeError(`expected byte value 0-255; received ${next}`) - return curr + next.toString(16).padStart(2, '0') - }, '') + str += buf[i].toString(16).padStart(2, '0') + } + return str } - const sign = function (msg: Uint8Array, secretKey: Uint8Array) { + const sign = function (msg: Uint8Array, secretKey: Uint8Array): Uint8Array { checkArrayTypes(msg, secretKey) if (secretKey.length !== crypto_sign_SECRETKEYBYTES) throw new Error('bad secret key size') @@ -775,26 +777,28 @@ const n = () => { return signedMsg } - const open = function (signedMsg: Uint8Array, publicKey: Uint8Array) { + const open = function (signedMsg: Uint8Array, publicKey: Uint8Array): Uint8Array | null { checkArrayTypes(signedMsg, publicKey) if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) throw new Error('bad public key size') - var tmp = new Uint8Array(signedMsg.length) + const tmp = new Uint8Array(signedMsg.length) var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey) + if (mlen < 0) return null + var m = new Uint8Array(mlen) for (var i = 0; i < m.length; i++) m[i] = tmp[i] return m } - const detached = function (msg: Uint8Array, secretKey: Uint8Array) { + const detached = function (msg: Uint8Array, secretKey: Uint8Array): Uint8Array { var signedMsg = sign(msg, secretKey) var sig = new Uint8Array(crypto_sign_BYTES) for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i] return sig } - const verify = function (msg: Uint8Array, sig: Uint8Array, publicKey: Uint8Array) { + const verify = function (msg: Uint8Array, sig: Uint8Array, publicKey: Uint8Array): boolean { checkArrayTypes(msg, sig, publicKey) if (sig.length !== crypto_sign_BYTES) throw new Error('bad signature size') -- 2.34.1