]> git.codecow.com Git - nano25519.git/commitdiff
Add some field element helpers.
authorChris Duncan <chris@codecow.com>
Sun, 25 Oct 2026 07:40:35 +0000 (00:40 -0700)
committerChris Duncan <chris@codecow.com>
Sun, 25 Oct 2026 07:40:35 +0000 (00:40 -0700)
src/assembly/ed25519/fe.ts
src/assembly/ed25519/p.ts

index b7e0a9fb7adc9cebec5edecf94327e9f36f88c56..0cea744a5d92f392616afeecf2fcabded0b360de 100644 (file)
@@ -60,12 +60,42 @@ export function fe_1 (h: FieldElement): void {
  * @param g FieldElement summand source
  */
 export function fe_add (h: FieldElement, f: FieldElement, g: FieldElement): void {
-       const f0 = v128.load(changetype<usize>(f), 0 << 4), f1 = v128.load(changetype<usize>(f), 1 << 4), f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
-       const g0 = v128.load(changetype<usize>(g), 0 << 4), g1 = v128.load(changetype<usize>(g), 1 << 4), g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
-       const h0 = v128.add<i32>(f0, g0), h1 = v128.add<i32>(f1, g1), h2 = v128.add<i32>(f2, g2)
-       v128.store(changetype<usize>(h), h0, 0 << 4)
-       v128.store(changetype<usize>(h), h1, 1 << 4)
-       v128.store_lane<i64>(changetype<usize>(h), h2, 0, 2 << 4)
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
+
+       v128.store(changetype<usize>(h), v128.add<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(h), v128.add<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(h), v128.add<i32>(f2, g2), 0, 2 << 4)
+}
+
+/**
+ * Add and subtract two FieldElements without a temporary and store the results.
+ *
+ * `f = f + g` and `g = f - g`
+ *
+ * @param f FieldElement sum destination, summand source, and minuend source
+ * @param g FieldElement difference destination, summand source, and subtrahend source
+ */
+export function fe_addsub (f: FieldElement, g: FieldElement): void {
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
+
+       v128.store(changetype<usize>(f), v128.add<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(f), v128.add<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(f), v128.add<i32>(f2, g2), 0, 2 << 4)
+       v128.store(changetype<usize>(g), v128.sub<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(g), v128.sub<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(g), v128.sub<i32>(f2, g2), 0, 2 << 4)
 }
 
 /**
@@ -412,145 +442,694 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        h45 = i64x2.add(h45, t4)
 
        // f[7]
-       t0 = i64x2.extmul_low_i32x4_s(f7, g9012_19)
-       t1 = i64x2.extmul_high_i32x4_s(f7, g9012)
-       t2 = i64x2.extmul_low_i32x4_s(f7, g3456_19)
-       t3 = i64x2.extmul_high_i32x4_s(f7, g3456_19)
-       t4 = i64x2.extmul_low_i32x4_s(f7, g78xx_19)
-
-       h67 = i64x2.add(h67, t0)
-       h89 = i64x2.add(h89, t1)
-       h01 = i64x2.add(h01, t2)
-       h23 = i64x2.add(h23, t3)
-       h45 = i64x2.add(h45, t4)
+       t0 = i64x2.extmul_low_i32x4_s(f7, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f7, g9012)
+       t2 = i64x2.extmul_low_i32x4_s(f7, g3456_19)
+       t3 = i64x2.extmul_high_i32x4_s(f7, g3456_19)
+       t4 = i64x2.extmul_low_i32x4_s(f7, g78xx_19)
+
+       h67 = i64x2.add(h67, t0)
+       h89 = i64x2.add(h89, t1)
+       h01 = i64x2.add(h01, t2)
+       h23 = i64x2.add(h23, t3)
+       h45 = i64x2.add(h45, t4)
+
+       // f[8]
+       t0 = i64x2.extmul_low_i32x4_s(f8, g0123)
+       t1 = i64x2.extmul_high_i32x4_s(f8, g0123_19)
+       t2 = i64x2.extmul_low_i32x4_s(f8, g4567_19)
+       t3 = i64x2.extmul_high_i32x4_s(f8, g4567_19)
+       t4 = i64x2.extmul_low_i32x4_s(f8, g89xx_19)
+
+       h89 = i64x2.add(h89, t0)
+       h01 = i64x2.add(h01, t1)
+       h23 = i64x2.add(h23, t2)
+       h45 = i64x2.add(h45, t3)
+       h67 = i64x2.add(h67, t4)
+
+       // f[9]
+       t0 = i64x2.extmul_low_i32x4_s(f9, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f9, g9012_19)
+       t2 = i64x2.extmul_low_i32x4_s(f9, g3456_19)
+       t3 = i64x2.extmul_high_i32x4_s(f9, g3456_19)
+       t4 = i64x2.extmul_low_i32x4_s(f9, g78xx_19)
+
+       h89 = i64x2.add(h89, t0)
+       h01 = i64x2.add(h01, t1)
+       h23 = i64x2.add(h23, t2)
+       h45 = i64x2.add(h45, t3)
+       h67 = i64x2.add(h67, t4)
+
+       // extract scalars from vectors
+       let h0: i64 = i64x2.extract_lane(h01, 0)
+       let h1: i64 = i64x2.extract_lane(h01, 1)
+       let h2: i64 = i64x2.extract_lane(h23, 0)
+       let h3: i64 = i64x2.extract_lane(h23, 1)
+       let h4: i64 = i64x2.extract_lane(h45, 0)
+       let h5: i64 = i64x2.extract_lane(h45, 1)
+       let h6: i64 = i64x2.extract_lane(h67, 0)
+       let h7: i64 = i64x2.extract_lane(h67, 1)
+       let h8: i64 = i64x2.extract_lane(h89, 0)
+       let h9: i64 = i64x2.extract_lane(h89, 1)
+
+       // reduce scalars with carry
+       const s25: i64 = <i64>1 << 25
+       const s24: i64 = <i64>1 << 24
+       const m26 = ~((<i64>1 << 26) - 1)
+       const m25 = ~((<i64>1 << 25) - 1)
+
+       let c: i64
+       let carry0: i64
+       let carry1: i64
+       let carry2: i64
+       let carry3: i64
+       let carry4: i64
+       let carry5: i64
+       let carry6: i64
+       let carry7: i64
+       let carry8: i64
+       let carry9: i64
+
+       c = h0 + s25
+       carry0 = c >> 26
+       h0 -= c & m26
+       h1 += carry0
+
+       c = h4 + s25
+       carry4 = c >> 26
+       h4 -= c & m26
+       h5 += carry4
+
+       c = h1 + s24
+       carry1 = c >> 25
+       h1 -= c & m25
+       h2 += carry1
+
+       c = h5 + s24
+       carry5 = c >> 25
+       h5 -= c & m25
+       h6 += carry5
+
+       c = h2 + s25
+       carry2 = c >> 26
+       h2 -= c & m26
+       h3 += carry2
+
+       c = h6 + s25
+       carry6 = c >> 26
+       h6 -= c & m26
+       h7 += carry6
+
+       c = h3 + s24
+       carry3 = c >> 25
+       h3 -= c & m25
+       h4 += carry3
+
+       c = h7 + s24
+       carry7 = c >> 25
+       h7 -= c & m25
+       h8 += carry7
+
+       c = h4 + s25
+       carry4 = c >> 26
+       h4 -= c & m26
+       h5 += carry4
+
+       c = h8 + s25
+       carry8 = c >> 26
+       h8 -= c & m26
+       h9 += carry8
+
+       c = h9 + s24
+       carry9 = c >> 25
+       h9 -= c & m25
+       h0 += carry9 * 19
+
+       c = h0 + s25
+       carry0 = c >> 26
+       h0 -= c & m26
+       h1 += carry0
+
+       // assign results to output
+       store<i32>(changetype<usize>(h), h0, 0)
+       store<i32>(changetype<usize>(h), h1, 4)
+       store<i32>(changetype<usize>(h), h2, 8)
+       store<i32>(changetype<usize>(h), h3, 12)
+       store<i32>(changetype<usize>(h), h4, 16)
+       store<i32>(changetype<usize>(h), h5, 20)
+       store<i32>(changetype<usize>(h), h6, 24)
+       store<i32>(changetype<usize>(h), h7, 28)
+       store<i32>(changetype<usize>(h), h8, 32)
+       store<i32>(changetype<usize>(h), h9, 36)
+}
+
+/**
+ * Multiply two FieldElements and store the product.
+ *
+ * @param h FieldElement product destination
+ * @param f FieldElement multiplicand source
+ * @param g FieldElement multiplicand source
+ */
+export function fe_mul2 (h: FieldElement, f: FieldElement, g: FieldElement): void {
+       const f0123: v128 = v128.load(changetype<usize>(f), 0 << 4)
+       const f4567: v128 = v128.load(changetype<usize>(f), 1 << 4)
+       const f89xx: v128 = v128.load64_zero(changetype<usize>(f), 2 << 4)
+
+       const f0123_2: v128 = v128.add<i32>(f0123, f0123)
+       const f4567_2: v128 = v128.add<i32>(f4567, f4567)
+       const f89xx_2: v128 = v128.add<i32>(f89xx, f89xx)
+
+       const f0: v128 = v128.shuffle<i32>(f0123, f0123, 0, 0, 0, 0)
+       const f1: v128 = v128.shuffle<i32>(f0123_2, f0123, 1, 5, 1, 5)
+       const f2: v128 = v128.shuffle<i32>(f0123, f0123, 2, 2, 2, 2)
+       const f3: v128 = v128.shuffle<i32>(f0123_2, f0123, 3, 7, 3, 7)
+       const f4: v128 = v128.shuffle<i32>(f4567, f4567, 0, 0, 0, 0)
+       const f5: v128 = v128.shuffle<i32>(f4567_2, f4567, 1, 5, 1, 5)
+       const f6: v128 = v128.shuffle<i32>(f4567, f4567, 2, 2, 2, 2)
+       const f7: v128 = v128.shuffle<i32>(f4567_2, f4567, 3, 7, 3, 7)
+       const f8: v128 = v128.shuffle<i32>(f89xx, f89xx, 0, 0, 0, 0)
+       const f9: v128 = v128.shuffle<i32>(f89xx_2, f89xx, 1, 5, 1, 5)
+
+       const g0123: v128 = v128.load(changetype<usize>(g), 0 << 4)
+       const g4567: v128 = v128.load(changetype<usize>(g), 1 << 4)
+       const g89xx: v128 = v128.load64_zero(changetype<usize>(g), 2 << 4)
+
+       const g9012: v128 = v128.shuffle<i32>(g89xx, g0123, 1, 4, 5, 6)
+       const g3456: v128 = v128.shuffle<i32>(g0123, g4567, 3, 4, 5, 6)
+       const g78xx: v128 = v128.shuffle<i32>(g4567, g89xx, 3, 4, 6, 7)
+
+       // factor of 19 for wrap from h9 to h0
+       const g0123_19: v128 = v128.add<i32>(g0123, v128.add<i32>(v128.add<i32>(g0123, g0123), v128.shl<i32>(g0123, 4)))
+       const g4567_19: v128 = v128.add<i32>(g4567, v128.add<i32>(v128.add<i32>(g4567, g4567), v128.shl<i32>(g4567, 4)))
+       const g89xx_19: v128 = v128.add<i32>(g89xx, v128.add<i32>(v128.add<i32>(g89xx, g89xx), v128.shl<i32>(g89xx, 4)))
+
+       const g9012_19: v128 = v128.bitselect(v128.add<i32>(g9012, v128.add<i32>(v128.add<i32>(g9012, g9012), v128.shl<i32>(g9012, 4))), g9012, i32x4(-1, 0, -1, -1))
+       const g3456_19: v128 = v128.add<i32>(g3456, v128.add<i32>(v128.add<i32>(g3456, g3456), v128.shl<i32>(g3456, 4)))
+       const g78xx_19: v128 = v128.add<i32>(g78xx, v128.add<i32>(v128.add<i32>(g78xx, g78xx), v128.shl<i32>(g78xx, 4)))
+
+       // f[0]
+       let h01: v128 = i64x2.extmul_low_i32x4_s(f0, g0123)
+       let h23: v128 = i64x2.extmul_high_i32x4_s(f0, g0123)
+       let h45: v128 = i64x2.extmul_low_i32x4_s(f0, g4567)
+       let h67: v128 = i64x2.extmul_high_i32x4_s(f0, g4567)
+       let h89: v128 = i64x2.extmul_low_i32x4_s(f0, g89xx)
+
+       // f[1]
+       let t0: v128 = i64x2.extmul_low_i32x4_s(f1, g9012_19)
+       let t1: v128 = i64x2.extmul_high_i32x4_s(f1, g9012)
+       let t2: v128 = i64x2.extmul_low_i32x4_s(f1, g3456)
+       let t3: v128 = i64x2.extmul_high_i32x4_s(f1, g3456)
+       let t4: v128 = i64x2.extmul_low_i32x4_s(f1, g78xx)
+
+       h01 = i64x2.add(h01, t0)
+       h23 = i64x2.add(h23, t1)
+       h45 = i64x2.add(h45, t2)
+       h67 = i64x2.add(h67, t3)
+       h89 = i64x2.add(h89, t4)
+
+       // f[2]
+       t0 = i64x2.extmul_low_i32x4_s(f2, g0123)
+       t1 = i64x2.extmul_high_i32x4_s(f2, g0123)
+       t2 = i64x2.extmul_low_i32x4_s(f2, g4567)
+       t3 = i64x2.extmul_high_i32x4_s(f2, g4567)
+       t4 = i64x2.extmul_low_i32x4_s(f2, g89xx_19)
+
+       h23 = i64x2.add(h23, t0)
+       h45 = i64x2.add(h45, t1)
+       h67 = i64x2.add(h67, t2)
+       h89 = i64x2.add(h89, t3)
+       h01 = i64x2.add(h01, t4)
+
+       // f[3]
+       t0 = i64x2.extmul_low_i32x4_s(f3, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f3, g9012)
+       t2 = i64x2.extmul_low_i32x4_s(f3, g3456)
+       t3 = i64x2.extmul_high_i32x4_s(f3, g3456)
+       t4 = i64x2.extmul_low_i32x4_s(f3, g78xx_19)
+
+       h23 = i64x2.add(h23, t0)
+       h45 = i64x2.add(h45, t1)
+       h67 = i64x2.add(h67, t2)
+       h89 = i64x2.add(h89, t3)
+       h01 = i64x2.add(h01, t4)
+
+       // f[4]
+       t0 = i64x2.extmul_low_i32x4_s(f4, g0123)
+       t1 = i64x2.extmul_high_i32x4_s(f4, g0123)
+       t2 = i64x2.extmul_low_i32x4_s(f4, g4567)
+       t3 = i64x2.extmul_high_i32x4_s(f4, g4567_19)
+       t4 = i64x2.extmul_low_i32x4_s(f4, g89xx_19)
+
+       h45 = i64x2.add(h45, t0)
+       h67 = i64x2.add(h67, t1)
+       h89 = i64x2.add(h89, t2)
+       h01 = i64x2.add(h01, t3)
+       h23 = i64x2.add(h23, t4)
+
+       // f[5]
+       t0 = i64x2.extmul_low_i32x4_s(f5, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f5, g9012)
+       t2 = i64x2.extmul_low_i32x4_s(f5, g3456)
+       t3 = i64x2.extmul_high_i32x4_s(f5, g3456_19)
+       t4 = i64x2.extmul_low_i32x4_s(f5, g78xx_19)
+
+       h45 = i64x2.add(h45, t0)
+       h67 = i64x2.add(h67, t1)
+       h89 = i64x2.add(h89, t2)
+       h01 = i64x2.add(h01, t3)
+       h23 = i64x2.add(h23, t4)
+
+       // f[6]
+       t0 = i64x2.extmul_low_i32x4_s(f6, g0123)
+       t1 = i64x2.extmul_high_i32x4_s(f6, g0123)
+       t2 = i64x2.extmul_low_i32x4_s(f6, g4567_19)
+       t3 = i64x2.extmul_high_i32x4_s(f6, g4567_19)
+       t4 = i64x2.extmul_low_i32x4_s(f6, g89xx_19)
+
+       h67 = i64x2.add(h67, t0)
+       h89 = i64x2.add(h89, t1)
+       h01 = i64x2.add(h01, t2)
+       h23 = i64x2.add(h23, t3)
+       h45 = i64x2.add(h45, t4)
+
+       // f[7]
+       t0 = i64x2.extmul_low_i32x4_s(f7, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f7, g9012)
+       t2 = i64x2.extmul_low_i32x4_s(f7, g3456_19)
+       t3 = i64x2.extmul_high_i32x4_s(f7, g3456_19)
+       t4 = i64x2.extmul_low_i32x4_s(f7, g78xx_19)
+
+       h67 = i64x2.add(h67, t0)
+       h89 = i64x2.add(h89, t1)
+       h01 = i64x2.add(h01, t2)
+       h23 = i64x2.add(h23, t3)
+       h45 = i64x2.add(h45, t4)
+
+       // f[8]
+       t0 = i64x2.extmul_low_i32x4_s(f8, g0123)
+       t1 = i64x2.extmul_high_i32x4_s(f8, g0123_19)
+       t2 = i64x2.extmul_low_i32x4_s(f8, g4567_19)
+       t3 = i64x2.extmul_high_i32x4_s(f8, g4567_19)
+       t4 = i64x2.extmul_low_i32x4_s(f8, g89xx_19)
+
+       h89 = i64x2.add(h89, t0)
+       h01 = i64x2.add(h01, t1)
+       h23 = i64x2.add(h23, t2)
+       h45 = i64x2.add(h45, t3)
+       h67 = i64x2.add(h67, t4)
+
+       // f[9]
+       t0 = i64x2.extmul_low_i32x4_s(f9, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f9, g9012_19)
+       t2 = i64x2.extmul_low_i32x4_s(f9, g3456_19)
+       t3 = i64x2.extmul_high_i32x4_s(f9, g3456_19)
+       t4 = i64x2.extmul_low_i32x4_s(f9, g78xx_19)
+
+       h89 = i64x2.add(h89, t0)
+       h01 = i64x2.add(h01, t1)
+       h23 = i64x2.add(h23, t2)
+       h45 = i64x2.add(h45, t3)
+       h67 = i64x2.add(h67, t4)
+
+       // extract scalars from vectors and double
+       let h0: i64 = i64x2.extract_lane(h01, 0)
+       let h1: i64 = i64x2.extract_lane(h01, 1)
+       let h2: i64 = i64x2.extract_lane(h23, 0)
+       let h3: i64 = i64x2.extract_lane(h23, 1)
+       let h4: i64 = i64x2.extract_lane(h45, 0)
+       let h5: i64 = i64x2.extract_lane(h45, 1)
+       let h6: i64 = i64x2.extract_lane(h67, 0)
+       let h7: i64 = i64x2.extract_lane(h67, 1)
+       let h8: i64 = i64x2.extract_lane(h89, 0)
+       let h9: i64 = i64x2.extract_lane(h89, 1)
+
+       h0 += h0
+       h1 += h1
+       h2 += h2
+       h3 += h3
+       h4 += h4
+       h5 += h5
+       h6 += h6
+       h7 += h7
+       h8 += h8
+       h9 += h9
+
+       // reduce scalars with carry
+       const s25: i64 = <i64>1 << 25
+       const s24: i64 = <i64>1 << 24
+       const m26 = ~((<i64>1 << 26) - 1)
+       const m25 = ~((<i64>1 << 25) - 1)
+
+       let c: i64
+       let carry0: i64
+       let carry1: i64
+       let carry2: i64
+       let carry3: i64
+       let carry4: i64
+       let carry5: i64
+       let carry6: i64
+       let carry7: i64
+       let carry8: i64
+       let carry9: i64
+
+       c = h0 + s25
+       carry0 = c >> 26
+       h0 -= c & m26
+       h1 += carry0
+
+       c = h4 + s25
+       carry4 = c >> 26
+       h4 -= c & m26
+       h5 += carry4
+
+       c = h1 + s24
+       carry1 = c >> 25
+       h1 -= c & m25
+       h2 += carry1
+
+       c = h5 + s24
+       carry5 = c >> 25
+       h5 -= c & m25
+       h6 += carry5
+
+       c = h2 + s25
+       carry2 = c >> 26
+       h2 -= c & m26
+       h3 += carry2
+
+       c = h6 + s25
+       carry6 = c >> 26
+       h6 -= c & m26
+       h7 += carry6
+
+       c = h3 + s24
+       carry3 = c >> 25
+       h3 -= c & m25
+       h4 += carry3
+
+       c = h7 + s24
+       carry7 = c >> 25
+       h7 -= c & m25
+       h8 += carry7
+
+       c = h4 + s25
+       carry4 = c >> 26
+       h4 -= c & m26
+       h5 += carry4
+
+       c = h8 + s25
+       carry8 = c >> 26
+       h8 -= c & m26
+       h9 += carry8
+
+       c = h9 + s24
+       carry9 = c >> 25
+       h9 -= c & m25
+       h0 += carry9 * 19
+
+       c = h0 + s25
+       carry0 = c >> 26
+       h0 -= c & m26
+       h1 += carry0
+
+       // assign results to output
+       store<i32>(changetype<usize>(h), h0, 0)
+       store<i32>(changetype<usize>(h), h1, 4)
+       store<i32>(changetype<usize>(h), h2, 8)
+       store<i32>(changetype<usize>(h), h3, 12)
+       store<i32>(changetype<usize>(h), h4, 16)
+       store<i32>(changetype<usize>(h), h5, 20)
+       store<i32>(changetype<usize>(h), h6, 24)
+       store<i32>(changetype<usize>(h), h7, 28)
+       store<i32>(changetype<usize>(h), h8, 32)
+       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 {
+       // pack each f index twice
+       const f_a_0123: v128 = v128.load(changetype<usize>(f_a), 0 << 4)
+       const f_a_4567: v128 = v128.load(changetype<usize>(f_a), 1 << 4)
+       const f_a_89xx: v128 = v128.load64_zero(changetype<usize>(f_a), 2 << 4)
+
+       const f_b_0123: v128 = v128.load(changetype<usize>(f_b), 0 << 4)
+       const f_b_4567: v128 = v128.load(changetype<usize>(f_b), 1 << 4)
+       const f_b_89xx: v128 = v128.load64_zero(changetype<usize>(f_b), 2 << 4)
+
+       const f01: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 0, 4, 1, 5)
+       const f23: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 2, 6, 3, 7)
+       const f45: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 0, 4, 1, 5)
+       const f67: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 2, 6, 3, 7)
+       const f89: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 0, 4, 1, 5)
+
+       const f0: v128 = v128.shuffle<i32>(f01, f01, 0, 1, 0, 1)
+       const f1: v128 = v128.shuffle<i32>(f01, v128.add<i32>(f01, f01), 2, 3, 6, 7)
+       const f2: v128 = v128.shuffle<i32>(f23, f23, 0, 1, 0, 1)
+       const f3: v128 = v128.shuffle<i32>(f23, v128.add<i32>(f23, f23), 2, 3, 6, 7)
+       const f4: v128 = v128.shuffle<i32>(f45, f45, 0, 1, 0, 1)
+       const f5: v128 = v128.shuffle<i32>(f45, v128.add<i32>(f45, f45), 2, 3, 6, 7)
+       const f6: v128 = v128.shuffle<i32>(f67, f67, 0, 1, 0, 1)
+       const f7: v128 = v128.shuffle<i32>(f67, v128.add<i32>(f67, f67), 2, 3, 6, 7)
+       const f8: v128 = v128.shuffle<i32>(f89, f89, 0, 1, 0, 1)
+       const f9: v128 = v128.shuffle<i32>(f89, v128.add<i32>(f89, f89), 2, 3, 6, 7)
+
+       // pack each g index once
+       const g_a_0123: v128 = v128.load(changetype<usize>(g_a), 0 << 4)
+       const g_a_4567: v128 = v128.load(changetype<usize>(g_a), 1 << 4)
+       const g_a_89xx: v128 = v128.load64_zero(changetype<usize>(g_a), 2 << 4)
+
+       const g_b_0123: v128 = v128.load(changetype<usize>(g_b), 0 << 4)
+       const g_b_4567: v128 = v128.load(changetype<usize>(g_b), 1 << 4)
+       const g_b_89xx: v128 = v128.load64_zero(changetype<usize>(g_b), 2 << 4)
+
+       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 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 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 g01_19: v128 = v128.add<i32>(g01, v128.add<i32>(v128.add<i32>(g01, g01), v128.shl<i32>(g01, 4)))
+       const g23_19: v128 = v128.add<i32>(g23, v128.add<i32>(v128.add<i32>(g23, g23), v128.shl<i32>(g23, 4)))
+       const g45_19: v128 = v128.add<i32>(g45, v128.add<i32>(v128.add<i32>(g45, g45), v128.shl<i32>(g45, 4)))
+       const g67_19: v128 = v128.add<i32>(g67, v128.add<i32>(v128.add<i32>(g67, g67), v128.shl<i32>(g67, 4)))
+       const g89_19: v128 = v128.add<i32>(g89, v128.add<i32>(v128.add<i32>(g89, g89), v128.shl<i32>(g89, 4)))
+
+       // 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]
+       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]
+       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]
+       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]
+       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]
-       t0 = i64x2.extmul_low_i32x4_s(f8, g0123)
-       t1 = i64x2.extmul_high_i32x4_s(f8, g0123_19)
-       t2 = i64x2.extmul_low_i32x4_s(f8, g4567_19)
-       t3 = i64x2.extmul_high_i32x4_s(f8, g4567_19)
-       t4 = i64x2.extmul_low_i32x4_s(f8, g89xx_19)
-
-       h89 = i64x2.add(h89, t0)
-       h01 = i64x2.add(h01, t1)
-       h23 = i64x2.add(h23, t2)
-       h45 = i64x2.add(h45, t3)
-       h67 = i64x2.add(h67, t4)
+       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]
-       t0 = i64x2.extmul_low_i32x4_s(f9, g9012_19)
-       t1 = i64x2.extmul_high_i32x4_s(f9, g9012_19)
-       t2 = i64x2.extmul_low_i32x4_s(f9, g3456_19)
-       t3 = i64x2.extmul_high_i32x4_s(f9, g3456_19)
-       t4 = i64x2.extmul_low_i32x4_s(f9, g78xx_19)
+       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))
 
