From: Chris Duncan Date: Tue, 3 Nov 2026 05:05:58 +0000 (-0800) Subject: Consolidate several shared temporary FieldElement variables into one temporary p3. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=deb72abc453d80fa42e85603f09a2e125bcce721;p=nano25519.git Consolidate several shared temporary FieldElement variables into one temporary p3. --- diff --git a/src/assembly/crypto_derive.ts b/src/assembly/crypto_derive.ts index 942a87b..4742d98 100644 --- a/src/assembly/crypto_derive.ts +++ b/src/assembly/crypto_derive.ts @@ -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, key: StaticArray): 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() } diff --git a/src/assembly/crypto_sign.ts b/src/assembly/crypto_sign.ts index b68fc53..c8ad634 100644 --- a/src/assembly/crypto_sign.ts +++ b/src/assembly/crypto_sign.ts @@ -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, M: StaticArray, 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` diff --git a/src/assembly/crypto_verify.ts b/src/assembly/crypto_verify.ts index 4c2baf7..6c0d5f6 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -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, M: StaticArray, 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, M: StaticArray, 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) } diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index be8fa42..942b3a9 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -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, a: StaticArray, - 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, a: StaticArray, 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, a: StaticArray, 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, 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 } diff --git a/src/assembly/ed25519/t.ts b/src/assembly/ed25519/t.ts index 566f8c9..9bd3fc2 100644 --- a/src/assembly/ed25519/t.ts +++ b/src/assembly/ed25519/t.ts @@ -12,21 +12,16 @@ * 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) }