]> git.codecow.com Git - nano25519.git/commitdiff
Add paired multiplication against constant 2d.
authorChris Duncan <chris@codecow.com>
Fri, 30 Oct 2026 02:14:38 +0000 (19:14 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 30 Oct 2026 02:14:38 +0000 (19:14 -0700)
src/assembly/ed25519/fe.ts
src/assembly/ed25519/p.ts

index 4c4bb7556dfc6eb087c05dc1b068e34f75600be8..db729620d18eb9dd115491e4bcf3452f9e86c43f 100644 (file)
@@ -808,9 +808,28 @@ export function fe_mul_pair (f_0: FieldElement, a_0: FieldElement, b_0: FieldEle
        carry_v128(changetype<usize>(f_0), changetype<usize>(f_1), f0, f1, f2, f3, f4, f5, f6, f7, f8, f9)
 }
 
+/**
+ * For two pairs of FieldElements, multiply pairwise in parallel, multiply the
+ * second lane by the Ed25519 constant `2d`, and store the product.
+ *
+ * @param f_0 FieldElement A product destination
+ * @param a_0 FieldElement A multiplicand source
+ * @param b_0 FieldElement A multiplicand source
+ * @param f_1 FieldElement B product destination
+ * @param a_1 FieldElement B multiplicand source
+ * @param b_1 FieldElement B multiplicand source
+ *
+ * @param f FieldElement product destination
+ * @param a FieldElement multiplicand source
+ */
+export function fe_mul_pair_2d (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement, b_1: FieldElement): void {
+       fe_mul_pair(f_0, a_0, b_0, f_1, a_1, b_1)
+       fe_extmul(f_1, f_1, d2_0123, d2_4567, d2_89xx, d2_9012, d2_3456, d2_78xx, d2_0123_19, d2_4567_19, d2_89xx_19, d2_9012_19, d2_3456_19, d2_78xx_19)
+}
+
 /**
  * For two pairs of FieldElements, multiply pairwise in parallel, double the
- * second lane, and store the product.
+ * first lane, and store the product.
  *
  * @param f_0 FieldElement A product destination
  * @param a_0 FieldElement A multiplicand source
@@ -1010,8 +1029,8 @@ export function fe_mul_pair_dbl (f_0: FieldElement, a_0: FieldElement, b_0: Fiel
        f7 = v128.add<i64>(f7, v128.extmul_low<i32>(a9, b89_19))
        f8 = v128.add<i64>(f8, v128.extmul_high<i32>(a9, b89_19))
 
-       // double second field element only
-       const m = i64x2(0, -1)
+       // Double first field element only
+       const m = i64x2(-1, 0)
        f0 = v128.add<i64>(f0, v128.and(f0, m))
        f1 = v128.add<i64>(f1, v128.and(f1, m))
        f2 = v128.add<i64>(f2, v128.and(f2, m))
@@ -1026,6 +1045,23 @@ export function fe_mul_pair_dbl (f_0: FieldElement, a_0: FieldElement, b_0: Fiel
        carry_v128(changetype<usize>(f_0), changetype<usize>(f_1), f0, f1, f2, f3, f4, f5, f6, f7, f8, f9)
 }
 
+/**
+ * For two pairs of FieldElements, multiply pairwise in parallel, double the
+ * first lane, multiply the second lane by the Ed25519 constant `2d`, and store
+ * the product.
+ *
+ * @param f_0 FieldElement A product destination * 2
+ * @param a_0 FieldElement A multiplicand source
+ * @param b_0 FieldElement A multiplicand source
+ * @param f_1 FieldElement B product destination * 2d
+ * @param a_1 FieldElement B multiplicand source
+ * @param b_1 FieldElement B multiplicand source
+ */
+export function fe_mul_pair_dbl_2d (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement, b_1: FieldElement): void {
+       fe_mul_pair_dbl(f_0, a_0, b_0, f_1, a_1, b_1)
+       fe_extmul(f_1, f_1, d2_0123, d2_4567, d2_89xx, d2_9012, d2_3456, d2_78xx, d2_0123_19, d2_4567_19, d2_89xx_19, d2_9012_19, d2_3456_19, d2_78xx_19)
+}
+
 /**
  * Negate the values of a FieldElement and store the result.
  *
index 39b960367d616534a25857566593f311d6df20b0..560ea560741cda1b2334504e52d908331df8e59c 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: ISC
 
-import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_2d, fe_mul_pair_dbl, fe_mul_pair, fe_neg, fe_sq_pair_dbl, fe_sq_pair, fe_sub, fe_tobytes } from './fe'
+import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_pair_dbl_2d, fe_mul_2d, fe_mul_pair_dbl, fe_mul_pair, fe_mul_pair_2d, fe_sq_pair_dbl, fe_sq_pair, fe_sub, fe_tobytes } from './fe'
 
 /**
  * Base class for building branded subclasses of coordinate systems.
@@ -139,8 +139,7 @@ export function ge_p3_to_cached (r: ge_cached, p: ge_p3): void {
  */
 export function ge_p1p1_to_cached (r: ge_cached, p: ge_p1p1): void {
        fe_mul_pair(r.YminusX, p.X, p.T, r.YplusX, p.Y, p.Z)
-       fe_mul_pair(r.Z, p.Z, p.T, r.T2d, p.X, p.Y)
-       fe_mul_2d(r.T2d, r.T2d)
+       fe_mul_pair_2d(r.Z, p.Z, p.T, r.T2d, p.X, p.Y)
        fe_addsub(r.YplusX, r.YminusX, r.YplusX, r.YminusX)
 }
 
@@ -177,8 +176,7 @@ export function ge_p3_add_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_fe: FieldElement):
        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_pair(r.X, r.X, r.Z, r.Y, r.Y, r.T) // X = (Y₁+X₁)(Y₂+X₂), Y = (Y₁-X₁)(Y₂-X₂)
