]> git.codecow.com Git - nano25519.git/commitdiff
Rearrange doubling. next/redesign-group-ops
authorChris Duncan <chris@codecow.com>
Mon, 26 Oct 2026 21:33:22 +0000 (14:33 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 26 Oct 2026 21:33:22 +0000 (14:33 -0700)
src/assembly/ed25519/p2.ts

index c63b442a7f393117ea37e21139a702b378e8fb5c..3b4d6c5e38ab1e831a11ef50059b7d20ee6e00a9 100644 (file)
@@ -58,6 +58,13 @@ export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY
        const XY_3 = v128.load(changetype<usize>(pXY), 3 << 4)
        const XY_4 = v128.load(changetype<usize>(pXY), 4 << 4)
 
+       // load (Z₁, Z₁) ignore second lane
+       const Z_0 = v128.load(changetype<usize>(pZ), 0 << 4)
+       const Z_1 = v128.load(changetype<usize>(pZ), 1 << 4)
+       const Z_2 = v128.load(changetype<usize>(pZ), 2 << 4)
+       const Z_3 = v128.load(changetype<usize>(pZ), 3 << 4)
+       const Z_4 = v128.load(changetype<usize>(pZ), 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))
@@ -65,19 +72,12 @@ export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY
        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)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(Z_0, XpY0, 0, 5, 2, 7), 0 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(Z_1, XpY1, 0, 5, 2, 7), 1 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(Z_2, XpY2, 0, 5, 2, 7), 2 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(Z_3, XpY3, 0, 5, 2, 7), 3 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(Z_4, XpY4, 0, 5, 2, 7), 4 << 4)
 
        // square both lanes and double first lane (2Z₁², (X₁+Y₁)²)
        fep_sq2(rZT, rZT)
@@ -92,6 +92,13 @@ export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY
        const x2y2_3 = v128.load(changetype<usize>(rXY), 3 << 4)
        const x2y2_4 = v128.load(changetype<usize>(rXY), 4 << 4)
 
+       // 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)
+
        // 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)
@@ -120,13 +127,6 @@ export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY
        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)
 
-       // 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)
-
        // 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)
@@ -135,32 +135,32 @@ export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY
        const XpYZ_4 = v128.shuffle<i32>(ZXpY_4, ZXpY_4, 1, 0, 3, 2)
 
        // 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)
+       const rXT_0 = v128.sub<i32>(XpYZ_0, YpXYmX_0)
+       const rXT_1 = v128.sub<i32>(XpYZ_1, YpXYmX_1)
+       const rXT_2 = v128.sub<i32>(XpYZ_2, YpXYmX_2)
+       const rXT_3 = v128.sub<i32>(XpYZ_3, YpXYmX_3)
+       const rXT_4 = v128.sub<i32>(XpYZ_4, YpXYmX_4)
+
+       // swap (2Z₁²-(Y₁²-X₁²), (X₁+Y₁)²-(Y₁²+X₁²)) i.e. (Z', Y')
+       const rZY_0 = v128.shuffle<i32>(YpXYmX_0, YpXYmX_0, 1, 0, 3, 2)
+       const rZY_1 = v128.shuffle<i32>(YpXYmX_1, YpXYmX_1, 1, 0, 3, 2)
+       const rZY_2 = v128.shuffle<i32>(YpXYmX_2, YpXYmX_2, 1, 0, 3, 2)
+       const rZY_3 = v128.shuffle<i32>(YpXYmX_3, YpXYmX_3, 1, 0, 3, 2)
+       const rZY_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)
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(rXT_0, rZY_0, 0, 5, 2, 7), 0 << 4)
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(rXT_1, rZY_1, 0, 5, 2, 7), 1 << 4)
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(rXT_2, rZY_2, 0, 5, 2, 7), 2 << 4)
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(rXT_3, rZY_3, 0, 5, 2, 7), 3 << 4)
+       v128.store(changetype<usize>(rXY), v128.shuffle<i32>(rXT_4, rZY_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)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(rZY_0, rXT_0, 0, 5, 2, 7), 0 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(rZY_1, rXT_1, 0, 5, 2, 7), 1 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(rZY_2, rXT_2, 0, 5, 2, 7), 2 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(rZY_3, rXT_3, 0, 5, 2, 7), 3 << 4)
+       v128.store(changetype<usize>(rZT), v128.shuffle<i32>(rZY_4, rXT_4, 0, 5, 2, 7), 4 << 4)
 }
 
 /**