store<i32>(changetype<usize>(h), h9, 36)
}
+/**
+ * For two pairs of FieldElements, multiply pairwise in parallel and store the
+ * product.
+ *
+ * @param h_a FieldElement A product destination
+ * @param f_a FieldElement A multiplicand source
+ * @param g_a FieldElement A multiplicand source
+ * @param h_b FieldElement B product destination
+ * @param f_b FieldElement B multiplicand source
+ * @param g_b FieldElement B multiplicand source
+ */
+export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldElement, h_b: FieldElement, f_b: FieldElement, g_b: FieldElement): void {
+ // mask to double odd-on-odd indexes
+ const odds: v128 = i32x4(0, 0, -1, -1)
+
+ // pack each f index twice
+ const f_a_0123: v128 = v128.load(changetype<usize>(f_a), 0)
+ const f_b_0123: v128 = v128.load(changetype<usize>(f_b), 0)
+ const f0: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 0, 4, 0, 4)
+ let f1: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 1, 5, 1, 5)
+ const f2: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 2, 6, 2, 6)
+ let f3: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 3, 7, 3, 7)
+
+ const f_a_4567: v128 = v128.load(changetype<usize>(f_a), 16)
+ const f_b_4567: v128 = v128.load(changetype<usize>(f_b), 16)
+ const f4: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 0, 4, 0, 4)
+ let f5: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 1, 5, 1, 5)
+ const f6: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 2, 6, 2, 6)
+ let f7: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 3, 7, 3, 7)
+
+ const f_a_89xx: v128 = v128.load(changetype<usize>(f_a), 32)
+ const f_b_89xx: v128 = v128.load(changetype<usize>(f_b), 32)
+ const f8: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 0, 4, 0, 4)
+ let f9: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 1, 5, 1, 5)
+
+ // pack each g index once
+ const g_a_0123: v128 = v128.load(changetype<usize>(g_a), 0)
+ const g_b_0123: v128 = v128.load(changetype<usize>(g_b), 0)
+ const g01: v128 = v128.shuffle<i32>(g_a_0123, g_b_0123, 0, 4, 1, 5)
+ const g23: v128 = v128.shuffle<i32>(g_a_0123, g_b_0123, 2, 6, 3, 7)
+
+ const g_a_4567: v128 = v128.load(changetype<usize>(g_a), 16)
+ const g_b_4567: v128 = v128.load(changetype<usize>(g_b), 16)
+ const g45: v128 = v128.shuffle<i32>(g_a_4567, g_b_4567, 0, 4, 1, 5)
+ const g67: v128 = v128.shuffle<i32>(g_a_4567, g_b_4567, 2, 6, 3, 7)
+
+ const g_a_89xx: v128 = v128.load(changetype<usize>(g_a), 32)
+ const g_b_89xx: v128 = v128.load(changetype<usize>(g_b), 32)
+ const g89: v128 = v128.shuffle<i32>(g_a_89xx, g_b_89xx, 0, 4, 1, 5)
+
+ // factor of 19 for wrap from h9 to h0
+ const v19: v128 = i32x4.splat(19)
+ const g01_19: v128 = v128.mul<i32>(g01, v19)
+ const g23_19: v128 = v128.mul<i32>(g23, v19)
+ const g45_19: v128 = v128.mul<i32>(g45, v19)
+ const g67_19: v128 = v128.mul<i32>(g67, v19)
+ const g89_19: v128 = v128.mul<i32>(g89, v19)
+
+ // f[0]
+ let h0: v128 = v128.extmul_low<i32>(f0, g01)
+ let h1: v128 = v128.extmul_high<i32>(f0, g01)
+ let h2: v128 = v128.extmul_low<i32>(f0, g23)
+ let h3: v128 = v128.extmul_high<i32>(f0, g23)
+ let h4: v128 = v128.extmul_low<i32>(f0, g45)
+ let h5: v128 = v128.extmul_high<i32>(f0, g45)
+ let h6: v128 = v128.extmul_low<i32>(f0, g67)
+ let h7: v128 = v128.extmul_high<i32>(f0, g67)
+ let h8: v128 = v128.extmul_low<i32>(f0, g89)
+ let h9: v128 = v128.extmul_high<i32>(f0, g89)
+
+ // f[1]
+ f1 = v128.add<i32>(f1, v128.and(f1, odds))
+
+ h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f1, g01))
+ h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f1, g01))
+ h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f1, g23))
+ h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f1, g23))
+ h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f1, g45))
+ h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f1, g45))
+ h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f1, g67))
+ h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f1, g67))
+ h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f1, g89))
+ h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f1, g89_19))
+
+ // f[2]
+ h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f2, g01))
+ h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f2, g01))
+ h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f2, g23))
+ h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f2, g23))
+ h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f2, g45))
+ h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f2, g45))
+ h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f2, g67))
+ h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f2, g67))
+ h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f2, g89_19))
+ h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f2, g89_19))
+
+ // f[3]
+ f3 = v128.add<i32>(f3, v128.and(f3, odds))
+
+ h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f3, g01))
+ h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f3, g01))
+ h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f3, g23))
+ h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f3, g23))
+ h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f3, g45))
+ h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f3, g45))
+ h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f3, g67))
+ h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f3, g67_19))
+ h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f3, g89_19))
+ h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f3, g89_19))
+
+ // f[4]
+ h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f4, g01))
+ h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f4, g01))
+ h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f4, g23))
+ h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f4, g23))
+ h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f4, g45))
+ h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f4, g45))
+ h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f4, g67_19))
+ h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f4, g67_19))
+ h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f4, g89_19))
+ h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f4, g89_19))
+
+ // f[5]
+ f5 = v128.add<i32>(f5, v128.and(f5, odds))
+
+ h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f5, g01))
+ h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f5, g01))
+ h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f5, g23))
+ h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f5, g23))
+ h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f5, g45))
+ h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f5, g45_19))
+ h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f5, g67_19))
+ h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f5, g67_19))
+ h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f5, g89_19))
+ h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f5, g89_19))
+
+ // f[6]
+ h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f6, g01))
+ h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f6, g01))
+ h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f6, g23))
+ h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f6, g23))
+ h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f6, g45_19))
+ h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f6, g45_19))
+ h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f6, g67_19))
+ h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f6, g67_19))
+ h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f6, g89_19))
+ h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f6, g89_19))
+
+ // f[7]
+ f7 = v128.add<i32>(f7, v128.and(f7, odds))
+
+ h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f7, g01))
+ h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f7, g01))
+ h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f7, g23))
+ h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f7, g23_19))
+ h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f7, g45_19))
+ h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f7, g45_19))
+ h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f7, g67_19))
+ h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f7, g67_19))
+ h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f7, g89_19))
+ h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f7, g89_19))
+
+ // f[8]
+ h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f8, g01))
+ h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f8, g01))
+ h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f8, g23_19))
+ h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f8, g23_19))
+ h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f8, g45_19))
+ h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f8, g45_19))
+ h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f8, g67_19))
+ h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f8, g67_19))
+ h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f8, g89_19))
+ h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f8, g89_19))
+
+ // f[9]
+ f9 = v128.add<i32>(f9, v128.and(f9, odds))
+
+ h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f9, g01))
+ h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f9, g01_19))
+ h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f9, g23_19))
+ h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f9, g23_19))
+ h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f9, g45_19))
+ h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f9, g45_19))
+ h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f9, g67_19))
+ h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f9, g67_19))
+ h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f9, g89_19))
+ h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f9, g89_19))
+
+ // reduce scalars with carry
+ const bias25 = i64x2.splat(1 << 25)
+ const bias24 = i64x2.splat(1 << 24)
+ let carry0: v128
+ let carry1: v128
+ let carry2: v128
+ let carry3: v128
+ let carry4: v128
+ let carry5: v128
+ let carry6: v128
+ let carry7: v128
+ let carry8: v128
+ let carry9: v128
+
+ carry0 = v128.shr<i64>(v128.add<i64>(h0, bias25), 26)
+ h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+ h1 = v128.add<i64>(h1, carry0)
+
+ carry4 = v128.shr<i64>(v128.add<i64>(h4, bias25), 26)
+ h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+ h5 = v128.add<i64>(h5, carry4)
+
+ carry1 = v128.shr<i64>(v128.add<i64>(h1, bias24), 25)
+ h1 = v128.sub<i64>(h1, v128.shl<i64>(carry1, 25))
+ h2 = v128.add<i64>(h2, carry1)
+
+ carry5 = v128.shr<i64>(v128.add<i64>(h5, bias24), 25)
+ h5 = v128.sub<i64>(h5, v128.shl<i64>(carry5, 25))
+ h6 = v128.add<i64>(h6, carry5)
+
+ carry2 = v128.shr<i64>(v128.add<i64>(h2, bias25), 26)
+ h2 = v128.sub<i64>(h2, v128.shl<i64>(carry2, 26))
+ h3 = v128.add<i64>(h3, carry2)
+
+ carry6 = v128.shr<i64>(v128.add<i64>(h6, bias25), 26)
+ h6 = v128.sub<i64>(h6, v128.shl<i64>(carry6, 26))
+ h7 = v128.add<i64>(h7, carry6)
+
+ carry3 = v128.shr<i64>(v128.add<i64>(h3, bias24), 25)
+ h3 = v128.sub<i64>(h3, v128.shl<i64>(carry3, 25))
+ h4 = v128.add<i64>(h4, carry3)
+
+ carry7 = v128.shr<i64>(v128.add<i64>(h7, bias24), 25)
+ h7 = v128.sub<i64>(h7, v128.shl<i64>(carry7, 25))
+ h8 = v128.add<i64>(h8, carry7)
+
+ carry4 = v128.shr<i64>(v128.add<i64>(h4, bias25), 26)
+ h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+ h5 = v128.add<i64>(h5, carry4)
+
+ carry8 = v128.shr<i64>(v128.add<i64>(h8, bias25), 26)
+ h8 = v128.sub<i64>(h8, v128.shl<i64>(carry8, 26))
+ h9 = v128.add<i64>(h9, carry8)
+
+ carry9 = v128.shr<i64>(v128.add<i64>(h9, bias24), 25)
+ h9 = v128.sub<i64>(h9, v128.shl<i64>(carry9, 25))
+ h0 = v128.add<i64>(h0, v128.mul<i64>(carry9, i64x2.splat(19)))
+
+ carry0 = v128.shr<i64>(v128.add<i64>(h0, bias25), 26)
+ h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+ h1 = v128.add<i64>(h1, carry0)
+
+ // extract and assign results to parallel outputs
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h0, 0), 0)
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h1, 0), 4)
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h2, 0), 8)
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h3, 0), 12)
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h4, 0), 16)
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h5, 0), 20)
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h6, 0), 24)
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h7, 0), 28)
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h8, 0), 32)
+ store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h9, 0), 36)
+
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h0, 1), 0)
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h1, 1), 4)
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h2, 1), 8)
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h3, 1), 12)
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h4, 1), 16)
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h5, 1), 20)
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h6, 1), 24)
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h7, 1), 28)
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h8, 1), 32)
+ store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h9, 1), 36)
+}
+
/**
* Negate the values of a FieldElement and store the result.
*
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! SPDX-License-Identifier: ISC
-import { FieldElement, fe, fe_0, fe_1, fe_add, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_neg, fe_sq, fe_sq2, fe_sub, fe_tobytes } from './fe'
+import { FieldElement, fe, fe_0, fe_1, fe_add, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_dual, fe_neg, fe_sq, fe_sq2, fe_sub, fe_tobytes } from './fe'
/**
* 2d = −121665 / 60833
* r = p
*/
export function ge_p1p1_to_p2 (r: ge_p2, p: ge_p1p1): void {
- fe_mul(r.X, p.X, p.T)
- fe_mul(r.Y, p.Y, p.Z)
+ fe_mul_dual(r.X, p.X, p.T, r.Y, p.Y, p.Z)
fe_mul(r.Z, p.Z, p.T)
}
export function ge_p1p1_to_p3 (r: ge_p3, p: ge_p1p1): void {
- fe_mul(r.X, p.X, p.T)
- fe_mul(r.Y, p.Y, p.Z)
- fe_mul(r.Z, p.Z, p.T)
- fe_mul(r.T, p.X, p.Y)
+ fe_mul_dual(r.X, p.X, p.T, r.Y, p.Y, p.Z)
+ fe_mul_dual(r.Z, p.Z, p.T, r.T, p.X, p.Y)
}
/**
export function ge_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached, t: FieldElement): void {
fe_add(r.X, p.Y, p.X)
fe_sub(r.Y, p.Y, p.X)
- fe_mul(r.Z, r.X, q.YplusX)
- fe_mul(r.Y, r.Y, q.YminusX)
- fe_mul(r.T, q.T2d, p.T)
- fe_mul(r.X, p.Z, q.Z)
+ fe_mul_dual(r.Z, r.X, q.YplusX, r.Y, r.Y, q.YminusX)
+ fe_mul_dual(r.T, q.T2d, p.T, r.X, p.Z, q.Z)
fe_dbl(t, r.X)
fe_sub(r.X, r.Z, r.Y)
fe_add(r.Y, r.Z, r.Y)
export function ge_add_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void {
fe_add(r.X, p.Y, p.X)
fe_sub(r.Y, p.Y, p.X)
- fe_mul(r.Z, r.X, q.yplusx)
- fe_mul(r.Y, r.Y, q.yminusx)
+ fe_mul_dual(r.Z, r.X, q.yplusx, r.Y, r.Y, q.yminusx)
fe_mul(r.T, q.xy2d, p.T)
fe_dbl(p.Z, p.Z)
fe_sub(r.X, r.Z, r.Y)
export function ge_sub_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void {
fe_add(r.X, p.Y, p.X)
fe_sub(r.Y, p.Y, p.X)
- fe_mul(r.Z, r.X, q.YminusX)
- fe_mul(r.Y, r.Y, q.YplusX)
- fe_mul(r.T, q.T2d, p.T)
- fe_mul(r.X, p.Z, q.Z)
+ fe_mul_dual(r.Z, r.X, q.YminusX, r.Y, r.Y, q.YplusX)
+ fe_mul_dual(r.T, q.T2d, p.T, r.X, p.Z, q.Z)
fe_dbl(p.X, r.X)
fe_sub(r.X, r.Z, r.Y)
fe_add(r.Y, r.Z, r.Y)
export function ge_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void {
fe_add(r.X, p.Y, p.X)
fe_sub(r.Y, p.Y, p.X)
- fe_mul(r.Z, r.X, q.yminusx)
- fe_mul(r.Y, r.Y, q.yplusx)
+ fe_mul_dual(r.Z, r.X, q.yminusx, r.Y, r.Y, q.yplusx)
fe_mul(r.T, q.xy2d, p.T)
fe_dbl(p.Z, p.Z)
fe_sub(r.X, r.Z, r.Y)
t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_fe_3: FieldElement
): void {
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_mul_dual(x, h.X, reciprocal, y, h.Y, reciprocal)
fe_tobytes(s, y, t_fe_0)
s[31] ^= fe_isnegative(x, t_fe_0) << 7
}