From 68b96eb80667d6dc22da74fd29c5962dd16a4a2b Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 17 Aug 2025 02:55:00 -0700 Subject: [PATCH] Unroll loop to eliminate branching. --- src/lib/crypto/nano-nacl.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/lib/crypto/nano-nacl.ts b/src/lib/crypto/nano-nacl.ts index c1e401d..104e5af 100644 --- a/src/lib/crypto/nano-nacl.ts +++ b/src/lib/crypto/nano-nacl.ts @@ -62,12 +62,18 @@ export class NanoNaCl { static inv25519 (out: Float64Array, i: Float64Array): void { const c: Float64Array = new Float64Array(16) c.set(i.subarray(0, 16), 0) - for (let a = 253; a >= 0; a--) { + for (let a = 0; a < 249; a++) { this.Square(c, c) - if (a !== 2 && a !== 4) { - this.Multiply(c, c, i) - } + this.Multiply(c, c, i) } + this.Square(c, c) + this.Square(c, c) + this.Multiply(c, c, i) + this.Square(c, c) + this.Square(c, c) + this.Multiply(c, c, i) + this.Square(c, c) + this.Multiply(c, c, i) out.set(c, 0) } -- 2.47.3