From 7e7323f23b4d42393f7ab5d6490d49aa082a5d54 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 2 Oct 2026 03:28:20 -0700 Subject: [PATCH] Reorganize coordinates to bytes as the only p function with scratch buffers. --- src/assembly/ed25519/p.ts | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index c9d9f6c..8769b43 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -105,28 +105,6 @@ 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_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_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_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_0) - s[31] ^= fe_isnegative(x, fe_scratch_0) << 7 -} - /** * r = 2 * p */ @@ -244,3 +222,18 @@ export function ge_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void { fe_sub(r.Z, p.Z, r.T) fe_add(r.T, p.Z, r.T) } + +const fe_scratch_0: FieldElement = fe() +const fe_scratch_1: FieldElement = fe() +const fe_scratch_2: FieldElement = fe() +const fe_scratch_3: FieldElement = fe() +const recip: FieldElement = fe() +const x: FieldElement = fe() +const y: FieldElement = fe() +export function ge_p3_tobytes (s: StaticArray, h: ge_p3): void { + 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_0) + s[31] ^= fe_isnegative(x, fe_scratch_0) << 7 +} -- 2.52.0