]> git.codecow.com Git - nano25519.git/commitdiff
Extract extended projective point addition to free up some temporary buffers.
authorChris Duncan <chris@codecow.com>
Thu, 29 Oct 2026 14:46:10 +0000 (07:46 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 29 Oct 2026 14:46:10 +0000 (07:46 -0700)
src/assembly/crypto_verify.ts
src/assembly/ed25519/p.ts

index ca1978ba53f6d2846712679309373eeede596e79..218888776476668de1bf310c7a3abcc052080516 100644 (file)
@@ -31,8 +31,8 @@ const h = new StaticArray<u8>(64)
 const sb_ah = new ge_p3()
 const check = new ge_p3()
 const check_r = new StaticArray<u8>(32)
-const t_ge_cached = new ge_cached()
 const t_ge_p1p1 = new ge_p1p1()
+const t_ge_p3 = new ge_p3()
 const t_u8x32 = new StaticArray<u8>(32)
 
 /**
@@ -104,7 +104,7 @@ export function crypto_verify_sodium (sig: StaticArray<u8>, M: StaticArray<u8>,
 
        ge_p3_cache_odd_multiples(Ai, A, t_ge_p1p1, t_fe_0)
        ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S)
-       ge_p3_sub_p3(check, expected_r, sb_ah, t_ge_cached, t_ge_p1p1)
+       ge_p3_sub_p3(check, expected_r, sb_ah, t_ge_p3, t_fe_0)
 
        // after cofactoring, `check` should be small order and not malformed in order
        // to pass, unlike `verify_pubkey()` and `verify_signature()` below
index 4c1183f32ca723413382093b1b3d05fe55cbcc8a..550f6ddd2fb3bd2599f9973b62c48eb32902d9f0 100644 (file)
@@ -171,6 +171,29 @@ export function ge_p2_dbl (r: ge_p1p1, p: ge_p2): void {
        fe_sub(r.T, r.T, r.Z)
 }
 
+/**
+ * r = p + q
+ *
+ * ```
+ * X = X₃T₃ = ((Y₁+X₁)(Y₂+X₂) - (Y₁-X₁)(Y₂-X₂))(2Z₁Z₂ - 2T₁T₂d)
+ * Y = Y₃Z₃ = ((Y₁+X₁)(Y₂+X₂) + (Y₁-X₁)(Y₂-X₂))(2Z₁Z₂ + 2T₁T₂d)
+ * Z = Z₃T₃ = (2Z₁Z₂ + 2T₁T₂d)(2Z₁Z₂ - 2T₁T₂d)
+ * T = X₃Y₃ = ((Y₁+X₁)(Y₂+X₂) - (Y₁-X₁)(Y₂-X₂))((Y₁+X₁)(Y₂+X₂) + (Y₁-X₁)(Y₂-X₂))
+ * ```
+ */
+export function ge_p3_add_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_fe: FieldElement): void {
+       fe_addsub(r.X, r.Y, p.Y, p.X) // X = Y₁+X₁, Y = Y₁-X₁
+       fe_addsub(r.Z, r.T, q.Y, q.X) // Z = Y₂+X₂, T = Y₂-X₂
+       fe_mul_dual(r.X, r.X, r.Z, r.Y, r.Y, r.T) // X = (Y₁+X₁)(Y₂+X₂), Y = (Y₁-X₁)(Y₂-X₂)
+       fe_mul(r.T, q.T, ed25519_d2) // T = 2T₂d
+       fe_mul2_dual(r.T, r.T, p.T, r.Z, p.Z, q.Z) // T = 2T₁T₂d, Z = 2Z₁Z₂
+       fe_addsub(r.Y, r.X, r.X, r.Y) // Y = (Y₁+X₁)(Y₂+X₂) + (Y₁-X₁)(Y₂-X₂), X = (Y₁+X₁)(Y₂+X₂) - (Y₁-X₁)(Y₂-X₂)
+       fe_addsub(r.Z, t_fe, r.Z, r.T) // Z = 2Z₁Z₂ + 2T₁T₂d, t = 2Z₁Z₂ - 2T₁T₂d
+
+       fe_mul_dual(r.Y, r.Y, r.Z, r.T, r.X, r.Y)
+       fe_mul_dual(r.X, r.X, t_fe, r.Z, r.Z, t_fe)
+}
+
 /**
  * `r = 2p`
  *
@@ -288,15 +311,13 @@ export function ge_p3_add_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void {
 /**
  * `r = p - q`
  */
-export function ge_p3_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_ge_cached: ge_cached, t_ge_p1p1: ge_p1p1): void {
-       fe_neg(r.X, q.X)
-       fe_copy(r.Y, q.Y)
-       fe_copy(r.Z, q.Z)
-       fe_neg(r.T, q.T)
+export function ge_p3_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_p3: ge_p3, t_fe: FieldElement): void {
+       fe_neg(t_p3.X, q.X)
+       fe_copy(t_p3.Y, q.Y)
+       fe_copy(t_p3.Z, q.Z)
+       fe_neg(t_p3.T, q.T)
 
-       ge_p3_to_cached(t_ge_cached, r)
-       ge_p3_add_cached(t_ge_p1p1, p, t_ge_cached)
-       ge_p1p1_to_p3(r, t_ge_p1p1)
+       ge_p3_add_p3(r, p, t_p3, t_fe)
 }
 
 /**