From: Chris Duncan Date: Fri, 2 Oct 2026 09:43:29 +0000 (-0700) Subject: Move final field element scratch buffer. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=588703317d43c627bd51922db636514595325195;p=nano25519.git Move final field element scratch buffer. --- diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 632ef5c..ec0660e 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -178,14 +178,14 @@ export function fe_frombytes (h: FieldElement, s: StaticArray): void { store(h_ptr, h9, 36) } -const t0: FieldElement = fe() -const t1: FieldElement = fe() -const t2: FieldElement = fe() -const t3: FieldElement = fe() /** * Inversion - sets `out` = 0 if `z` == 0 */ -export function fe_invert (out: FieldElement, z: FieldElement): void { +export function fe_invert (out: FieldElement, z: FieldElement, fe_scratch_0: FieldElement, fe_scratch_1: FieldElement, fe_scratch_2: FieldElement, fe_scratch_3: FieldElement): void { + const t0: FieldElement = fe_scratch_0 + const t1: FieldElement = fe_scratch_1 + const t2: FieldElement = fe_scratch_2 + const t3: FieldElement = fe_scratch_3 fe_sq(t0, z) fe_sq(t1, t0) fe_sq(t1, t1) @@ -505,9 +505,6 @@ export function fe_neg (h: FieldElement, f: FieldElement): void { v128.store_lane(h_ptr, v128.neg(v128.load(f_ptr, 32)), 0, 32) } -const fe_pow22523_t0: FieldElement = fe() -const fe_pow22523_t1: FieldElement = fe() -const fe_pow22523_t2: FieldElement = fe() /** * returns z^((p-5)/8) = z^(2²⁵²-3) * used to compute square roots since we have p=5 (mod 8); see Cohen and Frey. diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 6874727..fe5a952 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -105,20 +105,26 @@ export function ge_p3_to_cached (r: ge_cached, p: ge_p3): void { fe_mul(r.T2d, p.T, ed25519_d2) } -const ge_p3_tobytes_fe_scratch: FieldElement = fe() +const ge_p3_tobytes_fe_scratch_0: FieldElement = fe() +const ge_p3_tobytes_fe_scratch_1: FieldElement = fe() +const ge_p3_tobytes_fe_scratch_2: FieldElement = fe() +const ge_p3_tobytes_fe_scratch_3: FieldElement = fe() const ge_p3_tobytes_recip: FieldElement = fe() const ge_p3_tobytes_x: FieldElement = fe() const ge_p3_tobytes_y: FieldElement = fe() export function ge_p3_tobytes (s: StaticArray, h: ge_p3): void { - const fe_scratch = ge_p3_tobytes_fe_scratch + const fe_scratch_0 = ge_p3_tobytes_fe_scratch_0 + const fe_scratch_1 = ge_p3_tobytes_fe_scratch_1 + const fe_scratch_2 = ge_p3_tobytes_fe_scratch_2 + const fe_scratch_3 = ge_p3_tobytes_fe_scratch_3 const recip = ge_p3_tobytes_recip const x = ge_p3_tobytes_x const y = ge_p3_tobytes_y - fe_invert(recip, h.Z) + fe_invert(recip, h.Z, fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3) fe_mul(x, h.X, recip) fe_mul(y, h.Y, recip) - fe_tobytes(s, y, fe_scratch) - s[31] ^= fe_isnegative(x, fe_scratch) << 7 + fe_tobytes(s, y, fe_scratch_0) + s[31] ^= fe_isnegative(x, fe_scratch_0) << 7 } const ge_p2_dbl_t: FieldElement = fe()