v128.store(changetype<usize>(h), v128.shuffle<i32>(h8, h9, 0, 2, 4, 6), 4 << 4) // h8a, h8b, h9a, h9b
}
-const fep_mul_lane_t_fe_0 = fe()
-const fep_mul_lane_t_fe_1 = fe()
-const fep_mul_lane_t_fe_2 = fe()
-const fep_mul_lane_t_fe_3 = fe()
/**
* Multiply the first interleaved lane of two FieldElementPairs and store the
* product.
* @param {FieldElementPair} g multiplicand source
*/
export function fep_mul_lane (h: FieldElementPair, f: FieldElementPair, g: FieldElementPair): void {
- fep_unpack(fep_mul_lane_t_fe_0, fep_mul_lane_t_fe_1, f)
- fep_unpack(fep_mul_lane_t_fe_2, fep_mul_lane_t_fe_3, g)
- fe_mul(fep_mul_lane_t_fe_0, fep_mul_lane_t_fe_0, fep_mul_lane_t_fe_2)
- fep_pack(h, fep_mul_lane_t_fe_0, fep_mul_lane_t_fe_1)
+ const f01: v128 = v128.load(changetype<usize>(f), 0 << 4)
+ const f23: v128 = v128.load(changetype<usize>(f), 1 << 4)
+ const f45: v128 = v128.load(changetype<usize>(f), 2 << 4)
+ const f67: v128 = v128.load(changetype<usize>(f), 3 << 4)
+ const f89: v128 = v128.load(changetype<usize>(f), 4 << 4)
+
+ const f0: v128 = v128.shuffle<i32>(f01, f01, 0, 0, 0, 0)
+ const f1: v128 = v128.shuffle<i32>(v128.add<i32>(f01, f01), f01, 2, 6, 2, 6)
+ const f2: v128 = v128.shuffle<i32>(f23, f23, 0, 0, 0, 0)
+ const f3: v128 = v128.shuffle<i32>(v128.add<i32>(f23, f23), f23, 2, 6, 2, 6)
+ const f4: v128 = v128.shuffle<i32>(f45, f45, 0, 0, 0, 0)
+ const f5: v128 = v128.shuffle<i32>(v128.add<i32>(f45, f45), f45, 2, 6, 2, 6)
+ const f6: v128 = v128.shuffle<i32>(f67, f67, 0, 0, 0, 0)
+ const f7: v128 = v128.shuffle<i32>(v128.add<i32>(f67, f67), f67, 2, 6, 2, 6)
+ const f8: v128 = v128.shuffle<i32>(f89, f89, 0, 0, 0, 0)
+ const f9: v128 = v128.shuffle<i32>(v128.add<i32>(f89, f89), f89, 2, 6, 2, 6)
+
+ const g01: v128 = v128.load(changetype<usize>(g), 0 << 4)
+ const g23: v128 = v128.load(changetype<usize>(g), 1 << 4)
+ const g45: v128 = v128.load(changetype<usize>(g), 2 << 4)
+ const g67: v128 = v128.load(changetype<usize>(g), 3 << 4)
+ const g89: v128 = v128.load(changetype<usize>(g), 4 << 4)
+
+ const g0123: v128 = v128.shuffle<i32>(g01, g23, 0, 2, 4, 6)
+ const g4567: v128 = v128.shuffle<i32>(g45, g67, 0, 2, 4, 6)
+ const g89xx: v128 = v128.shuffle<i32>(g89, i32x4.splat(0), 0, 2, 4, 6)
+
+ const g9012: v128 = v128.shuffle<i32>(g89xx, g0123, 1, 4, 5, 6)
+ const g3456: v128 = v128.shuffle<i32>(g0123, g4567, 3, 4, 5, 6)
+ const g78xx: v128 = v128.shuffle<i32>(g4567, g89xx, 3, 4, 6, 7)
+
+ // factor of 19 for wrap from h9 to h0
+ const g0123_19: v128 = v128.add<i32>(g0123, v128.add<i32>(v128.add<i32>(g0123, g0123), v128.shl<i32>(g0123, 4)))
+ const g4567_19: v128 = v128.add<i32>(g4567, v128.add<i32>(v128.add<i32>(g4567, g4567), v128.shl<i32>(g4567, 4)))
+ const g89xx_19: v128 = v128.add<i32>(g89xx, v128.add<i32>(v128.add<i32>(g89xx, g89xx), v128.shl<i32>(g89xx, 4)))
+
+ const g9012_19: v128 = v128.bitselect(v128.add<i32>(g9012, v128.add<i32>(v128.add<i32>(g9012, g9012), v128.shl<i32>(g9012, 4))), g9012, i32x4(-1, 0, -1, -1))
+ const g3456_19: v128 = v128.add<i32>(g3456, v128.add<i32>(v128.add<i32>(g3456, g3456), v128.shl<i32>(g3456, 4)))
+ const g78xx_19: v128 = v128.add<i32>(g78xx, v128.add<i32>(v128.add<i32>(g78xx, g78xx), v128.shl<i32>(g78xx, 4)))
+
+ // f[0]
+ let h01: v128 = i64x2.extmul_low_i32x4_s(f0, g0123)
+ let h23: v128 = i64x2.extmul_high_i32x4_s(f0, g0123)
+ let h45: v128 = i64x2.extmul_low_i32x4_s(f0, g4567)
+ let h67: v128 = i64x2.extmul_high_i32x4_s(f0, g4567)
+ let h89: v128 = i64x2.extmul_low_i32x4_s(f0, g89xx)
+
+ // f[1]
+ let t0: v128 = i64x2.extmul_low_i32x4_s(f1, g9012_19)
+ let t1: v128 = i64x2.extmul_high_i32x4_s(f1, g9012)
+ let t2: v128 = i64x2.extmul_low_i32x4_s(f1, g3456)
+ let t3: v128 = i64x2.extmul_high_i32x4_s(f1, g3456)
+ let t4: v128 = i64x2.extmul_low_i32x4_s(f1, g78xx)
+
+ h01 = i64x2.add(h01, t0)
+ h23 = i64x2.add(h23, t1)
+ h45 = i64x2.add(h45, t2)
+ h67 = i64x2.add(h67, t3)
+ h89 = i64x2.add(h89, t4)
+
+ // f[2]
+ t0 = i64x2.extmul_low_i32x4_s(f2, g0123)
+ t1 = i64x2.extmul_high_i32x4_s(f2, g0123)
+ t2 = i64x2.extmul_low_i32x4_s(f2, g4567)
+ t3 = i64x2.extmul_high_i32x4_s(f2, g4567)
+ t4 = i64x2.extmul_low_i32x4_s(f2, g89xx_19)
+
+ h23 = i64x2.add(h23, t0)
+ h45 = i64x2.add(h45, t1)
+ h67 = i64x2.add(h67, t2)
+ h89 = i64x2.add(h89, t3)
+ h01 = i64x2.add(h01, t4)
+
+ // f[3]
+ t0 = i64x2.extmul_low_i32x4_s(f3, g9012_19)
+ t1 = i64x2.extmul_high_i32x4_s(f3, g9012)
+ t2 = i64x2.extmul_low_i32x4_s(f3, g3456)
+ t3 = i64x2.extmul_high_i32x4_s(f3, g3456)
+ t4 = i64x2.extmul_low_i32x4_s(f3, g78xx_19)
+
+ h23 = i64x2.add(h23, t0)
+ h45 = i64x2.add(h45, t1)
+ h67 = i64x2.add(h67, t2)
+ h89 = i64x2.add(h89, t3)
+ h01 = i64x2.add(h01, t4)
+
+ // f[4]
+ t0 = i64x2.extmul_low_i32x4_s(f4, g0123)
+ t1 = i64x2.extmul_high_i32x4_s(f4, g0123)
+ t2 = i64x2.extmul_low_i32x4_s(f4, g4567)
+ t3 = i64x2.extmul_high_i32x4_s(f4, g4567_19)
+ t4 = i64x2.extmul_low_i32x4_s(f4, g89xx_19)
+
+ h45 = i64x2.add(h45, t0)
+ h67 = i64x2.add(h67, t1)
+ h89 = i64x2.add(h89, t2)
+ h01 = i64x2.add(h01, t3)
+ h23 = i64x2.add(h23, t4)
+
+ // f[5]
+ t0 = i64x2.extmul_low_i32x4_s(f5, g9012_19)
+ t1 = i64x2.extmul_high_i32x4_s(f5, g9012)
+ t2 = i64x2.extmul_low_i32x4_s(f5, g3456)
+ t3 = i64x2.extmul_high_i32x4_s(f5, g3456_19)
+ t4 = i64x2.extmul_low_i32x4_s(f5, g78xx_19)
+
+ h45 = i64x2.add(h45, t0)
+ h67 = i64x2.add(h67, t1)
+ h89 = i64x2.add(h89, t2)
+ h01 = i64x2.add(h01, t3)
+ h23 = i64x2.add(h23, t4)
+
+ // f[6]
+ t0 = i64x2.extmul_low_i32x4_s(f6, g0123)
+ t1 = i64x2.extmul_high_i32x4_s(f6, g0123)
+ t2 = i64x2.extmul_low_i32x4_s(f6, g4567_19)
+ t3 = i64x2.extmul_high_i32x4_s(f6, g4567_19)
+ t4 = i64x2.extmul_low_i32x4_s(f6, g89xx_19)
+
+ h67 = i64x2.add(h67, t0)
+ h89 = i64x2.add(h89, t1)
+ h01 = i64x2.add(h01, t2)
+ h23 = i64x2.add(h23, t3)
+ h45 = i64x2.add(h45, t4)
+
+ // f[7]
+ t0 = i64x2.extmul_low_i32x4_s(f7, g9012_19)
+ t1 = i64x2.extmul_high_i32x4_s(f7, g9012)
+ t2 = i64x2.extmul_low_i32x4_s(f7, g3456_19)
+ t3 = i64x2.extmul_high_i32x4_s(f7, g3456_19)
+ t4 = i64x2.extmul_low_i32x4_s(f7, g78xx_19)
+
+ h67 = i64x2.add(h67, t0)
+ h89 = i64x2.add(h89, t1)
+ h01 = i64x2.add(h01, t2)
+ h23 = i64x2.add(h23, t3)
+ h45 = i64x2.add(h45, t4)
+
+ // f[8]
+ t0 = i64x2.extmul_low_i32x4_s(f8, g0123)
+ t1 = i64x2.extmul_high_i32x4_s(f8, g0123_19)
+ t2 = i64x2.extmul_low_i32x4_s(f8, g4567_19)
+ t3 = i64x2.extmul_high_i32x4_s(f8, g4567_19)
+ t4 = i64x2.extmul_low_i32x4_s(f8, g89xx_19)
+
+ h89 = i64x2.add(h89, t0)
+ h01 = i64x2.add(h01, t1)
+ h23 = i64x2.add(h23, t2)
+ h45 = i64x2.add(h45, t3)
+ h67 = i64x2.add(h67, t4)
+
+ // f[9]
+ t0 = i64x2.extmul_low_i32x4_s(f9, g9012_19)
+ t1 = i64x2.extmul_high_i32x4_s(f9, g9012_19)
+ t2 = i64x2.extmul_low_i32x4_s(f9, g3456_19)
+ t3 = i64x2.extmul_high_i32x4_s(f9, g3456_19)
+ t4 = i64x2.extmul_low_i32x4_s(f9, g78xx_19)
+
+ h89 = i64x2.add(h89, t0)
+ h01 = i64x2.add(h01, t1)
+ h23 = i64x2.add(h23, t2)
+ h45 = i64x2.add(h45, t3)
+ h67 = i64x2.add(h67, t4)
+
+ // extract scalars from vectors
+ let h0: i64 = i64x2.extract_lane(h01, 0)
+ let h1: i64 = i64x2.extract_lane(h01, 1)
+ let h2: i64 = i64x2.extract_lane(h23, 0)
+ let h3: i64 = i64x2.extract_lane(h23, 1)
+ let h4: i64 = i64x2.extract_lane(h45, 0)
+ let h5: i64 = i64x2.extract_lane(h45, 1)
+ let h6: i64 = i64x2.extract_lane(h67, 0)
+ let h7: i64 = i64x2.extract_lane(h67, 1)
+ let h8: i64 = i64x2.extract_lane(h89, 0)
+ let h9: i64 = i64x2.extract_lane(h89, 1)
+
+ // reduce scalars with carry
+ const s25: i64 = <i64>1 << 25
+ const s24: i64 = <i64>1 << 24
+ const m26 = ~((<i64>1 << 26) - 1)
+ const m25 = ~((<i64>1 << 25) - 1)
+
+ let c: i64
+ let carry0: i64
+ let carry1: i64
+ let carry2: i64
+ let carry3: i64
+ let carry4: i64
+ let carry5: i64
+ let carry6: i64
+ let carry7: i64
+ let carry8: i64
+ let carry9: i64
+
+ c = h0 + s25
+ carry0 = c >> 26
+ h0 -= c & m26
+ h1 += carry0
+
+ c = h4 + s25
+ carry4 = c >> 26
+ h4 -= c & m26
+ h5 += carry4
+
+ c = h1 + s24
+ carry1 = c >> 25
+ h1 -= c & m25
+ h2 += carry1
+
+ c = h5 + s24
+ carry5 = c >> 25
+ h5 -= c & m25
+ h6 += carry5
+
+ c = h2 + s25
+ carry2 = c >> 26
+ h2 -= c & m26
+ h3 += carry2
+
+ c = h6 + s25
+ carry6 = c >> 26
+ h6 -= c & m26
+ h7 += carry6
+
+ c = h3 + s24
+ carry3 = c >> 25
+ h3 -= c & m25
+ h4 += carry3
+
+ c = h7 + s24
+ carry7 = c >> 25
+ h7 -= c & m25
+ h8 += carry7
+
+ c = h4 + s25
+ carry4 = c >> 26
+ h4 -= c & m26
+ h5 += carry4
+
+ c = h8 + s25
+ carry8 = c >> 26
+ h8 -= c & m26
+ h9 += carry8
+
+ c = h9 + s24
+ carry9 = c >> 25
+ h9 -= c & m25
+ h0 += carry9 * 19
+
+ c = h0 + s25
+ carry0 = c >> 26
+ h0 -= c & m26
+ h1 += carry0
+
+ // reduce i64x2 to i32x4 now that high bits are zero and low bits fit into i32
+ h01 = i32x4(<i32>h0, 0, <i32>h1, 0)
+ h23 = i32x4(<i32>h2, 0, <i32>h3, 0)
+ h45 = i32x4(<i32>h4, 0, <i32>h5, 0)
+ h67 = i32x4(<i32>h6, 0, <i32>h7, 0)
+ h89 = i32x4(<i32>h8, 0, <i32>h9, 0)
+
+ // assign results to output
+ v128.store(changetype<usize>(h), v128.shuffle<i32>(h01, f01, 0, 5, 2, 7), 0 << 4) // h0, f0, h1, f1
+ v128.store(changetype<usize>(h), v128.shuffle<i32>(h23, f23, 0, 5, 2, 7), 1 << 4) // h2, f2, h3, f3
+ v128.store(changetype<usize>(h), v128.shuffle<i32>(h45, f45, 0, 5, 2, 7), 2 << 4) // h4, f4, h5, f5
+ v128.store(changetype<usize>(h), v128.shuffle<i32>(h67, f67, 0, 5, 2, 7), 3 << 4) // h6, f6, h7, f7
+ v128.store(changetype<usize>(h), v128.shuffle<i32>(h89, f89, 0, 5, 2, 7), 4 << 4) // h8, f8, h9, f9
}
/**