-       h89 = i64x2.add(h89, t0)
-       h01 = i64x2.add(h01, t1)
-       h23 = i64x2.add(h23, t2)
-       h45 = i64x2.add(h45, t3)
-       h67 = i64x2.add(h67, t4)
+       // reduce scalars with carry
+       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
 
-       // extract scalars from vectors
-       let h0: i64 = i64x2.extract_lane(h01, 0)
-       let h1: i64 = i64x2.extract_lane(h01, 1)
-       let h2: i64 = i64x2.extract_lane(h23, 0)
-       let h3: i64 = i64x2.extract_lane(h23, 1)
-       let h4: i64 = i64x2.extract_lane(h45, 0)
-       let h5: i64 = i64x2.extract_lane(h45, 1)
-       let h6: i64 = i64x2.extract_lane(h67, 0)
-       let h7: i64 = i64x2.extract_lane(h67, 1)
-       let h8: i64 = i64x2.extract_lane(h89, 0)
-       let h9: i64 = i64x2.extract_lane(h89, 1)
+       // Signed i64x2 shifts are emulated on x64, unsigned ones are native. Adding
+       // 2⁶² keeps every sum non-negative (|h| ≤ 2⁶⁰·⁴), and the unsigned shift then
+       // floors in fewer instructions. The bias is removed by subtraction, and the
+       // value to remove is the bias minus the original size: 2⁶²⁻²⁶ = 2³⁶ (or
+       // 2⁶²⁻²⁵ = 2³⁷). After unbiasing, the result is the signed carry.
+       const bias24 = v128.splat<i64>((<i64>1 << 24) + (<i64>1 << 62))
+       const bias25 = v128.splat<i64>((<i64>1 << 25) + (<i64>1 << 62))
+       const unbias25 = v128.splat<i64>(<i64>1 << 37)
+       const unbias26 = v128.splat<i64>(<i64>1 << 36)
 
