]> git.codecow.com Git - nano25519.git/commitdiff
Eliminate some temporary variables. Create parallel addition-subtraction of field...
authorChris Duncan <chris@codecow.com>
Tue, 27 Oct 2026 13:19:04 +0000 (06:19 -0700)
committerChris Duncan <chris@codecow.com>
Tue, 27 Oct 2026 13:19:04 +0000 (06:19 -0700)
src/assembly/crypto_verify.ts
src/assembly/ed25519/fe.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p.ts
test/mutants.mjs

index 58264bc8d14ec572c9acc2ac430fd846fcf78cfe..862da1aeab0ac420ac869bc51b0261d65aab3292 100644 (file)
@@ -61,7 +61,7 @@ export function crypto_verify_donna (sig: StaticArray<u8>, M: StaticArray<u8>, A
        blake2b.init().update(sig, 32).update(pub, KEY_BYTELENGTH).update(M, BLOCKHASH_BYTELENGTH).digest(h)
        sc_reduce(h)
 
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
+       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
        ge_p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
        clear_t()
 
@@ -89,8 +89,8 @@ export function crypto_verify_sodium (sig: StaticArray<u8>, M: StaticArray<u8>,
        blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
        sc_reduce(h)
 
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
-       ge_sub_p3(check, expected_r, sb_ah, t_fe_0, t_ge_cached, t_ge_p1p1)
+       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
+       ge_sub_p3(check, expected_r, sb_ah, t_ge_cached, t_ge_p1p1)
 
        // after cofactoring, `check` should be small order and not malformed in order
        // to pass, unlike `verify_pubkey()` and `verify_signature()` below
@@ -123,7 +123,7 @@ export function crypto_verify_strict (sig: StaticArray<u8>, M: StaticArray<u8>,
        blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
        sc_reduce(h)
 
-       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
+       ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
        ge_p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
        clear_t()
        return equalbytes(sig, check_r, KEY_BYTELENGTH)
index 9ca581a78ecd27f0658b88af521a51b066c2932c..d3d378f8e6c5c834b55f054e62025b047e26610b 100644 (file)
@@ -60,12 +60,44 @@ export function fe_1 (h: FieldElement): void {
  * @param g FieldElement summand source
  */
 export function fe_add (h: FieldElement, f: FieldElement, g: FieldElement): void {
-       const f0 = v128.load(changetype<usize>(f), 0 << 4), f1 = v128.load(changetype<usize>(f), 1 << 4), f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
-       const g0 = v128.load(changetype<usize>(g), 0 << 4), g1 = v128.load(changetype<usize>(g), 1 << 4), g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
-       const h0 = v128.add<i32>(f0, g0), h1 = v128.add<i32>(f1, g1), h2 = v128.add<i32>(f2, g2)
-       v128.store(changetype<usize>(h), h0, 0 << 4)
-       v128.store(changetype<usize>(h), h1, 1 << 4)
-       v128.store_lane<i64>(changetype<usize>(h), h2, 0, 2 << 4)
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
+
+       v128.store(changetype<usize>(h), v128.add<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(h), v128.add<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(h), v128.add<i32>(f2, g2), 0, 2 << 4)
+}
+
+/**
+ * Add and subtract two FieldElements without a temporary and store the results.
+ *
+ * `a = f + g` and `b = f - g`
+ *
+ * @param {FieldElement} a sum destination
+ * @param {FieldElement} b difference destination
+ * @param {FieldElement} f sum destination, summand source, and minuend source
+ * @param {FieldElement} g difference destination, summand source, and subtrahend source
+ */
+export function fe_addsub (a: FieldElement, b: FieldElement, f: FieldElement, g: FieldElement): void {
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
+
+       v128.store(changetype<usize>(a), v128.add<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(a), v128.add<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(a), v128.add<i32>(f2, g2), 0, 2 << 4)
+       v128.store(changetype<usize>(b), v128.sub<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(b), v128.sub<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(b), v128.sub<i32>(f2, g2), 0, 2 << 4)
 }
 
 /**
@@ -752,54 +784,334 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        // 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)
+       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)
+}
+
+/**
+ * For two pairs of FieldElements, multiply pairwise in parallel and store the
+ * product.
+ *
+ * @param h_a FieldElement A product destination
+ * @param f_a FieldElement A multiplicand source
+ * @param g_a FieldElement A multiplicand source
+ * @param h_b FieldElement B product destination
+ * @param f_b FieldElement B multiplicand source
+ * @param g_b FieldElement B multiplicand source
+ */
+export function fe_mul2_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldElement, h_b: FieldElement, f_b: FieldElement, g_b: FieldElement): void {
+       // pack each f index twice
+       const f_a_0123: v128 = v128.load(changetype<usize>(f_a), 0 << 4)
+       const f_a_4567: v128 = v128.load(changetype<usize>(f_a), 1 << 4)
+       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))
+
+       // 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), unbias25)
+       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), unbias25)
+       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), unbias26)
+       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), unbias26)
+       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), unbias25)
+       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), unbias25)
+       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), unbias26)
+       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), unbias26)
+       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), unbias25)
+       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), unbias25)
+       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), unbias26)
+       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), unbias25)
+       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)
 
