From: Chris Duncan Date: Mon, 9 Nov 2026 07:00:15 +0000 (-0800) Subject: Add extended to affine point conversion using Montogery's trick to compute a shared... X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=e873c14c7d59a550fd8edc2642ef5abbfaf47cc1;p=nano25519.git Add extended to affine point conversion using Montogery's trick to compute a shared inversion. --- diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 723541f..6485362 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -407,3 +407,39 @@ export function p3_tobytes (s: StaticArray, 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, p_0: p3, s_1: StaticArray, 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 +}