]> git.codecow.com Git - nano25519.git/commitdiff
Move final remaining scratch buffers out of p.
authorChris Duncan <chris@codecow.com>
Tue, 6 Oct 2026 07:17:22 +0000 (00:17 -0700)
committerChris Duncan <chris@codecow.com>
Tue, 6 Oct 2026 07:17:22 +0000 (00:17 -0700)
src/assembly/crypto_verify.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p.ts

index 46741d5c5c4e4112459c9a878ed2b38d44a4b3c0..729f6b3dfb544497ac900d14b9806b7d6f4dafa2 100644 (file)
@@ -20,9 +20,15 @@ const h = new StaticArray<u8>(64)
 const sb_ah = new ge_p3()
 const check = new ge_p3()
 const check_r = new StaticArray<u8>(32)
-const fe_scratch = fe()
 const ge_cached_scratch = new ge_cached()
 const ge_p1p1_scratch = new ge_p1p1()
+const fe_scratch_0 = fe()
+const fe_scratch_1 = fe()
+const fe_scratch_2 = fe()
+const fe_scratch_3 = fe()
+const fe_scratch_4 = fe()
+const fe_scratch_5 = fe()
+const fe_scratch_6 = fe()
 
 /**
  * Verify public key `pub` can be decoded.
@@ -61,7 +67,9 @@ export function crypto_verify_donna (sig: StaticArray<u8>, M: StaticArray<u8>, A
        sc_reduce(h)
 
        ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
-       ge_p3_tobytes(check_r, sb_ah)
+       ge_p3_tobytes(check_r, sb_ah,
+               fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3, fe_scratch_4, fe_scratch_5, fe_scratch_6
+       )
 
        return equalbytes(sig, check_r, KEY_BYTELENGTH)
 }
@@ -88,7 +96,7 @@ export function crypto_verify_sodium (sig: StaticArray<u8>, M: StaticArray<u8>,
        sc_reduce(h)
 
        ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
-       ge_sub_p3(check, expected_r, sb_ah, fe_scratch, ge_cached_scratch, ge_p1p1_scratch)
+       ge_sub_p3(check, expected_r, sb_ah, fe_scratch_0, ge_cached_scratch, ge_p1p1_scratch)
 
        return ge_has_small_order(check)
 }
@@ -116,9 +124,11 @@ export function crypto_verify_strict (sig: StaticArray<u8>, M: StaticArray<u8>,
        sc_reduce(h)
 
        ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
-       ge_sub_p3(check, expected_r, sb_ah, fe_scratch, ge_cached_scratch, ge_p1p1_scratch)
+       ge_sub_p3(check, expected_r, sb_ah, fe_scratch_0, ge_cached_scratch, ge_p1p1_scratch)
 
-       ge_p3_tobytes(check_r, sb_ah)
+       ge_p3_tobytes(check_r, sb_ah,
+               fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3, fe_scratch_4, fe_scratch_5, fe_scratch_6
+       )
        return equalbytes(sig, check_r, KEY_BYTELENGTH)
 }
 
index 6c0f400be0b3688308c2e1545098dc9d2862b3a9..c3b71a1926e65a0b52737829d5c7ad7ccd32d3df 100644 (file)
@@ -287,7 +287,13 @@ const ge_scalarmult_base_tobytes_e: StaticArray<i8> = new StaticArray<i8>(64)
 const ge_scalarmult_base_tobytes_r: ge_p1p1 = new ge_p1p1()
 const ge_scalarmult_base_tobytes_s: ge_p2 = new ge_p2()
 const ge_scalarmult_base_tobytes_t: ge_precomp = new ge_precomp()
-const ge_scalarmult_base_tobytes_fe_scratch: FieldElement = fe()
+const ge_scalarmult_base_tobytes_fe_scratch_0: FieldElement = fe()
+const ge_scalarmult_base_tobytes_fe_scratch_1: FieldElement = fe()
+const ge_scalarmult_base_tobytes_fe_scratch_2: FieldElement = fe()
+const ge_scalarmult_base_tobytes_fe_scratch_3: FieldElement = fe()
+const ge_scalarmult_base_tobytes_fe_scratch_4: FieldElement = fe()
+const ge_scalarmult_base_tobytes_fe_scratch_5: FieldElement = fe()
+const ge_scalarmult_base_tobytes_fe_scratch_6: FieldElement = fe()
 /**
  * `h = a * B` (with precomputation)
  *
@@ -311,7 +317,13 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
        const r = ge_scalarmult_base_tobytes_r
        const s = ge_scalarmult_base_tobytes_s
        const t = ge_scalarmult_base_tobytes_t
-       const fe_scratch = ge_scalarmult_base_tobytes_fe_scratch
+       const fe_scratch_0 = ge_scalarmult_base_tobytes_fe_scratch_0
+       const fe_scratch_1 = ge_scalarmult_base_tobytes_fe_scratch_1
+       const fe_scratch_2 = ge_scalarmult_base_tobytes_fe_scratch_2
+       const fe_scratch_3 = ge_scalarmult_base_tobytes_fe_scratch_3
+       const fe_scratch_4 = ge_scalarmult_base_tobytes_fe_scratch_4
+       const fe_scratch_5 = ge_scalarmult_base_tobytes_fe_scratch_5
+       const fe_scratch_6 = ge_scalarmult_base_tobytes_fe_scratch_6
 
        for (let i = 0; i < 32; ++i) {
                e[(i << 1) + 0] = (a[i] >> 0) & 15
@@ -339,13 +351,13 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
                ge_p1p1_to_p3(h, r)
        }
 
-       ge_p3_dbl(r, h, fe_scratch)
+       ge_p3_dbl(r, h, fe_scratch_0)
        ge_p1p1_to_p2(s, r)
-       ge_p2_dbl(r, s, fe_scratch)
+       ge_p2_dbl(r, s, fe_scratch_0)
        ge_p1p1_to_p2(s, r)
-       ge_p2_dbl(r, s, fe_scratch)
+       ge_p2_dbl(r, s, fe_scratch_0)
        ge_p1p1_to_p2(s, r)
-       ge_p2_dbl(r, s, fe_scratch)
+       ge_p2_dbl(r, s, fe_scratch_0)
        ge_p1p1_to_p3(h, r)
 
        // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
@@ -361,7 +373,9 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
        ge_p2_0(s)
        ge_precomp_0(t)
 
-       ge_p3_tobytes(S, h)
+       ge_p3_tobytes(S, h,
+               fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3, fe_scratch_4, fe_scratch_5, fe_scratch_6
+       )
 }
 
 const ge_double_scalarmult_vartime_aslide: StaticArray<i8> = new StaticArray<i8>(256)
index 8769b43d781c7e0bc98a94b824508925765ff4b8..6e39622b6c6a301ddccc64f60541fc176aecb217 100644 (file)
@@ -223,17 +223,18 @@ export function ge_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void {
        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<u8>, 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
+export function ge_p3_tobytes (s: StaticArray<u8>, h: ge_p3,
+       reciprocal_scratch: FieldElement,
+       x_scratch: FieldElement,
+       y_scratch: FieldElement,
+       fe_scratch_0: FieldElement,
+       fe_scratch_1: FieldElement,
+       fe_scratch_2: FieldElement,
+       fe_scratch_3: FieldElement
+): void {
+       fe_invert(reciprocal_scratch, h.Z, fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3)
+       fe_mul(x_scratch, h.X, reciprocal_scratch)
+       fe_mul(y_scratch, h.Y, reciprocal_scratch)
+       fe_tobytes(s, y_scratch, fe_scratch_0)
+       s[31] ^= fe_isnegative(x_scratch, fe_scratch_0) << 7
 }