From: Chris Duncan Date: Fri, 2 Oct 2026 08:36:13 +0000 (-0700) Subject: Move scratch buffer from fe_isnegative. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=0002c84177e10cc7fefc1a50ade761380bb36b6f;p=nano25519.git Move scratch buffer from fe_isnegative. --- diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index ec1adcd..5d072cd 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -232,7 +232,6 @@ export function fe_invert (out: FieldElement, z: FieldElement): void { fe_mul(out, t1, t0) } -const fe_isnegative_t = fe() /** * return 1 if f is in {1,3,5,...,q-2} * return 0 if f is in {0,2,4,...,q-1} @@ -240,10 +239,9 @@ const fe_isnegative_t = fe() * Preconditions: * |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc. */ -export function fe_isnegative (f: FieldElement): u8 { - const t = fe_isnegative_t +export function fe_isnegative (f: FieldElement, t: FieldElement): u8 { fe_reduce(t, f) - return load(changetype(t), 0) & 1 + return u8(t[0] & 1) } const fe_iszero_s = new StaticArray(32) diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index 93acadb..6bb9928 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -85,6 +85,7 @@ function ge_cmov8_base (t: ge_precomp, bp: usize, b: i8): void { v128.store_lane(td, v128.bitselect(v128.neg(d2), d2, n), 0, 32) } +const t: FieldElement = fe() const u: FieldElement = fe() const v: FieldElement = fe() const v3: FieldElement = fe() @@ -140,7 +141,7 @@ export function ge_frombytes_negate_vartime (h: ge_p3, s: StaticArray): i32 fe_mul(h.X, h.X, fe_sqrtm1) } - if (fe_isnegative(h.X) == (s[31] >> 7)) { + if (fe_isnegative(h.X, t) == (s[31] >> 7)) { fe_neg(h.X, h.X) } fe_mul(h.T, h.X, h.Y) @@ -148,6 +149,7 @@ export function ge_frombytes_negate_vartime (h: ge_p3, s: StaticArray): i32 return 0 } +const ge_frombytes_t: FieldElement = fe() const ge_frombytes_u: FieldElement = fe() const ge_frombytes_v: FieldElement = fe() const ge_frombytes_vxx: FieldElement = fe() @@ -161,6 +163,7 @@ const x_sqrtm1: FieldElement = fe() * @returns {i32} 0 if `s` decodes to a valid point, else -1 */ export function ge_frombytes (h: ge_p3, s: StaticArray): i32 { + const t = ge_frombytes_t const u = ge_frombytes_u const v = ge_frombytes_v const vxx = ge_frombytes_vxx @@ -202,7 +205,7 @@ export function ge_frombytes (h: ge_p3, s: StaticArray): i32 { fe_cmov(h.X, x_sqrtm1, 1 - has_m_root) fe_neg(negx, h.X) - fe_cmov(h.X, negx, fe_isnegative(h.X) ^ (s[31] >> 7)) + fe_cmov(h.X, negx, fe_isnegative(h.X, t) ^ (s[31] >> 7)) fe_mul(h.T, h.X, h.Y) return i32(has_m_root | has_p_root) - 1 diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 4ee65f4..401cd0f 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -118,7 +118,7 @@ export function ge_p3_tobytes (s: StaticArray, h: ge_p3): void { fe_mul(x, h.X, recip) fe_mul(y, h.Y, recip) fe_tobytes(s, y, t) - s[31] ^= fe_isnegative(x) << 7 + s[31] ^= fe_isnegative(x, t) << 7 } const ge_p2_dbl_t: FieldElement = fe()