@@ -1248,111 +1560,98 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        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
@@ -1670,19 +1969,24 @@ export function fe_sq2_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldEle
 }
 
 /**
- * Subtract a FieldElement another and store the result.
+ * Subtract a FieldElement from another and store the result.
  *
  * @param h FieldElement difference destination
  * @param f FieldElement minuend source
  * @param g FieldElement subtrahend source
  */
 export function fe_sub (h: FieldElement, f: FieldElement, g: FieldElement): void {
-       const f0 = v128.load(changetype<usize>(f), 0 << 4), f1 = v128.load(changetype<usize>(f), 1 << 4), f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
-       const g0 = v128.load(changetype<usize>(g), 0 << 4), g1 = v128.load(changetype<usize>(g), 1 << 4), g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
-       const h0 = v128.sub<i32>(f0, g0), h1 = v128.sub<i32>(f1, g1), h2 = v128.sub<i32>(f2, g2)
-       v128.store(changetype<usize>(h), h0, 0 << 4)
-       v128.store(changetype<usize>(h), h1, 1 << 4)
-       v128.store_lane<i64>(changetype<usize>(h), h2, 0, 2 << 4)
+       const f0 = v128.load(changetype<usize>(f), 0 << 4)
+       const f1 = v128.load(changetype<usize>(f), 1 << 4)
+       const f2 = v128.load64_zero(changetype<usize>(f), 2 << 4)
+
+       const g0 = v128.load(changetype<usize>(g), 0 << 4)
+       const g1 = v128.load(changetype<usize>(g), 1 << 4)
+       const g2 = v128.load64_zero(changetype<usize>(g), 2 << 4)
+
+       v128.store(changetype<usize>(h), v128.sub<i32>(f0, g0), 0 << 4)
+       v128.store(changetype<usize>(h), v128.sub<i32>(f1, g1), 1 << 4)
+       v128.store_lane<i64>(changetype<usize>(h), v128.sub<i32>(f2, g2), 0, 2 << 4)
 }
 
 /**
index 26f3006cd3a66a1b34e79fb88043b7189afae913..9e2705254c9e884518db18f0d7102fe8342fe221 100644 (file)
@@ -20,7 +20,7 @@
 import { base } from './base'
 import { base2 } from './base2'
 import { FieldElement, fe, fe_0, fe_1, fe_add, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_neg, fe_pow22523, fe_sq, fe_sub } from './fe'
-import { ge_add_cached, ge_add_precomp, ge_cached, ge_p1p1, ge_p1p1_0, ge_p1p1_to_p2, ge_p1p1_to_p3, ge_p2, ge_p2_0, ge_p2_dbl, ge_p3, ge_p3_0, ge_p3_dbl, ge_p3_to_cached, ge_p3_tobytes, ge_precomp, ge_precomp_0, ge_sub_cached, ge_sub_precomp } from './p'
+import { ge_add_cached, ge_add_precomp, ge_p3_dbl, ge_cached, ge_p1p1, ge_p1p1_0, ge_p1p1_to_p2, ge_p1p1_to_p3, ge_p2, ge_p2_0, ge_p2_dbl, ge_p3, ge_p3_0, ge_p3_to_cached, ge_p3_tobytes, ge_precomp, ge_precomp_0, ge_sub_cached, ge_sub_precomp } from './p'
 
 /**
  * √-1
@@ -354,13 +354,13 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
                ge_p1p1_to_p3(h, r)
        }
 
-       ge_p3_dbl(r, h, t_fe_0)
+       ge_p3_dbl(r, h)
        ge_p1p1_to_p2(s, r)
-       ge_p2_dbl(r, s, t_fe_0)
+       ge_p2_dbl(r, s)
        ge_p1p1_to_p2(s, r)
-       ge_p2_dbl(r, s, t_fe_0)
+       ge_p2_dbl(r, s)
        ge_p1p1_to_p2(s, r)
-       ge_p2_dbl(r, s, t_fe_0)
+       ge_p2_dbl(r, s)
        ge_p1p1_to_p3(h, r)
 
        // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
@@ -380,8 +380,7 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
        e.fill(0)
 }
 
-const ge_double_scalarmult_vartime_aslide: StaticArray<i8> = new StaticArray<i8>(256)
-const ge_double_scalarmult_vartime_bslide: StaticArray<i8> = new StaticArray<i8>(256)
+/** A,3A,5A,7A,9A,11A,13A,15A */
 const ge_double_scalarmult_vartime_Ai: StaticArray<ge_cached> = StaticArray.fromArray<ge_cached>([
        new ge_cached(),
        new ge_cached(),
@@ -391,7 +390,9 @@ const ge_double_scalarmult_vartime_Ai: StaticArray<ge_cached> = StaticArray.from
        new ge_cached(),
        new ge_cached(),
        new ge_cached()
-]) /* A,3A,5A,7A,9A,11A,13A,15A */
+])
+const ge_double_scalarmult_vartime_aslide: StaticArray<i8> = new StaticArray<i8>(256)
+const ge_double_scalarmult_vartime_bslide: StaticArray<i8> = new StaticArray<i8>(256)
 const ge_double_scalarmult_vartime_p: ge_p2 = new ge_p2()
 const ge_double_scalarmult_vartime_t: ge_p1p1 = new ge_p1p1()
 const ge_double_scalarmult_vartime_u: ge_p3 = new ge_p3()
