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