]> git.codecow.com Git - nano25519.git/commitdiff
Extract p2 doubling to save on loads and stores.
authorChris Duncan <chris@codecow.com>
Mon, 26 Oct 2026 21:11:28 +0000 (14:11 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 26 Oct 2026 21:11:28 +0000 (14:11 -0700)
src/assembly/ed25519/ge.ts
src/assembly/ed25519/p2.ts

index eb044198eeb701074d109d6f343fd15da725d9d7..28e778ca7c21ab532792a8b402b376ae8d301379 100644 (file)
@@ -498,7 +498,7 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
        fep_pack(pB, p.Z, p.X) // T = p.X = 0
 
        while (i >= 0) {
-               gepair_p2_dbl(tA, tB, pA, pB, t_fep)
+               gepair_p2_dbl(tA, tB, pA, pB)
 
                if (aslide[i] > 0) {
                        const aslidei = aslide[i] >> 1
index c1872e1c75e809fb05940835fc934e05b2c930c6..8905e1a4e2c6401a0098c695a85387fdf1fbc05a 100644 (file)
@@ -40,6 +40,41 @@ export function gepair_p1p1_to_p3 (rA: FieldElementPair, rB: FieldElementPair, p
        fep_mul(rB, rB, t_fep) // rB = (Z₁T₁, X₁Y₁)
 }
 
+/**
+ * Swap, add, blend, and square & double.
+ */
+function gepair_p2_dbl_sq2 (rZT: FieldElementPair, pXY: FieldElementPair, pZ: FieldElementPair): void {
+       // load (X₁, Y₁)
+       const XY_0 = v128.load(changetype<usize>(pXY), 0 << 4)
+       const XY_1 = v128.load(changetype<usize>(pXY), 1 << 4)
+       const XY_2 = v128.load(changetype<usize>(pXY), 2 << 4)
+       const XY_3 = v128.load(changetype<usize>(pXY), 3 << 4)
+       const XY_4 = v128.load(changetype<usize>(pXY), 4 << 4)
+
+       // add lanes (Y₁+X₁, X₁+Y₁)
+       const XpY0 = v128.add<i32>(XY_0, v128.shuffle<i32>(XY_0, XY_0, 1, 0, 3, 2))
+       const XpY1 = v128.add<i32>(XY_1, v128.shuffle<i32>(XY_1, XY_1, 1, 0, 3, 2))
+       const XpY2 = v128.add<i32>(XY_2, v128.shuffle<i32>(XY_2, XY_2, 1, 0, 3, 2))
+       const XpY3 = v128.add<i32>(XY_3, v128.shuffle<i32>(XY_3, XY_3, 1, 0, 3, 2))
+       const XpY4 = v128.add<i32>(XY_4, v128.shuffle<i32>(XY_4, XY_4, 1, 0, 3, 2))
+
+       // load (Z₁, Z₁) ignore second lane
+       const pZ_0 = v128.load(changetype<usize>(pZ), 0 << 4)
+       const pZ_1 = v128.load(changetype<usize>(pZ), 1 << 4)
+       const pZ_2 = v128.load(changetype<usize>(pZ), 2 << 4)
+       const pZ_3 = v128.load(changetype<usize>(pZ), 3 << 4)
+       const pZ_4 = v128.load(changetype<usize>(pZ), 4 << 4)
+
+       // blend (Z₁, X₁+Y₁)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_0, XpY0, 0, 5, 2, 7), 0 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_1, XpY1, 0, 5, 2, 7), 1 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_2, XpY2, 0, 5, 2, 7), 2 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_3, XpY3, 0, 5, 2, 7), 3 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(pZ_4, XpY4, 0, 5, 2, 7), 4 << 4)
+
+       fep_sq2(rZT, rZT) // rB = (2Z₁², (X₁+Y₁)²)
+}
+
 /**
  * `r = 2p`
  *
@@ -50,23 +85,101 @@ export function gepair_p1p1_to_p3 (rA: FieldElementPair, rB: FieldElementPair, p
  * T = 2Z₁²-(Y₁²-X₁²)
  * ```
  */
-export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY: FieldElementPair, pZ: FieldElementPair, t_fep: FieldElementPair): void {
-       fep_swap(t_fep, pXY) // t = (Y₁, X₁)
-       fep_add(t_fep, t_fep, pXY) // t = (Y₁+X₁, X₁+Y₁)
-       fep_blend(rZT, pZ, t_fep) // rB = (Z₁, X₁+Y₁)
-       fep_sq2(rZT, rZT) // rB = (2Z₁², (X₁+Y₁)²)
+function gepair_p2_dbl_sq (rXY: FieldElementPair, rZT: FieldElementPair, pXY: FieldElementPair): void {
+       fep_sq(rXY, pXY)
+
+       // load (X₁², Y₁²)
+       const x2y2_0 = v128.load(changetype<usize>(rXY), 0 << 4)
+       const x2y2_1 = v128.load(changetype<usize>(rXY), 1 << 4)
+       const x2y2_2 = v128.load(changetype<usize>(rXY), 2 << 4)
+       const x2y2_3 = v128.load(changetype<usize>(rXY), 3 << 4)
+       const x2y2_4 = v128.load(changetype<usize>(rXY), 4 << 4)
+
+       // swap (Y₁², X₁²)
+       const y2x2_0 = v128.shuffle<i32>(x2y2_0, x2y2_0, 1, 0, 3, 2)
+       const y2x2_1 = v128.shuffle<i32>(x2y2_1, x2y2_1, 1, 0, 3, 2)
+       const y2x2_2 = v128.shuffle<i32>(x2y2_2, x2y2_2, 1, 0, 3, 2)
+       const y2x2_3 = v128.shuffle<i32>(x2y2_3, x2y2_3, 1, 0, 3, 2)
+       const y2x2_4 = v128.shuffle<i32>(x2y2_4, x2y2_4, 1, 0, 3, 2)
+
+       // add (X₁²+Y₁², Y₁²+X₁²)
+       const plus_0 = v128.add<i32>(x2y2_0, y2x2_0)
+       const plus_1 = v128.add<i32>(x2y2_1, y2x2_1)
+       const plus_2 = v128.add<i32>(x2y2_2, y2x2_2)
+       const plus_3 = v128.add<i32>(x2y2_3, y2x2_3)
+       const plus_4 = v128.add<i32>(x2y2_4, y2x2_4)
+
+
+       // subtract (X₁²-Y₁², Y₁²-X₁²)
+       const minus_0 = v128.sub<i32>(x2y2_0, y2x2_0)
+       const minus_1 = v128.sub<i32>(x2y2_1, y2x2_1)
+       const minus_2 = v128.sub<i32>(x2y2_2, y2x2_2)
+       const minus_3 = v128.sub<i32>(x2y2_3, y2x2_3)
+       const minus_4 = v128.sub<i32>(x2y2_4, y2x2_4)
+
+       // blend (X₁²+Y₁², Y₁²-X₁²) i.e. (Y₁²+X₁², Y₁²-X₁²) i.e. (Y', Z')
+       const YpXYmX_0 = v128.shuffle<i32>(plus_0, minus_0, 0, 5, 2, 7)
+       const YpXYmX_1 = v128.shuffle<i32>(plus_1, minus_1, 0, 5, 2, 7)
+       const YpXYmX_2 = v128.shuffle<i32>(plus_2, minus_2, 0, 5, 2, 7)
+       const YpXYmX_3 = v128.shuffle<i32>(plus_3, minus_3, 0, 5, 2, 7)
+       const YpXYmX_4 = v128.shuffle<i32>(plus_4, minus_4, 0, 5, 2, 7)
 
-       fep_sq(rXY, pXY) // rA = (X₁², Y₁²)
-       fep_swap(t_fep, rXY) // t = (Y₁², X₁²)
-       fep_addsub(rXY, t_fep, rXY, t_fep) // rA = (X₁²+Y₁², Y₁²+X₁²); t = (X₁²-Y₁², Y₁²-X₁²)
-       fep_blend(rXY, rXY, t_fep) // rA = (X₁²+Y₁², Y₁²-X₁²) i.e. (Y₁²+X₁², Y₁²-X₁²) i.e. (Y', Z')
+       // load (2Z₁², (X₁+Y₁)²)
+       const ZXpY_0 = v128.load(changetype<usize>(rZT), 0 << 4)
+       const ZXpY_1 = v128.load(changetype<usize>(rZT), 1 << 4)
+       const ZXpY_2 = v128.load(changetype<usize>(rZT), 2 << 4)
+       const ZXpY_3 = v128.load(changetype<usize>(rZT), 3 << 4)
+       const ZXpY_4 = v128.load(changetype<usize>(rZT), 4 << 4)
 
-       fep_swap(t_fep, rZT) // t = ((X₁+Y₁)², 2Z₁²)
-       fep_sub(rZT, t_fep, rXY) // rB = ((X₁+Y₁)²-(Y₁²+X₁²), 2Z₁²-(Y₁²-X₁²)) i.e. (X', T')
+       // swap ((X₁+Y₁)², 2Z₁²)
+       const XpYZ_0 = v128.shuffle<i32>(ZXpY_0, ZXpY_0, 1, 0, 3, 2)
+       const XpYZ_1 = v128.shuffle<i32>(ZXpY_1, ZXpY_1, 1, 0, 3, 2)
+       const XpYZ_2 = v128.shuffle<i32>(ZXpY_2, ZXpY_2, 1, 0, 3, 2)
+       const XpYZ_3 = v128.shuffle<i32>(ZXpY_3, ZXpY_3, 1, 0, 3, 2)
+       const XpYZ_4 = v128.shuffle<i32>(ZXpY_4, ZXpY_4, 1, 0, 3, 2)
 
-       fep_swap(t_fep, rXY) // t = (Z', Y')
-       fep_blend(rXY, rZT, t_fep) // rA = (X', Y')
-       fep_blend(rZT, t_fep, rZT) // rA = (Z', T')
+       // subtract ((X₁+Y₁)²-(Y₁²+X₁²), 2Z₁²-(Y₁²-X₁²)) i.e. (X', T')
+       const h0 = v128.sub<i32>(XpYZ_0, YpXYmX_0)
+       const h1 = v128.sub<i32>(XpYZ_1, YpXYmX_1)
+       const h2 = v128.sub<i32>(XpYZ_2, YpXYmX_2)
+       const h3 = v128.sub<i32>(XpYZ_3, YpXYmX_3)
+       const h4 = v128.sub<i32>(XpYZ_4, YpXYmX_4)
+
+       // swap (Z', Y')
+       const YmXYpX_0 = v128.shuffle<i32>(YpXYmX_0, YpXYmX_0, 1, 0, 3, 2)
+       const YmXYpX_1 = v128.shuffle<i32>(YpXYmX_1, YpXYmX_1, 1, 0, 3, 2)
+       const YmXYpX_2 = v128.shuffle<i32>(YpXYmX_2, YpXYmX_2, 1, 0, 3, 2)
+       const YmXYpX_3 = v128.shuffle<i32>(YpXYmX_3, YpXYmX_3, 1, 0, 3, 2)
+       const YmXYpX_4 = v128.shuffle<i32>(YpXYmX_4, YpXYmX_4, 1, 0, 3, 2)
+
+       // blend (X', Y')
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h0, YmXYpX_0, 0, 5, 2, 7), 0 << 4)
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h1, YmXYpX_1, 0, 5, 2, 7), 1 << 4)
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h2, YmXYpX_2, 0, 5, 2, 7), 2 << 4)
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h3, YmXYpX_3, 0, 5, 2, 7), 3 << 4)
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(h4, YmXYpX_4, 0, 5, 2, 7), 4 << 4)
+
+       // blend (Z', T')
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_0, h0, 0, 5, 2, 7), 0 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_1, h1, 0, 5, 2, 7), 1 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_2, h2, 0, 5, 2, 7), 2 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_3, h3, 0, 5, 2, 7), 3 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(YmXYpX_4, h4, 0, 5, 2, 7), 4 << 4)
+}
+
+/**
+ * `r = 2p`
+ *
+ * ```
+ * X = (X₁+Y₁)²-(Y₁²+X₁²)
+ * Y = Y₁²+X₁²
+ * Z = Y₁²-X₁²
+ * T = 2Z₁²-(Y₁²-X₁²)
+ * ```
+ */
+export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY: FieldElementPair, pZ: FieldElementPair): void {
+       gepair_p2_dbl_sq2(rZT, pXY, pZ)
+       gepair_p2_dbl_sq(rXY, rZT, pXY)
 }
 
 /**