@@ -407,11 +408,12 @@ const ge_double_scalarmult_vartime_A2: ge_p3 = new ge_p3()
  *
  * Variable-time; only used for signature verification.
  */
-export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>, A: ge_p3, b: StaticArray<u8>, t_fe: FieldElement): void {
+export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>, A: ge_p3, b: StaticArray<u8>): void {
+       /** A,3A,5A,7A,9A,11A,13A,15A */
+       const Ai = ge_double_scalarmult_vartime_Ai
        const Bi = base2
        const aslide = ge_double_scalarmult_vartime_aslide
        const bslide = ge_double_scalarmult_vartime_bslide
-       const Ai = ge_double_scalarmult_vartime_Ai /* A,3A,5A,7A,9A,11A,13A,15A */
        const p = ge_double_scalarmult_vartime_p
        const t = ge_double_scalarmult_vartime_t
        const u = ge_double_scalarmult_vartime_u
@@ -424,34 +426,34 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
 
        ge_p3_to_cached(Ai[0], A)
 
-       ge_p3_dbl(t, A, t_fe)
+       ge_p3_dbl(t, A)
        ge_p1p1_to_p3(A2, t)
 
-       ge_add_cached(t, A2, Ai[0], t_fe)
+       ge_add_cached(t, A2, Ai[0])
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[1], u)
 
-       ge_add_cached(t, A2, Ai[1], t_fe)
+       ge_add_cached(t, A2, Ai[1])
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[2], u)
 
-       ge_add_cached(t, A2, Ai[2], t_fe)
+       ge_add_cached(t, A2, Ai[2])
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[3], u)
 
