]> git.codecow.com Git - nano25519.git/commitdiff
Move scratch buffer from fe_isnegative.
authorChris Duncan <chris@zoso.dev>
Fri, 2 Oct 2026 08:36:13 +0000 (01:36 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 2 Oct 2026 08:36:13 +0000 (01:36 -0700)
src/assembly/ed25519/fe.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p.ts

index ec1adcda13427e419da7417b8a49d0612d9853a7..5d072cd428c54149163e1656261db5afd097069c 100644 (file)
@@ -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<u8>(changetype<usize>(t), 0) & 1
+       return u8(t[0] & 1)
 }
 
 const fe_iszero_s = new StaticArray<u8>(32)
index 93acadbd2a758fc477667c5fc8cd36c3c072137e..6bb99287a83663fcb1861921b4404e0c6bf77f14 100644 (file)
@@ -85,6 +85,7 @@ function ge_cmov8_base (t: ge_precomp, bp: usize, b: i8): void {
        v128.store_lane<u64>(td, v128.bitselect(v128.neg<i32>(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<u8>): 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<u8>): 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<u8>): 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<u8>): 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
index 4ee65f455ad55e5b96b42df71c389de8238c86b1..401cd0f05b20d30c6942afd9fe2d220c4dedc039 100644 (file)
@@ -118,7 +118,7 @@ export function ge_p3_tobytes (s: StaticArray<u8>, 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()