-       fe_mul_2d(r.T, q.T) // T = 2T₂d
-       fe_mul_pair_dbl(r.T, r.T, p.T, r.Z, p.Z, q.Z) // T = 2T₁T₂d, Z = 2Z₁Z₂
+       fe_mul_pair_dbl_2d(r.Z, p.Z, q.Z, r.T, p.T, q.T) // Z = 2Z₁Z₂, T = 2T₁T₂d
        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
 
@@ -238,10 +236,9 @@ export function ge_p3_dbl (r: ge_p1p1, p: ge_p3): void {
  * ```
  */
 export function ge_p3_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void {
-       fe_add(r.X, p.Y, p.X)
-       fe_sub(r.Y, p.Y, p.X)
+       fe_addsub(r.X, r.Y, p.Y, p.X)
        fe_mul_pair(r.X, r.X, q.YplusX, r.Y, r.Y, q.YminusX)
-       fe_mul_pair_dbl(r.T, q.T2d, p.T, r.Z, p.Z, q.Z)
+       fe_mul_pair_dbl(r.Z, p.Z, q.Z, r.T, q.T2d, p.T)
        fe_addsub(r.Y, r.X, r.X, r.Y)
        fe_addsub(r.Z, r.T, r.Z, r.T)
 }
@@ -269,14 +266,13 @@ export function ge_p3_add_cached_to_cached (r: ge_cached, p: ge_p3, q: ge_cached
        // p3 + cached
        fe_addsub(r.YplusX, r.YminusX, p.Y, p.X)
        fe_mul_pair(r.YplusX, r.YplusX, q.YplusX, r.YminusX, r.YminusX, q.YminusX)
-       fe_mul_pair_dbl(r.T2d, q.T2d, p.T, r.Z, p.Z, q.Z)
+       fe_mul_pair_dbl(r.Z, p.Z, q.Z, r.T2d, q.T2d, p.T)
        fe_addsub(r.YplusX, r.YminusX, r.YplusX, r.YminusX)
-       fe_addsub(r.Z, r.T2d, r.Z, r.T2d)
+       fe_addsub(r.Z, t_fe, r.Z, r.T2d)
 
        // p1p1 -> cached
-       fe_mul_pair(r.YminusX, r.YminusX, r.T2d, t_fe, r.YminusX, r.YplusX)
-       fe_mul_pair(r.Z, r.Z, r.T2d, r.YplusX, r.YplusX, r.Z)
-       fe_mul_2d(r.T2d, t_fe)
+       fe_mul_pair_2d(r.YminusX, r.YminusX, t_fe, r.T2d, r.YminusX, r.YplusX)
+       fe_mul_pair(r.YplusX, r.YplusX, r.Z, r.Z, r.Z, t_fe)
        fe_addsub(r.YplusX, r.YminusX, r.YplusX, r.YminusX)
 }
 
@@ -313,10 +309,7 @@ export function ge_p3_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_fe: FieldElement):
        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_pair(r.X, r.X, r.T, r.Y, r.Y, r.Z) // X = (Y₁+X₁)(Y₂-X₂), Y = (Y₁-X₁)(Y₂+X₂)
-
-       fe_mul_2d(r.T, q.T) // T = 2T₂d
-       fe_mul_pair_dbl(r.T, r.T, p.T, r.Z, p.Z, q.Z) // Z = 2Z₁Z₂, T = 2T₁T₂d
-
+       fe_mul_pair_dbl_2d(r.Z, p.Z, q.Z, r.T, p.T, q.T) // Z = 2Z₁Z₂, T = 2T₁T₂d
        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(t_fe, r.Z, r.Z, r.T) // t = 2Z₁Z₂ + 2T₁T₂d, Z = 2Z₁Z₂ - 2T₁T₂d
 
@@ -335,10 +328,9 @@ export function ge_p3_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_fe: FieldElement):
  * ```
  */
 export function ge_p3_sub_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void {
-       fe_add(r.X, p.Y, p.X)
-       fe_sub(r.Y, p.Y, p.X)
+       fe_addsub(r.X, r.Y, p.Y, p.X)
        fe_mul_pair(r.X, r.X, q.YminusX, r.Y, r.Y, q.YplusX)
-       fe_mul_pair_dbl(r.T, q.T2d, p.T, r.Z, p.Z, q.Z)
+       fe_mul_pair_dbl(r.Z, p.Z, q.Z, r.T, q.T2d, p.T)
        fe_addsub(r.Y, r.X, r.X, r.Y)
        fe_addsub(r.T, r.Z, r.Z, r.T)
 }
@@ -354,8 +346,7 @@ export function ge_p3_sub_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void {
  * ```
  */
 export function ge_p3_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_addsub(r.X, r.Y, p.Y, p.X)
        fe_mul_pair(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)