]> git.codecow.com Git - nano25519.git/commitdiff
Consolidate several shared temporary FieldElement variables into one temporary p3.
authorChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 05:05:58 +0000 (21:05 -0800)
committerChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 05:05:58 +0000 (21:05 -0800)
src/assembly/crypto_derive.ts
src/assembly/crypto_sign.ts
src/assembly/crypto_verify.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p.ts
src/assembly/ed25519/t.ts

index 942a87be3253612d384cb9709d6fb1fc89ccb85e..4742d9839ef98db88ae57b8caba20483c4229172 100644 (file)
@@ -3,7 +3,7 @@
 
 import { Blake2b } from './ed25519/blake2b'
 import { ge_scalarmult_base_tobytes } from './ed25519/ge'
-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 { clear_t, t_p3 } from './ed25519/t'
 import { clamp } from './ed25519/utils'
 
 const PRIVATEKEY_BYTES: i32 = 32
@@ -40,7 +40,7 @@ export function crypto_derive (A: StaticArray<u8>, key: StaticArray<u8>): void {
        clamp(s)
 
        // Perform fixed-base scalar multiplication `[s]B`, output to public key `A`
-       ge_scalarmult_base_tobytes(A, s, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
+       ge_scalarmult_base_tobytes(A, s, t_p3)
        s.fill(0)
        clear_t()
 }
index b68fc536e1ecf0acf8752bd6fa6ada10e6084d46..c8ad63488efac88684bad0d0268da1c39359fa15 100644 (file)
@@ -6,7 +6,7 @@ import { crypto_derive } from './crypto_derive'
 import { Blake2b } from './ed25519/blake2b'
 import { ge_scalarmult_base_tobytes } from './ed25519/ge'
 import { sc_muladd, 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 { clear_t, t_p3 } from './ed25519/t'
 import { clamp } from './ed25519/utils'
 
 // crypto_hash function
@@ -72,7 +72,7 @@ export function crypto_sign (RS: StaticArray<u8>, M: StaticArray<u8>, mlen: i32,
        sc_reduce(r)
 
        // Perform fixed-base scalar multiplication `[r]B`, output to point `R`
-       ge_scalarmult_base_tobytes(R, r, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
+       ge_scalarmult_base_tobytes(R, r, t_p3)
        clear_t()
 
        // Compute challenge hash `blake2b(R || A || M)`, output to `k`
index 4c2baf768f96f74093b83439b912a7789255798d..6c0d5f6a2f91becbf762a43949522396c3a74354 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, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5 } from './ed25519/t'
+import { clear_t, t_fe_0, t_fe_1, t_fe_2 } from './ed25519/t'
 import { equalbytes } from './ed25519/utils'
 
 // crypto_hash function
@@ -78,7 +78,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, Ai, S)
-       p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5)
+       p3_tobytes(check_r, sb_ah, t_p3)
        clear_t()
 
        return equalbytes(sig, check_r, KEY_BYTELENGTH)
@@ -142,7 +142,7 @@ export function crypto_verify_strict (sig: StaticArray<u8>, M: StaticArray<u8>,
 
        ge_cache_odd_multiples(Ai, A, t_p3, t_fe_0)
        ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S)
-       p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5)
+       p3_tobytes(check_r, sb_ah, t_p3)
        clear_t()
        return equalbytes(sig, check_r, KEY_BYTELENGTH)
 }
index be8fa42252c548dd30e420b8a236cdc5e1a280e4..942b3a98c3ea63ad0e41fe941a1f240c4fa48e54 100644 (file)
@@ -252,9 +252,7 @@ const ge_scalarmult_base_tobytes_t: p_precomp = new p_precomp()
  * @param S 32-byte output buffer for scalar * base point
  * @param a 32-byte input buffer for scalar
  */
-export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u8>,
-       t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_fe_3: FieldElement, t_fe_4: FieldElement, t_fe_5: FieldElement, t_fe_6: FieldElement
-): void {
+export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u8>, t_p3: p3): void {
        const h = ge_scalarmult_base_tobytes_h
        const e = ge_scalarmult_base_tobytes_e
        const r = ge_scalarmult_base_tobytes_r
@@ -303,7 +301,7 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
                p1p1_to_p3(h, r)
        }
 
-       p3_tobytes(S, h, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5)
+       p3_tobytes(S, h, t_p3)
 
        // clear sensitive data
        p1p1_0(r)
index 444b0285e4ade7bedfc7d066536630c7a02d9ba5..afb4b7f59ffd368f985b7051536bedd31998ad23 100644 (file)
@@ -354,12 +354,9 @@ export function p3_sub_precomp (r: p1p1, p: p3, q: p_precomp): void {
        fe_addsub(r.T, r.Z, r.Z, r.T)
 }
 
-export function p3_tobytes (s: StaticArray<u8>, h: p3,
-       reciprocal: FieldElement, x: FieldElement, y: FieldElement,
-       t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement
-): void {
-       fe_invert(reciprocal, h.Z, t_fe_0, t_fe_1, t_fe_2)
-       fe_mul_pair(x, h.X, reciprocal, y, h.Y, reciprocal)
-       fe_tobytes(s, y, t_fe_0)
-       s[31] ^= fe_isnegative(x, t_fe_0) << 7
+export function p3_tobytes (s: StaticArray<u8>, h: p3, t_p3: p3): void {
+       fe_invert(t_p3.T, h.Z, t_p3.X, t_p3.Y, t_p3.Z)
+       fe_mul_pair(t_p3.X, h.X, t_p3.T, t_p3.Y, h.Y, t_p3.T)
+       fe_tobytes(s, t_p3.Y, t_p3.T)
+       s[31] ^= fe_isnegative(t_p3.X, t_p3.T) << 7
 }
index 566f8c949e03079bcb82efdff09747200ff9b4e3..9bd3fc2e9549c3130f9ee8fedee7d2faba5245c4 100644 (file)
  * if it presently holds a value in the current function.
  */
 import { fe } 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_3 = fe()
-export const t_fe_4 = fe()
-export const t_fe_5 = fe()
-export const t_fe_6 = 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)
-       t_fe_3.fill(0)
-       t_fe_4.fill(0)
-       t_fe_5.fill(0)
-       t_fe_6.fill(0)
+       p3_0(t_p3)
 }