]> git.codecow.com Git - nano25519.git/commitdiff
Extract A multiplication table to decoding step before bulk verification.
authorChris Duncan <chris@codecow.com>
Wed, 28 Oct 2026 21:22:17 +0000 (14:22 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 28 Oct 2026 21:22:17 +0000 (14:22 -0700)
src/assembly/crypto_verify.ts
src/assembly/index.ts

index e69379257f43939e77dcc036751ffa3abc602c52..1f434b0439d2c65d12322b30354cecbadfc3e60f 100644 (file)
@@ -39,10 +39,12 @@ const t_u8x32 = new StaticArray<u8>(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<u8>): ge_p3 | null {
+export function crypto_verify_decodepubkey (pub: StaticArray<u8>): StaticArray<ge_cached> | 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<u8>): 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<u8>, M: StaticArray<u8>, A: ge_p3, pub: StaticArray<u8>): i32 {
+export function crypto_verify_donna (sig: StaticArray<u8>, M: StaticArray<u8>, Ai: StaticArray<ge_cached>, pub: StaticArray<u8>): 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<u8>, M: StaticArray<u8>, 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()
index 69bbd75271ea71e63035186a59c87fd915582b7b..3ab1adeaeee96999fea45ad25c91346292baf5f3 100644 (file)
@@ -271,9 +271,9 @@ export function verify_blocks (count: i32): void {
        memory.copy(changetype<usize>(pub), changetype<usize>(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<usize>(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<usize>(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))
                }
        }