-       // reduce scalars with carry
-       const s25: i64 = <i64>1 << 25
-       const s24: i64 = <i64>1 << 24
-       const m26 = ~((<i64>1 << 26) - 1)
-       const m25 = ~((<i64>1 << 25) - 1)
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias26)
+       h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+       h1 = v128.add<i64>(h1, carry0)
 
-       let c: i64
-       let carry0: i64
-       let carry1: i64
-       let carry2: i64
-       let carry3: i64
-       let carry4: i64
-       let carry5: i64
-       let carry6: i64
-       let carry7: i64
-       let carry8: i64
-       let carry9: i64
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias26)
+       h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+       h5 = v128.add<i64>(h5, carry4)
 
-       c = h0 + s25
-       carry0 = c >> 26
-       h0 -= c & m26
-       h1 += carry0
+       carry1 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h1, bias24), 25), unbias25)
+       h1 = v128.sub<i64>(h1, v128.shl<i64>(carry1, 25))
+       h2 = v128.add<i64>(h2, carry1)
 
-       c = h4 + s25
-       carry4 = c >> 26
-       h4 -= c & m26
-       h5 += carry4
+       carry5 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h5, bias24), 25), unbias25)
+       h5 = v128.sub<i64>(h5, v128.shl<i64>(carry5, 25))
+       h6 = v128.add<i64>(h6, carry5)
 
