From 3fa4e76324947828cad3f313cd2bf48400c7c337 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 7 Nov 2026 11:50:48 -0800 Subject: [PATCH] Refactor doubling function to take xyz coordinates directly so that other functions can pass alternate components like p1p1_dbl_to_p1p1 does, which eliminates a tempvar and an extra type-differentiating point class. --- src/assembly/ed25519/p.ts | 66 ++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 8623855..e20fe45 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -22,24 +22,17 @@ export class p1p1 extends Point { __brand: 'p1p1' = 'p1p1' } -/** - * Base class for a point in projective space. - */ -class ProjectivePoint extends Point { - __class: 'p' = 'p' -} - /** * Projective coordinates. */ -export class p2 extends ProjectivePoint { +export class p2 extends Point { __brand: 'p2' = 'p2' } /** * Extended projective coordinates. */ -export class p3 extends ProjectivePoint { +export class p3 extends Point { T: FieldElement = fe() __brand: 'p3' = 'p3' } @@ -65,10 +58,9 @@ export class p_precomp { } /** - * Shared temporary variables used to hold intermediate results when doubling. + * Shared temporary variable used to hold intermediate results when doubling. */ const t = new p1p1() -const t2 = new p2() /** * `r = 2p` @@ -79,11 +71,14 @@ const t2 = new p2() * Z = Y₁² - X₁² * T = 2Z₁² - (Y₁²-X₁²) * ``` + * + * The order of operations here are specific, and changing them can break other + * functions (see `p1p1_dbl_to_p1p1` for more information). */ -function dbl_to_p1p1 (r: p1p1, p: ProjectivePoint): void { - fe_add(r.X, p.X, p.Y) // X = X₁+Y₁ - fe_sq_pair_dbl(r.X, r.X, r.T, p.Z) // X = (X₁+Y₁)², T = 2Z₁² - fe_sq_pair(r.Z, p.X, r.Y, p.Y) // Z = X₁², Y = Y₁² +function dbl_to_p1p1 (r: p1p1, X: FieldElement, Y: FieldElement, Z: FieldElement): void { + fe_add(r.X, X, Y) // X = X₁+Y₁ + fe_sq_pair_dbl(r.X, r.X, r.T, Z) // X = (X₁+Y₁)², T = 2Z₁² + fe_sq_pair(r.Z, X, r.Y, Y) // Z = X₁², Y = Y₁² fe_addsub(r.Y, r.Z, r.Y, r.Z) // Y = Y₁²+X₁², Z = Y₁²-X₁² fe_sub(r.X, r.X, r.Y) // X = (X₁+Y₁)² - (Y₁²+X₁²) fe_sub(r.T, r.T, r.Z) // T = 2Z₁² - (Y₁²-X₁²) @@ -180,24 +175,24 @@ export function p1p1_to_cached (r: p_cached, p: p1p1): void { * `r = 2p` * * ``` - * X = X₁T₁ - * Y = Y₁Z₁ - * Z = Z₁T₁ + * X = (X₁T₁ + Y₁Z₁)² - (Y₁²Z₁² + X₁²T₁²) + * Y = Y₁²Z₁² + X₁²T₁² + * Z = Y₁²Z₁² - X₁²T₁² + * T = 2Z₁²T₁² - (Y₁²Z₁² - X₁²T₁²) * ``` * - * then - * - * ``` - * X = (X₁+Y₁)² - (Y₁²+X₁²) - * Y = Y₁² + X₁² - * Z = Y₁² - X₁² - * T = 2Z₁² - (Y₁²-X₁²) - * ``` + * Converts to projective coordinates in place instead of calling `p1p1_to_p2` + * so that `dbl_to_p1p1` overwrites each component only by squaring it after its + * final read. This allows `r` and `p` to reference the same underlying object + * without aliasing issues. This works here and not for other functions because + * it condenses 4 coordinates to 3 and leaves 1 available for scratch space so + * that `X` can be used to compute `X₂ + Y₂`. This depends on the order of + * operations in `dbl_to_p1p1`, so a change there requires a change here. */ export function p1p1_dbl_to_p1p1 (r: p1p1, p: p1p1): void { - p1p1_to_p2(t2, p) - dbl_to_p1p1(r, t2) - p2_0(t2) + fe_mul(r.Y, p.Y, p.Z) // Y = Y₂ = Y₁Z₁ + fe_mul_pair(r.Z, p.X, p.T, r.T, p.Z, p.T) // Z = X₂ = X₁T₁, T = Z₂ = Z₁T₁ + dbl_to_p1p1(r, r.Z, r.Y, r.T) } /** @@ -210,9 +205,8 @@ export function p1p1_dbl_to_p1p1 (r: p1p1, p: p1p1): void { * ``` */ export function p2_dbl_to_p2 (r: p2, p: p2): void { - dbl_to_p1p1(t, p) + dbl_to_p1p1(t, p.X, p.Y, p.Z) p1p1_to_p2(r, t) - p1p1_0(t) } /** @@ -226,7 +220,7 @@ export function p2_dbl_to_p2 (r: p2, p: p2): void { * ``` */ export function p2_dbl_to_p3 (r: p3, p: p2): void { - dbl_to_p1p1(t, p) + dbl_to_p1p1(t, p.X, p.Y, p.Z) p1p1_to_p3(r, t) p1p1_0(t) } @@ -238,10 +232,10 @@ export function p2_dbl_to_p3 (r: p3, p: p2): void { * X = ((X₁+Y₁)² - (Y₁²+X₁²))(2Z₁² - (Y₁²-X₁²)) * Y = (Y₁² + X₁²)(Y₁² - X₁²) * Z = (Y₁² - X₁²)(2Z₁² - (Y₁²-X₁²)) - * ``` + * ``` */ export function p3_dbl_to_p2 (r: p2, p: p3): void { - dbl_to_p1p1(t, p) + dbl_to_p1p1(t, p.X, p.Y, p.Z) p1p1_to_p2(r, t) p1p1_0(t) } @@ -253,11 +247,11 @@ export function p3_dbl_to_p2 (r: p2, p: p3): void { * X = ((X₁+Y₁)² - (Y₁²+X₁²))(2Z₁² - (Y₁²-X₁²)) * Y = (Y₁² + X₁²)(Y₁² - X₁²) * Z = (Y₁² - X₁²)(2Z₁² - (Y₁²-X₁²)) -* T = ((X₁+Y₁)² - (Y₁²+X₁²))(Y₁² + X₁²) + * T = ((X₁+Y₁)² - (Y₁²+X₁²))(Y₁² + X₁²) * ``` */ export function p3_dbl_to_p3 (r: p3, p: p3): void { - dbl_to_p1p1(t, p) + dbl_to_p1p1(t, p.X, p.Y, p.Z) p1p1_to_p3(r, t) p1p1_0(t) } -- 2.52.0