]> git.codecow.com Git - nano25519.git/commitdiff
Extract A multiple caching to top-level verify functions.
authorChris Duncan <chris@codecow.com>
Wed, 28 Oct 2026 21:01:48 +0000 (14:01 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 28 Oct 2026 21:01:48 +0000 (14:01 -0700)
src/assembly/crypto_verify.ts
src/assembly/ed25519/ge.ts

index ac9aa7c26823933a790146bb84fd094d691a04ec..e69379257f43939e77dcc036751ffa3abc602c52 100644 (file)
@@ -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<ge_cached> = StaticArray.fromArray<ge_cached>([
+       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<u8>(32)
 const expected_r = new ge_p3()
 const h = new StaticArray<u8>(64)
@@ -61,7 +72,8 @@ 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_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<u8>, M: StaticArray<u8>,
        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<u8>, M: StaticArray<u8>,
        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)
index bfd59e3be92fd2bb7a4af9fb1b4cf1db6cdacd95..a52c9eba36e44319cb1e21e63e400d4029dee5dd 100644 (file)
@@ -396,17 +396,6 @@ export function ge_p3_cache_odd_multiples (Ai: StaticArray<ge_cached>, A: ge_p3,
        }
 }
 
-/** A,3A,5A,7A,9A,11A,13A,15A */
-const ge_double_scalarmult_vartime_Ai: StaticArray<ge_cached> = StaticArray.fromArray<ge_cached>([
-       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<i8> = new StaticArray<i8>(256)
 const ge_double_scalarmult_vartime_bslide: StaticArray<i8> = new StaticArray<i8>(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<u8>, A: ge_p3, b: StaticArray<u8>): void {
-       const Ai = ge_double_scalarmult_vartime_Ai
+export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>, Ai: StaticArray<ge_cached>, b: StaticArray<u8>): 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<u8>
        slide_vartime(aslide, a)
        slide_vartime(bslide, b)
 
-       ge_p3_cache_odd_multiples(Ai, A, t)
-
        ge_p2_0(p)
 
        let i: i32 = 255