]> git.codecow.com Git - nano25519.git/commitdiff
Move up scratch buffers.
authorChris Duncan <chris@codecow.com>
Fri, 9 Oct 2026 06:16:11 +0000 (23:16 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 9 Oct 2026 06:16:11 +0000 (23:16 -0700)
src/assembly/crypto_verify.ts
src/assembly/ed25519/ge.ts

index 729f6b3dfb544497ac900d14b9806b7d6f4dafa2..7a04c8d8826ada4b1dff27373fe54a73433ced50 100644 (file)
@@ -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<u8>(64)
 const sb_ah = new ge_p3()
 const check = new ge_p3()
 const check_r = new StaticArray<u8>(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<u8>(32)
+const t_fe = StaticArray.fromArray<FieldElement>([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<u8>, M: StaticArray<u8>, 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<u8>, M: StaticArray<u8>,
        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<u8>, M: StaticArray<u8>,
        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<u8>): 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<u8>): 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
 }
index d2b418e79c7a86645c8997f0e4dd1267676cd284..06f669d9c049dfe9bfb1e933eb8a27bf44e01346 100644 (file)
@@ -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<u8>(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<u8>): i32 {
+export function ge_frombytes (h: ge_p3, s: StaticArray<u8>,
+       t0_fe: FieldElement, t1_fe: FieldElement, t2_fe: FieldElement, t_u8x32: StaticArray<u8>
+): 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<u8>): 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<u8>): 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<u8>): 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<u8>): 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<u8>(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<u8>): 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<i8> = new StaticArray<i8>(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)
  *