-       c = h1 + s24
-       carry1 = c >> 25
-       h1 -= c & m25
-       h2 += carry1
+       carry2 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h2, bias25), 26), unbias26)
+       h2 = v128.sub<i64>(h2, v128.shl<i64>(carry2, 26))
+       h3 = v128.add<i64>(h3, carry2)
 
-       c = h5 + s24
-       carry5 = c >> 25
-       h5 -= c & m25
-       h6 += carry5
+       carry6 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h6, bias25), 26), unbias26)
+       h6 = v128.sub<i64>(h6, v128.shl<i64>(carry6, 26))
+       h7 = v128.add<i64>(h7, carry6)
 
-       c = h2 + s25
-       carry2 = c >> 26
-       h2 -= c & m26
-       h3 += carry2
+       carry3 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h3, bias24), 25), unbias25)
+       h3 = v128.sub<i64>(h3, v128.shl<i64>(carry3, 25))
+       h4 = v128.add<i64>(h4, carry3)
 
-       c = h6 + s25
-       carry6 = c >> 26
-       h6 -= c & m26
-       h7 += carry6
+       carry7 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h7, bias24), 25), unbias25)
+       h7 = v128.sub<i64>(h7, v128.shl<i64>(carry7, 25))
+       h8 = v128.add<i64>(h8, carry7)
 
