]> git.codecow.com Git - nano25519.git/commitdiff
Update paired squaring function. Rename "dual" functions since they are dual processo...
authorChris Duncan <chris@codecow.com>
Fri, 23 Oct 2026 17:56:12 +0000 (10:56 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 23 Oct 2026 17:56:12 +0000 (10:56 -0700)
src/assembly/ed25519/fep.ts

index c5a72f849b726716a541a83e9920f432858e51d1..52de94885813f7dbb0d8211633e9594073fd590d 100644 (file)
@@ -52,9 +52,9 @@ export function fep_1 (h: FieldElementPair): void {
 /**
  * Add two FieldElement pairs and store the sum.
  *
- * @param h FieldElementPair sum destination
- * @param f FieldElementPair summand source
- * @param g FieldElementPair summand source
+ * @param {FieldElementPair} h sum destination
+ * @param {FieldElementPair} f summand source
+ * @param {FieldElementPair} g summand source
  */
 export function fep_add (h: FieldElementPair, f: FieldElementPair, g: FieldElementPair): void {
        const f0 = v128.load(changetype<usize>(f), 0 << 4)
@@ -329,9 +329,9 @@ export function fep_invert (out: FieldElementPair, z: FieldElementPair, t_fep_0:
 /**
  * Multiply two FieldElements and store the product.
  *
- * @param h FieldElementPair product destination
- * @param f FieldElementPair multiplicand source
- * @param g FieldElementPair multiplicand source
+ * @param {FieldElementPair} h product destination
+ * @param {FieldElementPair} f multiplicand source
+ * @param {FieldElementPair} g multiplicand source
  */
 export function fep_mul (h: FieldElementPair, f: FieldElementPair, g: FieldElementPair): void {
        const f01: v128 = v128.load(changetype<usize>(f), 0 << 4)
@@ -357,12 +357,6 @@ export function fep_mul (h: FieldElementPair, f: FieldElementPair, g: FieldEleme
        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)))
@@ -370,12 +364,6 @@ export function fep_mul (h: FieldElementPair, f: FieldElementPair, g: FieldEleme
        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)
@@ -566,288 +554,12 @@ export function fep_mul (h: FieldElementPair, f: FieldElementPair, g: FieldEleme
        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)
+       // reduce i64x2 to i32x4 now that high bits are zero and low bits fit into i32
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h0, h1, 0, 2, 4, 6), 0 << 4) // h0a, h0b, h1a, h1b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h2, h3, 0, 2, 4, 6), 1 << 4) // h2a, h2b ,h3a, h3b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h4, h5, 0, 2, 4, 6), 2 << 4) // h4a, h4b, h5a, h5b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h6, h7, 0, 2, 4, 6), 3 << 4) // h6a, h6b, h7a, h7b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h8, h9, 0, 2, 4, 6), 4 << 4) // h8a, h8b, h9a, h9b
 }
 
 /**
@@ -1013,220 +725,7 @@ export function fep_reduce (h: FieldElementPair, f: FieldElementPair): void {
 }
 
 /**
- * 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.
+ * Square two pairs of FieldElement 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]`
@@ -1257,20 +756,12 @@ export function fep_sq (h: FieldElementPair, f: FieldElementPair): void {
  * @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)
+export function fep_sq (h: FieldElementPair, f: 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 f01_2: v128 = v128.add<i32>(f01, f01)
        const f23_2: v128 = v128.add<i32>(f23, f23)
@@ -1278,111 +769,98 @@ export function fep_sq_dual (h_a: FieldElementPair, f_a: FieldElementPair, h_b:
        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 f1: v128 = v128.shuffle<i32>(f01, f01_2, 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 f3: v128 = v128.shuffle<i32>(f23, f23_2, 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 f5: v128 = v128.shuffle<i32>(f45, f45_2, 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 f7: v128 = v128.shuffle<i32>(f67, f67_2, 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)))
+       const f9: v128 = v128.shuffle<i32>(f89, f89_2, 2, 3, 6, 7)
+
+       // factor of 19 for wrap from h9 to h0
+       const f45_19: v128 = v128.add<i32>(f45, v128.add<i32>(v128.add<i32>(f45, f45), v128.shl<i32>(f45, 4)))
+       const f67_19: v128 = v128.add<i32>(f67, v128.add<i32>(v128.add<i32>(f67, f67), v128.shl<i32>(f67, 4)))
+       const f89_19: v128 = v128.add<i32>(f89, v128.add<i32>(v128.add<i32>(f89, f89), v128.shl<i32>(f89, 4)))
+       const f67_38: v128 = v128.add<i32>(f67_2, v128.add<i32>(v128.add<i32>(f67_2, f67_2), v128.shl<i32>(f67_2, 4)))
+       const f89_38: v128 = v128.add<i32>(f89_2, v128.add<i32>(v128.add<i32>(f89_2, f89_2), v128.shl<i32>(f89_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)
+       let h0: v128 = v128.extmul_low<i32>(f0, f01)
+       let h1: v128 = v128.extmul_high<i32>(f0, f01_2)
+       let h2: v128 = v128.extmul_low<i32>(f0, f23_2)
+       let h3: v128 = v128.extmul_high<i32>(f0, f23_2)
+       let h4: v128 = v128.extmul_low<i32>(f0, f45_2)
+       let h5: v128 = v128.extmul_high<i32>(f0, f45_2)
+       let h6: v128 = v128.extmul_low<i32>(f0, f67_2)
+       let h7: v128 = v128.extmul_high<i32>(f0, f67_2)
+       let h8: v128 = v128.extmul_low<i32>(f0, f89_2)
+       let h9: v128 = v128.extmul_high<i32>(f0, f89_2)
 
        // 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))
+       h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f1, f01))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f1, f23_2))
+       h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f1, f23_2))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f1, f45_2))
+       h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f1, f45_2))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f1, f67_2))
+       h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f1, f67_2))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f1, f89_2))
+       h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f1, f89_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))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f2, f23))
+       h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f2, f23_2))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f2, f45_2))
+       h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f2, f45_2))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f2, f67_2))
+       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f2, f67_2))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f2, f89_38))
+       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f2, f89_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))
+       h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f3, f23))
+       h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f3, f45_2))
+       h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f3, f45_2))
+       h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f3, f67_2))
+       h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f3, f67_38))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f3, f89_38))
+       h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f3, f89_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))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f4, f45))
+       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f4, f45_2))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f4, f67_38))
+       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f4, f67_38))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f4, f89_38))
+       h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f4, f89_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))
+       h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f5, f45_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f5, f67_38))
+       h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f5, f67_38))
+       h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f5, f89_38))
+       h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f5, f89_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))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f6, f67_19))
+       h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f6, f67_38))
+       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f6, f89_38))
+       h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f6, f89_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))
+       h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f7, f67_19))
+       h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f7, f89_38))
+       h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f7, f89_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))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f8, f89_19))
+       h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f8, f89_38))
 
        // f[9]
-       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f9, f9_38))
+       h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f9, f89_19))
 
        // reduce scalars with carry
        let carry0: v128
@@ -1454,21 +932,12 @@ export function fep_sq_dual (h_a: FieldElementPair, f_a: FieldElementPair, h_b:
        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)
+       // reduce i64x2 to i32x4 now that high bits are zero and low bits fit into i32
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h0, h1, 0, 2, 4, 6), 0 << 4) // h0a, h0b, h1a, h1b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h2, h3, 0, 2, 4, 6), 1 << 4) // h2a, h2b ,h3a, h3b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h4, h5, 0, 2, 4, 6), 2 << 4) // h4a, h4b, h5a, h5b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h6, h7, 0, 2, 4, 6), 3 << 4) // h6a, h6b, h7a, h7b
+       v128.store(changetype<usize>(h), v128.shuffle<i32>(h8, h9, 0, 2, 4, 6), 4 << 4) // h8a, h8b, h9a, h9b
 }
 
 /**
@@ -1700,19 +1169,36 @@ export function fep_sq2_dual (h_a: FieldElementPair, f_a: FieldElementPair, h_b:
 }
 
 /**
- * Subtract a FieldElementPair another and store the result.
+ * Subtract a FieldElement pair from another and store the result.
  *
- * @param h FieldElementPair difference destination
- * @param f FieldElementPair minuend source
- * @param g FieldElementPair subtrahend source
+ * @param {FieldElementPair} h destination
+ * @param {FieldElementPair} f minuend source
+ * @param {FieldElementPair} g 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)
+       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.sub<i32>(f0, g0)
+       const h1 = v128.sub<i32>(f1, g1)
+       const h2 = v128.sub<i32>(f2, g2)
+       const h3 = v128.sub<i32>(f3, g3)
+       const h4 = v128.sub<i32>(f4, g4)
+
        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)
+       v128.store(changetype<usize>(h), h2, 2 << 4)
+       v128.store(changetype<usize>(h), h3, 3 << 4)
+       v128.store(changetype<usize>(h), h4, 4 << 4)
 }
 
 /**
@@ -1722,49 +1208,49 @@ export function fep_sub (h: FieldElementPair, f: FieldElementPair, g: FieldEleme
  *
  * 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)
-}
+// 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)
+// }