-       ge_add_cached(t, A2, Ai[3], t_fe)
+       ge_add_cached(t, A2, Ai[3])
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[4], u)
 
-       ge_add_cached(t, A2, Ai[4], t_fe)
+       ge_add_cached(t, A2, Ai[4])
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[5], u)
 
-       ge_add_cached(t, A2, Ai[5], t_fe)
+       ge_add_cached(t, A2, Ai[5])
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[6], u)
 
-       ge_add_cached(t, A2, Ai[6], t_fe)
+       ge_add_cached(t, A2, Ai[6])
        ge_p1p1_to_p3(u, t)
        ge_p3_to_cached(Ai[7], u)
 
@@ -463,12 +465,12 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
        }
 
        while (i >= 0) {
-               ge_p2_dbl(t, p, t_fe)
+               ge_p2_dbl(t, p)
 
                const aslidei = aslide[i]
                if (aslidei > 0) {
                        ge_p1p1_to_p3(u, t)
-                       ge_add_cached(t, u, Ai[aslidei >> 1], t_fe)
+                       ge_add_cached(t, u, Ai[aslidei >> 1])
                } else if (aslidei < 0) {
                        ge_p1p1_to_p3(u, t)
                        ge_sub_cached(t, u, Ai[(-aslidei) >> 1])
@@ -490,7 +492,6 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
                }
                i--
        }
-       fe_0(t_fe)
 }
 
 function slide_vartime (r: StaticArray<i8>, a: StaticArray<u8>): void {
index 7117652ca353f07fb43a6a782596f6f99c9ea99f..6cacfca40db731c7fb0ce88a362f3076f27677a9 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: ISC
 
-import { FieldElement, fe, fe_0, fe_1, fe_add, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_dual, fe_neg, fe_sq2_dual, fe_sq_dual, fe_sub, fe_tobytes } from './fe'
+import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul2_dual, fe_mul_dual, fe_neg, fe_sq2_dual, fe_sq_dual, fe_sub, fe_tobytes } from './fe'
 
 /**
  * 2d = −121665 / 60833
@@ -12,25 +12,41 @@ const ed25519_d2: FieldElement = StaticArray.fromArray<i32>([
        -21827239, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, 29715968, 9444199, 0, 0
 ])
 
+/**
+ * Base class for building branded subclasses of coordinate systems.
+ */
 class ge_coord {
        X: FieldElement = fe()
        Y: FieldElement = fe()
        Z: FieldElement = fe()
 }
 
+/**
+ * Projective coordinates.
+ */
 export class ge_p2 extends ge_coord {
        __brand: 'ge_p2' = 'ge_p2'
 }
 
+/**
+ * Completed `(x, y)` where`x = X/Z`, `y = Y/T`
+ */
 export class ge_p1p1 extends ge_coord {
        T: FieldElement = fe()
        __brand: 'ge_p1p1' = 'ge_p1p1'
 }
 
+/**
+ * Extended projective coordinates.
+ */
 export class ge_p3 extends ge_coord {
        T: FieldElement = fe()
        __brand: 'ge_p3' = 'ge_p3'
 }
