]> git.codecow.com Git - nano25519.git/commitdiff
Simplify precomp arithmetic.
authorChris Duncan <chris@codecow.com>
Sun, 25 Oct 2026 10:48:32 +0000 (03:48 -0700)
committerChris Duncan <chris@codecow.com>
Sun, 25 Oct 2026 10:48:32 +0000 (03:48 -0700)
src/assembly/ed25519/p.ts
src/assembly/ed25519/p2.ts

index 4257a0cb2201066870da146e37e07e90b5eca7d0..dcab67fc12198a523291c965a6b45e1eed36a24c 100644 (file)
@@ -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<u8>, h: ge_p3,
index 65de6ff1e9c00e47e9023f74fe316b658d35d467..043e3f85f5cdcc2cb82c82df5840c46322fb8af5 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! 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<u8>, h: gepair_p3,