s[31] ^= fe_isnegative(t.X) << 7
p1p1_0(t)
}
+
+/**
+ * `s₀ = p₀; s₁ = p₁`
+ *
+ * For a pair of extended projective points, computes two inversions
+ * simultaneously from one shared inversion and three multiplications instead of
+ * two separate inversions using Montgomery's trick:
+ *
+ * `n = (Z₀Z₁)⁻¹` → `nZ₁ = Z₀⁻¹, nZ₀ = Z₁⁻¹`
+ *
+ * The resulting points are each then encoded to bytes. The order of operations
+ * here are specific and enable computation with only one temporary variable.
+ * Clearing that temporary variable is critical to ensure secret material is not
+ * retained in memory.
+ *
+ * ```
+ * u = (Z₀Z₁)⁻¹
+ * x₀ = X₀Z₁u, y₀ = Y₀Z₁u
+ * x₁ = X₁Z₀u, y₁ = Y₁Z₀u
+ * ```
+ */
+export function p3_tobytes_pair (s_0: StaticArray<u8>, p_0: p3, s_1: StaticArray<u8>, p_1: p3): void {
+ fe_mul(t.T, p_0.Z, p_1.Z) // T = Z₀Z₁
+ fe_invert(t.Z, t.T) // Z = (Z₀Z₁)⁻¹
+ fe_mul_pair(t.X, t.Z, p_1.Z, t.Y, t.Z, p_0.Z) // X = Z₀⁻¹, Y = Z₁⁻¹
+
+ fe_mul_pair(t.Z, p_0.X, t.X, t.T, p_0.Y, t.X) // Z = x₀, T = y₀
+ fe_tobytes(s_0, t.T)
+ s_0[31] ^= fe_isnegative(t.Z) << 7
+
+ fe_mul_pair(t.Z, p_1.X, t.Y, t.T, p_1.Y, t.Y) // Z = x₁, T = y₁
+ fe_tobytes(s_1, t.T)
+ s_1[31] ^= fe_isnegative(t.Z) << 7
+
+ p1p1_0(t) // Critical since this function is called in a secret context
+}