From e60d42f200df87c78ea8d85daa10bede5c480508 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 8 Oct 2026 23:23:42 -0700 Subject: [PATCH] Move up scratch buffers and develop initial naming conventions. --- src/assembly/crypto_verify.ts | 10 +-- src/assembly/ed25519/fe.ts | 130 +++++++++++++++++----------------- src/assembly/ed25519/ge.ts | 81 ++++++++++----------- src/assembly/ed25519/p.ts | 8 +-- 4 files changed, 110 insertions(+), 119 deletions(-) diff --git a/src/assembly/crypto_verify.ts b/src/assembly/crypto_verify.ts index 7a04c8d..2977e74 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -31,7 +31,7 @@ const t_fe = StaticArray.fromArray([fe(), fe(), fe(), fe(), fe(), */ export function crypto_verify_decodepubkey (pub: StaticArray): ge_p3 | null { // fail if public key cannot be decoded - return (ge_frombytes_negate_vartime(A, pub) != 0) ? null : A + return (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe[0], t_fe[1], t_fe[2]) != 0) ? null : A } /** @@ -61,7 +61,7 @@ export function crypto_verify_donna (sig: StaticArray, M: StaticArray, 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_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe[0]) 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) @@ -88,7 +88,7 @@ export function crypto_verify_sodium (sig: StaticArray, M: StaticArray, 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_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe[0]) ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1) return ge_has_small_order(check, t_fe[0], t_u8x32) @@ -116,7 +116,7 @@ export function crypto_verify_strict (sig: StaticArray, M: StaticArray, 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_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe[0]) ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1) 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]) @@ -134,7 +134,7 @@ function verify_pubkey (pub: StaticArray): boolean { if (ge_is_canonical(pub) != 0) return false // fail if public key cannot be decoded - if (ge_frombytes_negate_vartime(A, pub) != 0) return false + if (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe[0], t_fe[1], t_fe[2]) != 0) return false // fail if public key `k` is small order if (ge_has_small_order(A, t_fe[0], t_u8x32) != 0) return false diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 0c00f9c..b8913f3 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -181,51 +181,51 @@ export function fe_frombytes (h: FieldElement, s: StaticArray): void { /** * Inversion - sets `out` = 0 if `z` == 0 */ -export function fe_invert (out: FieldElement, z: FieldElement, t0: FieldElement, t1: FieldElement, t2: FieldElement, t3: FieldElement): void { - fe_sq(t0, z) - fe_sq(t1, t0) - fe_sq(t1, t1) - fe_mul(t1, z, t1) - fe_mul(t0, t0, t1) - fe_sq(t2, t0) - fe_mul(t1, t1, t2) - fe_sq(t2, t1) +export function fe_invert (out: FieldElement, z: FieldElement, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_fe_3: FieldElement): void { + fe_sq(t_fe_0, z) + fe_sq(t_fe_1, t_fe_0) + fe_sq(t_fe_1, t_fe_1) + fe_mul(t_fe_1, z, t_fe_1) + fe_mul(t_fe_0, t_fe_0, t_fe_1) + fe_sq(t_fe_2, t_fe_0) + fe_mul(t_fe_1, t_fe_1, t_fe_2) + fe_sq(t_fe_2, t_fe_1) for (let i = 1; i < 5; ++i) { - fe_sq(t2, t2) + fe_sq(t_fe_2, t_fe_2) } - fe_mul(t1, t2, t1) - fe_sq(t2, t1) + fe_mul(t_fe_1, t_fe_2, t_fe_1) + fe_sq(t_fe_2, t_fe_1) for (let i = 1; i < 10; ++i) { - fe_sq(t2, t2) + fe_sq(t_fe_2, t_fe_2) } - fe_mul(t2, t2, t1) - fe_sq(t3, t2) + fe_mul(t_fe_2, t_fe_2, t_fe_1) + fe_sq(t_fe_3, t_fe_2) for (let i = 1; i < 20; ++i) { - fe_sq(t3, t3) + fe_sq(t_fe_3, t_fe_3) } - fe_mul(t2, t3, t2) + fe_mul(t_fe_2, t_fe_3, t_fe_2) for (let i = 1; i < 11; ++i) { - fe_sq(t2, t2) + fe_sq(t_fe_2, t_fe_2) } - fe_mul(t1, t2, t1) - fe_sq(t2, t1) + fe_mul(t_fe_1, t_fe_2, t_fe_1) + fe_sq(t_fe_2, t_fe_1) for (let i = 1; i < 50; ++i) { - fe_sq(t2, t2) + fe_sq(t_fe_2, t_fe_2) } - fe_mul(t2, t2, t1) - fe_sq(t3, t2) + fe_mul(t_fe_2, t_fe_2, t_fe_1) + fe_sq(t_fe_3, t_fe_2) for (let i = 1; i < 100; ++i) { - fe_sq(t3, t3) + fe_sq(t_fe_3, t_fe_3) } - fe_mul(t2, t3, t2) + fe_mul(t_fe_2, t_fe_3, t_fe_2) for (let i = 1; i < 51; ++i) { - fe_sq(t2, t2) + fe_sq(t_fe_2, t_fe_2) } - fe_mul(t1, t2, t1) + fe_mul(t_fe_1, t_fe_2, t_fe_1) for (let i = 1; i < 6; ++i) { - fe_sq(t1, t1) + fe_sq(t_fe_1, t_fe_1) } - fe_mul(out, t1, t0) + fe_mul(out, t_fe_1, t_fe_0) } /** @@ -247,11 +247,11 @@ export function fe_isnegative (f: FieldElement, t: FieldElement): u8 { * Preconditions: * |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc. */ -export function fe_iszero (f: FieldElement, u8x32_scratch: StaticArray, t: FieldElement): u8 { - fe_tobytes(u8x32_scratch, f, t) +export function fe_iszero (f: FieldElement, t_u8x32: StaticArray, t: FieldElement): u8 { + fe_tobytes(t_u8x32, f, t) let d: i32 = 0 for (let i: i32 = 0; i < 32; i++) { - d |= u8x32_scratch[i] + d |= t_u8x32[i] } d &= 255 return u8(((d - 1) >> 8) & 1) @@ -529,50 +529,50 @@ export function fe_neg (h: FieldElement, f: FieldElement): void { * returns z^((p-5)/8) = z^(2²⁵²-3) * used to compute square roots since we have p=5 (mod 8); see Cohen and Frey. */ -export function fe_pow22523 (out: FieldElement, z: FieldElement, t0: FieldElement, t1: FieldElement, t2: FieldElement): void { - fe_sq(t0, z) - fe_sq(t1, t0) - fe_sq(t1, t1) - fe_mul(t1, z, t1) - fe_mul(t0, t0, t1) - fe_sq(t0, t0) - fe_mul(t0, t1, t0) - fe_sq(t1, t0) +export function fe_pow22523 (out: FieldElement, z: FieldElement, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement): void { + fe_sq(t_fe_0, z) + fe_sq(t_fe_1, t_fe_0) + fe_sq(t_fe_1, t_fe_1) + fe_mul(t_fe_1, z, t_fe_1) + fe_mul(t_fe_0, t_fe_0, t_fe_1) + fe_sq(t_fe_0, t_fe_0) + fe_mul(t_fe_0, t_fe_1, t_fe_0) + fe_sq(t_fe_1, t_fe_0) for (let i = 1; i < 5; ++i) { - fe_sq(t1, t1) + fe_sq(t_fe_1, t_fe_1) } - fe_mul(t0, t1, t0) - fe_sq(t1, t0) + fe_mul(t_fe_0, t_fe_1, t_fe_0) + fe_sq(t_fe_1, t_fe_0) for (let i = 1; i < 10; ++i) { - fe_sq(t1, t1) + fe_sq(t_fe_1, t_fe_1) } - fe_mul(t1, t1, t0) - fe_sq(t2, t1) + fe_mul(t_fe_1, t_fe_1, t_fe_0) + fe_sq(t_fe_2, t_fe_1) for (let i = 1; i < 20; ++i) { - fe_sq(t2, t2) + fe_sq(t_fe_2, t_fe_2) } - fe_mul(t1, t2, t1) + fe_mul(t_fe_1, t_fe_2, t_fe_1) for (let i = 1; i < 11; ++i) { - fe_sq(t1, t1) + fe_sq(t_fe_1, t_fe_1) } - fe_mul(t0, t1, t0) - fe_sq(t1, t0) + fe_mul(t_fe_0, t_fe_1, t_fe_0) + fe_sq(t_fe_1, t_fe_0) for (let i = 1; i < 50; ++i) { - fe_sq(t1, t1) + fe_sq(t_fe_1, t_fe_1) } - fe_mul(t1, t1, t0) - fe_sq(t2, t1) + fe_mul(t_fe_1, t_fe_1, t_fe_0) + fe_sq(t_fe_2, t_fe_1) for (let i = 1; i < 100; ++i) { - fe_sq(t2, t2) + fe_sq(t_fe_2, t_fe_2) } - fe_mul(t1, t2, t1) + fe_mul(t_fe_1, t_fe_2, t_fe_1) for (let i = 1; i < 51; ++i) { - fe_sq(t1, t1) + fe_sq(t_fe_1, t_fe_1) } - fe_mul(t0, t1, t0) - fe_sq(t0, t0) - fe_sq(t0, t0) - fe_mul(out, t0, z) + fe_mul(t_fe_0, t_fe_1, t_fe_0) + fe_sq(t_fe_0, t_fe_0) + fe_sq(t_fe_0, t_fe_0) + fe_mul(out, t_fe_0, z) } /** @@ -1086,9 +1086,9 @@ export function fe_sub (h: FieldElement, f: FieldElement, g: FieldElement): void * * Goal: Output h0+...+2²³⁰ h9. */ -export function fe_tobytes (s: StaticArray, h: FieldElement, t: FieldElement): void { - fe_reduce(t, h) - const t_ptr: usize = changetype(t) +export function fe_tobytes (s: StaticArray, h: FieldElement, t_fe: FieldElement): void { + fe_reduce(t_fe, h) + const t_ptr: usize = changetype(t_fe) const t0 = load(t_ptr, 0) const t1 = load(t_ptr, 4) const t2 = load(t_ptr, 8) diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index 06f669d..5cda5ae 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -91,10 +91,6 @@ const ge_frombytes_negate_vartime_v3: FieldElement = fe() const ge_frombytes_negate_vartime_vxx: FieldElement = fe() const ge_frombytes_negate_vartime_m_root_check: FieldElement = fe() const ge_frombytes_negate_vartime_p_root_check: FieldElement = fe() -const ge_frombytes_negate_vartime_u8x32_scratch = new StaticArray(32) -const ge_frombytes_negate_vartime_fe_scratch_0: FieldElement = fe() -const ge_frombytes_negate_vartime_fe_scratch_1: FieldElement = fe() -const ge_frombytes_negate_vartime_fe_scratch_2: FieldElement = fe() /** * Variable-time point negation converted from byte representation. * @@ -102,7 +98,9 @@ const ge_frombytes_negate_vartime_fe_scratch_2: FieldElement = fe() * * @returns {i32} 0 if `s` decodes to a valid point, else -1 */ -export function ge_frombytes_negate_vartime (h: ge_p3, s: StaticArray): i32 { +export function ge_frombytes_negate_vartime (h: ge_p3, s: StaticArray, + t_u8x32: StaticArray, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement +): i32 { const u = ge_frombytes_negate_vartime_u const v = ge_frombytes_negate_vartime_v const v3 = ge_frombytes_negate_vartime_v3 @@ -110,11 +108,6 @@ export function ge_frombytes_negate_vartime (h: ge_p3, s: StaticArray): i32 const m_root_check = ge_frombytes_negate_vartime_m_root_check const p_root_check = ge_frombytes_negate_vartime_p_root_check - const u8x32_scratch = ge_frombytes_negate_vartime_u8x32_scratch - const fe_scratch_0 = ge_frombytes_negate_vartime_fe_scratch_0 - const fe_scratch_1 = ge_frombytes_negate_vartime_fe_scratch_1 - const fe_scratch_2 = ge_frombytes_negate_vartime_fe_scratch_2 - fe_frombytes(h.Y, s) fe_1(h.Z) @@ -135,7 +128,7 @@ export function ge_frombytes_negate_vartime (h: ge_p3, s: StaticArray): i32 fe_mul(h.X, h.X, u) // x = (uv⁷)^((q-5)/8) - fe_pow22523(h.X, h.X, fe_scratch_0, fe_scratch_1, fe_scratch_2) + fe_pow22523(h.X, h.X, t_fe_0, t_fe_1, t_fe_2) // x = uv³((uv⁷)^((q-5)/8)) fe_mul(h.X, h.X, v3) @@ -147,16 +140,16 @@ export function ge_frombytes_negate_vartime (h: ge_p3, s: StaticArray): i32 // m = vx²-u fe_sub(m_root_check, vxx, u) - if (fe_iszero(m_root_check, u8x32_scratch, fe_scratch_0) == 0) { + if (fe_iszero(m_root_check, t_u8x32, t_fe_0) == 0) { // p = vx²+u fe_add(p_root_check, vxx, u) - if (fe_iszero(p_root_check, u8x32_scratch, fe_scratch_0) == 0) { + if (fe_iszero(p_root_check, t_u8x32, t_fe_0) == 0) { return -1 } fe_mul(h.X, h.X, fe_sqrtm1) } - if (fe_isnegative(h.X, fe_scratch_0) == (s[31] >> 7)) { + if (fe_isnegative(h.X, t_fe_0) == (s[31] >> 7)) { fe_neg(h.X, h.X) } fe_mul(h.T, h.X, h.Y) @@ -177,7 +170,7 @@ const ge_frombytes_x_sqrtm1: FieldElement = fe() * @returns {i32} 0 if `s` decodes to a valid point, else -1 */ export function ge_frombytes (h: ge_p3, s: StaticArray, - t0_fe: FieldElement, t1_fe: FieldElement, t2_fe: FieldElement, t_u8x32: StaticArray + t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_u8x32: StaticArray ): i32 { const u = ge_frombytes_u const v = ge_frombytes_v @@ -202,7 +195,7 @@ export function ge_frombytes (h: ge_p3, s: StaticArray, /* x = u((uv)^((q-5)/8)) */ fe_mul(h.X, u, v) - fe_pow22523(h.X, h.X, t0_fe, t1_fe, t2_fe) + fe_pow22523(h.X, h.X, t_fe_0, t_fe_1, t_fe_2) fe_mul(h.X, u, h.X) // vxx = vx² @@ -211,18 +204,18 @@ export function ge_frombytes (h: ge_p3, s: StaticArray, // m = vx²-u fe_sub(m_root_check, vxx, u) - has_m_root = fe_iszero(m_root_check, t_u8x32, t0_fe) + has_m_root = fe_iszero(m_root_check, t_u8x32, t_fe_0) // p = vx²+u fe_add(p_root_check, vxx, u) - has_p_root = fe_iszero(p_root_check, t_u8x32, t0_fe) + has_p_root = fe_iszero(p_root_check, t_u8x32, t_fe_0) // 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, t0_fe) ^ (s[31] >> 7)) + fe_cmov(h.X, negx, fe_isnegative(h.X, t_fe_0) ^ (s[31] >> 7)) fe_mul(h.T, h.X, h.Y) return i32(has_m_root | has_p_root) - 1 @@ -293,7 +286,7 @@ const ge_scalarmult_base_tobytes_t: ge_precomp = new ge_precomp() * @param a 32-byte input buffer for scalar */ export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray, - t0: FieldElement, t1: FieldElement, t2: FieldElement, t3: FieldElement, t4: FieldElement, t5: FieldElement, t6: FieldElement + 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 { const h = ge_scalarmult_base_tobytes_h const e = ge_scalarmult_base_tobytes_e @@ -327,13 +320,13 @@ export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray, a: StaticArray, a: StaticArray = new StaticArray(256) @@ -378,7 +371,6 @@ const ge_double_scalarmult_vartime_p: ge_p2 = new ge_p2() const ge_double_scalarmult_vartime_t: ge_p1p1 = new ge_p1p1() const ge_double_scalarmult_vartime_u: ge_p3 = new ge_p3() const ge_double_scalarmult_vartime_A2: ge_p3 = new ge_p3() -const ge_double_scalarmult_vartime_fe_scratch: FieldElement = fe() /** * `r = a[A] + b[B]` * @@ -390,7 +382,7 @@ const ge_double_scalarmult_vartime_fe_scratch: FieldElement = fe() * * Variable-time; only used for signature verification. */ -export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray, A: ge_p3, b: StaticArray): void { +export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray, A: ge_p3, b: StaticArray, t_fe: FieldElement): void { const Bi = base2 const aslide = ge_double_scalarmult_vartime_aslide const bslide = ge_double_scalarmult_vartime_bslide @@ -399,7 +391,6 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray const t = ge_double_scalarmult_vartime_t const u = ge_double_scalarmult_vartime_u const A2 = ge_double_scalarmult_vartime_A2 - const fe_scratch = ge_double_scalarmult_vartime_fe_scratch let i: i32 = 0 slide_vartime(aslide, a) @@ -407,34 +398,34 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray ge_p3_to_cached(Ai[0], A) - ge_p3_dbl(t, A, fe_scratch) + ge_p3_dbl(t, A, t_fe) ge_p1p1_to_p3(A2, t) - ge_add_cached(t, A2, Ai[0], fe_scratch) + ge_add_cached(t, A2, Ai[0], t_fe) ge_p1p1_to_p3(u, t) ge_p3_to_cached(Ai[1], u) - ge_add_cached(t, A2, Ai[1], fe_scratch) + ge_add_cached(t, A2, Ai[1], t_fe) ge_p1p1_to_p3(u, t) ge_p3_to_cached(Ai[2], u) - ge_add_cached(t, A2, Ai[2], fe_scratch) + ge_add_cached(t, A2, Ai[2], t_fe) ge_p1p1_to_p3(u, t) ge_p3_to_cached(Ai[3], u) - ge_add_cached(t, A2, Ai[3], fe_scratch) + ge_add_cached(t, A2, Ai[3], t_fe) ge_p1p1_to_p3(u, t) ge_p3_to_cached(Ai[4], u) - ge_add_cached(t, A2, Ai[4], fe_scratch) + ge_add_cached(t, A2, Ai[4], t_fe) ge_p1p1_to_p3(u, t) ge_p3_to_cached(Ai[5], u) - ge_add_cached(t, A2, Ai[5], fe_scratch) + ge_add_cached(t, A2, Ai[5], t_fe) ge_p1p1_to_p3(u, t) ge_p3_to_cached(Ai[6], u) - ge_add_cached(t, A2, Ai[6], fe_scratch) + ge_add_cached(t, A2, Ai[6], t_fe) ge_p1p1_to_p3(u, t) ge_p3_to_cached(Ai[7], u) @@ -447,11 +438,11 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray } for (; i >= 0; --i) { - ge_p2_dbl(t, p, fe_scratch) + ge_p2_dbl(t, p, t_fe) if (aslide[i] > 0) { ge_p1p1_to_p3(u, t) - ge_add_cached(t, u, Ai[aslide[i] / 2], fe_scratch) + ge_add_cached(t, u, Ai[aslide[i] / 2], t_fe) } else if (aslide[i] < 0) { ge_p1p1_to_p3(u, t) ge_sub_cached(t, u, Ai[(-aslide[i]) / 2]) diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 655509f..9f2176f 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -224,11 +224,11 @@ export function ge_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void { export function ge_p3_tobytes (s: StaticArray, h: ge_p3, reciprocal: FieldElement, x: FieldElement, y: FieldElement, - t0: FieldElement, t1: FieldElement, t2: FieldElement, t3: FieldElement + t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_fe_3: FieldElement ): void { - fe_invert(reciprocal, h.Z, t0, t1, t2, t3) + fe_invert(reciprocal, h.Z, t_fe_0, t_fe_1, t_fe_2, t_fe_3) fe_mul(x, h.X, reciprocal) fe_mul(y, h.Y, reciprocal) - fe_tobytes(s, y, t0) - s[31] ^= fe_isnegative(x, t0) << 7 + fe_tobytes(s, y, t_fe_0) + s[31] ^= fe_isnegative(x, t_fe_0) << 7 } -- 2.52.0