]> git.codecow.com Git - nano25519.git/commitdiff
Initial FieldElementPair draft.
authorChris Duncan <chris@codecow.com>
Thu, 22 Oct 2026 22:33:06 +0000 (15:33 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 22 Oct 2026 22:33:06 +0000 (15:33 -0700)
src/assembly/ed25519/fep.ts [new file with mode: 0644]

diff --git a/src/assembly/ed25519/fep.ts b/src/assembly/ed25519/fep.ts
new file mode 100644 (file)
index 0000000..c5a72f8
--- /dev/null
@@ -0,0 +1,1770 @@
+//! SPDX-FileCopyrightText: 2013-2026 Frank Denis <j@pureftpd.org>
+//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
+//! SPDX-License-Identifier: ISC
+
+/**
+ * Arithmetic on field elements. These are integers in the range
+ *
+ * `0 ≤ n < p`
+ *
+ * where
+ *
+ * `p = 2²⁵⁵-19`
+ *
+ * They are represented as 10 limbs of alternating 25- and 26-bit values so that
+ *
+ * `n = 2⁰a[0] + 2²⁶a[1] + 2⁷⁷a[2] ... + 2²³⁰a[9]`
+ */
+import { load_3, load_4 } from './utils'
+
+/**
+ * 10x 25- and 26-bit array of values representing a large integer `n mod p`. In
+ * this implementation, 20 elements are allocated, 10 for each of two distinct
+ * FieldElement arrays. The FieldElement alignment issues described in `fe.ts`
+ * are not a problem here because 20 elements can be loaded by exactly 5 calls
+ * to `v128.load()`.
+ */
+export type FieldElementPair = StaticArray<i32>
+export function fep (): FieldElementPair {
+       return new StaticArray<i32>(20)
+}
+
+/**
+ * Set all values of a FieldElementPair to zero.
+ *
+ * @param h FieldElementPair destination
+ */
+export function fep_0 (h: FieldElementPair): void {
+       memory.fill(changetype<usize>(h), 0, 80)
+}
+
+/**
+ * Set the first limb of each FieldElement in the pair to 1 and all subsequent
+ * limbs to zero.
+ *
+ * @param h FieldElementPair destination
+ */
+export function fep_1 (h: FieldElementPair): void {
+       fep_0(h)
+       store<i64>(changetype<usize>(h), (<i64>1 << 32) | 1, 0)
+}
+
+/**
+ * Add two FieldElement pairs and store the sum.
+ *
+ * @param h FieldElementPair sum destination
+ * @param f FieldElementPair summand source
+ * @param g FieldElementPair summand source
+ */
+export function fep_add (h: FieldElementPair, f: FieldElementPair, g: FieldElementPair): void {
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load(changetype<usize>(f), 2 << 4)
+       const f3 = v128.load(changetype<usize>(f), 3 << 4)
+       const f4 = v128.load(changetype<usize>(f), 4 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load(changetype<usize>(g), 2 << 4)
+       const g3 = v128.load(changetype<usize>(g), 3 << 4)
+       const g4 = v128.load(changetype<usize>(g), 4 << 4)
+
+       const h0 = v128.add<i32>(f0, g0)
+       const h1 = v128.add<i32>(f1, g1)
+       const h2 = v128.add<i32>(f2, g2)
+       const h3 = v128.add<i32>(f3, g3)
+       const h4 = v128.add<i32>(f4, g4)
+
+       v128.store(changetype<usize>(h), h0, 0 << 4)
+       v128.store(changetype<usize>(h), h1, 1 << 4)
+       v128.store(changetype<usize>(h), h2, 2 << 4)
+       v128.store(changetype<usize>(h), h3, 3 << 4)
+       v128.store(changetype<usize>(h), h4, 4 << 4)
+}
+
+/**
+ * Conditionally copy values from one FieldElementPair to another.
+ *
+ * @param {FieldElementPair} f destination
+ * @param {FieldElementPair} g source
+ * @param {u64} b Copy `g` into `f` if 1, copy `f` into `f` if 0
+ */
+export function fep_cmov (f: FieldElementPair, g: FieldElementPair, b: u64): void {
+       const c = v128.splat<u64>(0 - b)
+
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load(changetype<usize>(f), 2 << 4)
+       const f3 = v128.load(changetype<usize>(f), 3 << 4)
+       const f4 = v128.load(changetype<usize>(f), 4 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load(changetype<usize>(g), 2 << 4)
+       const g3 = v128.load(changetype<usize>(g), 3 << 4)
+       const g4 = v128.load(changetype<usize>(g), 4 << 4)
+
+       const h0 = v128.bitselect(g0, f0, c)
+       const h1 = v128.bitselect(g1, f1, c)
+       const h2 = v128.bitselect(g2, f2, c)
+       const h3 = v128.bitselect(g3, f3, c)
+       const h4 = v128.bitselect(g4, f4, c)
+
+       v128.store(changetype<usize>(f), h0, 0 << 4)
+       v128.store(changetype<usize>(f), h1, 1 << 4)
+       v128.store(changetype<usize>(f), h2, 2 << 4)
+       v128.store(changetype<usize>(f), h3, 3 << 4)
+       v128.store(changetype<usize>(f), h4, 4 << 4)
+}
+
+/**
+ * Copy values from one FieldElementPair to another.
+ *
+ * @param {FieldElementPair} h destination
+ * @param {FieldElementPair} f source
+ */
+export function fep_copy (h: FieldElementPair, f: FieldElementPair): void {
+       memory.copy(changetype<usize>(h), changetype<usize>(f), 80)
+}
+
+/**
+ * Add a FieldElementPair to itself and store the sum.
+ *
+ * @param h FieldElementPair sum destination
+ * @param f FieldElementPair summand source
+ */
+export function fep_dbl (h: FieldElementPair, f: FieldElementPair): void {
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load(changetype<usize>(f), 2 << 4)
+       const f3 = v128.load(changetype<usize>(f), 3 << 4)
+       const f4 = v128.load(changetype<usize>(f), 4 << 4)
+
+       const h0 = v128.add<i32>(f0, f0)
+       const h1 = v128.add<i32>(f1, f1)
+       const h2 = v128.add<i32>(f2, f2)
+       const h3 = v128.add<i32>(f3, f3)
+       const h4 = v128.add<i32>(f4, f4)
+
+       v128.store(changetype<usize>(h), h0, 0 << 4)
+       v128.store(changetype<usize>(h), h1, 1 << 4)
+       v128.store(changetype<usize>(h), h2, 2 << 4)
+       v128.store(changetype<usize>(h), h3, 3 << 4)
+       v128.store(changetype<usize>(h), h4, 4 << 4)
+}
+
+/**
+ * Decompress bytes to a FieldElementPair. Ignores top bit of s.
+ */
+// export function fep_frombytes (h: FieldElementPair, s: StaticArray<u8>): void {
+//     let h0: i64 = load_4(s, 0)
+//     let h1: i64 = load_3(s, 4) << 6
+//     let h2: i64 = load_3(s, 7) << 5
+//     let h3: i64 = load_3(s, 10) << 3
+//     let h4: i64 = load_3(s, 13) << 2
+//     let h5: i64 = load_4(s, 16)
+//     let h6: i64 = load_3(s, 20) << 7
+//     let h7: i64 = load_3(s, 23) << 5
+//     let h8: i64 = load_3(s, 26) << 4
+//     let h9: i64 = (load_3(s, 29) & 8388607) << 2
+
+//     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
+
+//     // odd limbs
+//     c = h9 + s24
+//     carry9 = c >> 25
+//     h9 -= c & m25
+//     h0 += carry9 * 19
+
+//     c = h1 + s24
+//     carry1 = c >> 25
+//     h1 -= c & m25
+//     h2 += carry1
+
+//     c = h3 + s24
+//     carry3 = c >> 25
+//     h3 -= c & m25
+//     h4 += carry3
+
+//     c = h5 + s24
+//     carry5 = c >> 25
+//     h5 -= c & m25
+//     h6 += carry5
+
+//     c = h7 + s24
+//     carry7 = c >> 25
+//     h7 -= c & m25
+//     h8 += carry7
+
+//     // even limbs
+//     c = h0 + s25
+//     carry0 = c >> 26
+//     h0 -= c & m26
+//     h1 += carry0
+
+//     c = h2 + s25
+//     carry2 = c >> 26
+//     h2 -= c & m26
+//     h3 += carry2
+
+//     c = h4 + s25
+//     carry4 = c >> 26
+//     h4 -= c & m26
+//     h5 += carry4
+
+//     c = h6 + s25
+//     carry6 = c >> 26
+//     h6 -= c & m26
+//     h7 += carry6
+
+//     c = h8 + s25
+//     carry8 = c >> 26
+//     h8 -= c & m26
+//     h9 += carry8
+
+//     store<i32>(changetype<usize>(h), h0, 0 << 2)
+//     store<i32>(changetype<usize>(h), h1, 1 << 2)
+//     store<i32>(changetype<usize>(h), h2, 2 << 2)
+//     store<i32>(changetype<usize>(h), h3, 3 << 2)
+//     store<i32>(changetype<usize>(h), h4, 4 << 2)
+//     store<i32>(changetype<usize>(h), h5, 5 << 2)
+//     store<i32>(changetype<usize>(h), h6, 6 << 2)
+//     store<i32>(changetype<usize>(h), h7, 7 << 2)
+//     store<i32>(changetype<usize>(h), h8, 8 << 2)
+//     store<i32>(changetype<usize>(h), h9, 9 << 2)
+// }
+
+/**
+ * Inversion - sets `out` = 0 if `z` == 0
+ */
+export function fep_invert (out: FieldElementPair, z: FieldElementPair, t_fep_0: FieldElementPair, t_fep_1: FieldElementPair, t_fep_2: FieldElementPair, t_fep_3: FieldElementPair): void {
+       fep_sq(t_fep_0, z)
+       fep_sq(t_fep_1, t_fep_0)
+       fep_sq(t_fep_1, t_fep_1)
+       fep_mul(t_fep_1, z, t_fep_1)
+       fep_mul(t_fep_0, t_fep_0, t_fep_1)
+       fep_sq(t_fep_2, t_fep_0)
+       fep_mul(t_fep_1, t_fep_1, t_fep_2)
+       fep_sq(t_fep_2, t_fep_1)
+       for (let i = 1; i < 5; ++i) {
+               fep_sq(t_fep_2, t_fep_2)
+       }
+       fep_mul(t_fep_1, t_fep_2, t_fep_1)
+       fep_sq(t_fep_2, t_fep_1)
+       for (let i = 1; i < 10; ++i) {
+               fep_sq(t_fep_2, t_fep_2)
+       }
+       fep_mul(t_fep_2, t_fep_2, t_fep_1)
+       fep_sq(t_fep_3, t_fep_2)
+       for (let i = 1; i < 20; ++i) {
+               fep_sq(t_fep_3, t_fep_3)
+       }
+       fep_mul(t_fep_2, t_fep_3, t_fep_2)
+       for (let i = 1; i < 11; ++i) {
+               fep_sq(t_fep_2, t_fep_2)
+       }
+       fep_mul(t_fep_1, t_fep_2, t_fep_1)
+       fep_sq(t_fep_2, t_fep_1)
+       for (let i = 1; i < 50; ++i) {
+               fep_sq(t_fep_2, t_fep_2)
+       }
+       fep_mul(t_fep_2, t_fep_2, t_fep_1)
+       fep_sq(t_fep_3, t_fep_2)
+       for (let i = 1; i < 100; ++i) {
+               fep_sq(t_fep_3, t_fep_3)
+       }
+       fep_mul(t_fep_2, t_fep_3, t_fep_2)
+       for (let i = 1; i < 51; ++i) {
+               fep_sq(t_fep_2, t_fep_2)
+       }
+       fep_mul(t_fep_1, t_fep_2, t_fep_1)
+       for (let i = 1; i < 6; ++i) {
+               fep_sq(t_fep_1, t_fep_1)
+       }
+       fep_mul(out, t_fep_1, t_fep_0)
+}
+
+/**
+ * return 1 if f is in {1,3,5,...,q-2}
+ * return 0 if f is in {0,2,4,...,q-1}
+ *
+ * Preconditions:
+ * |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
+ */
+// export function fep_isnegative (f: FieldElementPair, t: FieldElementPair): u8 {
+//     fep_reduce(t, f)
+//     return u8(load<i32>(changetype<usize>(t), 0) & 1)
+// }
+
+/**
+ * return 1 if f == 0
+ * return 0 if f != 0
+ *
+ * Preconditions:
+ * |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
+ */
+// export function fep_iszero (f: FieldElementPair, t_fe: FieldElementPair, t_u8x32: StaticArray<u8>): u8 {
+//     fep_tobytes(t_u8x32, f, t_fe)
+//     const t0 = v128.load(changetype<usize>(t_u8x32), 0 << 4)
+//     const t1 = v128.load(changetype<usize>(t_u8x32), 1 << 4)
+//     const d = v128.or(t0, t1)
+//     return u8(v128.any_true(d)) ^ 1
+// }
+
+/**
+ * Multiply two FieldElements and store the product.
+ *
+ * @param h FieldElementPair product destination
+ * @param f FieldElementPair multiplicand source
+ * @param g FieldElementPair multiplicand source
+ */
+export function fep_mul (h: FieldElementPair, f: FieldElementPair, g: FieldElementPair): void {
+       const f01: v128 = v128.load(changetype<usize>(f), 0 << 4)
+       const f23: v128 = v128.load(changetype<usize>(f), 1 << 4)
+       const f45: v128 = v128.load(changetype<usize>(f), 2 << 4)
+       const f67: v128 = v128.load(changetype<usize>(f), 3 << 4)
+       const f89: v128 = v128.load(changetype<usize>(f), 4 << 4)
+
+       const f0: v128 = v128.shuffle<i32>(f01, f01, 0, 1, 0, 1)
+       const f1: v128 = v128.shuffle<i32>(v128.add<i32>(f01, f01), f01, 2, 3, 6, 7)
+       const f2: v128 = v128.shuffle<i32>(f23, f23, 0, 1, 0, 1)
+       const f3: v128 = v128.shuffle<i32>(v128.add<i32>(f23, f23), f23, 2, 3, 6, 7)
+       const f4: v128 = v128.shuffle<i32>(f45, f45, 0, 1, 0, 1)
+       const f5: v128 = v128.shuffle<i32>(v128.add<i32>(f45, f45), f45, 2, 3, 6, 7)
+       const f6: v128 = v128.shuffle<i32>(f67, f67, 0, 1, 0, 1)
+       const f7: v128 = v128.shuffle<i32>(v128.add<i32>(f67, f67), f67, 2, 3, 6, 7)
+       const f8: v128 = v128.shuffle<i32>(f89, f89, 0, 1, 0, 1)
+       const f9: v128 = v128.shuffle<i32>(v128.add<i32>(f89, f89), f89, 2, 3, 6, 7)
+
+       const g01: v128 = v128.load(changetype<usize>(g), 0 << 4)
+       const g23: v128 = v128.load(changetype<usize>(g), 1 << 4)
+       const g45: v128 = v128.load(changetype<usize>(g), 2 << 4)
+       const g67: v128 = v128.load(changetype<usize>(g), 3 << 4)
+       const g89: v128 = v128.load(changetype<usize>(g), 4 << 4)
+
+       const g90: v128 = v128.shuffle<i32>(g89, g01, 2, 3, 4, 5)
+       const g12: v128 = v128.shuffle<i32>(g01, g23, 2, 3, 4, 5)
+       const g34: v128 = v128.shuffle<i32>(g23, g45, 2, 3, 4, 5)
+       const g56: v128 = v128.shuffle<i32>(g45, g67, 2, 3, 4, 5)
+       const g78: v128 = v128.shuffle<i32>(g67, g89, 2, 3, 4, 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)))
+
+       const g90_19: v128 = v128.bitselect(v128.add<i32>(g90, v128.add<i32>(v128.add<i32>(g90, g90), v128.shl<i32>(g90, 4))), g90, i32x4(-1, -1, 0, 0))
+       const g12_19: v128 = v128.add<i32>(g12, v128.add<i32>(v128.add<i32>(g12, g12), v128.shl<i32>(g12, 4)))
+       const g34_19: v128 = v128.add<i32>(g34, v128.add<i32>(v128.add<i32>(g34, g34), v128.shl<i32>(g34, 4)))
+       const g56_19: v128 = v128.add<i32>(g56, v128.add<i32>(v128.add<i32>(g56, g56), v128.shl<i32>(g56, 4)))
+       const g78_19: v128 = v128.add<i32>(g78, v128.add<i32>(v128.add<i32>(g78, g78), v128.shl<i32>(g78, 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]
+       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]
+       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
+       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
+
+       // 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 unbias26 = v128.splat<i64>(<i64>1 << 37)
+       const unbias25 = v128.splat<i64>(<i64>1 << 36)
+
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias25)
+       h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+       h1 = v128.add<i64>(h1, carry0)
+
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias25)
+       h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+       h5 = v128.add<i64>(h5, carry4)
+
+       carry1 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h1, bias24), 25), unbias26)
+       h1 = v128.sub<i64>(h1, v128.shl<i64>(carry1, 25))
+       h2 = v128.add<i64>(h2, carry1)
+
+       carry5 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h5, bias24), 25), unbias26)
+       h5 = v128.sub<i64>(h5, v128.shl<i64>(carry5, 25))
+       h6 = v128.add<i64>(h6, carry5)
+
+       carry2 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h2, bias25), 26), unbias25)
+       h2 = v128.sub<i64>(h2, v128.shl<i64>(carry2, 26))
+       h3 = v128.add<i64>(h3, carry2)
+
+       carry6 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h6, bias25), 26), unbias25)
+       h6 = v128.sub<i64>(h6, v128.shl<i64>(carry6, 26))
+       h7 = v128.add<i64>(h7, carry6)
+
+       carry3 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h3, bias24), 25), unbias26)
+       h3 = v128.sub<i64>(h3, v128.shl<i64>(carry3, 25))
+       h4 = v128.add<i64>(h4, carry3)
+
+       carry7 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h7, bias24), 25), unbias26)
+       h7 = v128.sub<i64>(h7, v128.shl<i64>(carry7, 25))
+       h8 = v128.add<i64>(h8, carry7)
+
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias25)
+       h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+       h5 = v128.add<i64>(h5, carry4)
+
+       carry8 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h8, bias25), 26), unbias25)
+       h8 = v128.sub<i64>(h8, v128.shl<i64>(carry8, 26))
+       h9 = v128.add<i64>(h9, carry8)
+
+       carry9 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h9, bias24), 25), unbias26)
+       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))))
+
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias25)
+       h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+       h1 = v128.add<i64>(h1, carry0)
+
+       // 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
+
+       // 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)
+
+       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)
+}
+
+/**
+ * For two pairs of FieldElements, multiply pairwise in parallel and store the
+ * product.
+ *
+ * @param h_a FieldElementPair A product destination
+ * @param f_a FieldElementPair A multiplicand source
+ * @param g_a FieldElementPair A multiplicand source
+ * @param h_b FieldElementPair B product destination
+ * @param f_b FieldElementPair B multiplicand source
+ * @param g_b FieldElementPair B multiplicand source
+ */
+export function fep_mul_dual (h_a: FieldElementPair, f_a: FieldElementPair, g_a: FieldElementPair, h_b: FieldElementPair, f_b: FieldElementPair, g_b: FieldElementPair): 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]
+       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]
+       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
+       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
+
+       // 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 unbias26 = v128.splat<i64>(<i64>1 << 37)
+       const unbias25 = v128.splat<i64>(<i64>1 << 36)
+
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias25)
+       h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+       h1 = v128.add<i64>(h1, carry0)
+
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias25)
+       h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+       h5 = v128.add<i64>(h5, carry4)
+
+       carry1 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h1, bias24), 25), unbias26)
+       h1 = v128.sub<i64>(h1, v128.shl<i64>(carry1, 25))
+       h2 = v128.add<i64>(h2, carry1)
+
+       carry5 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h5, bias24), 25), unbias26)
+       h5 = v128.sub<i64>(h5, v128.shl<i64>(carry5, 25))
+       h6 = v128.add<i64>(h6, carry5)
+
+       carry2 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h2, bias25), 26), unbias25)
+       h2 = v128.sub<i64>(h2, v128.shl<i64>(carry2, 26))
+       h3 = v128.add<i64>(h3, carry2)
+
+       carry6 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h6, bias25), 26), unbias25)
+       h6 = v128.sub<i64>(h6, v128.shl<i64>(carry6, 26))
+       h7 = v128.add<i64>(h7, carry6)
+
+       carry3 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h3, bias24), 25), unbias26)
+       h3 = v128.sub<i64>(h3, v128.shl<i64>(carry3, 25))
+       h4 = v128.add<i64>(h4, carry3)
+
+       carry7 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h7, bias24), 25), unbias26)
+       h7 = v128.sub<i64>(h7, v128.shl<i64>(carry7, 25))
+       h8 = v128.add<i64>(h8, carry7)
+
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias25)
+       h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
+       h5 = v128.add<i64>(h5, carry4)
+
+       carry8 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h8, bias25), 26), unbias25)
+       h8 = v128.sub<i64>(h8, v128.shl<i64>(carry8, 26))
+       h9 = v128.add<i64>(h9, carry8)
+
+       carry9 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h9, bias24), 25), unbias26)
+       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))))
+
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias25)
+       h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
+       h1 = v128.add<i64>(h1, carry0)
+
+       // 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
+
+       // 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)
+
+       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)
+}
+
+/**
+ * Negate the values of a FieldElementPair and store the result.
+ *
+ * @param {FieldElementPair} h result destination
+ * @param {FieldElementPair} f operand source
+ */
+export function fep_neg (h: FieldElementPair, f: FieldElementPair): 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 h0 = v128.neg<i32>(f0), h1 = v128.neg<i32>(f1), h2 = v128.neg<i32>(f2)
+       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)
+}
+
+/**
+ * returns z^((p-5)/8) = z^(2²⁵²-3)
+ * used to compute square roots since we have p=5 (mod 8); see Cohen and Frey.
+ */
+export function fep_pow22523 (out: FieldElementPair, z: FieldElementPair, t_fep_0: FieldElementPair, t_fep_1: FieldElementPair, t_fep_2: FieldElementPair): void {
+       fep_sq(t_fep_0, z)
+       fep_sq(t_fep_1, t_fep_0)
+       fep_sq(t_fep_1, t_fep_1)
+       fep_mul(t_fep_1, z, t_fep_1)
+       fep_mul(t_fep_0, t_fep_0, t_fep_1)
+       fep_sq(t_fep_0, t_fep_0)
+       fep_mul(t_fep_0, t_fep_1, t_fep_0)
+       fep_sq(t_fep_1, t_fep_0)
+       for (let i = 1; i < 5; ++i) {
+               fep_sq(t_fep_1, t_fep_1)
+       }
+       fep_mul(t_fep_0, t_fep_1, t_fep_0)
+       fep_sq(t_fep_1, t_fep_0)
+       for (let i = 1; i < 10; ++i) {
+               fep_sq(t_fep_1, t_fep_1)
+       }
+       fep_mul(t_fep_1, t_fep_1, t_fep_0)
+       fep_sq(t_fep_2, t_fep_1)
+       for (let i = 1; i < 20; ++i) {
+               fep_sq(t_fep_2, t_fep_2)
+       }
+       fep_mul(t_fep_1, t_fep_2, t_fep_1)
+       for (let i = 1; i < 11; ++i) {
+               fep_sq(t_fep_1, t_fep_1)
+       }
+       fep_mul(t_fep_0, t_fep_1, t_fep_0)
+       fep_sq(t_fep_1, t_fep_0)
+       for (let i = 1; i < 50; ++i) {
+               fep_sq(t_fep_1, t_fep_1)
+       }
+       fep_mul(t_fep_1, t_fep_1, t_fep_0)
+       fep_sq(t_fep_2, t_fep_1)
+       for (let i = 1; i < 100; ++i) {
+               fep_sq(t_fep_2, t_fep_2)
+       }
+       fep_mul(t_fep_1, t_fep_2, t_fep_1)
+       for (let i = 1; i < 51; ++i) {
+               fep_sq(t_fep_1, t_fep_1)
+       }
+       fep_mul(t_fep_0, t_fep_1, t_fep_0)
+       fep_sq(t_fep_0, t_fep_0)
+       fep_sq(t_fep_0, t_fep_0)
+       fep_mul(out, t_fep_0, z)
+}
+
+/**
+ * Preconditions:
+ * |h| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
+ *
+ * Write p=2²⁵⁵-19; q=floor(h/p).
+ * Basic claim: q = floor(2⁻²⁵⁵(h + 19 2⁻²⁵h9 + 2⁻¹)).
+ *
+ * Proof:
+ * Have |h|≤p so |q|≤1 so |19² 2⁻²⁵⁵ q|<1/4.
+ * Also have |h-2²³⁰ h9|<2²³¹ so |19 2⁻²⁵⁵(h-2²³⁰ h9)|<1/4.
+ *
+ * Write y=2⁻¹-19² 2⁻²⁵⁵q-19 2⁻²⁵⁵(h-2²³⁰ h9).
+ * Then 0<y<1.
+ *
+ * Write r=h-pq.
+ * Have 0 ≤ r ≤ p-1 = 2²⁵⁵-20.
+ * Thus 0 ≤ r+19(2⁻²⁵⁵)r < r+19(2⁻²⁵⁵)2²⁵⁵ ≤ 2²⁵⁵-1.
+ *
+ * Write x=r+19(2⁻²⁵⁵)r+y.
+ * Then 0 < x < 2²⁵⁵ so floor(2⁻²⁵⁵x) = 0 so floor(q+2⁻²⁵⁵x) = q.
+ *
+ * Have q+2⁻²⁵⁵x = 2⁻²⁵⁵(h + 19 2⁻²⁵ h9 + 2⁻¹)
+ * so floor(2⁻²⁵⁵(h + 19 2⁻²⁵ h9 + 2⁻¹)) = q.
+ */
+export function fep_reduce (h: FieldElementPair, f: FieldElementPair): void {
+       const f_ptr: usize = changetype<usize>(f)
+       let f0: i32 = load<i32>(f_ptr, 0)
+       let f1: i32 = load<i32>(f_ptr, 4)
+       let f2: i32 = load<i32>(f_ptr, 8)
+       let f3: i32 = load<i32>(f_ptr, 12)
+       let f4: i32 = load<i32>(f_ptr, 16)
+       let f5: i32 = load<i32>(f_ptr, 20)
+       let f6: i32 = load<i32>(f_ptr, 24)
+       let f7: i32 = load<i32>(f_ptr, 28)
+       let f8: i32 = load<i32>(f_ptr, 32)
+       let f9: i32 = load<i32>(f_ptr, 36)
+       let q: i32
+       let c0: i32, c1: i32, c2: i32, c3: i32, c4: i32, c5: i32, c6: i32, c7: i32, c8: i32, c9: i32
+
+       q = (19 * f9 + (1 << 24)) >> 25
+       q = (f0 + q) >> 26
+       q = (f1 + q) >> 25
+       q = (f2 + q) >> 26
+       q = (f3 + q) >> 25
+       q = (f4 + q) >> 26
+       q = (f5 + q) >> 25
+       q = (f6 + q) >> 26
+       q = (f7 + q) >> 25
+       q = (f8 + q) >> 26
+       q = (f9 + q) >> 25
+
+       /* Goal: Output h-(2²⁵⁵-19)q, which is between 0 and 2²⁵⁵-20. */
+       f0 += 19 * q
+       /* Goal: Output h-2²⁵⁵ q, which is between 0 and 2²⁵⁵-20. */
+
+       c0 = f0 >> 26
+       f1 += c0
+       f0 -= c0 << 26
+       c1 = f1 >> 25
+       f2 += c1
+       f1 -= c1 << 25
+       c2 = f2 >> 26
+       f3 += c2
+       f2 -= c2 << 26
+       c3 = f3 >> 25
+       f4 += c3
+       f3 -= c3 << 25
+       c4 = f4 >> 26
+       f5 += c4
+       f4 -= c4 << 26
+       c5 = f5 >> 25
+       f6 += c5
+       f5 -= c5 << 25
+       c6 = f6 >> 26
+       f7 += c6
+       f6 -= c6 << 26
+       c7 = f7 >> 25
+       f8 += c7
+       f7 -= c7 << 25
+       c8 = f8 >> 26
+       f9 += c8
+       f8 -= c8 << 26
+       c9 = f9 >> 25
+       f9 -= c9 << 25
+
+       const h_ptr: usize = changetype<usize>(h)
+       store<i32>(h_ptr, f0, 0)
+       store<i32>(h_ptr, f1, 4)
+       store<i32>(h_ptr, f2, 8)
+       store<i32>(h_ptr, f3, 12)
+       store<i32>(h_ptr, f4, 16)
+       store<i32>(h_ptr, f5, 20)
+       store<i32>(h_ptr, f6, 24)
+       store<i32>(h_ptr, f7, 28)
+       store<i32>(h_ptr, f8, 32)
+       store<i32>(h_ptr, f9, 36)
+}
+
+/**
+ * Square a FieldElementPair and store the result.
+ *
+ * Some iterations of the inner loop can be skipped since
+ * `f[x]*f[y] + f[y]*f[x] = 2*f[x]*f[y]`
+ *
+ * ```
+ * // non-constant-time example
+ * for (let i = 0; i < 10; i++) {
+ *     for (let j = i; j < 10; j++) {
+ *             h[i + j] += f[i] * g[j]
+ *             if (i < j) {
+ *                     h[i + j] *= 2
+ *             }
+ *     }
+ * }
+ * ```
+ *
+ * h = f * f
+ * Can overlap h with f.
+ *
+ * Preconditions:
+ * |f| bounded by 1.65*2²⁶,1.65*2²⁵,1.65*2²⁶,1.65*2²⁵,etc.
+ *
+ * Postconditions:
+ * |h| bounded by 1.01*2²⁵,1.01*2²⁴,1.01*2²⁵,1.01*2²⁴,etc.
+ *
+ * @param h FieldElementPair power destination
+ * @param f FieldElementPair base source
+ */
+export function fep_sq (h: FieldElementPair, f: FieldElementPair): void {
+       const f0: i64 = i64(load<i32>(changetype<usize>(f), 0 << 2))
+       const f1: i64 = i64(load<i32>(changetype<usize>(f), 1 << 2))
+       const f2: i64 = i64(load<i32>(changetype<usize>(f), 2 << 2))
+       const f3: i64 = i64(load<i32>(changetype<usize>(f), 3 << 2))
+       const f4: i64 = i64(load<i32>(changetype<usize>(f), 4 << 2))
+       const f5: i64 = i64(load<i32>(changetype<usize>(f), 5 << 2))
+       const f6: i64 = i64(load<i32>(changetype<usize>(f), 6 << 2))
+       const f7: i64 = i64(load<i32>(changetype<usize>(f), 7 << 2))
+       const f8: i64 = i64(load<i32>(changetype<usize>(f), 8 << 2))
+       const f9: i64 = i64(load<i32>(changetype<usize>(f), 9 << 2))
+
+       const f0_2: i64 = f0 + f0
+       const f1_2: i64 = f1 + f1
+       const f2_2: i64 = f2 + f2
+       const f3_2: i64 = f3 + f3
+       const f4_2: i64 = f4 + f4
+       const f5_2: i64 = f5 + f5
+       const f6_2: i64 = f6 + f6
+       const f7_2: i64 = f7 + f7
+
+       const f6_19: i64 = f6 * 19 /* 1.959375*2³⁰ */
+       const f8_19: i64 = f8 * 19 /* 1.959375*2³⁰ */
+
+       const f5_38: i64 = f5 * 38 /* 1.959375*2³⁰ */
+       const f7_38: i64 = f7 * 38 /* 1.959375*2³⁰ */
+       const f9_38: i64 = f9 * 38 /* 1.959375*2³⁰ */
+
+       let h0: i64 = f0 * f0
+       let h1: i64 = f0_2 * f1
+       let h2: i64 = f0_2 * f2
+       let h3: i64 = f0_2 * f3
+       let h4: i64 = f0_2 * f4
+       let h5: i64 = f0_2 * f5
+       let h6: i64 = f0_2 * f6
+       let h7: i64 = f0_2 * f7
+       let h8: i64 = f0_2 * f8
+       let h9: i64 = f0_2 * f9
+
+       h2 += f1_2 * f1
+       h3 += f1_2 * f2
+       h4 += f1_2 * f3_2
+       h5 += f1_2 * f4
+       h6 += f1_2 * f5_2
+       h7 += f1_2 * f6
+       h8 += f1_2 * f7_2
+       h9 += f1_2 * f8
+       h0 += f1_2 * f9_38
+
+       h4 += f2 * f2
+       h5 += f2_2 * f3
+       h6 += f2_2 * f4
+       h7 += f2_2 * f5
+       h8 += f2_2 * f6
+       h9 += f2_2 * f7
+       h0 += f2_2 * f8_19
+       h1 += f2 * f9_38
+
+       h6 += f3_2 * f3
+       h7 += f3_2 * f4
+       h8 += f3_2 * f5_2
+       h9 += f3_2 * f6
+       h0 += f3_2 * f7_38
+       h1 += f3_2 * f8_19
+       h2 += f3_2 * f9_38
+
+       h8 += f4 * f4
+       h9 += f4_2 * f5
+       h0 += f4_2 * f6_19
+       h1 += f4 * f7_38
+       h2 += f4_2 * f8_19
+       h3 += f4 * f9_38
+
+       h0 += f5 * f5_38
+       h1 += f5_2 * f6_19
+       h2 += f5_2 * f7_38
+       h3 += f5_2 * f8_19
+       h4 += f5_2 * f9_38
+
+       h2 += f6 * f6_19
+       h3 += f6 * f7_38
+       h4 += f6_2 * f8_19
+       h5 += f6 * f9_38
+
+       h4 += f7 * f7_38
+       h5 += f7_2 * f8_19
+       h6 += f7_2 * f9_38
+
+       h6 += f8 * f8_19
+       h7 += f8 * f9_38
+
+       h8 += f9 * f9_38
+
+       // 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
+
+       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)
+}
+
+/**
+ * Square two FieldElementPair values in parallel and store the results.
+ *
+ * Some iterations of the inner loop can be skipped since
+ * `f[x]*f[y] + f[y]*f[x] = 2*f[x]*f[y]`
+ *
+ * ```
+ * // non-constant-time example
+ * for (let i = 0; i < 10; i++) {
+ *     for (let j = i; j < 10; j++) {
+ *             h[i + j] += f[i] * g[j]
+ *             if (i < j) {
+ *                     h[i + j] *= 2
+ *             }
+ *     }
+ * }
+ * ```
+ *
+ * h = f * f
+ * Can overlap h with f.
+ *
+ * Preconditions:
+ * |f| bounded by 1.65*2²⁶,1.65*2²⁵,1.65*2²⁶,1.65*2²⁵,etc.
+ *
+ * Postconditions:
+ * |h| bounded by 1.01*2²⁵,1.01*2²⁴,1.01*2²⁵,1.01*2²⁴,etc.
+ *
+ * @param h_a FieldElementPair A product destination
+ * @param f_a FieldElementPair A multiplicand source
+ * @param h_b FieldElementPair B product destination
+ * @param f_b FieldElementPair B multiplicand source
+ */
+export function fep_sq_dual (h_a: FieldElementPair, f_a: FieldElementPair, h_b: FieldElementPair, f_b: FieldElementPair): void {
+       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 f01_2: v128 = v128.add<i32>(f01, f01)
+       const f23_2: v128 = v128.add<i32>(f23, f23)
+       const f45_2: v128 = v128.add<i32>(f45, f45)
+       const f67_2: v128 = v128.add<i32>(f67, f67)
+       const f89_2: v128 = v128.add<i32>(f89, f89)
+
+       // pack each f index in low lanes, high lanes are ignored
+       const f0: v128 = v128.shuffle<i32>(f01, f01, 0, 1, 0, 1)
+       const f1: v128 = v128.shuffle<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, f23, 2, 3, 6, 7)
+       const f4: v128 = v128.shuffle<i32>(f45, f45, 0, 1, 0, 1)
+       const f5: v128 = v128.shuffle<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, f67, 2, 3, 6, 7)
+       const f8: v128 = v128.shuffle<i32>(f89, f89, 0, 1, 0, 1)
+       const f9: v128 = v128.shuffle<i32>(f89, f89, 2, 3, 6, 7)
+
+       // precomputed doublings for square special and odd-on-odd
+       const f0_2: v128 = v128.shuffle<i32>(f01_2, f01_2, 0, 1, 0, 1)
+       const f1_2: v128 = v128.shuffle<i32>(f01_2, f01_2, 2, 3, 6, 7)
+       const f2_2: v128 = v128.shuffle<i32>(f23_2, f23_2, 0, 1, 0, 1)
+       const f3_2: v128 = v128.shuffle<i32>(f23_2, f23_2, 2, 3, 6, 7)
+       const f4_2: v128 = v128.shuffle<i32>(f45_2, f45_2, 0, 1, 0, 1)
+       const f5_2: v128 = v128.shuffle<i32>(f45_2, f45_2, 2, 3, 6, 7)
+       const f6_2: v128 = v128.shuffle<i32>(f67_2, f67_2, 0, 1, 0, 1)
+       const f7_2: v128 = v128.shuffle<i32>(f67_2, f67_2, 2, 3, 6, 7)
+       const f8_2: v128 = v128.shuffle<i32>(f89_2, f89_2, 0, 1, 0, 1)
+       const f9_2: v128 = v128.shuffle<i32>(f89_2, f89_2, 2, 3, 6, 7)
+
+       // factor of 19 for wrap from h9 to h0 with odds doubled
+       const f5_38: v128 = v128.add<i32>(f5_2, v128.add<i32>(v128.add<i32>(f5_2, f5_2), v128.shl<i32>(f5_2, 4)))
+       const f6_19: v128 = v128.add<i32>(f6, v128.add<i32>(f6_2, v128.shl<i32>(f6_2, 3)))
+       const f7_38: v128 = v128.add<i32>(f7_2, v128.add<i32>(v128.add<i32>(f7_2, f7_2), v128.shl<i32>(f7_2, 4)))
+       const f8_19: v128 = v128.add<i32>(f8, v128.add<i32>(f8_2, v128.shl<i32>(f8_2, 3)))
+       const f9_38: v128 = v128.add<i32>(f9_2, v128.add<i32>(v128.add<i32>(f9_2, f9_2), v128.shl<i32>(f9_2, 4)))
+
+       // f[0]
+       let h0: v128 = v128.extmul_low<i32>(f0, f0)
+       let h1: v128 = v128.extmul_low<i32>(f0_2, f1)
+       let h2: v128 = v128.extmul_low<i32>(f0_2, f2)
+       let h3: v128 = v128.extmul_low<i32>(f0_2, f3)
+       let h4: v128 = v128.extmul_low<i32>(f0_2, f4)
+       let h5: v128 = v128.extmul_low<i32>(f0_2, f5)
+       let h6: v128 = v128.extmul_low<i32>(f0_2, f6)
+       let h7: v128 = v128.extmul_low<i32>(f0_2, f7)
+       let h8: v128 = v128.extmul_low<i32>(f0_2, f8)
+       let h9: v128 = v128.extmul_low<i32>(f0_2, f9)
+
+       // f[1]
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f1_2, f1))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f1_2, f2))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f1_2, f3_2))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f1_2, f4))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f1_2, f5_2))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f1_2, f6))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f1_2, f7_2))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f1_2, f8))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f1_2, f9_38))
+
+       // f[2]
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f2, f2))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f2_2, f3))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f2_2, f4))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f2_2, f5))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f2_2, f6))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f2_2, f7))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f2_2, f8_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f2, f9_38))
+
+       // f[3]
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f3_2, f3))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f3_2, f4))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f3_2, f5_2))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f3_2, f6))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f3_2, f7_38))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f3_2, f8_19))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f3_2, f9_38))
+
+       // f[4]
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f4, f4))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f4_2, f5))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f4_2, f6_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f4, f7_38))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f4_2, f8_19))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f4, f9_38))
+
+       // f[5]
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f5, f5_38))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f5_2, f6_19))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f5_2, f7_38))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f5_2, f8_19))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f5_2, f9_38))
+
+       // f[6]
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f6, f6_19))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f6, f7_38))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f6_2, f8_19))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f6, f9_38))
+
+       // f[7]
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f7, f7_38))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f7_2, f8_19))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f7_2, f9_38))
+
+       // f[8]
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f8, f8_19))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f8, f9_38))
+
+       // f[9]
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f9, f9_38))
+
+       // 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
+
+       // 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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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))))
+
+       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)
+
+       // 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
+
+       // 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)
+
+       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)
+}
+
+/**
+ * Square two FieldElementPair values in parallel, double the second one, and store
+ * the results.
+ *
+ * `h = 2 * f * f`
+ *
+ * Can overlap h with f.
+ *
+ * Preconditions:
+ * |f| bounded by 1.65*2²⁶,1.65*2²⁵,1.65*2²⁶,1.65*2²⁵,etc.
+ *
+ * Postconditions:
+ * |h| bounded by 1.01*2²⁵,1.01*2²⁴,1.01*2²⁵,1.01*2²⁴,etc.
+ *
+ * @param h_a FieldElementPair A power destination
+ * @param f_a FieldElementPair A base source
+ * @param h_b FieldElementPair B power destination, doubled
+ * @param f_b FieldElementPair B base source
+ */
+export function fep_sq2_dual (h_a: FieldElementPair, f_a: FieldElementPair, h_b: FieldElementPair, f_b: FieldElementPair): void {
+       // pack each f index in low lanes, high lanes are ignored
+       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 f0: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 0, 4, 0, 4)
+       const 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)
+       const f3: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 3, 7, 3, 7)
+       const f4: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 0, 4, 0, 4)
+       const 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)
+       const f7: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 3, 7, 3, 7)
+       const f8: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 0, 4, 2, 6)
+       const f9: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 1, 5, 3, 7)
+
+       // precomputed doublings for square special as well as odd-on-odd
+       const f0_2: v128 = v128.add<i32>(f0, f0)
+       const f1_2: v128 = v128.add<i32>(f1, f1)
+       const f2_2: v128 = v128.add<i32>(f2, f2)
+       const f3_2: v128 = v128.add<i32>(f3, f3)
+       const f4_2: v128 = v128.add<i32>(f4, f4)
+       const f5_2: v128 = v128.add<i32>(f5, f5)
+       const f6_2: v128 = v128.add<i32>(f6, f6)
+       const f7_2: v128 = v128.add<i32>(f7, f7)
+       const f8_2: v128 = v128.add<i32>(f8, f8)
+       const f9_2: v128 = v128.add<i32>(f9, f9)
+
+       // factor of 19 for wrap from h9 to h0
+       const f5_19: v128 = v128.add<i32>(f5, v128.add<i32>(f5_2, v128.shl<i32>(f5_2, 3)))
+       const f6_19: v128 = v128.add<i32>(f6, v128.add<i32>(f6_2, v128.shl<i32>(f6_2, 3)))
+       const f7_19: v128 = v128.add<i32>(f7, v128.add<i32>(f7_2, v128.shl<i32>(f7_2, 3)))
+       const f8_19: v128 = v128.add<i32>(f8, v128.add<i32>(f8_2, v128.shl<i32>(f8_2, 3)))
+       const f9_19: v128 = v128.add<i32>(f9, v128.add<i32>(f9_2, v128.shl<i32>(f9_2, 3)))
+
+       // doubled odd-on-odd factor of 19
+       const f7_38: v128 = v128.add<i32>(f7_19, f7_19)
+       const f9_38: v128 = v128.add<i32>(f9_19, f9_19)
+
+       let h0: v128 = v128.extmul_low<i32>(f0, f0)
+       let h1: v128 = v128.extmul_low<i32>(f0_2, f1)
+       let h2: v128 = v128.extmul_low<i32>(f0_2, f2)
+       let h3: v128 = v128.extmul_low<i32>(f0_2, f3)
+       let h4: v128 = v128.extmul_low<i32>(f0_2, f4)
+       let h5: v128 = v128.extmul_low<i32>(f0_2, f5)
+       let h6: v128 = v128.extmul_low<i32>(f0_2, f6)
+       let h7: v128 = v128.extmul_low<i32>(f0_2, f7)
+       let h8: v128 = v128.extmul_low<i32>(f0_2, f8)
+       let h9: v128 = v128.extmul_low<i32>(f0_2, f9)
+
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f1_2, f1))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f1_2, f2))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f1_2, f3_2))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f1_2, f4))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f1_2, f5_2))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f1_2, f6))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f1_2, f7_2))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f1_2, f8))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f1_2, f9_38))
+
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f2, f2))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f2_2, f3))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f2_2, f4))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f2_2, f5))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f2_2, f6))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f2_2, f7))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f2_2, f8_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f2_2, f9_19))
+
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f3_2, f3))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f3_2, f4))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f3_2, f5_2))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f3_2, f6))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f3_2, f7_38))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f3_2, f8_19))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f3_2, f9_38))
+
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f4, f4))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f4_2, f5))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f4_2, f6_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f4_2, f7_19))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f4_2, f8_19))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f4_2, f9_19))
+
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f5_2, f5_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f5_2, f6_19))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f5_2, f7_38))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f5_2, f8_19))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f5_2, f9_38))
+
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f6, f6_19))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f6_2, f7_19))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f6_2, f8_19))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f6_2, f9_19))
+
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f7_2, f7_19))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f7_2, f8_19))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f7_2, f9_38))
+
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f8, f8_19))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f8_2, f9_19))
+
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f9_2, f9_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
+       let carry2: v128
+       let carry3: v128
+       let carry4: v128
+       let carry5: v128
+       let carry6: v128
+       let carry7: v128
+       let carry8: v128
+       let carry9: v128
+
+       // 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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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)
+
+       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))))
+
+       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)
+
+       // 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
+
+       // 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)
+
+       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)
+}
+
+/**
+ * Subtract a FieldElementPair another and store the result.
+ *
+ * @param h FieldElementPair difference destination
+ * @param f FieldElementPair minuend source
+ * @param g FieldElementPair subtrahend source
+ */
+export function fep_sub (h: FieldElementPair, f: FieldElementPair, g: FieldElementPair): 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)
+}
+
+/**
+ * Goal: Output h0+...+2²⁵⁵ h10-2²⁵⁵ q, which is between 0 and 2²⁵⁵-20.
+ * Have h0+...+2²³⁰ h9 between 0 and 2²⁵⁵-1;
+ * evidently 2²⁵⁵ h10-2²⁵⁵ q = 0.
+ *
+ * Goal: Output h0+...+2²³⁰ h9.
+ */
+export function fep_tobytes (s: StaticArray<u8>, h: FieldElementPair, t_fe: FieldElementPair): void {
+       fep_reduce(t_fe, h)
+       const t0 = load<i32>(changetype<usize>(t_fe), 0 << 2)
+       const t1 = load<i32>(changetype<usize>(t_fe), 1 << 2)
+       const t2 = load<i32>(changetype<usize>(t_fe), 2 << 2)
+       const t3 = load<i32>(changetype<usize>(t_fe), 3 << 2)
+       const t4 = load<i32>(changetype<usize>(t_fe), 4 << 2)
+       const t5 = load<i32>(changetype<usize>(t_fe), 5 << 2)
+       const t6 = load<i32>(changetype<usize>(t_fe), 6 << 2)
+       const t7 = load<i32>(changetype<usize>(t_fe), 7 << 2)
+       const t8 = load<i32>(changetype<usize>(t_fe), 8 << 2)
+       const t9 = load<i32>(changetype<usize>(t_fe), 9 << 2)
+
+       store<u8>(changetype<usize>(s), t0 >> 0, 0)
+       store<u8>(changetype<usize>(s), t0 >> 8, 1)
+       store<u8>(changetype<usize>(s), t0 >> 16, 2)
+       store<u8>(changetype<usize>(s), (t0 >> 24) | (t1 << 2), 3)
+       store<u8>(changetype<usize>(s), t1 >> 6, 4)
+       store<u8>(changetype<usize>(s), t1 >> 14, 5)
+       store<u8>(changetype<usize>(s), (t1 >> 22) | (t2 << 3), 6)
+       store<u8>(changetype<usize>(s), t2 >> 5, 7)
+       store<u8>(changetype<usize>(s), t2 >> 13, 8)
+       store<u8>(changetype<usize>(s), (t2 >> 21) | (t3 << 5), 9)
+       store<u8>(changetype<usize>(s), t3 >> 3, 10)
+       store<u8>(changetype<usize>(s), t3 >> 11, 11)
+       store<u8>(changetype<usize>(s), (t3 >> 19) | (t4 << 6), 12)
+       store<u8>(changetype<usize>(s), t4 >> 2, 13)
+       store<u8>(changetype<usize>(s), t4 >> 10, 14)
+       store<u8>(changetype<usize>(s), t4 >> 18, 15)
+       store<u8>(changetype<usize>(s), t5 >> 0, 16)
+       store<u8>(changetype<usize>(s), t5 >> 8, 17)
+       store<u8>(changetype<usize>(s), t5 >> 16, 18)
+       store<u8>(changetype<usize>(s), (t5 >> 24) | (t6 << 1), 19)
+       store<u8>(changetype<usize>(s), t6 >> 7, 20)
+       store<u8>(changetype<usize>(s), t6 >> 15, 21)
+       store<u8>(changetype<usize>(s), (t6 >> 23) | (t7 << 3), 22)
+       store<u8>(changetype<usize>(s), t7 >> 5, 23)
+       store<u8>(changetype<usize>(s), t7 >> 13, 24)
+       store<u8>(changetype<usize>(s), (t7 >> 21) | (t8 << 4), 25)
+       store<u8>(changetype<usize>(s), t8 >> 4, 26)
+       store<u8>(changetype<usize>(s), t8 >> 12, 27)
+       store<u8>(changetype<usize>(s), (t8 >> 20) | (t9 << 6), 28)
+       store<u8>(changetype<usize>(s), t9 >> 2, 29)
+       store<u8>(changetype<usize>(s), t9 >> 10, 30)
+       store<u8>(changetype<usize>(s), t9 >> 18, 31)
+}