//! 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)
* 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)
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))