From 83255b5524e7e6886164b4de291267cea0002b13 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 8 Oct 2026 23:16:11 -0700 Subject: [PATCH] Move up scratch buffers. --- src/assembly/crypto_verify.ts | 35 +++++++++++++---------------------- src/assembly/ed25519/ge.ts | 35 ++++++++--------------------------- 2 files changed, 21 insertions(+), 49 deletions(-) diff --git a/src/assembly/crypto_verify.ts b/src/assembly/crypto_verify.ts index 729f6b3..7a04c8d 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -3,7 +3,7 @@ import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants' import { Blake2b } from './ed25519/blake2b' -import { fe } from './ed25519/fe' +import { FieldElement, fe } from './ed25519/fe' import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_has_small_order, ge_is_canonical } from './ed25519/ge' import { ge_cached, ge_p1p1, ge_p3, ge_p3_tobytes, ge_sub_p3 } from './ed25519/p' import { sc_is_canonical, sc_reduce } from './ed25519/sc' @@ -20,15 +20,10 @@ const h = new StaticArray(64) const sb_ah = new ge_p3() const check = new ge_p3() const check_r = new StaticArray(32) -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() +const t_ge_cached = new ge_cached() +const t_ge_p1p1 = new ge_p1p1() +const t_u8x32 = new StaticArray(32) +const t_fe = StaticArray.fromArray([fe(), fe(), fe(), fe(), fe(), fe(), fe()]) /** * Verify public key `pub` can be decoded. @@ -67,9 +62,7 @@ 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, - fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3, fe_scratch_4, fe_scratch_5, fe_scratch_6 - ) + ge_p3_tobytes(check_r, sb_ah, t_fe[0], t_fe[1], t_fe[2], t_fe[3], t_fe[4], t_fe[5], t_fe[6]) return equalbytes(sig, check_r, KEY_BYTELENGTH) } @@ -96,9 +89,9 @@ 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_0, ge_cached_scratch, ge_p1p1_scratch) + ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1) - return ge_has_small_order(check) + return ge_has_small_order(check, t_fe[0], t_u8x32) } /** @@ -124,11 +117,9 @@ 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_0, ge_cached_scratch, ge_p1p1_scratch) + ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1) - 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 - ) + ge_p3_tobytes(check_r, sb_ah, t_fe[0], t_fe[1], t_fe[2], t_fe[3], t_fe[4], t_fe[5], t_fe[6]) return equalbytes(sig, check_r, KEY_BYTELENGTH) } @@ -146,7 +137,7 @@ function verify_pubkey (pub: StaticArray): boolean { if (ge_frombytes_negate_vartime(A, pub) != 0) return false // fail if public key `k` is small order - if (ge_has_small_order(A) != 0) return false + if (ge_has_small_order(A, t_fe[0], t_u8x32) != 0) return false return true } @@ -165,10 +156,10 @@ function verify_signature (sig: StaticArray): boolean { if (ge_is_canonical(sig) != 0) return false // fail if nonce `R` does not decode to a valid curve point - if (ge_frombytes(expected_r, sig) != 0) return false + if (ge_frombytes(expected_r, sig, t_fe[0], t_fe[1], t_fe[2], t_u8x32) != 0) return false // fail if nonce `R` is small order - if (ge_has_small_order(expected_r) != 0) return false + if (ge_has_small_order(expected_r, t_fe[0], t_u8x32) != 0) return false return true } diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index d2b418e..06f669d 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -171,16 +171,14 @@ const ge_frombytes_m_root_check: FieldElement = fe() const ge_frombytes_p_root_check: FieldElement = fe() const ge_frombytes_negx: FieldElement = fe() const ge_frombytes_x_sqrtm1: FieldElement = fe() -const ge_frombytes_u8x32_scratch = new StaticArray(32) -const ge_frombytes_fe_scratch_0: FieldElement = fe() -const ge_frombytes_fe_scratch_1: FieldElement = fe() -const ge_frombytes_fe_scratch_2: FieldElement = fe() /** * Constant-time point conversion from byte representation. * * @returns {i32} 0 if `s` decodes to a valid point, else -1 */ -export function ge_frombytes (h: ge_p3, s: StaticArray): i32 { +export function ge_frombytes (h: ge_p3, s: StaticArray, + t0_fe: FieldElement, t1_fe: FieldElement, t2_fe: FieldElement, t_u8x32: StaticArray +): i32 { const u = ge_frombytes_u const v = ge_frombytes_v const vxx = ge_frombytes_vxx @@ -189,11 +187,6 @@ export function ge_frombytes (h: ge_p3, s: StaticArray): i32 { const negx = ge_frombytes_negx const x_sqrtm1 = ge_frombytes_x_sqrtm1 - const u8x32_scratch = ge_frombytes_u8x32_scratch - const fe_scratch_0 = ge_frombytes_fe_scratch_0 - const fe_scratch_1 = ge_frombytes_fe_scratch_1 - const fe_scratch_2 = ge_frombytes_fe_scratch_2 - let has_m_root: u8 let has_p_root: u8 @@ -209,7 +202,7 @@ export function ge_frombytes (h: ge_p3, s: StaticArray): i32 { /* x = u((uv)^((q-5)/8)) */ fe_mul(h.X, u, v) - fe_pow22523(h.X, h.X, fe_scratch_0, fe_scratch_1, fe_scratch_2) + fe_pow22523(h.X, h.X, t0_fe, t1_fe, t2_fe) fe_mul(h.X, u, h.X) // vxx = vx² @@ -218,18 +211,18 @@ export function ge_frombytes (h: ge_p3, s: StaticArray): i32 { // m = vx²-u fe_sub(m_root_check, vxx, u) - has_m_root = fe_iszero(m_root_check, u8x32_scratch, fe_scratch_0) + has_m_root = fe_iszero(m_root_check, t_u8x32, t0_fe) // p = vx²+u fe_add(p_root_check, vxx, u) - has_p_root = fe_iszero(p_root_check, u8x32_scratch, fe_scratch_0) + has_p_root = fe_iszero(p_root_check, t_u8x32, t0_fe) // x√-1 fe_mul(x_sqrtm1, h.X, fe_sqrtm1) fe_cmov(h.X, x_sqrtm1, 1 - has_m_root) fe_neg(negx, h.X) - fe_cmov(h.X, negx, fe_isnegative(h.X, fe_scratch_0) ^ (s[31] >> 7)) + fe_cmov(h.X, negx, fe_isnegative(h.X, t0_fe) ^ (s[31] >> 7)) fe_mul(h.T, h.X, h.Y) return i32(has_m_root | has_p_root) - 1 @@ -237,19 +230,14 @@ export function ge_frombytes (h: ge_p3, s: StaticArray): i32 { const ge_has_small_order_y_sqrtm1: FieldElement = fe() const ge_has_small_order_c: FieldElement = fe() -const ge_has_small_order_t_u8x32 = new StaticArray(32) -const ge_has_small_order_t_fe: FieldElement = fe() /** * @param {ge_p3} p * @returns 1 if `p` has small order, else 0 */ -export function ge_has_small_order (p: ge_p3): i32 { +export function ge_has_small_order (p: ge_p3, t_fe: FieldElement, t_u8x32: StaticArray): i32 { const y_sqrtm1 = ge_has_small_order_y_sqrtm1 const c = ge_has_small_order_c - const t_u8x32 = ge_has_small_order_t_u8x32 - const t_fe = ge_has_small_order_t_fe - let ret: i32 = 0 ret |= fe_iszero(p.X, t_u8x32, t_fe) @@ -287,13 +275,6 @@ 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_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) * -- 2.52.0