From: Chris Duncan Date: Tue, 3 Dec 2024 22:51:07 +0000 (-0800) Subject: More typings. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=724e21eda3673a8c9d0670bc606e87de0111b07c;p=libnemo.git More typings. --- diff --git a/src/lib/nano-nacl.ts b/src/lib/nano-nacl.ts index 70dc671..62d2732 100644 --- a/src/lib/nano-nacl.ts +++ b/src/lib/nano-nacl.ts @@ -21,7 +21,7 @@ const n = () => { /** * Listens for messages from a calling function. */ - addEventListener('message', (message) => { + addEventListener('message', message => { const data = JSON.parse(new TextDecoder().decode(message.data ?? message)) for (const d of data) { d.publicKey = hexify(convert(d.privateKey).publicKey) @@ -30,31 +30,31 @@ const n = () => { postMessage(buf, [buf]) }) - var gf = function (init?: number[]) { + const gf = function (init?: number[]): Float64Array { const r = new Float64Array(16) if (init) for (let i = 0; i < init.length; i++) r[i] = init[i] return r } - var gf0 = gf(), - gf1 = gf([1]), - D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]), - D2 = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]), - X = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]), - Y = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]), - I = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]) + const gf0 = gf() + const gf1 = gf([1]) + const D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]) + const D2 = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]) + const X = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]) + const Y = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]) + const I = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]) - function vn (x, xi, y, yi, n) { + function vn (x, xi, y, yi, n): number { var i, d = 0 for (i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i] return (1 & ((d - 1) >>> 8)) - 1 } - function crypto_verify_32 (x, xi, y, yi) { + function crypto_verify_32 (x, xi, y, yi): number { return vn(x, xi, y, yi, 32) } - function set25519 (r, a) { + function set25519 (r, a): void { var i for (i = 0; i < 16; i++) r[i] = a[i] | 0 } @@ -69,7 +69,7 @@ const n = () => { o[0] += c - 1 + 37 * (c - 1) } - function sel25519 (p, q, b) { + function sel25519 (p, q, b): void { var t, c = ~(b - 1) for (var i = 0; i < 16; i++) { t = c & (p[i] ^ q[i]) @@ -78,7 +78,7 @@ const n = () => { } } - function pack25519 (o, n) { + function pack25519 (o, n): void { var i, j, b var m = gf(), t = gf() for (i = 0; i < 16; i++) t[i] = n[i] @@ -102,34 +102,34 @@ const n = () => { } } - function neq25519 (a, b) { + function neq25519 (a, b): number { var c = new Uint8Array(32), d = new Uint8Array(32) pack25519(c, a) pack25519(d, b) return crypto_verify_32(c, 0, d, 0) } - function par25519 (a) { + function par25519 (a): number { var d = new Uint8Array(32) pack25519(d, a) return d[0] & 1 } - function unpack25519 (o, n) { + function unpack25519 (o, n): void { var i for (i = 0; i < 16; i++) o[i] = n[2 * i] + (n[2 * i + 1] << 8) o[15] &= 0x7fff } - function A (o, a, b) { + function A (o, a, b): void { for (let i = 0; i < 16; i++) o[i] = a[i] + b[i] } - function Z (o, a, b) { + function Z (o, a, b): void { for (let i = 0; i < 16; i++) o[i] = a[i] - b[i] } - function M (o, a, b) { + function M (o, a, b): void { let v, c, t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0, t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0, @@ -500,11 +500,11 @@ const n = () => { o[15] = t15 } - function S (o, a) { + function S (o, a): void { M(o, a, a) } - function inv25519 (o, i) { + function inv25519 (o, i): void { var c = gf() var a for (a = 0; a < 16; a++) c[a] = i[a] @@ -515,7 +515,7 @@ const n = () => { for (a = 0; a < 16; a++) o[a] = c[a] } - function pow2523 (o, i) { + function pow2523 (o, i): void { var c = gf() var a for (a = 0; a < 16; a++) c[a] = i[a] @@ -527,7 +527,7 @@ const n = () => { } // Note: difference from TweetNaCl - BLAKE2b used to hash instead of SHA-512. - function crypto_hash (out: Uint8Array, m: Uint8Array, n: number) { + function crypto_hash (out: Uint8Array, m: Uint8Array, n: number): number { const input = new Uint8Array(n) for (let i = 0; i < n; ++i) input[i] = m[i] const hash = new Blake2b(64).update(m).digest() @@ -535,7 +535,7 @@ const n = () => { return 0 } - function add (p, q) { + function add (p, q): void { const a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf(), g = gf(), h = gf(), t = gf() @@ -561,13 +561,13 @@ const n = () => { M(p[3], e, h) } - function cswap (p, q, b) { + function cswap (p, q, b): void { for (let i = 0; i < 4; i++) { sel25519(p[i], q[i], b) } } - function pack (r, p) { + function pack (r, p): void { const tx = gf(), ty = gf(), zi = gf() inv25519(zi, p[2]) M(tx, p[0], zi) @@ -576,7 +576,7 @@ const n = () => { r[31] ^= par25519(tx) << 7 } - function scalarmult (p, q, s) { + function scalarmult (p, q, s): void { set25519(p[0], gf0) set25519(p[1], gf1) set25519(p[2], gf1) @@ -590,7 +590,7 @@ const n = () => { } } - function scalarbase (p, s) { + function scalarbase (p, s): void { const q = [gf(), gf(), gf(), gf()] set25519(q[0], X) set25519(q[1], Y) @@ -601,7 +601,7 @@ const n = () => { const L = new Float64Array([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10]) - function modL (r, x) { + function modL (r, x): void { let carry, i, j, k for (i = 63; i >= 32; --i) { carry = 0 @@ -626,7 +626,7 @@ const n = () => { } } - function reduce (r) { + function reduce (r): void { let x = new Float64Array(64), i for (i = 0; i < 64; i++) x[i] = r[i] for (i = 0; i < 64; i++) r[i] = 0 @@ -634,7 +634,7 @@ const n = () => { } // Note: difference from C - smlen returned, not passed as argument. - function crypto_sign (sm: Uint8Array, m: Uint8Array, n: number, sk: Uint8Array) { + function crypto_sign (sm: Uint8Array, m: Uint8Array, n: number, sk: Uint8Array): number { const d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64) const x = new Float64Array(64) const p = [gf(), gf(), gf(), gf()] @@ -669,7 +669,7 @@ const n = () => { return smlen } - function unpackneg (r, p) { + function unpackneg (r, p): -1 | 0 { const t = gf(), chk = gf(), num = gf(), den = gf(), den2 = gf(), den4 = gf(), den6 = gf() @@ -699,10 +699,10 @@ const n = () => { S(chk, r[0]) M(chk, chk, den) + if (neq25519(chk, num)) return -1 if (par25519(r[0]) === (p[31] >> 7)) Z(r[0], gf0, r[0]) - M(r[3], r[0], r[1]) return 0 }