* - p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
* - precomp (Duif): (y+x,y-x,2dxy)
*/
-import { base } from './base'
-import { base2 } from './base2'
-import { FieldElement, fe, fe_1, fe_add, fe_addsub, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_mul_d, fe_mul_pair_sqrtm1, fe_mul_sqrtm1, fe_neg, fe_sq, fe_sub } from './fe'
-import { p1p1, p1p1_0, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_cached, p3_add_cached_output_cached, p3_add_precomp, p3_dbl, p3_dbl_to_p3, p3_sub_cached, p3_sub_precomp, p3_to_cached, p3_tobytes, p_cached, p_precomp, precomp_0 } from './p'
-import { fe_pow22523 } from './z'
-
-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))
-
- // identity: (1, 1, 0)
- // yplusx = 1
- let p0 = i32x4(1, 0, 0, 0), p1 = i32x4(0, 0, 0, 0), p2 = i32x4(0, 0, 0, 0)
- // yminusx = 1
- let m0 = i32x4(1, 0, 0, 0), m1 = i32x4(0, 0, 0, 0), m2 = i32x4(0, 0, 0, 0)
- // xy2d = 0
- let d0 = i32x4(0, 0, 0, 0), d1 = i32x4(0, 0, 0, 0), d2 = i32x4(0, 0, 0, 0)
-
- // base pointer offset += 144n = (4 bytes * 12 values * 3 fields) * n
- for (let i: i8 = 1; i <= 8; i++, bp += 144) {
- const isbabs: i8 = (((babs ^ i) - 1) >> 7) & 1 /* xor == 0: yes; xor == 1..255: no */
- const c = v128.splat<u64>(0 - u64(isbabs))
- p0 = v128.bitselect(v128.load(bp), p0, c)
- p1 = v128.bitselect(v128.load(bp, 16), p1, c)
- p2 = v128.bitselect(v128.load(bp, 32), p2, c)
- m0 = v128.bitselect(v128.load(bp, 48), m0, c)
- m1 = v128.bitselect(v128.load(bp, 64), m1, c)
- m2 = v128.bitselect(v128.load(bp, 80), m2, c)
- d0 = v128.bitselect(v128.load(bp, 96), d0, c)
- d1 = v128.bitselect(v128.load(bp, 112), d1, c)
- d2 = v128.bitselect(v128.load(bp, 128), d2, c)
- }
-
- // -(x, y) == (-x, y); for -x, y+x <-> y-x and negate xy2d
- const n = v128.splat<u64>(0 - u64(isbneg))
- const tp = changetype<usize>(t.yplusx)
- const tm = changetype<usize>(t.yminusx)
- const td = changetype<usize>(t.xy2d)
-
- // y+x = y-x if n, else y+x
- v128.store(tp, v128.bitselect(m0, p0, n))
- v128.store(tp, v128.bitselect(m1, p1, n), 16)
- v128.store_lane<u64>(tp, v128.bitselect(m2, p2, n), 0, 32)
-
- // y-x = y+x if n, else y-x
- v128.store(tm, v128.bitselect(p0, m0, n))
- v128.store(tm, v128.bitselect(p1, m1, n), 16)
- v128.store_lane<u64>(tm, v128.bitselect(p2, m2, n), 0, 32)
-
- // xy2d = -xy2d if n, else xy2d
- v128.store(td, v128.bitselect(v128.neg<i32>(d0), d0, n))
- v128.store(td, v128.bitselect(v128.neg<i32>(d1), d1, n), 16)
- v128.store_lane<u64>(td, v128.bitselect(v128.neg<i32>(d2), d2, n), 0, 32)
-}
+import { base2 } from '../base2'
+import { FieldElement, fe, fe_1, fe_add, fe_addsub, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_mul_d, fe_mul_pair_sqrtm1, fe_mul_sqrtm1, fe_neg, fe_sq, fe_sub } from '../fe'
+import { p1p1, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_cached, p3_add_cached_output_cached, p3_add_precomp, p3_sub_cached, p3_sub_precomp, p3_to_cached, p_cached } from '../p'
+import { fe_pow22523 } from '../z'
const ge_frombytes_u: FieldElement = fe()
const ge_frombytes_v: FieldElement = fe()
return 0 - (c & d & 1)
}
-const ge_scalarmult_base_tobytes_h: p3 = new p3()
-const ge_scalarmult_base_tobytes_e: StaticArray<i8> = new StaticArray<i8>(64)
-const ge_scalarmult_base_tobytes_r: p1p1 = new p1p1()
-const ge_scalarmult_base_tobytes_s: p2 = new p2()
-const ge_scalarmult_base_tobytes_t: p_precomp = new p_precomp()
-/**
- * `h = a * B` (with precomputation)
- *
- * where
- *
- * `a = 256⁰a[0] + 256¹a[1] + ... + 256³¹a[31]`
- *
- * B is the Ed25519 base point `(x,⅘)` with `x` positive. As bytes:
- *
- * `0x5866666666666666666666666666666666666666666666666666666666666666`
- *
- * Preconditions:
- * - `a[31] ≤ 127`
- *
- * @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 {
- const h = ge_scalarmult_base_tobytes_h
- const e = ge_scalarmult_base_tobytes_e
- const r = ge_scalarmult_base_tobytes_r
- const s = ge_scalarmult_base_tobytes_s
- const t = ge_scalarmult_base_tobytes_t
-
- for (let i = 0; i < 32; ++i) {
- e[(i << 1) + 0] = (a[i] >> 0) & 15
- e[(i << 1) + 1] = (a[i] >> 4) & 15
- }
- /* each e[i] is between 0 and 15 */
- /* e[63] is between 0 and 7 */
-
- let carry: i8 = 0
- for (let i = 0; i < 63; ++i) {
- e[i] += carry
- carry = e[i] + 8
- carry >>= 4
- e[i] -= carry * (i8(1) << 4)
- }
- e[63] += carry
- /* each e[i] is between -8 and 8 */
-
- p3_0(h)
-
- // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
- for (let i = 1, bp = 0; i < 64; i += 2, bp += 1152) {
- ge_cmov8_base(t, base + bp, load<i8>(changetype<usize>(e) + i))
- p3_add_precomp(r, h, t)
- p1p1_to_p3(h, r)
- }
-
- p3_dbl(r, h)
- p1p1_to_p2(s, r)
- p2_dbl(r, s)
- p1p1_to_p2(s, r)
- p2_dbl(r, s)
- p1p1_to_p2(s, r)
- p2_dbl(r, s)
- p1p1_to_p3(h, r)
-
- // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
- for (let i = 0, bp = 0; i < 64; i += 2, bp += 1152) {
- ge_cmov8_base(t, base + bp, load<i8>(changetype<usize>(e) + i))
- p3_add_precomp(r, h, t)
- p1p1_to_p3(h, r)
- }
-
- p3_tobytes(S, h)
-
- // clear sensitive data
- p1p1_0(r)
- p2_0(s)
- precomp_0(t)
- p3_0(h)
- e.fill(0)
-}
-
/**
* Computes a lookup table for multiples of the odd limbs of an extended
* projective point so it can be cached and reused for performance.
--- /dev/null
+//! SPDX-FileCopyrightText: 2013-2026 Frank Denis <j@pureftpd.org>
+//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
+//! SPDX-License-Identifier: ISC
+
+import { base } from '../base'
+import { p1p1, p1p1_0, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_precomp, p3_dbl, p3_tobytes, p_precomp, precomp_0 } from '../p'
+
+const h: p3 = new p3()
+const e: StaticArray<i8> = new StaticArray<i8>(64)
+const r: p1p1 = new p1p1()
+const s: p2 = new p2()
+const t: p_precomp = new p_precomp()
+
+/**
+ * `h = a * B` (with precomputation)
+ *
+ * where
+ *
+ * `a = 256⁰a[0] + 256¹a[1] + ... + 256³¹a[31]`
+ *
+ * B is the Ed25519 base point `(x,⅘)` with `x` positive. As bytes:
+ *
+ * `0x5866666666666666666666666666666666666666666666666666666666666666`
+ *
+ * Preconditions:
+ * - `a[31] ≤ 127`
+ *
+ * @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 {
+ for (let i = 0; i < 32; ++i) {
+ e[(i << 1) + 0] = (a[i] >> 0) & 15
+ e[(i << 1) + 1] = (a[i] >> 4) & 15
+ }
+ /* each e[i] is between 0 and 15 */
+ /* e[63] is between 0 and 7 */
+
+ let carry: i8 = 0
+ for (let i = 0; i < 63; ++i) {
+ e[i] += carry
+ carry = e[i] + 8
+ carry >>= 4
+ e[i] -= carry * (i8(1) << 4)
+ }
+ e[63] += carry
+ /* each e[i] is between -8 and 8 */
+
+ p3_0(h)
+
+ // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
+ for (let i = 1, bp = 0; i < 64; i += 2, bp += 1152) {
+ ge_cmov8_base(t, base + bp, load<i8>(changetype<usize>(e) + i))
+ p3_add_precomp(r, h, t)
+ p1p1_to_p3(h, r)
+ }
+
+ p3_dbl(r, h)
+ p1p1_to_p2(s, r)
+ p2_dbl(r, s)
+ p1p1_to_p2(s, r)
+ p2_dbl(r, s)
+ p1p1_to_p2(s, r)
+ p2_dbl(r, s)
+ p1p1_to_p3(h, r)
+
+ // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
+ for (let i = 0, bp = 0; i < 64; i += 2, bp += 1152) {
+ ge_cmov8_base(t, base + bp, load<i8>(changetype<usize>(e) + i))
+ p3_add_precomp(r, h, t)
+ p1p1_to_p3(h, r)
+ }
+
+ p3_tobytes(S, h)
+
+ // clear sensitive data
+ p1p1_0(r)
+ p2_0(s)
+ precomp_0(t)
+ p3_0(h)
+ e.fill(0)
+}
+
+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))
+
+ // identity: (1, 1, 0)
+ // yplusx = 1
+ let p0 = i32x4(1, 0, 0, 0), p1 = i32x4(0, 0, 0, 0), p2 = i32x4(0, 0, 0, 0)
+ // yminusx = 1
+ let m0 = i32x4(1, 0, 0, 0), m1 = i32x4(0, 0, 0, 0), m2 = i32x4(0, 0, 0, 0)
+ // xy2d = 0
+ let d0 = i32x4(0, 0, 0, 0), d1 = i32x4(0, 0, 0, 0), d2 = i32x4(0, 0, 0, 0)
+
+ // base pointer offset += 144n = (4 bytes * 12 values * 3 fields) * n
+ for (let i: i8 = 1; i <= 8; i++, bp += 144) {
+ const isbabs: i8 = (((babs ^ i) - 1) >> 7) & 1 /* xor == 0: yes; xor == 1..255: no */
+ const c = v128.splat<u64>(0 - u64(isbabs))
+ p0 = v128.bitselect(v128.load(bp), p0, c)
+ p1 = v128.bitselect(v128.load(bp, 16), p1, c)
+ p2 = v128.bitselect(v128.load(bp, 32), p2, c)
+ m0 = v128.bitselect(v128.load(bp, 48), m0, c)
+ m1 = v128.bitselect(v128.load(bp, 64), m1, c)
+ m2 = v128.bitselect(v128.load(bp, 80), m2, c)
+ d0 = v128.bitselect(v128.load(bp, 96), d0, c)
+ d1 = v128.bitselect(v128.load(bp, 112), d1, c)
+ d2 = v128.bitselect(v128.load(bp, 128), d2, c)
+ }
+
+ // -(x, y) == (-x, y); for -x, y+x <-> y-x and negate xy2d
+ const n = v128.splat<u64>(0 - u64(isbneg))
+ const tp = changetype<usize>(t.yplusx)
+ const tm = changetype<usize>(t.yminusx)
+ const td = changetype<usize>(t.xy2d)
+
+ // y+x = y-x if n, else y+x
+ v128.store(tp, v128.bitselect(m0, p0, n))
+ v128.store(tp, v128.bitselect(m1, p1, n), 16)
+ v128.store_lane<u64>(tp, v128.bitselect(m2, p2, n), 0, 32)
+
+ // y-x = y+x if n, else y-x
+ v128.store(tm, v128.bitselect(p0, m0, n))
+ v128.store(tm, v128.bitselect(p1, m1, n), 16)
+ v128.store_lane<u64>(tm, v128.bitselect(p2, m2, n), 0, 32)
+
+ // xy2d = -xy2d if n, else xy2d
+ v128.store(td, v128.bitselect(v128.neg<i32>(d0), d0, n))
+ v128.store(td, v128.bitselect(v128.neg<i32>(d1), d1, n), 16)
+ v128.store_lane<u64>(td, v128.bitselect(v128.neg<i32>(d2), d2, n), 0, 32)
+}