+
+/**
+ * Computed at runtime.
+ */
 export class ge_cached {
        YplusX: FieldElement = fe()
        YminusX: FieldElement = fe()
@@ -38,6 +54,10 @@ export class ge_cached {
        T2d: FieldElement = fe()
 }
 
+/**
+ * Precomputed from base point and compiled into the module.
+ * Requires an expensive `fe_invert` up front but saves one `fe_mul` at runtime.
+ */
 export class ge_precomp {
        yplusx: FieldElement = fe()
        yminusx: FieldElement = fe()
@@ -72,10 +92,11 @@ export function ge_precomp_0 (h: ge_precomp): void {
 
 /**
  * r = p
+ *
  * ```
- * X' = X * T
- * Y' = Y * Z
- * Z' = Z * T
+ * X = X₁T₁
+ * Y = Y₁Z₁
+ * Z = Z₁T₁
  * ```
  */
 export function ge_p1p1_to_p2 (r: ge_p2, p: ge_p1p1): void {
@@ -85,11 +106,12 @@ export function ge_p1p1_to_p2 (r: ge_p2, p: ge_p1p1): void {
 
 /**
  * r = p
+ *
  * ```
- * X' = X * T
- * Y' = Y * Z
- * Z' = Z * T
- * T' = X * Y
+ * X = X₁T₁
+ * Y = Y₁Z₁
+ * Z = Z₁T₁
+ * T = X₁Y₁
  * ```
  */
 export function ge_p1p1_to_p3 (r: ge_p3, p: ge_p1p1): void {
@@ -108,122 +130,133 @@ export function ge_p3_to_cached (r: ge_cached, p: ge_p3): void {
 }
 
 /**
- * r = 2 * p
+ * `r = 2p`
+ *
  * ```
- * X' = (X + Y)² - (Y² + X²)
- * Y' = Y² + X²
- * Z' = Y² - X²
- * T' = 2Z² - (Y² - X²)
+ * X = (X₁+Y₁)² - (Y₁²+X₁²)
+ * Y = Y₁² + X₁²
+ * Z = Y₁² - X₁²
+ * T = 2Z₁² - (Y₁²-X₁²)
  * ```
  */
-export function ge_p2_dbl (r: ge_p1p1, p: ge_p2, t: FieldElement): void {
-       fe_add(r.Y, p.X, p.Y)
-       fe_sq_dual(r.X, p.X, r.Z, p.Y)
-       fe_sq2_dual(t, r.Y, r.T, p.Z)
-       fe_add(r.Y, r.Z, r.X)
-       fe_sub(r.Z, r.Z, r.X)
-       fe_sub(r.X, t, r.Y)
+export function ge_p2_dbl (r: ge_p1p1, p: ge_p2): void {
+       fe_add(r.T, p.X, p.Y)
+       fe_sq2_dual(r.X, r.T, r.T, p.Z)
+       fe_sq_dual(r.Z, p.X, r.Y, p.Y)
+       fe_addsub(r.Y, r.Z, r.Y, r.Z)
+       fe_sub(r.X, r.X, r.Y)
        fe_sub(r.T, r.T, r.Z)
 }
 
 /**
- * r = 2 * p
+ * `r = 2p`
+ *
  * ```
- * X' = (X + Y)² - (Y² + X²)
- * Y' = Y² + X²
- * Z' = Y² - X²
- * T' = 2Z² - (Y² - X²)
+ * X = (X₁+Y₁)² - (Y₁²+X₁²)
+ * Y = Y₁² + X₁²
+ * Z = Y₁² - X₁²
+ * T = 2Z₁² - (Y₁²-X₁²)
  * ```
  */
-export function ge_p3_dbl (r: ge_p1p1, p: ge_p3, t: FieldElement): void {
-       fe_add(r.Y, p.X, p.Y)
-       fe_sq_dual(r.X, p.X, r.Z, p.Y)
-       fe_sq2_dual(t, r.Y, r.T, p.Z)
-       fe_add(r.Y, r.Z, r.X)
-       fe_sub(r.Z, r.Z, r.X)
-       fe_sub(r.X, t, r.Y)
+export function ge_p3_dbl (r: ge_p1p1, p: ge_p3): void {
+       fe_add(r.T, p.X, p.Y)
+       fe_sq2_dual(r.X, r.T, r.T, p.Z)
+       fe_sq_dual(r.Z, p.X, r.Y, p.Y)
+       fe_addsub(r.Y, r.Z, r.Y, r.Z)
+       fe_sub(r.X, r.X, r.Y)
        fe_sub(r.T, r.T, r.Z)
 }
 
 /**
- * r = p + q
+ * `r = p + q`
+ *
+ * ```
+ * X = (Y₁+X₁)(Y₂+X₂) - (Y₁-X₁)(Y₂-X₂)
+ * Y = (Y₁+X₁)(Y₂+X₂) + (Y₁-X₁)(Y₂-X₂)
+ * Z = 2Z₁Z₂ + 2T₁T₂d
+ * T = 2Z₁Z₂ - 2T₁T₂d
+ * ```
  */
-export function ge_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached, t: FieldElement): void {
+export function ge_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void {
        fe_add(r.X, p.Y, p.X)
        fe_sub(r.Y, p.Y, p.X)
-       fe_mul_dual(r.Z, r.X, q.YplusX, r.Y, r.Y, q.YminusX)
-       fe_mul_dual(r.T, q.T2d, p.T, r.X, p.Z, q.Z)
-       fe_dbl(t, r.X)
-       fe_sub(r.X, r.Z, r.Y)
-       fe_add(r.Y, r.Z, r.Y)
-       fe_add(r.Z, t, r.T)
-       fe_sub(r.T, t, r.T)
+       fe_mul_dual(r.X, r.X, q.YplusX, r.Y, r.Y, q.YminusX)
+       fe_mul2_dual(r.T, q.T2d, p.T, r.Z, p.Z, q.Z)
+       fe_addsub(r.Y, r.X, r.X, r.Y)
+       fe_addsub(r.Z, r.T, r.Z, r.T)
 }
 
 /**
  * `r = p + q`
  *
- * `p.Z` is doubled and stored to itself because its value is not referenced
- * again before being reassigned at all three call sites of this function.
+ * ```
+ * X = (Y₁+X₁)(Y₂+X₂) - (Y₁-X₁)(Y₂-X₂)
+ * Y = (Y₁+X₁)(Y₂+X₂) + (Y₁-X₁)(Y₂-X₂)
+ * Z = 2Z₁ + 2T₁X₂Y₂d
+ * T = 2Z₁ - 2T₁X₂Y₂d
+ * ```
  */
 export function ge_add_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void {
        fe_add(r.X, p.Y, p.X)
        fe_sub(r.Y, p.Y, p.X)
-       fe_mul_dual(r.Z, r.X, q.yplusx, r.Y, r.Y, q.yminusx)
+       fe_mul_dual(r.X, r.X, q.yplusx, r.Y, r.Y, q.yminusx)
+       fe_dbl(r.Z, p.Z)
        fe_mul(r.T, q.xy2d, p.T)
-       fe_dbl(p.Z, p.Z)
-       fe_sub(r.X, r.Z, r.Y)
-       fe_add(r.Y, r.Z, r.Y)
-       fe_add(r.Z, p.Z, r.T)
-       fe_sub(r.T, p.Z, r.T)
+       fe_addsub(r.Y, r.X, r.X, r.Y)
+       fe_addsub(r.Z, r.T, r.Z, r.T)
 }
 
-/* r = p-q */
-export function ge_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_fe: FieldElement, t_ge_cached: ge_cached, t_ge_p1p1: ge_p1p1): void {
+/**
+ * `r = p - q`
+ */
+export function ge_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_ge_cached: ge_cached, t_ge_p1p1: ge_p1p1): void {
        fe_neg(r.X, q.X)
        fe_copy(r.Y, q.Y)
        fe_copy(r.Z, q.Z)
        fe_neg(r.T, q.T)
 
        ge_p3_to_cached(t_ge_cached, r)
-       ge_add_cached(t_ge_p1p1, p, t_ge_cached, t_fe)
+       ge_add_cached(t_ge_p1p1, p, t_ge_cached)
        ge_p1p1_to_p3(r, t_ge_p1p1)
 }
 
 /**
  * `r = p - q`
  *
- * `r.X` is doubled and stored to `p.X` because `p` is not referenced again
- * before being reassigned at the only call site of this function.
+ * ```
+ * X = (Y₁+X₁)(Y₂-X₂) - (Y₁-X₁)(Y₂+X₂)
+ * Y = (Y₁+X₁)(Y₂-X₂) + (Y₁-X₁)(Y₂+X₂)
+ * Z = 2Z₁Z₂ - 2T₁T₂d
+ * T = 2Z₁Z₂ + 2T₁T₂d
+ * ```
  */
 export function ge_sub_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void {
        fe_add(r.X, p.Y, p.X)
        fe_sub(r.Y, p.Y, p.X)
-       fe_mul_dual(r.Z, r.X, q.YminusX, r.Y, r.Y, q.YplusX)
-       fe_mul_dual(r.T, q.T2d, p.T, r.X, p.Z, q.Z)
-       fe_dbl(p.X, r.X)
-       fe_sub(r.X, r.Z, r.Y)
-       fe_add(r.Y, r.Z, r.Y)
-       fe_sub(r.Z, p.X, r.T)
-       fe_add(r.T, p.X, r.T)
+       fe_mul_dual(r.X, r.X, q.YminusX, r.Y, r.Y, q.YplusX)
+       fe_mul2_dual(r.T, q.T2d, p.T, r.Z, p.Z, q.Z)
+       fe_addsub(r.Y, r.X, r.X, r.Y)
+       fe_addsub(r.T, r.Z, r.Z, r.T)
 }
 
 /**
  * `r = p - q`
  *
- * `p.Z` is doubled and stored to itself because its value is not referenced
- * again before being reassigned at the only call site of this function.
+ * ```
+ * X = (Y₁+X₁)(Y₂-X₂) - (Y₁-X₁)(Y₂+X₂)
+ * Y = (Y₁+X₁)(Y₂-X₂) + (Y₁-X₁)(Y₂+X₂)
+ * Z = 2Z₁ - 2T₁X₂Y₂d
+ * T = 2Z₁ + 2T₁X₂Y₂d
+ * ```
  */
 export function ge_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void {
        fe_add(r.X, p.Y, p.X)
        fe_sub(r.Y, p.Y, p.X)
-       fe_mul_dual(r.Z, r.X, q.yminusx, r.Y, r.Y, q.yplusx)
+       fe_mul_dual(r.X, r.X, q.yminusx, r.Y, r.Y, q.yplusx)
+       fe_dbl(r.Z, p.Z)
        fe_mul(r.T, q.xy2d, p.T)
-       fe_dbl(p.Z, p.Z)
-       fe_sub(r.X, r.Z, r.Y)
-       fe_add(r.Y, r.Z, r.Y)
-       fe_sub(r.Z, p.Z, r.T)
-       fe_add(r.T, p.Z, r.T)
+       fe_addsub(r.Y, r.X, r.X, r.Y)
+       fe_addsub(r.T, r.Z, r.Z, r.T)
 }
 
 export function ge_p3_tobytes (s: StaticArray<u8>, h: ge_p3,
index 23eed95ed504e7cc36d7eb9580c4a52354da9150..2dd8fde2e1b74257214456acc84d3cb13947878f 100644 (file)
@@ -48,7 +48,7 @@ const mutants = [
        { guard: FE_ISZERO, from: 'const d = v128.or(t0, t1)', to: 'const d = t0', why: 'tests only the low half' },
        { guard: FE_ISZERO, from: 'const d = v128.or(t0, t1)', to: 'const d = v128.xor(t0, t1)', why: 'combines the halves with xor, so equal bytes 16 apart cancel' },
        { guard: FE_ISZERO, from: 'const d = v128.or(t0, t1)', to: 'const d = v128.and(t0, t1)', why: 'combines the halves with and' },
-       { guard: FE_ISZERO, from: 'v128.load(changetype<usize>(t_u8x32), 16)', to: 'v128.load(changetype<usize>(t_u8x32), 15)', why: 'loads the high half a byte early, so byte 31 is never tested' },
+       { guard: FE_ISZERO, from: 'v128.load(changetype<usize>(t_u8x32), 1 << 4)', to: 'v128.load(changetype<usize>(t_u8x32), 15)', why: 'loads the high half a byte early, so byte 31 is never tested' },
        { guard: FE_ISZERO, from: 'return u8(v128.any_true(d)) ^ 1', to: 'return u8(v128.any_true(d))', why: 'inverts its result' },
        { guard: FE_ISZERO, in: 'fe_reduce', from: 'q = (f9 + q) >> 25', to: 'q = (f9 + q) >> 26', why: 'reduces with a 26-bit shift where the top limb is 25 bits, so loose limbs past 2²⁵⁵ come out wrong' },
 ]