From ba3671c6172bb8a67dd0b09cd78bee6d85a6689c Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 25 Oct 2026 03:48:32 -0700 Subject: [PATCH] Simplify precomp arithmetic. --- src/assembly/ed25519/p.ts | 35 +++++++++++++++++++++-------------- src/assembly/ed25519/p2.ts | 37 ++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index 4257a0c..dcab67f 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -190,19 +190,21 @@ export function ge_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void { /** * `r = p + q` * - * `p.Z` is doubled and stored to itself because its value is not referenced - * again before being reassigned at all three call sites of this function. + * ``` + * X = (Y₁ + X₁)(Y₂ + X₂) - (Y₁ - X₁)(Y₂ - X₂) + * Y = (Y₁ + X₁)(Y₂ + X₂) + (Y₁ - X₁)(Y₂ - X₂) + * Z = 2Z₁ + 2dT₁X₂Y₂ + * T = 2Z₁ - 2dT₁X₂Y₂ + * ``` */ export function ge_add_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void { fe_add(r.X, p.Y, p.X) fe_sub(r.Y, p.Y, p.X) - fe_mul_dual(r.Z, r.X, q.yplusx, r.Y, r.Y, q.yminusx) + fe_mul_dual(r.X, r.X, q.yplusx, r.Y, r.Y, q.yminusx) + fe_dbl(r.Z, p.Z) fe_mul(r.T, q.xy2d, p.T) - fe_dbl(p.Z, p.Z) - fe_sub(r.X, r.Z, r.Y) - fe_add(r.Y, r.Z, r.Y) - fe_add(r.Z, p.Z, r.T) - fe_sub(r.T, p.Z, r.T) + fe_subadd(r.X, r.Y) + fe_addsub(r.Z, r.T) } /** @@ -237,19 +239,24 @@ export function ge_sub_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void { /** * `r = p - q` * + * ``` + * X = (Y₁ + X₁)(Y₂ - X₂) - (Y₁ - X₁)(Y₂ + X₂) + * Y = (Y₁ + X₁)(Y₂ - X₂) + (Y₁ - X₁)(Y₂ + X₂) + * Z = 2Z₁ - 2dT₁X₂Y₂ + * T = 2Z₁ + 2dT₁X₂Y₂ + * ``` + * * `p.Z` is doubled and stored to itself because its value is not referenced * again before being reassigned at the only call site of this function. */ export function ge_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void { fe_add(r.X, p.Y, p.X) fe_sub(r.Y, p.Y, p.X) - fe_mul_dual(r.Z, r.X, q.yminusx, r.Y, r.Y, q.yplusx) + fe_mul_dual(r.X, r.X, q.yminusx, r.Y, r.Y, q.yplusx) + fe_dbl(r.Z, p.Z) fe_mul(r.T, q.xy2d, p.T) - fe_dbl(p.Z, p.Z) - fe_sub(r.X, r.Z, r.Y) - fe_add(r.Y, r.Z, r.Y) - fe_sub(r.Z, p.Z, r.T) - fe_add(r.T, p.Z, r.T) + fe_subadd(r.X, r.Y) + fe_subadd(r.Z, r.T) } export function ge_p3_tobytes (s: StaticArray, h: ge_p3, diff --git a/src/assembly/ed25519/p2.ts b/src/assembly/ed25519/p2.ts index 65de6ff..043e3f8 100644 --- a/src/assembly/ed25519/p2.ts +++ b/src/assembly/ed25519/p2.ts @@ -2,7 +2,7 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: ISC -import { FieldElement, fe_add, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_dual, fe_sub, fe_tobytes } from './fe' +import { FieldElement, fe_add, fe_addsub, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_dual, fe_sub, fe_subadd, fe_tobytes } from './fe' import { FieldElementPair, fep, fep_add, fep_addsub, fep_blend, fep_mul, fep_mul2, fep_mul_lane, fep_pack, fep_sq, fep_sq2, fep_sub, fep_subadd, fep_swap, fep_unpack, fep_unpack_a } from './fep' import { ge_cached, ge_p1p1, ge_p2, ge_p3, ge_precomp } from './p' @@ -215,19 +215,21 @@ export function gepair_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void { /** * `r = p + q` * - * `p.Z` is doubled and stored to itself because its value is not referenced - * again before being reassigned at all three call sites of this function. + * ``` + * X = (Y₁ + X₁)(Y₂ + X₂) - (Y₁ - X₁)(Y₂ - X₂) + * Y = (Y₁ + X₁)(Y₂ + X₂) + (Y₁ - X₁)(Y₂ - X₂) + * Z = 2Z₁ + 2dT₁X₂Y₂ + * T = 2Z₁ - 2dT₁X₂Y₂ + * ``` */ export function gepair_add_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void { fe_add(r.X, p.Y, p.X) fe_sub(r.Y, p.Y, p.X) - fe_mul_dual(r.Z, r.X, q.yplusx, r.Y, r.Y, q.yminusx) + fe_mul_dual(r.X, r.X, q.yplusx, r.Y, r.Y, q.yminusx) + fe_dbl(r.Z, p.Z) fe_mul(r.T, q.xy2d, p.T) - fe_dbl(p.Z, p.Z) - fe_sub(r.X, r.Z, r.Y) - fe_add(r.Y, r.Z, r.Y) - fe_add(r.Z, p.Z, r.T) - fe_sub(r.T, p.Z, r.T) + fe_subadd(r.X, r.Y) + fe_addsub(r.Z, r.T) } const gepair_sub_cached_pA = fep() @@ -272,19 +274,24 @@ export function gepair_sub_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void { /** * `r = p - q` * + * ``` + * X = (Y₁ + X₁)(Y₂ - X₂) - (Y₁ - X₁)(Y₂ + X₂) + * Y = (Y₁ + X₁)(Y₂ - X₂) + (Y₁ - X₁)(Y₂ + X₂) + * Z = 2Z₁ - 2dT₁X₂Y₂ + * T = 2Z₁ + 2dT₁X₂Y₂ + * ``` + * * `p.Z` is doubled and stored to itself because its value is not referenced * again before being reassigned at the only call site of this function. */ export function gepair_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void { fe_add(r.X, p.Y, p.X) fe_sub(r.Y, p.Y, p.X) - fe_mul_dual(r.Z, r.X, q.yminusx, r.Y, r.Y, q.yplusx) + fe_mul_dual(r.X, r.X, q.yminusx, r.Y, r.Y, q.yplusx) + fe_dbl(r.Z, p.Z) fe_mul(r.T, q.xy2d, p.T) - fe_dbl(p.Z, p.Z) - fe_sub(r.X, r.Z, r.Y) - fe_add(r.Y, r.Z, r.Y) - fe_sub(r.Z, p.Z, r.T) - fe_add(r.T, p.Z, r.T) + fe_subadd(r.X, r.Y) + fe_subadd(r.Z, r.T) } function ge_p3_tobytes (s: StaticArray, h: gepair_p3, -- 2.52.0