]> git.codecow.com Git - nano25519.git/commitdiff
Eliminate extra temp buffers.
authorChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 06:40:19 +0000 (22:40 -0800)
committerChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 06:40:19 +0000 (22:40 -0800)
src/assembly/crypto_verify.ts
src/assembly/ed25519/t.ts

index b8c206ea093422b11035ededfafa6f490b6088c2..714c099a3d0c85a5f0528bb5c870c4e3f3a94e33 100644 (file)
@@ -6,7 +6,7 @@ import { Blake2b } from './ed25519/blake2b'
 import { ge_accept_small_order, ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate, ge_is_canonical, ge_reject_small_order } from './ed25519/ge'
 import { p3, p3_0, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
 import { sc_is_canonical, sc_reduce } from './ed25519/sc'
-import { clear_t, t_fe_0 } from './ed25519/t'
+import { clear_t, t_fe } from './ed25519/t'
 import { equalbytes } from './ed25519/utils'
 
 // crypto_hash function
@@ -40,13 +40,13 @@ const t_u8x32 = new StaticArray<u8>(32)
  * @returns null if public key fails to decode, else return pointer to Ai
  */
 export function crypto_verify_decodepubkey (pub: StaticArray<u8>): StaticArray<p_cached> | null {
-       const b = ge_frombytes_negate(A, pub, t_fe_0, t_u8x32)
+       const b = ge_frombytes_negate(A, pub, t_fe, t_u8x32)
        clear_t()
        if (b != 0) {
                p3_0(A)
                return null
        }
-       ge_cache_odd_multiples(Ai, A, t_p3, t_fe_0)
+       ge_cache_odd_multiples(Ai, A, t_p3, t_fe)
        return Ai
 }
 
@@ -105,13 +105,13 @@ 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_cache_odd_multiples(Ai, A, t_p3, t_fe_0)
+       ge_cache_odd_multiples(Ai, A, t_p3, t_fe)
        ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S)
-       p3_sub_p3(check, expected_r, sb_ah, t_fe_0)
+       p3_sub_p3(check, expected_r, sb_ah, t_fe)
 
        // after cofactoring, `check` should be small order and not malformed in order
        // to pass, unlike `verify_pubkey()` and `verify_signature()` below
-       const b = ge_accept_small_order(check, t_fe_0, t_u8x32)
+       const b = ge_accept_small_order(check, t_fe, t_u8x32)
        clear_t()
        if (b != 0) return 0
 
@@ -140,7 +140,7 @@ 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_cache_odd_multiples(Ai, A, t_p3, t_fe_0)
+       ge_cache_odd_multiples(Ai, A, t_p3, t_fe)
        ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S)
        p3_tobytes(check_r, sb_ah, t_p3)
        clear_t()
@@ -162,14 +162,14 @@ export function verify_pubkey (pub: StaticArray<u8>): boolean {
        }
 
        // fail if public key cannot be decoded
-       if (ge_frombytes_negate(A, pub, t_fe_0, t_u8x32) != 0) {
+       if (ge_frombytes_negate(A, pub, t_fe, t_u8x32) != 0) {
                p3_0(A)
                clear_t()
                return false
        }
 
        // fail if public key `k` is small order
-       if (ge_reject_small_order(A, t_fe_0, t_u8x32) != 0) {
+       if (ge_reject_small_order(A, t_fe, t_u8x32) != 0) {
                p3_0(A)
                clear_t()
                return false
@@ -200,13 +200,13 @@ export function verify_signature (sig: StaticArray<u8>): boolean {
        }
 
        // fail if nonce `R` does not decode to a valid curve point
-       if (ge_frombytes(expected_r, sig, t_fe_0, t_u8x32) != 0) {
+       if (ge_frombytes(expected_r, sig, t_fe, t_u8x32) != 0) {
                clear_t()
                return false
        }
 
        // fail if nonce `R` is small order
-       if (ge_reject_small_order(expected_r, t_fe_0, t_u8x32) != 0) {
+       if (ge_reject_small_order(expected_r, t_fe, t_u8x32) != 0) {
                clear_t()
                return false
        }
index 9bd3fc2e9549c3130f9ee8fedee7d2faba5245c4..a14821c81060941b999301bfc761de8122a00226 100644 (file)
  * buffers at the same time. However, never pass a buffer to a called function
  * if it presently holds a value in the current function.
  */
-import { fe } from './fe'
+import { fe, fe_0 } from './fe'
 import { p3, p3_0 } from './p'
 
-export const t_fe_0 = fe()
-export const t_fe_1 = fe()
-export const t_fe_2 = fe()
+export const t_fe = fe()
 export const t_p3 = new p3()
 
 export function clear_t (): void {
-       t_fe_0.fill(0)
-       t_fe_1.fill(0)
-       t_fe_2.fill(0)
+       fe_0(t_fe)
        p3_0(t_p3)
 }