-       c = h3 + s24
-       carry3 = c >> 25
-       h3 -= c & m25
-       h4 += carry3
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias26)
+       h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+       h5 = v128.add<i64>(h5, carry4)
 
-       c = h7 + s24
-       carry7 = c >> 25
-       h7 -= c & m25
-       h8 += carry7
+       carry8 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h8, bias25), 26), unbias26)
+       h8 = v128.sub<i64>(h8, v128.shl<i64>(carry8, 26))
+       h9 = v128.add<i64>(h9, carry8)
 
-       c = h4 + s25
-       carry4 = c >> 26
-       h4 -= c & m26
-       h5 += carry4
+       carry9 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h9, bias24), 25), unbias25)
+       h9 = v128.sub<i64>(h9, v128.shl<i64>(carry9, 25))
+       h0 = v128.add<i64>(h0, v128.add<i64>(carry9, v128.add<i64>(v128.add<i64>(carry9, carry9), v128.shl<i64>(carry9, 4))))
 
-       c = h8 + s25
-       carry8 = c >> 26
-       h8 -= c & m26
-       h9 += carry8
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias26)
+       h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+       h1 = v128.add<i64>(h1, carry0)
 
-       c = h9 + s24
-       carry9 = c >> 25
-       h9 -= c & m25
-       h0 += carry9 * 19
+       // reduce i64x2 to i32x4 now that only low half has a value and fits into i32
+       const h01: v128 = v128.shuffle<i32>(h0, h1, 0, 2, 4, 6) // h0a, h0b, h1a, h1b
+       const h23: v128 = v128.shuffle<i32>(h2, h3, 0, 2, 4, 6) // h2a, h2b ,h3a, h3b
+       const h45: v128 = v128.shuffle<i32>(h4, h5, 0, 2, 4, 6) // h4a, h4b, h5a, h5b
+       const h67: v128 = v128.shuffle<i32>(h6, h7, 0, 2, 4, 6) // h6a, h6b, h7a, h7b
+       const h89: v128 = v128.shuffle<i32>(h8, h9, 0, 2, 4, 6) // h8a, h8b, h9a, h9b
 
