From aaccc2877246daed7f71ae0c27de6daeadbc34ee Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 26 Oct 2026 14:33:22 -0700 Subject: [PATCH] Rearrange doubling. --- src/assembly/ed25519/p2.ts | 82 +++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/assembly/ed25519/p2.ts b/src/assembly/ed25519/p2.ts index c63b442..3b4d6c5 100644 --- a/src/assembly/ed25519/p2.ts +++ b/src/assembly/ed25519/p2.ts @@ -58,6 +58,13 @@ export function gepair_p2_dbl (rXY: FieldElementPair, rZT: FieldElementPair, pXY const XY_3 = v128.load(changetype(pXY), 3 << 4) const XY_4 = v128.load(changetype(pXY), 4 << 4) + // load (Z₁, Z₁) ignore second lane + const Z_0 = v128.load(changetype(pZ), 0 << 4) + const Z_1 = v128.load(changetype(pZ), 1 << 4) + const Z_2 = v128.load(changetype(pZ), 2 << 4) + const Z_3 = v128.load(changetype(pZ), 3 << 4) + const Z_4 = v128.load(changetype(pZ), 4 << 4) + // add lanes (Y₁+X₁, X₁+Y₁) const XpY0 = v128.add(XY_0, v128.shuffle(XY_0, XY_0, 1, 0, 3, 2)) const XpY1 = v128.add(XY_1, v128.shuffle(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(XY_3, v128.shuffle(XY_3, XY_3, 1, 0, 3, 2)) const XpY4 = v128.add(XY_4, v128.shuffle(XY_4, XY_4, 1, 0, 3, 2)) - // load (Z₁, Z₁) ignore second lane - const pZ_0 = v128.load(changetype(pZ), 0 << 4) - const pZ_1 = v128.load(changetype(pZ), 1 << 4) - const pZ_2 = v128.load(changetype(pZ), 2 << 4) - const pZ_3 = v128.load(changetype(pZ), 3 << 4) - const pZ_4 = v128.load(changetype(pZ), 4 << 4) - // blend (Z₁, X₁+Y₁) - v128.store(changetype(rZT), v128.shuffle(pZ_0, XpY0, 0, 5, 2, 7), 0 << 4) - v128.store(changetype(rZT), v128.shuffle(pZ_1, XpY1, 0, 5, 2, 7), 1 << 4) - v128.store(changetype(rZT), v128.shuffle(pZ_2, XpY2, 0, 5, 2, 7), 2 << 4) - v128.store(changetype(rZT), v128.shuffle(pZ_3, XpY3, 0, 5, 2, 7), 3 << 4) - v128.store(changetype(rZT), v128.shuffle(pZ_4, XpY4, 0, 5, 2, 7), 4 << 4) + v128.store(changetype(rZT), v128.shuffle(Z_0, XpY0, 0, 5, 2, 7), 0 << 4) + v128.store(changetype(rZT), v128.shuffle(Z_1, XpY1, 0, 5, 2, 7), 1 << 4) + v128.store(changetype(rZT), v128.shuffle(Z_2, XpY2, 0, 5, 2, 7), 2 << 4) + v128.store(changetype(rZT), v128.shuffle(Z_3, XpY3, 0, 5, 2, 7), 3 << 4) + v128.store(changetype(rZT), v128.shuffle(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(rXY), 3 << 4) const x2y2_4 = v128.load(changetype(rXY), 4 << 4) + // load (2Z₁², (X₁+Y₁)²) + const ZXpY_0 = v128.load(changetype(rZT), 0 << 4) + const ZXpY_1 = v128.load(changetype(rZT), 1 << 4) + const ZXpY_2 = v128.load(changetype(rZT), 2 << 4) + const ZXpY_3 = v128.load(changetype(rZT), 3 << 4) + const ZXpY_4 = v128.load(changetype(rZT), 4 << 4) + // swap (Y₁², X₁²) const y2x2_0 = v128.shuffle(x2y2_0, x2y2_0, 1, 0, 3, 2) const y2x2_1 = v128.shuffle(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(plus_3, minus_3, 0, 5, 2, 7) const YpXYmX_4 = v128.shuffle(plus_4, minus_4, 0, 5, 2, 7) - // load (2Z₁², (X₁+Y₁)²) - const ZXpY_0 = v128.load(changetype(rZT), 0 << 4) - const ZXpY_1 = v128.load(changetype(rZT), 1 << 4) - const ZXpY_2 = v128.load(changetype(rZT), 2 << 4) - const ZXpY_3 = v128.load(changetype(rZT), 3 << 4) - const ZXpY_4 = v128.load(changetype(rZT), 4 << 4) - // swap ((X₁+Y₁)², 2Z₁²) const XpYZ_0 = v128.shuffle(ZXpY_0, ZXpY_0, 1, 0, 3, 2) const XpYZ_1 = v128.shuffle(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(ZXpY_4, ZXpY_4, 1, 0, 3, 2) // subtract ((X₁+Y₁)²-(Y₁²+X₁²), 2Z₁²-(Y₁²-X₁²)) i.e. (X', T') - const h0 = v128.sub(XpYZ_0, YpXYmX_0) - const h1 = v128.sub(XpYZ_1, YpXYmX_1) - const h2 = v128.sub(XpYZ_2, YpXYmX_2) - const h3 = v128.sub(XpYZ_3, YpXYmX_3) - const h4 = v128.sub(XpYZ_4, YpXYmX_4) - - // swap (Z', Y') - const YmXYpX_0 = v128.shuffle(YpXYmX_0, YpXYmX_0, 1, 0, 3, 2) - const YmXYpX_1 = v128.shuffle(YpXYmX_1, YpXYmX_1, 1, 0, 3, 2) - const YmXYpX_2 = v128.shuffle(YpXYmX_2, YpXYmX_2, 1, 0, 3, 2) - const YmXYpX_3 = v128.shuffle(YpXYmX_3, YpXYmX_3, 1, 0, 3, 2) - const YmXYpX_4 = v128.shuffle(YpXYmX_4, YpXYmX_4, 1, 0, 3, 2) + const rXT_0 = v128.sub(XpYZ_0, YpXYmX_0) + const rXT_1 = v128.sub(XpYZ_1, YpXYmX_1) + const rXT_2 = v128.sub(XpYZ_2, YpXYmX_2) + const rXT_3 = v128.sub(XpYZ_3, YpXYmX_3) + const rXT_4 = v128.sub(XpYZ_4, YpXYmX_4) + + // swap (2Z₁²-(Y₁²-X₁²), (X₁+Y₁)²-(Y₁²+X₁²)) i.e. (Z', Y') + const rZY_0 = v128.shuffle(YpXYmX_0, YpXYmX_0, 1, 0, 3, 2) + const rZY_1 = v128.shuffle(YpXYmX_1, YpXYmX_1, 1, 0, 3, 2) + const rZY_2 = v128.shuffle(YpXYmX_2, YpXYmX_2, 1, 0, 3, 2) + const rZY_3 = v128.shuffle(YpXYmX_3, YpXYmX_3, 1, 0, 3, 2) + const rZY_4 = v128.shuffle(YpXYmX_4, YpXYmX_4, 1, 0, 3, 2) // blend (X', Y') - v128.store(changetype(rXY), v128.shuffle(h0, YmXYpX_0, 0, 5, 2, 7), 0 << 4) - v128.store(changetype(rXY), v128.shuffle(h1, YmXYpX_1, 0, 5, 2, 7), 1 << 4) - v128.store(changetype(rXY), v128.shuffle(h2, YmXYpX_2, 0, 5, 2, 7), 2 << 4) - v128.store(changetype(rXY), v128.shuffle(h3, YmXYpX_3, 0, 5, 2, 7), 3 << 4) - v128.store(changetype(rXY), v128.shuffle(h4, YmXYpX_4, 0, 5, 2, 7), 4 << 4) + v128.store(changetype(rXY), v128.shuffle(rXT_0, rZY_0, 0, 5, 2, 7), 0 << 4) + v128.store(changetype(rXY), v128.shuffle(rXT_1, rZY_1, 0, 5, 2, 7), 1 << 4) + v128.store(changetype(rXY), v128.shuffle(rXT_2, rZY_2, 0, 5, 2, 7), 2 << 4) + v128.store(changetype(rXY), v128.shuffle(rXT_3, rZY_3, 0, 5, 2, 7), 3 << 4) + v128.store(changetype(rXY), v128.shuffle(rXT_4, rZY_4, 0, 5, 2, 7), 4 << 4) // blend (Z', T') - v128.store(changetype(rZT), v128.shuffle(YmXYpX_0, h0, 0, 5, 2, 7), 0 << 4) - v128.store(changetype(rZT), v128.shuffle(YmXYpX_1, h1, 0, 5, 2, 7), 1 << 4) - v128.store(changetype(rZT), v128.shuffle(YmXYpX_2, h2, 0, 5, 2, 7), 2 << 4) - v128.store(changetype(rZT), v128.shuffle(YmXYpX_3, h3, 0, 5, 2, 7), 3 << 4) - v128.store(changetype(rZT), v128.shuffle(YmXYpX_4, h4, 0, 5, 2, 7), 4 << 4) + v128.store(changetype(rZT), v128.shuffle(rZY_0, rXT_0, 0, 5, 2, 7), 0 << 4) + v128.store(changetype(rZT), v128.shuffle(rZY_1, rXT_1, 0, 5, 2, 7), 1 << 4) + v128.store(changetype(rZT), v128.shuffle(rZY_2, rXT_2, 0, 5, 2, 7), 2 << 4) + v128.store(changetype(rZT), v128.shuffle(rZY_3, rXT_3, 0, 5, 2, 7), 3 << 4) + v128.store(changetype(rZT), v128.shuffle(rZY_4, rXT_4, 0, 5, 2, 7), 4 << 4) } /** -- 2.52.0