From: Chris Duncan Date: Mon, 26 Oct 2026 21:11:28 +0000 (-0700) Subject: Extract p2 doubling to save on loads and stores. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=ff2c032677c6885218fa6bdc4d64a53c7de869a5;p=nano25519.git Extract p2 doubling to save on loads and stores. --- diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index eb04419..28e778c 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -498,7 +498,7 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray 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 diff --git a/src/assembly/ed25519/p2.ts b/src/assembly/ed25519/p2.ts index c1872e1..8905e1a 100644 --- a/src/assembly/ed25519/p2.ts +++ b/src/assembly/ed25519/p2.ts @@ -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(pXY), 0 << 4) + const XY_1 = v128.load(changetype(pXY), 1 << 4) + const XY_2 = v128.load(changetype(pXY), 2 << 4) + const XY_3 = v128.load(changetype(pXY), 3 << 4) + const XY_4 = v128.load(changetype(pXY), 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)) + const XpY2 = v128.add(XY_2, v128.shuffle(XY_2, XY_2, 1, 0, 3, 2)) + 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) + + 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(rXY), 0 << 4) + const x2y2_1 = v128.load(changetype(rXY), 1 << 4) + const x2y2_2 = v128.load(changetype(rXY), 2 << 4) + const x2y2_3 = v128.load(changetype(rXY), 3 << 4) + const x2y2_4 = v128.load(changetype(rXY), 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) + const y2x2_2 = v128.shuffle(x2y2_2, x2y2_2, 1, 0, 3, 2) + const y2x2_3 = v128.shuffle(x2y2_3, x2y2_3, 1, 0, 3, 2) + const y2x2_4 = v128.shuffle(x2y2_4, x2y2_4, 1, 0, 3, 2) + + // add (X₁²+Y₁², Y₁²+X₁²) + const plus_0 = v128.add(x2y2_0, y2x2_0) + const plus_1 = v128.add(x2y2_1, y2x2_1) + const plus_2 = v128.add(x2y2_2, y2x2_2) + const plus_3 = v128.add(x2y2_3, y2x2_3) + const plus_4 = v128.add(x2y2_4, y2x2_4) + + + // subtract (X₁²-Y₁², Y₁²-X₁²) + const minus_0 = v128.sub(x2y2_0, y2x2_0) + const minus_1 = v128.sub(x2y2_1, y2x2_1) + const minus_2 = v128.sub(x2y2_2, y2x2_2) + const minus_3 = v128.sub(x2y2_3, y2x2_3) + const minus_4 = v128.sub(x2y2_4, y2x2_4) + + // blend (X₁²+Y₁², Y₁²-X₁²) i.e. (Y₁²+X₁², Y₁²-X₁²) i.e. (Y', Z') + const YpXYmX_0 = v128.shuffle(plus_0, minus_0, 0, 5, 2, 7) + const YpXYmX_1 = v128.shuffle(plus_1, minus_1, 0, 5, 2, 7) + const YpXYmX_2 = v128.shuffle(plus_2, minus_2, 0, 5, 2, 7) + 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) - 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(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) - 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(ZXpY_0, ZXpY_0, 1, 0, 3, 2) + const XpYZ_1 = v128.shuffle(ZXpY_1, ZXpY_1, 1, 0, 3, 2) + const XpYZ_2 = v128.shuffle(ZXpY_2, ZXpY_2, 1, 0, 3, 2) + const XpYZ_3 = v128.shuffle(ZXpY_3, ZXpY_3, 1, 0, 3, 2) + const XpYZ_4 = v128.shuffle(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(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) + + // 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) + + // 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) +} + +/** + * `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) } /**