-       c = h0 + s25
-       carry0 = c >> 26
-       h0 -= c & m26
-       h1 += carry0
+       // assign results to parallel outputs
+       v128.store(changetype<usize>(h_a), v128.shuffle<i32>(h01, h23, 0, 2, 4, 6), 0 << 4)
+       v128.store(changetype<usize>(h_a), v128.shuffle<i32>(h45, h67, 0, 2, 4, 6), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(h_a), v128.shuffle<i32>(h89, h89, 0, 2, 4, 6), 0, 2 << 4)
 
-       // assign results to output
-       store<i32>(changetype<usize>(h), h0, 0)
-       store<i32>(changetype<usize>(h), h1, 4)
-       store<i32>(changetype<usize>(h), h2, 8)
-       store<i32>(changetype<usize>(h), h3, 12)
-       store<i32>(changetype<usize>(h), h4, 16)
-       store<i32>(changetype<usize>(h), h5, 20)
-       store<i32>(changetype<usize>(h), h6, 24)
-       store<i32>(changetype<usize>(h), h7, 28)
-       store<i32>(changetype<usize>(h), h8, 32)
-       store<i32>(changetype<usize>(h), h9, 36)
+       v128.store(changetype<usize>(h_b), v128.shuffle<i32>(h01, h23, 1, 3, 5, 7), 0 << 4)
+       v128.store(changetype<usize>(h_b), v128.shuffle<i32>(h45, h67, 1, 3, 5, 7), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(h_b), v128.shuffle<i32>(h89, h89, 1, 3, 5, 7), 0, 2 << 4)
 }
 
 /**
@@ -564,7 +1143,7 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
  * @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 {
+export function fe_mul2_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldElement, h_b: FieldElement, f_b: FieldElement, g_b: FieldElement): void {
        // pack each f index twice
        const f_a_0123: v128 = v128.load(changetype<usize>(f_a), 0 << 4)
        const f_a_4567: v128 = v128.load(changetype<usize>(f_a), 1 << 4)
@@ -733,6 +1312,19 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f9, g89_19))
        h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f9, g89_19))
 
+       // double second field element only
+       const m = i64x2(0, -1)
+       h0 = v128.add<i64>(h0, v128.and(h0, m))
+       h1 = v128.add<i64>(h1, v128.and(h1, m))
+       h2 = v128.add<i64>(h2, v128.and(h2, m))
+       h3 = v128.add<i64>(h3, v128.and(h3, m))
+       h4 = v128.add<i64>(h4, v128.and(h4, m))
+       h5 = v128.add<i64>(h5, v128.and(h5, m))
+       h6 = v128.add<i64>(h6, v128.and(h6, m))
+       h7 = v128.add<i64>(h7, v128.and(h7, m))
+       h8 = v128.add<i64>(h8, v128.and(h8, m))
+       h9 = v128.add<i64>(h9, v128.and(h9, m))
+
        // reduce scalars with carry
        let carry0: v128
        let carry1: v128
@@ -1664,12 +2256,50 @@ export function fe_sq2_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldEle
  * @param g FieldElement subtrahend source
  */
 export function fe_sub (h: FieldElement, f: FieldElement, g: FieldElement): void {
-       const f0 = v128.load(changetype<usize>(f), 0 << 4), f1 = v128.load(changetype<usize>(f), 1 << 4), f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
-       const g0 = v128.load(changetype<usize>(g), 0 << 4), g1 = v128.load(changetype<usize>(g), 1 << 4), g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
-       const h0 = v128.sub<i32>(f0, g0), h1 = v128.sub<i32>(f1, g1), h2 = v128.sub<i32>(f2, g2)
-       v128.store(changetype<usize>(h), h0, 0 << 4)
-       v128.store(changetype<usize>(h), h1, 1 << 4)
-       v128.store_lane<i64>(changetype<usize>(h), h2, 0, 2 << 4)
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
+
+       v128.store(changetype<usize>(h), v128.sub<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(h), v128.sub<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(h), v128.sub<i32>(f2, g2), 0, 2 << 4)
+}
+
+/**
+ * Subtract and add two FieldElements and store the result.
+ *
+ * @param h FieldElement difference destination
+ * @param f FieldElement minuend source
+ * @param g FieldElement subtrahend source
+ */
+
+/**
+ * Subtract and add two FieldElements without a temporary and store the results.
+ *
+ * `f = f - g` and `g = f + g`
+ *
+ * @param f FieldElement difference destination, summand source, and subtrahend source
+ * @param g FieldElement sum destination, summand source, and minuend source
+ */
+export function fe_subadd (f: FieldElement, g: FieldElement): void {
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
+
+       v128.store(changetype<usize>(f), v128.sub<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(f), v128.sub<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(f), v128.sub<i32>(f2, g2), 0, 2 << 4)
+       v128.store(changetype<usize>(g), v128.add<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(g), v128.add<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(g), v128.add<i32>(f2, g2), 0, 2 << 4)
 }
 
 /**
index 05197297c493731da1418054ce5dfdd46713f59f..e45ec8a69daf0a41c204f1f53208f2146ee3c816 100644 (file)
@@ -2,7 +2,7 @@
 //! 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_mul_dual, fe_neg, fe_sq2_dual, fe_sq_dual, fe_sub, fe_tobytes } from './fe'
+import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul2_dual, fe_mul_dual, fe_neg, fe_sq2_dual, fe_sq_dual, fe_sub, fe_subadd, fe_tobytes } from './fe'
 import { gepair_p1p1_to_p3 } from './p2'
 
 /**
@@ -183,13 +183,10 @@ export function ge_p3_dbl (r: ge_p1p1, p: ge_p3, t: FieldElement): void {
 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_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)
-       fe_add(r.Z, t, r.T)
-       fe_sub(r.T, t, r.T)
+       fe_mul_dual(r.X, r.X, q.YplusX, r.Y, r.Y, q.YminusX)
+       fe_mul2_dual(r.T, q.T2d, p.T, r.Z, p.Z, q.Z)
+       fe_subadd(r.X, r.Y)
+       fe_addsub(r.Z, r.T)
 }
 
 /**