]> git.codecow.com Git - nano25519.git/commitdiff
Move final field element scratch buffer.
authorChris Duncan <chris@zoso.dev>
Fri, 2 Oct 2026 09:43:29 +0000 (02:43 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 2 Oct 2026 09:43:29 +0000 (02:43 -0700)
src/assembly/ed25519/fe.ts
src/assembly/ed25519/p.ts

index 632ef5cc44420e2df9f53f1177a9159646815f8a..ec0660ed046997ece4f0c6e82a7fa5b71b8814f1 100644 (file)
@@ -178,14 +178,14 @@ export function fe_frombytes (h: FieldElement, s: StaticArray<u8>): void {
        store<i32>(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<i64>(h_ptr, v128.neg<i32>(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.
index 68747271e2ab86ed375549f0acd1076ee80d5dbc..fe5a952792d50275fa6896284102154e133a9517 100644 (file)
@@ -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<u8>, 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()