From 628fd0c8f70ef94452c760a5cfd5de27a1a2cde4 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 8 Nov 2026 23:25:54 -0800 Subject: [PATCH] Implement shared inversion in scalar multiplication by base point. --- src/assembly/ed25519/ge/index.ts | 3 +- src/assembly/ed25519/ge/scalarmult_base.ts | 52 ++++++++++++++++++---- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/assembly/ed25519/ge/index.ts b/src/assembly/ed25519/ge/index.ts index 892cac9..5f68f49 100644 --- a/src/assembly/ed25519/ge/index.ts +++ b/src/assembly/ed25519/ge/index.ts @@ -19,4 +19,5 @@ */ export { ge_double_scalarmult_vartime_to_bytes, ge_double_scalarmult_vartime_to_cached } from './double_scalarmult' export { ge_accept_small_order, ge_cache_odd_multiples, ge_frombytes, ge_frombytes_negate, ge_is_canonical, ge_reject_small_order } from './ge' -export { ge_scalarmult_base_tobytes } from './scalarmult_base' +export { ge_scalarmult_base_tobytes, ge_scalarmult_base_tobytes_pair } from './scalarmult_base' + diff --git a/src/assembly/ed25519/ge/scalarmult_base.ts b/src/assembly/ed25519/ge/scalarmult_base.ts index 709334a..cb39376 100644 --- a/src/assembly/ed25519/ge/scalarmult_base.ts +++ b/src/assembly/ed25519/ge/scalarmult_base.ts @@ -3,13 +3,13 @@ //! SPDX-License-Identifier: ISC import { base } from '../constants/base' -import { p1p1, p1p1_0, p2, p2_0, p2_dbl_to_p2, p2_dbl_to_p3, p3, p3_0, p3_add_precomp_to_p3, p3_dbl_to_p2, p3_tobytes, p_precomp, precomp_0 } from '../p' +import { p2, p2_0, p2_dbl_to_p2, p2_dbl_to_p3, p3, p3_0, p3_add_precomp_to_p3, p3_dbl_to_p2, p3_tobytes, p3_tobytes_pair, p_precomp, precomp_0 } from '../p' -const h: p3 = new p3() const e: usize = changetype(new StaticArray(64)) -const r: p1p1 = new p1p1() const s: p2 = new p2() const t: p_precomp = new p_precomp() +const u: p3 = new p3() +const v: p3 = new p3() /** * `h = a[B]` (with precomputation) @@ -25,10 +25,13 @@ const t: p_precomp = new p_precomp() * Preconditions: * - `a[31] ≤ 127` * - * @param S 32-byte output buffer for scalar * base point + * This function is not exported, but `h` holds secret-dependent material, so + * internal callers below must be sure to clear it once it is encoded. + * + * @param h point at scalar * base point * @param a 32-byte input buffer for scalar */ -export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray): void { +function ge_scalarmult_base (h: p3, a: StaticArray): void { for (let i = 0; i < 32; ++i) { store(e + (i << 1), (a[i] >> 0) & 15) store(e + ((i << 1) + 1), (a[i] >> 4) & 15) @@ -67,19 +70,50 @@ export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray, a: StaticArray): void { + ge_scalarmult_base(u, a) + p3_tobytes(S, u) + p3_0(u) +} + +/** + * `S₀ = enc(a₀[B]); S₁ = enc(a₁[B])` + * + * Two fixed-base scalar multiplications, encoded with one shared inversion. An + * encoding is about a third of the cost of one multiplication, nearly all of it + * the inversion, so this saves most of an encoding over two calls to + * `ge_scalarmult_base_tobytes`. See `ge_scalarmult_base` and `p3_tobytes_pair`. + * + * @param S_0 32-byte output buffer for first scalar * base point + * @param a_0 32-byte input buffer for first scalar + * @param S_1 32-byte output buffer for second scalar * base point + * @param a_1 32-byte input buffer for second scalar + */ +export function ge_scalarmult_base_tobytes_pair (S_0: StaticArray, a_0: StaticArray, S_1: StaticArray, a_1: StaticArray): void { + ge_scalarmult_base(u, a_0) + ge_scalarmult_base(v, a_1) + p3_tobytes_pair(S_0, u, S_1, v) + p3_0(u) + p3_0(v) +} + function ge_cmov8_base (t: p_precomp, bp: usize, b: i8): void { const isbneg: i8 = (b >> 7) & 1 /* 0: positive; 1: negative */ const babs: i8 = b - (((-isbneg) & b) * (i8(1) << 1)) -- 2.52.0