]> git.codecow.com Git - nano25519.git/commitdiff
Implement p3 subtraction directly to eliminate another tempvar.
authorChris Duncan <chris@codecow.com>
Thu, 29 Oct 2026 16:14:02 +0000 (09:14 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 29 Oct 2026 16:14:02 +0000 (09:14 -0700)
src/assembly/crypto_verify.ts
src/assembly/ed25519/p.ts

index ac264828ae639f45af1567c477505b4e729ce1fa..1ba0f274faee122eccd48d898bae39678ff91a29 100644 (file)
@@ -104,7 +104,7 @@ export function crypto_verify_sodium (sig: StaticArray<u8>, M: StaticArray<u8>,
 
        ge_p3_cache_odd_multiples(Ai, A, 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_p3, t_fe_0)
+       ge_p3_sub_p3(check, expected_r, sb_ah, 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 550f6ddd2fb3bd2599f9973b62c48eb32902d9f0..c5a10234f4eef1e24ba402d7a4a197f359479a10 100644 (file)
@@ -310,14 +310,28 @@ export function ge_p3_add_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void {
 
 /**
  * `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_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)
+export function ge_p3_sub_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.T, r.Y, r.Y, r.Z) // X = (Y₁+X₁)(Y₂-X₂), Y = (Y₁-X₁)(Y₂+X₂)
+
+       fe_neg(r.T, q.T) // T = -T₂
+       fe_mul(r.T, r.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
 
-       ge_p3_add_p3(r, p, t_p3, t_fe)
+       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)
 }
 
 /**