return 0\r
}\r
\r
- function crypto_sign_open (m: Uint8Array, sm: Uint8Array, n: number, pk: Uint8Array) {\r
+ function crypto_sign_open (m: Uint8Array, sm: Uint8Array, n: number, pk: Uint8Array): number {\r
var t = new Uint8Array(32), h = new Uint8Array(64)\r
var p = [gf(), gf(), gf(), gf()],\r
q = [gf(), gf(), gf(), gf()]\r
return n\r
}\r
\r
- var crypto_sign_BYTES = 64,\r
- crypto_sign_PUBLICKEYBYTES = 32,\r
- crypto_sign_SECRETKEYBYTES = 64,\r
- crypto_sign_SEEDBYTES = 32\r
+ const crypto_sign_BYTES = 64\r
+ const crypto_sign_PUBLICKEYBYTES = 32\r
+ const crypto_sign_SECRETKEYBYTES = 64\r
+ const crypto_sign_SEEDBYTES = 32\r
\r
/* High-level API */\r
\r
- function checkArrayTypes (...args: Uint8Array[]) {\r
- for (var i = 0; i < args.length; i++) {\r
+ function checkArrayTypes (...args: Uint8Array[]): void {\r
+ for (let i = 0; i < args.length; i++) {\r
if (!(args[i] instanceof Uint8Array))\r
throw new TypeError(`expected Uint8Array; received ${args[i].constructor?.name ?? typeof args[i]}`)\r
}\r
}\r
\r
- function parseHex (hex: string) {\r
+ function parseHex (hex: string): Uint8Array {\r
if (hex.length % 2 === 1) hex = `0${hex}`\r
const arr = hex.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16))\r
return Uint8Array.from(arr)\r
}\r
\r
- function hexify (buf: Uint8Array) {\r
- return buf.reduce((curr, next) => {\r
+ function hexify (buf: Uint8Array): string {\r
+ let str = ''\r
+ for (let i = 0; i < buf.length; i++) {\r
if (typeof next !== 'number')\r
throw new TypeError(`expected number to convert to hex; received ${typeof next}`)\r
if (next < 0 || next > 255)\r
throw new RangeError(`expected byte value 0-255; received ${next}`)\r
- return curr + next.toString(16).padStart(2, '0')\r
- }, '')\r
+ str += buf[i].toString(16).padStart(2, '0')\r
+ }\r
+ return str\r
}\r
\r
- const sign = function (msg: Uint8Array, secretKey: Uint8Array) {\r
+ const sign = function (msg: Uint8Array, secretKey: Uint8Array): Uint8Array {\r
checkArrayTypes(msg, secretKey)\r
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
throw new Error('bad secret key size')\r
return signedMsg\r
}\r
\r
- const open = function (signedMsg: Uint8Array, publicKey: Uint8Array) {\r
+ const open = function (signedMsg: Uint8Array, publicKey: Uint8Array): Uint8Array | null {\r
checkArrayTypes(signedMsg, publicKey)\r
if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
throw new Error('bad public key size')\r
- var tmp = new Uint8Array(signedMsg.length)\r
+ const tmp = new Uint8Array(signedMsg.length)\r
var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey)\r
+\r
if (mlen < 0) return null\r
+\r
var m = new Uint8Array(mlen)\r
for (var i = 0; i < m.length; i++) m[i] = tmp[i]\r
return m\r
}\r
\r
- const detached = function (msg: Uint8Array, secretKey: Uint8Array) {\r
+ const detached = function (msg: Uint8Array, secretKey: Uint8Array): Uint8Array {\r
var signedMsg = sign(msg, secretKey)\r
var sig = new Uint8Array(crypto_sign_BYTES)\r
for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i]\r
return sig\r
}\r
\r
- const verify = function (msg: Uint8Array, sig: Uint8Array, publicKey: Uint8Array) {\r
+ const verify = function (msg: Uint8Array, sig: Uint8Array, publicKey: Uint8Array): boolean {\r
checkArrayTypes(msg, sig, publicKey)\r
if (sig.length !== crypto_sign_BYTES)\r
throw new Error('bad signature size')\r