From: Chris Duncan Date: Fri, 2 Oct 2026 08:32:06 +0000 (-0700) Subject: Start combining scratch buffers to migrate them to highest level possible and reduce... X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=fb2e0dd057b6660502e0f38df3be2e473f4ce044;p=nano25519.git Start combining scratch buffers to migrate them to highest level possible and reduce instances. --- diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index adbd802..ec1adcd 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -247,6 +247,7 @@ export function fe_isnegative (f: FieldElement): u8 { } const fe_iszero_s = new StaticArray(32) +const fe_iszero_t: FieldElement = fe() /** * return 1 if f == 0 * return 0 if f != 0 @@ -256,7 +257,8 @@ const fe_iszero_s = new StaticArray(32) */ export function fe_iszero (f: FieldElement): u8 { const s = fe_iszero_s - fe_tobytes(s, f) + const t = fe_iszero_t + fe_tobytes(s, f, t) return sodium_is_zero(s, 32) } @@ -1065,7 +1067,6 @@ export function fe_sub (h: FieldElement, f: FieldElement, g: FieldElement): void v128.store_lane(h_ptr, v128.sub(v128.load(f_ptr, 32), v128.load(g_ptr, 32)), 0, 32) } -const fe_tobytes_t: FieldElement = fe() /** * Goal: Output h0+...+2²⁵⁵ h10-2²⁵⁵ q, which is between 0 and 2²⁵⁵-20. * Have h0+...+2²³⁰ h9 between 0 and 2²⁵⁵-1; @@ -1073,8 +1074,7 @@ const fe_tobytes_t: FieldElement = fe() * * Goal: Output h0+...+2²³⁰ h9. */ -export function fe_tobytes (s: StaticArray, h: FieldElement): void { - const t = fe_tobytes_t +export function fe_tobytes (s: StaticArray, h: FieldElement, t: FieldElement): void { fe_reduce(t, h) const t_ptr: usize = changetype(t) const t0 = load(t_ptr, 0) diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 726306a..4ee65f4 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -105,17 +105,19 @@ 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_t: 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 t = ge_p3_tobytes_t const recip = ge_p3_tobytes_recip const x = ge_p3_tobytes_x const y = ge_p3_tobytes_y fe_invert(recip, h.Z) fe_mul(x, h.X, recip) fe_mul(y, h.Y, recip) - fe_tobytes(s, y) + fe_tobytes(s, y, t) s[31] ^= fe_isnegative(x) << 7 }