From a809ea170b9cbe4985c41f762deab0076d1736f9 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 28 Oct 2026 14:22:17 -0700 Subject: [PATCH] Extract A multiplication table to decoding step before bulk verification. --- src/assembly/crypto_verify.ts | 9 +++++---- src/assembly/index.ts | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/assembly/crypto_verify.ts b/src/assembly/crypto_verify.ts index e693792..1f434b0 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -39,10 +39,12 @@ const t_u8x32 = new StaticArray(32) * Verify public key `pub` can be decoded. * @returns null if public key fails to decode, else return pointer to A */ -export function crypto_verify_decodepubkey (pub: StaticArray): ge_p3 | null { +export function crypto_verify_decodepubkey (pub: StaticArray): StaticArray | null { const b = ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe_0, t_fe_1, t_fe_2) clear_t() - return (b != 0) ? null : A + if (b != 0) return null + ge_p3_cache_odd_multiples(Ai, A, t_ge_p1p1) + return Ai } /** @@ -58,7 +60,7 @@ export function crypto_verify_decodepubkey (pub: StaticArray): ge_p3 | null * to `A` for the scalar multiplication step before checking `sB = R + hA`. * @returns 0 if signature fails to verify, else return 1 if signature is good */ -export function crypto_verify_donna (sig: StaticArray, M: StaticArray, A: ge_p3, pub: StaticArray): i32 { +export function crypto_verify_donna (sig: StaticArray, M: StaticArray, Ai: StaticArray, pub: StaticArray): i32 { // fail if private scalar `S` is out of range (`2²⁵³ ≤ S`) if ((sig[63] & 224) != 0) return 0 @@ -72,7 +74,6 @@ 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_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() diff --git a/src/assembly/index.ts b/src/assembly/index.ts index 69bbd75..3ab1ade 100644 --- a/src/assembly/index.ts +++ b/src/assembly/index.ts @@ -271,9 +271,9 @@ export function verify_blocks (count: i32): void { memory.copy(changetype(pub), changetype(INPUT_PUB), KEY_BYTELENGTH) INPUT_PUB.fill(0) - // Verify public key before proceeding with signature verification - const A = crypto_verify_decodepubkey(pub) - if (A != null) { + // Verify and cache public key before proceeding with signature verification + const Ai = crypto_verify_decodepubkey(pub) + if (Ai != null) { // Iterate over block signature/hash pairs for (let i = 0, ptr = changetype(INPUT_MSG); i < count; i++, ptr += SIGNEDBLOCK_BYTELENGTH) { // Copy block signature/hash from message buffer to locals @@ -281,7 +281,7 @@ export function verify_blocks (count: i32): void { memory.copy(changetype(h), ptr + SIGNATURE_BYTELENGTH, BLOCKHASH_BYTELENGTH) // Verify hash and signature, then write result to output buffer - OUTPUT_VERIFY[i] = u8(crypto_verify_donna(sig, h, A, pub)) + OUTPUT_VERIFY[i] = u8(crypto_verify_donna(sig, h, Ai, pub)) } } -- 2.52.0