From 7cbbb3062861abed49e45a792b5dc7ded7cc1429 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 28 Oct 2026 14:01:48 -0700 Subject: [PATCH] Extract A multiple caching to top-level verify functions. --- src/assembly/crypto_verify.ts | 24 +++++++++++++++++++----- src/assembly/ed25519/ge.ts | 16 +--------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/assembly/crypto_verify.ts b/src/assembly/crypto_verify.ts index ac9aa7c..e693792 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -3,8 +3,8 @@ import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants' import { Blake2b } from './ed25519/blake2b' -import { ge_accept_small_order, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_is_canonical, ge_reject_small_order } from './ed25519/ge' -import { ge_cached, ge_p1p1, ge_p3, ge_p3_tobytes, ge_p3_sub_p3 } from './ed25519/p' +import { ge_accept_small_order, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_is_canonical, ge_p3_cache_odd_multiples, ge_reject_small_order } from './ed25519/ge' +import { ge_cached, ge_p1p1, ge_p3, ge_p3_sub_p3, ge_p3_tobytes } from './ed25519/p' import { sc_is_canonical, sc_reduce } from './ed25519/sc' import { clear_t, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6 } from './ed25519/t' import { equalbytes } from './ed25519/utils' @@ -14,6 +14,17 @@ const blake2b = new Blake2b() // algorithm variables const A = new ge_p3() +/** A, 3A, 5A, 7A, 9A, 11A, 13A, 15A */ +const Ai: StaticArray = StaticArray.fromArray([ + new ge_cached(), + new ge_cached(), + new ge_cached(), + new ge_cached(), + new ge_cached(), + new ge_cached(), + new ge_cached(), + new ge_cached() +]) const S = new StaticArray(32) const expected_r = new ge_p3() const h = new StaticArray(64) @@ -61,7 +72,8 @@ export function crypto_verify_donna (sig: StaticArray, M: StaticArray, A blake2b.init().update(sig, 32).update(pub, KEY_BYTELENGTH).update(M, BLOCKHASH_BYTELENGTH).digest(h) sc_reduce(h) - ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S) + ge_p3_cache_odd_multiples(Ai, A, t_ge_p1p1) + ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S) 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) clear_t() @@ -89,7 +101,8 @@ export function crypto_verify_sodium (sig: StaticArray, M: StaticArray, blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h) sc_reduce(h) - ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S) + ge_p3_cache_odd_multiples(Ai, A, t_ge_p1p1) + ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S) ge_p3_sub_p3(check, expected_r, sb_ah, t_ge_cached, t_ge_p1p1) // after cofactoring, `check` should be small order and not malformed in order @@ -123,7 +136,8 @@ export function crypto_verify_strict (sig: StaticArray, M: StaticArray, blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h) sc_reduce(h) - ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S) + ge_p3_cache_odd_multiples(Ai, A, t_ge_p1p1) + ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S) 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) clear_t() return equalbytes(sig, check_r, KEY_BYTELENGTH) diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index bfd59e3..a52c9eb 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -396,17 +396,6 @@ export function ge_p3_cache_odd_multiples (Ai: StaticArray, A: ge_p3, } } -/** A,3A,5A,7A,9A,11A,13A,15A */ -const ge_double_scalarmult_vartime_Ai: StaticArray = StaticArray.fromArray([ - new ge_cached(), - new ge_cached(), - new ge_cached(), - new ge_cached(), - new ge_cached(), - new ge_cached(), - new ge_cached(), - new ge_cached() -]) const ge_double_scalarmult_vartime_aslide: StaticArray = new StaticArray(256) const ge_double_scalarmult_vartime_bslide: StaticArray = new StaticArray(256) const ge_double_scalarmult_vartime_p: ge_p2 = new ge_p2() @@ -423,8 +412,7 @@ const ge_double_scalarmult_vartime_u: ge_p3 = new ge_p3() * * Variable-time; only used for signature verification. */ -export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray, A: ge_p3, b: StaticArray): void { - const Ai = ge_double_scalarmult_vartime_Ai +export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray, Ai: StaticArray, b: StaticArray): void { const Bi = base2 const aslide = ge_double_scalarmult_vartime_aslide const bslide = ge_double_scalarmult_vartime_bslide @@ -437,8 +425,6 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray slide_vartime(aslide, a) slide_vartime(bslide, b) - ge_p3_cache_odd_multiples(Ai, A, t) - ge_p2_0(p) let i: i32 = 255 -- 2.52.0