fep_mul(rB, rB, t_fep) // rB = (Z₁T₁, X₁Y₁)
}
+/**
+ * Swap, add, blend, and square & double.
+ */
+function gepair_p2_dbl_sq2 (rZT: FieldElementPair, pXY: FieldElementPair, pZ: FieldElementPair): void {
+ // load (X₁, Y₁)
+ const XY_0 = v128.load(changetype<usize>(pXY), 0 << 4)
+ const XY_1 = v128.load(changetype<usize>(pXY), 1 << 4)
+ const XY_2 = v128.load(changetype<usize>(pXY), 2 << 4)
+ const XY_3 = v128.load(changetype<usize>(pXY), 3 << 4)
+ const XY_4 = v128.load(changetype<usize>(pXY), 4 << 4)
+
+ // add lanes (Y₁+X₁, X₁+Y₁)
+ const XpY0 = v128.add<i32>(XY_0, v128.shuffle<i32>(XY_0, XY_0, 1, 0, 3, 2))
+ const XpY1 = v128.add<i32>(XY_1, v128.shuffle<i32>(XY_1, XY_1, 1, 0, 3, 2))
+ const XpY2 = v128.add<i32>(XY_2, v128.shuffle<i32>(XY_2, XY_2, 1, 0, 3, 2))
+ const XpY3 = v128.add<i32>(XY_3, v128.shuffle<i32>(XY_3, XY_3, 1, 0, 3, 2))
+ const XpY4 = v128.add<i32>(XY_4, v128.shuffle<i32>(XY_4, XY_4, 1, 0, 3, 2))
+
+ // load (Z₁, Z₁) ignore second lane
+ const pZ_0 = v128.load(changetype<usize>(pZ), 0 << 4)
+ const pZ_1 = v128.load(changetype<usize>(pZ), 1 << 4)
+ const pZ_2 = v128.load(changetype<usize>(pZ), 2 << 4)
+ const pZ_3 = v128.load(changetype<usize>(pZ), 3 << 4)
+ const pZ_4 = v128.load(changetype<usize>(pZ), 4 << 4)
+
+ // blend (Z₁, X₁+Y₁)
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_0, XpY0, 0, 5, 2, 7), 0 << 4)
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_1, XpY1, 0, 5, 2, 7), 1 << 4)
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_2, XpY2, 0, 5, 2, 7), 2 << 4)
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_3, XpY3, 0, 5, 2, 7), 3 << 4)
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_4, XpY4, 0, 5, 2, 7), 4 << 4)
+
+ fep_sq2(rZT, rZT) // rB = (2Z₁², (X₁+Y₁)²)
+}
+
/**
* `r = 2p`
*
* T = 2Z₁²-(Y₁²-X₁²)
* ```
*/
-export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY: FieldElementPair, pZ: FieldElementPair, t_fep: FieldElementPair): void {
- fep_swap(t_fep, pXY) // t = (Y₁, X₁)
- fep_add(t_fep, t_fep, pXY) // t = (Y₁+X₁, X₁+Y₁)
- fep_blend(rZT, pZ, t_fep) // rB = (Z₁, X₁+Y₁)
- fep_sq2(rZT, rZT) // rB = (2Z₁², (X₁+Y₁)²)
+function gepair_p2_dbl_sq (rXY: FieldElementPair, rZT: FieldElementPair, pXY: FieldElementPair): void {
+ fep_sq(rXY, pXY)
+
+ // load (X₁², Y₁²)
+ const x2y2_0 = v128.load(changetype<usize>(rXY), 0 << 4)
+ const x2y2_1 = v128.load(changetype<usize>(rXY), 1 << 4)
+ const x2y2_2 = v128.load(changetype<usize>(rXY), 2 << 4)
+ const x2y2_3 = v128.load(changetype<usize>(rXY), 3 << 4)
+ const x2y2_4 = v128.load(changetype<usize>(rXY), 4 << 4)
+
+ // swap (Y₁², X₁²)
+ const y2x2_0 = v128.shuffle<i32>(x2y2_0, x2y2_0, 1, 0, 3, 2)
+ const y2x2_1 = v128.shuffle<i32>(x2y2_1, x2y2_1, 1, 0, 3, 2)
+ const y2x2_2 = v128.shuffle<i32>(x2y2_2, x2y2_2, 1, 0, 3, 2)
+ const y2x2_3 = v128.shuffle<i32>(x2y2_3, x2y2_3, 1, 0, 3, 2)
+ const y2x2_4 = v128.shuffle<i32>(x2y2_4, x2y2_4, 1, 0, 3, 2)
+
+ // add (X₁²+Y₁², Y₁²+X₁²)
+ const plus_0 = v128.add<i32>(x2y2_0, y2x2_0)
+ const plus_1 = v128.add<i32>(x2y2_1, y2x2_1)
+ const plus_2 = v128.add<i32>(x2y2_2, y2x2_2)
+ const plus_3 = v128.add<i32>(x2y2_3, y2x2_3)
+ const plus_4 = v128.add<i32>(x2y2_4, y2x2_4)
+
+
+ // subtract (X₁²-Y₁², Y₁²-X₁²)
+ const minus_0 = v128.sub<i32>(x2y2_0, y2x2_0)
+ const minus_1 = v128.sub<i32>(x2y2_1, y2x2_1)
+ const minus_2 = v128.sub<i32>(x2y2_2, y2x2_2)
+ const minus_3 = v128.sub<i32>(x2y2_3, y2x2_3)
+ const minus_4 = v128.sub<i32>(x2y2_4, y2x2_4)
+
+ // blend (X₁²+Y₁², Y₁²-X₁²) i.e. (Y₁²+X₁², Y₁²-X₁²) i.e. (Y', Z')
+ const YpXYmX_0 = v128.shuffle<i32>(plus_0, minus_0, 0, 5, 2, 7)
+ const YpXYmX_1 = v128.shuffle<i32>(plus_1, minus_1, 0, 5, 2, 7)
+ const YpXYmX_2 = v128.shuffle<i32>(plus_2, minus_2, 0, 5, 2, 7)
+ const YpXYmX_3 = v128.shuffle<i32>(plus_3, minus_3, 0, 5, 2, 7)
+ const YpXYmX_4 = v128.shuffle<i32>(plus_4, minus_4, 0, 5, 2, 7)
- fep_sq(rXY, pXY) // rA = (X₁², Y₁²)
- fep_swap(t_fep, rXY) // t = (Y₁², X₁²)
- fep_addsub(rXY, t_fep, rXY, t_fep) // rA = (X₁²+Y₁², Y₁²+X₁²); t = (X₁²-Y₁², Y₁²-X₁²)
- fep_blend(rXY, rXY, t_fep) // rA = (X₁²+Y₁², Y₁²-X₁²) i.e. (Y₁²+X₁², Y₁²-X₁²) i.e. (Y', Z')
+ // load (2Z₁², (X₁+Y₁)²)
+ const ZXpY_0 = v128.load(changetype<usize>(rZT), 0 << 4)
+ const ZXpY_1 = v128.load(changetype<usize>(rZT), 1 << 4)
+ const ZXpY_2 = v128.load(changetype<usize>(rZT), 2 << 4)
+ const ZXpY_3 = v128.load(changetype<usize>(rZT), 3 << 4)
+ const ZXpY_4 = v128.load(changetype<usize>(rZT), 4 << 4)
- fep_swap(t_fep, rZT) // t = ((X₁+Y₁)², 2Z₁²)
- fep_sub(rZT, t_fep, rXY) // rB = ((X₁+Y₁)²-(Y₁²+X₁²), 2Z₁²-(Y₁²-X₁²)) i.e. (X', T')
+ // swap ((X₁+Y₁)², 2Z₁²)
+ const XpYZ_0 = v128.shuffle<i32>(ZXpY_0, ZXpY_0, 1, 0, 3, 2)
+ const XpYZ_1 = v128.shuffle<i32>(ZXpY_1, ZXpY_1, 1, 0, 3, 2)
+ const XpYZ_2 = v128.shuffle<i32>(ZXpY_2, ZXpY_2, 1, 0, 3, 2)
+ const XpYZ_3 = v128.shuffle<i32>(ZXpY_3, ZXpY_3, 1, 0, 3, 2)
+ const XpYZ_4 = v128.shuffle<i32>(ZXpY_4, ZXpY_4, 1, 0, 3, 2)
- fep_swap(t_fep, rXY) // t = (Z', Y')
- fep_blend(rXY, rZT, t_fep) // rA = (X', Y')
- fep_blend(rZT, t_fep, rZT) // rA = (Z', T')
+ // subtract ((X₁+Y₁)²-(Y₁²+X₁²), 2Z₁²-(Y₁²-X₁²)) i.e. (X', T')
+ const h0 = v128.sub<i32>(XpYZ_0, YpXYmX_0)
+ const h1 = v128.sub<i32>(XpYZ_1, YpXYmX_1)
+ const h2 = v128.sub<i32>(XpYZ_2, YpXYmX_2)
+ const h3 = v128.sub<i32>(XpYZ_3, YpXYmX_3)
+ const h4 = v128.sub<i32>(XpYZ_4, YpXYmX_4)
+
+ // swap (Z', Y')
+ const YmXYpX_0 = v128.shuffle<i32>(YpXYmX_0, YpXYmX_0, 1, 0, 3, 2)
+ const YmXYpX_1 = v128.shuffle<i32>(YpXYmX_1, YpXYmX_1, 1, 0, 3, 2)
+ const YmXYpX_2 = v128.shuffle<i32>(YpXYmX_2, YpXYmX_2, 1, 0, 3, 2)
+ const YmXYpX_3 = v128.shuffle<i32>(YpXYmX_3, YpXYmX_3, 1, 0, 3, 2)
+ const YmXYpX_4 = v128.shuffle<i32>(YpXYmX_4, YpXYmX_4, 1, 0, 3, 2)
+
+ // blend (X', Y')
+ v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h0, YmXYpX_0, 0, 5, 2, 7), 0 << 4)
+ v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h1, YmXYpX_1, 0, 5, 2, 7), 1 << 4)
+ v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h2, YmXYpX_2, 0, 5, 2, 7), 2 << 4)
+ v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h3, YmXYpX_3, 0, 5, 2, 7), 3 << 4)
+ v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h4, YmXYpX_4, 0, 5, 2, 7), 4 << 4)
+
+ // blend (Z', T')
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_0, h0, 0, 5, 2, 7), 0 << 4)
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_1, h1, 0, 5, 2, 7), 1 << 4)
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_2, h2, 0, 5, 2, 7), 2 << 4)
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_3, h3, 0, 5, 2, 7), 3 << 4)
+ v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_4, h4, 0, 5, 2, 7), 4 << 4)
+}
+
+/**
+ * `r = 2p`
+ *
+ * ```
+ * X = (X₁+Y₁)²-(Y₁²+X₁²)
+ * Y = Y₁²+X₁²
+ * Z = Y₁²-X₁²
+ * T = 2Z₁²-(Y₁²-X₁²)
+ * ```
+ */
+export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY: FieldElementPair, pZ: FieldElementPair): void {
+ gepair_p2_dbl_sq2(rZT, pXY, pZ)
+ gepair_p2_dbl_sq(rXY, rZT, pXY)
}
/**