From 295dec2aafbe7c84abdcd6ca846b1dd0471dfd33 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 3 Aug 2025 14:57:20 -0700 Subject: [PATCH] Replace set method with builtin typed array methods. --- src/lib/nano-nacl.ts | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/lib/nano-nacl.ts b/src/lib/nano-nacl.ts index ef9dc4d..e16ea3a 100644 --- a/src/lib/nano-nacl.ts +++ b/src/lib/nano-nacl.ts @@ -54,12 +54,6 @@ export class NanoNaCl { return this.vn(x, xi, y, yi, 32) } - static set25519 (r: Float64Array, a: Float64Array): void { - for (let i = 0; i < 16; i++) { - r[i] = a[i] | 0 - } - } - static car25519 (o: Float64Array): void { let v, c = 1 for (let i = 0; i < 16; i++) { @@ -295,10 +289,10 @@ export class NanoNaCl { } static scalarmult (p: Float64Array[], q: Float64Array[], s: Uint8Array): void { - this.set25519(p[0], this.gf()) - this.set25519(p[1], this.gf([1])) - this.set25519(p[2], this.gf([1])) - this.set25519(p[3], this.gf()) + p[0].fill(0) + p[1].fill(0).set([1], 0) + p[2].fill(0).set([1], 0) + p[3].fill(0) for (let i = 255; i >= 0; --i) { const b = (s[(i / 8) | 0] >> (i & 7)) & 1 this.cswap(p, q, b) @@ -310,10 +304,10 @@ export class NanoNaCl { static scalarbase (p: Float64Array[], s: Uint8Array): void { const q: Float64Array[] = this.gf4() - this.set25519(q[0], this.X) - this.set25519(q[1], this.Y) - this.set25519(q[2], this.gf([1])) - this.set25519(q[3], this.XY) + q[0].set(this.X, 0) + q[1].set(this.Y, 0) + q[2].set([1], 0) + q[3].set(this.XY, 0) this.scalarmult(p, q, s) } @@ -398,7 +392,7 @@ export class NanoNaCl { const den4: Float64Array = this.gf() const den6: Float64Array = this.gf() - this.set25519(r[2], this.gf([1])) + r[2].fill(0).set([1], 0) this.unpack25519(r[1], p) this.Square(num, r[1]) this.Multiply(den, num, this.D) @@ -426,7 +420,9 @@ export class NanoNaCl { if (this.neq25519(chk, num)) return -1 - if (this.par25519(r[0]) === (p[31] >> 7)) this.Subtract(r[0], this.gf(), r[0]) + if (this.par25519(r[0]) === (p[31] >> 7)) { + this.Subtract(r[0], this.gf(), r[0]) + } this.Multiply(r[3], r[0], r[1]) return 0 } -- 2.47.3