From 92fbbfa3d2724ee4a6f5ba24843302edf2014237 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 6 Oct 2026 00:17:22 -0700 Subject: [PATCH] Move final remaining scratch buffers out of p. --- src/assembly/crypto_verify.ts | 20 +++++++++++++++----- src/assembly/ed25519/ge.ts | 28 +++++++++++++++++++++------- src/assembly/ed25519/p.ts | 27 ++++++++++++++------------- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/assembly/crypto_verify.ts b/src/assembly/crypto_verify.ts index 46741d5..729f6b3 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -20,9 +20,15 @@ const h = new StaticArray(64) const sb_ah = new ge_p3() const check = new ge_p3() const check_r = new StaticArray(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, M: StaticArray, 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, M: StaticArray, 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, M: StaticArray, 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) } diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index 6c0f400..c3b71a1 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -287,7 +287,13 @@ const ge_scalarmult_base_tobytes_e: StaticArray = new StaticArray(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, a: StaticArray> 0) & 15 @@ -339,13 +351,13 @@ export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray, a: StaticArray = new StaticArray(256) diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 8769b43..6e39622 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -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, 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, 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 } -- 2.52.0