]> git.codecow.com Git - nano25519.git/commitdiff
Add extended to affine point conversion using Montogery's trick to compute a shared...
authorChris Duncan <chris@codecow.com>
Mon, 9 Nov 2026 07:00:15 +0000 (23:00 -0800)
committerChris Duncan <chris@codecow.com>
Mon, 9 Nov 2026 07:00:15 +0000 (23:00 -0800)
src/assembly/ed25519/p.ts

index 723541f592f3b42c23891109de02cf5a183d3772..6485362e5691bb5493bd6c005735a06b34ecf068 100644 (file)
@@ -407,3 +407,39 @@ export function p3_tobytes (s: StaticArray<u8>, p: p3): void {
        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
+}