* @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)
}
/**
// 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)
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
}
/**
- * 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)
}
/**