From c3a8e00e0c15f9270e0989c31cdad7dfcbe0ef19 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 3 Dec 2024 15:58:27 -0800 Subject: [PATCH] Lots more typings for NanoNaCl. --- src/lib/nano-nacl.ts | 81 ++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/src/lib/nano-nacl.ts b/src/lib/nano-nacl.ts index 5fc5202..e9e9638 100644 --- a/src/lib/nano-nacl.ts +++ b/src/lib/nano-nacl.ts @@ -37,32 +37,31 @@ const n = () => { return r } - 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): number { - var i, d = 0 - for (i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i] + const gf0: Float64Array = gf() + const gf1: Float64Array = gf([1]) + const D: Float64Array = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]) + const D2: Float64Array = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]) + const X: Float64Array = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]) + const Y: Float64Array = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]) + const I: Float64Array = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]) + + function vn (x: Uint8Array, xi: number, y: Uint8Array, yi: number, n: number): number { + let d = 0 + for (let 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): number { + function crypto_verify_32 (x: Uint8Array, xi: number, y: Uint8Array, yi: number): number { return vn(x, xi, y, yi, 32) } - function set25519 (r, a): void { - var i - for (i = 0; i < 16; i++) r[i] = a[i] | 0 + function set25519 (r: Float64Array, a: Float64Array): void { + for (let i = 0; i < 16; i++) r[i] = a[i] | 0 } - function car25519 (o) { - var i, v, c = 1 - for (i = 0; i < 16; i++) { + function car25519 (o): void { + let v, c = 1 + for (let i = 0; i < 16; i++) { v = o[i] + c + 65535 c = Math.floor(v / 65536) o[i] = v - c * 65536 @@ -126,11 +125,11 @@ const n = () => { for (let i = 0; i < 16; i++) o[i] = a[i] + b[i] } - function Z (o, a, b): void { + function Z (o, a: Float64Array, b): void { for (let i = 0; i < 16; i++) o[i] = a[i] - b[i] } - function M (o, a, b): void { + function M (o, a: Float64Array, b: Float64Array): 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, @@ -518,13 +517,12 @@ const n = () => { function pow2523 (o, i): void { var c = gf() - var a - for (a = 0; a < 16; a++) c[a] = i[a] - for (a = 250; a >= 0; a--) { + for (let a = 0; a < 16; a++) c[a] = i[a] + for (let a = 250; a >= 0; a--) { S(c, c) if (a !== 1) M(c, c, i) } - for (a = 0; a < 16; a++) o[a] = c[a] + for (let a = 0; a < 16; a++) o[a] = c[a] } // Note: difference from TweetNaCl - BLAKE2b used to hash instead of SHA-512. @@ -577,7 +575,7 @@ const n = () => { r[31] ^= par25519(tx) << 7 } - function scalarmult (p, q, s): void { + function scalarmult (p: Float64Array[], q: Float64Array[], s: Uint8Array): void { set25519(p[0], gf0) set25519(p[1], gf1) set25519(p[2], gf1) @@ -591,8 +589,8 @@ const n = () => { } } - function scalarbase (p, s): void { - const q = [gf(), gf(), gf(), gf()] + function scalarbase (p: Float64Array[], s: Uint8Array): void { + const q: Float64Array[] = [gf(), gf(), gf(), gf()] set25519(q[0], X) set25519(q[1], Y) set25519(q[2], gf1) @@ -602,7 +600,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): void { + function modL (r: Uint8Array, x: Float64Array): void { let carry, i, j, k for (i = 63; i >= 32; --i) { carry = 0 @@ -627,18 +625,20 @@ const n = () => { } } - 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 + function reduce (r: Uint8Array): void { + let x = new Float64Array(64) + for (let i = 0; i < 64; i++) x[i] = r[i] + for (let i = 0; i < 64; i++) r[i] = 0 modL(r, x) } // Note: difference from C - smlen returned, not passed as argument. 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 d = new Uint8Array(64) + const h = new Uint8Array(64) + const r = new Uint8Array(64) const x = new Float64Array(64) - const p = [gf(), gf(), gf(), gf()] + const p: Float64Array[] = [gf(), gf(), gf(), gf()] crypto_hash(d, sk, 32) d[0] &= 248 @@ -670,7 +670,7 @@ const n = () => { return smlen } - function unpackneg (r, p): -1 | 0 { + function unpackneg (r: Float64Array[], p: Uint8Array): -1 | 0 { const t = gf(), chk = gf(), num = gf(), den = gf(), den2 = gf(), den4 = gf(), den6 = gf() @@ -709,9 +709,10 @@ const n = () => { } 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()] + const t = new Uint8Array(32) + const h = new Uint8Array(64) + const p: Float64Array[] = [gf(), gf(), gf(), gf()] + const q: Float64Array[] = [gf(), gf(), gf(), gf()] if (n < 64) return -1 @@ -754,7 +755,7 @@ const n = () => { 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) + return Uint8Array.from(arr ?? []) } function hexify (buf: Uint8Array): string { @@ -820,7 +821,7 @@ const n = () => { throw new Error('bad seed size') const pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES) - const p = [gf(), gf(), gf(), gf()] + const p: Float64Array[] = [gf(), gf(), gf(), gf()] const hash = new Blake2b(64).update(seed).digest() hash[0] &= 248 -- 2.34.1