]> git.codecow.com Git - nano25519.git/commitdiff
Implement shared inversion in scalar multiplication by base point.
authorChris Duncan <chris@codecow.com>
Mon, 9 Nov 2026 07:25:54 +0000 (23:25 -0800)
committerChris Duncan <chris@codecow.com>
Mon, 9 Nov 2026 07:25:54 +0000 (23:25 -0800)
src/assembly/ed25519/ge/index.ts
src/assembly/ed25519/ge/scalarmult_base.ts

index 892cac9184ffa0ddfbd7ef25b3b8f1c66659583f..5f68f49a09d465a097b3d1bf0af79c41acb54879 100644 (file)
@@ -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'
+
index 709334a2add9c0d00f778182c0f4d0006f349eee..cb39376ae1fa216607cb68c0a6e1974d45d76456 100644 (file)
@@ -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<usize>(new StaticArray<i8>(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<u8>, a: StaticArray<u8>): void {
+function ge_scalarmult_base (h: p3, a: StaticArray<u8>): void {
        for (let i = 0; i < 32; ++i) {
                store<i8>(e + (i << 1), (a[i] >> 0) & 15)
                store<i8>(e + ((i << 1) + 1), (a[i] >> 4) & 15)
@@ -67,19 +70,50 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
                p3_add_precomp_to_p3(h, h, t)
        }
 
-       p3_tobytes(S, h)
-
        // clear sensitive data
-       p1p1_0(r)
        p2_0(s)
        precomp_0(t)
-       p3_0(h)
        v128.store(e, i64x2.splat(0), 0)
        v128.store(e, i64x2.splat(0), 16)
        v128.store(e, i64x2.splat(0), 32)
        v128.store(e, i64x2.splat(0), 48)
 }
 
+/**
+ * `S = enc(a[B])`
+ *
+ * Fixed-base scalar multiplication encoded to bytes. See `ge_scalarmult_base`.
+ *
+ * @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>): 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<u8>, a_0: StaticArray<u8>, S_1: StaticArray<u8>, a_1: StaticArray<u8>): 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))