]> git.codecow.com Git - libnemo.git/commitdiff
Refactor vn and document behavior.
authorChris Duncan <chris@zoso.dev>
Mon, 18 Aug 2025 14:20:45 +0000 (07:20 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 18 Aug 2025 14:20:45 +0000 (07:20 -0700)
src/lib/crypto/nano-nacl.ts

index 63273a0ea4e1ccca7831fe9033564cde68e3771e..9d6d1b9ff6974306e9aa267c6d73ec5a9df0bbf1 100644 (file)
@@ -26,12 +26,17 @@ export class NanoNaCl {
        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
-       static #vn (x: Uint8Array, xi: number, y: Uint8Array, yi: number, n: number): number {\r
+       /**\r
+       * Checks first 32 bytes of two byte arrays to see if they are equivalent.\r
+       *\r
+       * @returns 0 if first 32 bytes are equal, else 1 if there is at least one bit difference\r
+       */\r
+       static #vn (x: Uint8Array, y: Uint8Array): number {\r
                let d: number = 0\r
-               for (let i = 0; i < n; i++) {\r
-                       d |= x[xi + i] ^ y[yi + i]\r
+               for (let i = 0; i < 32; i++) {\r
+                       d |= x[i] ^ y[i]\r
                }\r
-               return (1 & ((d - 1) >>> 8)) - 1\r
+               return ((1 & ((d - 1) >>> 8)) - 1) & 1\r
        }\r
 \r
        static pow2523 (out: Float64Array, i: Float64Array): void {\r
@@ -82,7 +87,7 @@ export class NanoNaCl {
                const d: Uint8Array = new Uint8Array(32)\r
                this.pack25519(c, a)\r
                this.pack25519(d, b)\r
-               return this.#vn(c, 0, d, 0, 32)\r
+               return this.#vn(c, d)\r
        }\r
 \r
        static pack25519 (out: Uint8Array, n: Float64Array): void {\r