]> git.codecow.com Git - libnemo.git/commitdiff
Define q[3] as XY constant value. Add gf[4] helper.
authorChris Duncan <chris@zoso.dev>
Sun, 3 Aug 2025 09:04:11 +0000 (02:04 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 3 Aug 2025 09:04:11 +0000 (02:04 -0700)
src/lib/nano-nacl.ts

index 52c6abdc6bad9faabf0690afbf51c97e626976ed..5cad82813212c59146816f2e617e828278e3f68b 100644 (file)
@@ -30,11 +30,17 @@ export class NanoNaCl {
                return r\r
        }\r
 \r
-       static D: Float64Array = this.gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203])\r
-       static D2: Float64Array = this.gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406])\r
-       static X: Float64Array = this.gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169])\r
-       static Y: Float64Array = this.gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666])\r
-       static I: Float64Array = this.gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83])\r
+       static gf4 (): Float64Array[] {\r
+               return [new Float64Array(16), new Float64Array(16), new Float64Array(16), new Float64Array(16)]\r
+       }\r
+\r
+       static D: Float64Array = new Float64Array([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203])\r
+       static D2: Float64Array = new Float64Array([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406])\r
+       static X: Float64Array = new Float64Array([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169])\r
+       static Y: Float64Array = new Float64Array([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666])\r
+       static I: Float64Array = new Float64Array([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83])\r
+       static XY: Float64Array = new Float64Array([0xDD90, 0xA5B7, 0x8AB3, 0x6DDE, 0x52F5, 0x7751, 0x9F80, 0x20F0, 0xE37D, 0x64AB, 0x4E8E, 0x66EA, 0x7665, 0xD78B, 0x5F0F, 0xE787])\r
+\r
 \r
        static vn (x: Uint8Array, xi: number, y: Uint8Array, yi: number, n: number): number {\r
                let d = 0\r
@@ -263,11 +269,11 @@ export class NanoNaCl {
        }\r
 \r
        static scalarbase (p: Float64Array[], s: Uint8Array): void {\r
-               const q: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
+               const q: Float64Array[] = this.gf4()\r
                this.set25519(q[0], this.X)\r
                this.set25519(q[1], this.Y)\r
                this.set25519(q[2], this.gf([1]))\r
-               this.M(q[3], this.X, this.Y)\r
+               this.set25519(q[3], this.XY)\r
                this.scalarmult(p, q, s)\r
        }\r
 \r
@@ -312,16 +318,14 @@ export class NanoNaCl {
                this.modL(r, x)\r
        }\r
 \r
-       // Note: difference from C - smlen returned, not passed as argument.\r
-       static crypto_sign (sm: Uint8Array, m: Uint8Array, n: number, sk: Uint8Array, pk: Uint8Array): number {\r
-               const p: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
+       static crypto_sign (sm: Uint8Array, m: Uint8Array, n: number, sk: Uint8Array, pk: Uint8Array): void {\r
+               const p: Float64Array[] = this.gf4()\r
 \r
                const d = new Blake2b(64).update(sk).digest()\r
                d[0] &= 248\r
                d[31] &= 127\r
                d[31] |= 64\r
 \r
-               const smlen = n + 64\r
                sm.set(m.subarray(0, n), 64)\r
                sm.set(d.subarray(32, 64), 32)\r
 \r
@@ -343,7 +347,6 @@ export class NanoNaCl {
                }\r
 \r
                this.modL(sm.subarray(32), x)\r
-               return smlen\r
        }\r
 \r
        static unpackneg (r: Float64Array[], p: Uint8Array): -1 | 0 {\r
@@ -390,8 +393,8 @@ export class NanoNaCl {
 \r
        static crypto_sign_open (m: Uint8Array, sm: Uint8Array, n: number, pk: Uint8Array): number {\r
                const t = new Uint8Array(32)\r
-               const p: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
-               const q: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
+               const p: Float64Array[] = this.gf4()\r
+               const q: Float64Array[] = this.gf4()\r
 \r
                if (n < 64) return -1\r
 \r
@@ -451,7 +454,7 @@ export class NanoNaCl {
                                throw new Error('Invalid seed size to convert to public key')\r
                        }\r
                        const pk = new Uint8Array(this.crypto_sign_PUBLICKEYBYTES)\r
-                       const p: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
+                       const p: Float64Array[] = this.gf4()\r
 \r
                        const hash = new Blake2b(64).update(s).digest()\r
                        hash[0] &= 248\r
@@ -515,9 +518,7 @@ export class NanoNaCl {
                        }\r
 \r
                        const m = new Uint8Array(mlen)\r
-                       for (let i = 0; i < m.length; i++) {\r
-                               m[i] = tmp[i]\r
-                       }\r
+                       m.set(tmp.subarray(0, mlen), 0)\r
                        return m\r
                } catch (err) {\r
                        throw new Error('Failed to open message', { cause: err })\r
@@ -575,7 +576,7 @@ export class NanoNaCl {
                        sm.set(msg, this.crypto_sign_BYTES)\r
                        return (this.crypto_sign_open(m, sm, sm.length, pub) >= 0)\r
                } catch (err) {\r
-                       throw new Error('Failed to sign and return signature', { cause: err })\r
+                       throw new Error('Failed to verify signature on message with the given public key', { cause: err })\r
                }\r
        }\r
 }\r