From f5e522b58082ceffa73a54dbf40b5978e9b35194 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 28 Oct 2026 07:47:10 -0700 Subject: [PATCH] Adjust vector loads and shuffles for field element multiplication and squaring for both consistency and performance. --- src/assembly/ed25519/fe.ts | 117 ++++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 28 deletions(-) diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 6fb4c8d..74a7c9d 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -322,38 +322,53 @@ export function fe_iszero (f: FieldElement, t_fe: FieldElement, t_u8x32: StaticA * @param g FieldElement multiplicand source */ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void { + // Load all 10 limbs of `f` const f0123: v128 = v128.load(changetype(f), 0 << 4) const f4567: v128 = v128.load(changetype(f), 1 << 4) const f89xx: v128 = v128.load64_zero(changetype(f), 2 << 4) - const f0123_2: v128 = v128.add(f0123, f0123) - const f4567_2: v128 = v128.add(f4567, f4567) - const f89xx_2: v128 = v128.add(f89xx, f89xx) - + // Splat even limbs const f0: v128 = v128.shuffle(f0123, f0123, 0, 0, 0, 0) - const f1: v128 = v128.shuffle(f0123_2, f0123, 1, 5, 1, 5) const f2: v128 = v128.shuffle(f0123, f0123, 2, 2, 2, 2) - const f3: v128 = v128.shuffle(f0123_2, f0123, 3, 7, 3, 7) const f4: v128 = v128.shuffle(f4567, f4567, 0, 0, 0, 0) - const f5: v128 = v128.shuffle(f4567_2, f4567, 1, 5, 1, 5) const f6: v128 = v128.shuffle(f4567, f4567, 2, 2, 2, 2) - const f7: v128 = v128.shuffle(f4567_2, f4567, 3, 7, 3, 7) const f8: v128 = v128.shuffle(f89xx, f89xx, 0, 0, 0, 0) - const f9: v128 = v128.shuffle(f89xx_2, f89xx, 1, 5, 1, 5) + // Splat, double, and blend odd limbs + const f1_1: v128 = v128.shuffle(f0123, f0123, 1, 1, 1, 1) + const f3_1: v128 = v128.shuffle(f0123, f0123, 3, 3, 3, 3) + const f5_1: v128 = v128.shuffle(f4567, f4567, 1, 1, 1, 1) + const f7_1: v128 = v128.shuffle(f4567, f4567, 3, 3, 3, 3) + const f9_1: v128 = v128.shuffle(f89xx, f89xx, 1, 1, 1, 1) + + const f1_2: v128 = v128.add(f1_1, f1_1) + const f3_2: v128 = v128.add(f3_1, f3_1) + const f5_2: v128 = v128.add(f5_1, f5_1) + const f7_2: v128 = v128.add(f7_1, f7_1) + const f9_2: v128 = v128.add(f9_1, f9_1) + + const f1: v128 = v128.shuffle(f1_2, f1_1, 0, 5, 0, 5) + const f3: v128 = v128.shuffle(f3_2, f3_1, 0, 5, 0, 5) + const f5: v128 = v128.shuffle(f5_2, f5_1, 0, 5, 0, 5) + const f7: v128 = v128.shuffle(f7_2, f7_1, 0, 5, 0, 5) + const f9: v128 = v128.shuffle(f9_2, f9_1, 0, 5, 0, 5) + + // Load all 10 limbs of `g` const g0123: v128 = v128.load(changetype(g), 0 << 4) const g4567: v128 = v128.load(changetype(g), 1 << 4) const g89xx: v128 = v128.load64_zero(changetype(g), 2 << 4) + // Shift limbs to align with even-odd `f` limbs const g9012: v128 = v128.shuffle(g89xx, g0123, 1, 4, 5, 6) const g3456: v128 = v128.shuffle(g0123, g4567, 3, 4, 5, 6) const g78xx: v128 = v128.shuffle(g4567, g89xx, 3, 4, 6, 7) - // factor of 19 for wrap from h9 to h0 + // Multiply by 19 for wrap from h9 to h0 since 2²⁵⁵ ≡ 19 (mod `p`) const g0123_19: v128 = v128.add(g0123, v128.add(v128.add(g0123, g0123), v128.shl(g0123, 4))) const g4567_19: v128 = v128.add(g4567, v128.add(v128.add(g4567, g4567), v128.shl(g4567, 4))) const g89xx_19: v128 = v128.add(g89xx, v128.add(v128.add(g89xx, g89xx), v128.shl(g89xx, 4))) + // Multiply shifts by 19 except limb 0 which never wraps const g9012_19: v128 = v128.bitselect(v128.add(g9012, v128.add(v128.add(g9012, g9012), v128.shl(g9012, 4))), g9012, i32x4(-1, 0, -1, -1)) const g3456_19: v128 = v128.add(g3456, v128.add(v128.add(g3456, g3456), v128.shl(g3456, 4))) const g78xx_19: v128 = v128.add(g78xx, v128.add(v128.add(g78xx, g78xx), v128.shl(g78xx, 4))) @@ -597,7 +612,7 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void * @param g_b FieldElement B multiplicand source */ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldElement, h_b: FieldElement, f_b: FieldElement, g_b: FieldElement): void { - // pack each f index twice + // Load all 10 limbs of each `f` const f_a_0123: v128 = v128.load(changetype(f_a), 0 << 4) const f_a_4567: v128 = v128.load(changetype(f_a), 1 << 4) const f_a_89xx: v128 = v128.load64_zero(changetype(f_a), 2 << 4) @@ -606,24 +621,40 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle const f_b_4567: v128 = v128.load(changetype(f_b), 1 << 4) const f_b_89xx: v128 = v128.load64_zero(changetype(f_b), 2 << 4) + // Blend each `f` index in alternating lanes const f01: v128 = v128.shuffle(f_a_0123, f_b_0123, 0, 4, 1, 5) const f23: v128 = v128.shuffle(f_a_0123, f_b_0123, 2, 6, 3, 7) const f45: v128 = v128.shuffle(f_a_4567, f_b_4567, 0, 4, 1, 5) const f67: v128 = v128.shuffle(f_a_4567, f_b_4567, 2, 6, 3, 7) const f89: v128 = v128.shuffle(f_a_89xx, f_b_89xx, 0, 4, 1, 5) + // Splat even limbs const f0: v128 = v128.shuffle(f01, f01, 0, 1, 0, 1) - const f1: v128 = v128.shuffle(f01, v128.add(f01, f01), 2, 3, 6, 7) const f2: v128 = v128.shuffle(f23, f23, 0, 1, 0, 1) - const f3: v128 = v128.shuffle(f23, v128.add(f23, f23), 2, 3, 6, 7) const f4: v128 = v128.shuffle(f45, f45, 0, 1, 0, 1) - const f5: v128 = v128.shuffle(f45, v128.add(f45, f45), 2, 3, 6, 7) const f6: v128 = v128.shuffle(f67, f67, 0, 1, 0, 1) - const f7: v128 = v128.shuffle(f67, v128.add(f67, f67), 2, 3, 6, 7) const f8: v128 = v128.shuffle(f89, f89, 0, 1, 0, 1) - const f9: v128 = v128.shuffle(f89, v128.add(f89, f89), 2, 3, 6, 7) - // pack each g index once + // Splat, double, and blend odd limbs + const f1_1: v128 = v128.shuffle(f01, f01, 2, 3, 2, 3) + const f3_1: v128 = v128.shuffle(f23, f23, 2, 3, 2, 3) + const f5_1: v128 = v128.shuffle(f45, f45, 2, 3, 2, 3) + const f7_1: v128 = v128.shuffle(f67, f67, 2, 3, 2, 3) + const f9_1: v128 = v128.shuffle(f89, f89, 2, 3, 2, 3) + + const f1_2: v128 = v128.add(f1_1, f1_1) + const f3_2: v128 = v128.add(f3_1, f3_1) + const f5_2: v128 = v128.add(f5_1, f5_1) + const f7_2: v128 = v128.add(f7_1, f7_1) + const f9_2: v128 = v128.add(f9_1, f9_1) + + const f1: v128 = v128.shuffle(f1_1, f1_2, 0, 1, 4, 5) + const f3: v128 = v128.shuffle(f3_1, f3_2, 0, 1, 4, 5) + const f5: v128 = v128.shuffle(f5_1, f5_2, 0, 1, 4, 5) + const f7: v128 = v128.shuffle(f7_1, f7_2, 0, 1, 4, 5) + const f9: v128 = v128.shuffle(f9_1, f9_2, 0, 1, 4, 5) + + // Load all 10 limbs of each `g` const g_a_0123: v128 = v128.load(changetype(g_a), 0 << 4) const g_a_4567: v128 = v128.load(changetype(g_a), 1 << 4) const g_a_89xx: v128 = v128.load64_zero(changetype(g_a), 2 << 4) @@ -632,13 +663,14 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle const g_b_4567: v128 = v128.load(changetype(g_b), 1 << 4) const g_b_89xx: v128 = v128.load64_zero(changetype(g_b), 2 << 4) + // Blend each `g` index in alternating lanes const g01: v128 = v128.shuffle(g_a_0123, g_b_0123, 0, 4, 1, 5) const g23: v128 = v128.shuffle(g_a_0123, g_b_0123, 2, 6, 3, 7) const g45: v128 = v128.shuffle(g_a_4567, g_b_4567, 0, 4, 1, 5) const g67: v128 = v128.shuffle(g_a_4567, g_b_4567, 2, 6, 3, 7) const g89: v128 = v128.shuffle(g_a_89xx, g_b_89xx, 0, 4, 1, 5) - // factor of 19 for wrap from h9 to h0 + // Multiply by 19 for wrap from h9 to h0 since 2²⁵⁵ ≡ 19 (mod `p`) const g01_19: v128 = v128.add(g01, v128.add(v128.add(g01, g01), v128.shl(g01, 4))) const g23_19: v128 = v128.add(g23, v128.add(v128.add(g23, g23), v128.shl(g23, 4))) const g45_19: v128 = v128.add(g45, v128.add(v128.add(g45, g45), v128.shl(g45, 4))) @@ -864,7 +896,7 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle * @param g_b FieldElement B multiplicand source */ export function fe_mul2_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldElement, h_b: FieldElement, f_b: FieldElement, g_b: FieldElement): void { - // pack each f index twice + // Load all 10 limbs of each `f` const f_a_0123: v128 = v128.load(changetype(f_a), 0 << 4) const f_a_4567: v128 = v128.load(changetype(f_a), 1 << 4) const f_a_89xx: v128 = v128.load64_zero(changetype(f_a), 2 << 4) @@ -873,24 +905,40 @@ export function fe_mul2_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEl const f_b_4567: v128 = v128.load(changetype(f_b), 1 << 4) const f_b_89xx: v128 = v128.load64_zero(changetype(f_b), 2 << 4) + // Blend each `f` index in alternating lanes const f01: v128 = v128.shuffle(f_a_0123, f_b_0123, 0, 4, 1, 5) const f23: v128 = v128.shuffle(f_a_0123, f_b_0123, 2, 6, 3, 7) const f45: v128 = v128.shuffle(f_a_4567, f_b_4567, 0, 4, 1, 5) const f67: v128 = v128.shuffle(f_a_4567, f_b_4567, 2, 6, 3, 7) const f89: v128 = v128.shuffle(f_a_89xx, f_b_89xx, 0, 4, 1, 5) + // Splat even limbs const f0: v128 = v128.shuffle(f01, f01, 0, 1, 0, 1) - const f1: v128 = v128.shuffle(f01, v128.add(f01, f01), 2, 3, 6, 7) const f2: v128 = v128.shuffle(f23, f23, 0, 1, 0, 1) - const f3: v128 = v128.shuffle(f23, v128.add(f23, f23), 2, 3, 6, 7) const f4: v128 = v128.shuffle(f45, f45, 0, 1, 0, 1) - const f5: v128 = v128.shuffle(f45, v128.add(f45, f45), 2, 3, 6, 7) const f6: v128 = v128.shuffle(f67, f67, 0, 1, 0, 1) - const f7: v128 = v128.shuffle(f67, v128.add(f67, f67), 2, 3, 6, 7) const f8: v128 = v128.shuffle(f89, f89, 0, 1, 0, 1) - const f9: v128 = v128.shuffle(f89, v128.add(f89, f89), 2, 3, 6, 7) - // pack each g index once + // Splat, double, and blend odd limbs + const f1_1: v128 = v128.shuffle(f01, f01, 2, 3, 2, 3) + const f3_1: v128 = v128.shuffle(f23, f23, 2, 3, 2, 3) + const f5_1: v128 = v128.shuffle(f45, f45, 2, 3, 2, 3) + const f7_1: v128 = v128.shuffle(f67, f67, 2, 3, 2, 3) + const f9_1: v128 = v128.shuffle(f89, f89, 2, 3, 2, 3) + + const f1_2: v128 = v128.add(f1_1, f1_1) + const f3_2: v128 = v128.add(f3_1, f3_1) + const f5_2: v128 = v128.add(f5_1, f5_1) + const f7_2: v128 = v128.add(f7_1, f7_1) + const f9_2: v128 = v128.add(f9_1, f9_1) + + const f1: v128 = v128.shuffle(f1_1, f1_2, 0, 1, 4, 5) + const f3: v128 = v128.shuffle(f3_1, f3_2, 0, 1, 4, 5) + const f5: v128 = v128.shuffle(f5_1, f5_2, 0, 1, 4, 5) + const f7: v128 = v128.shuffle(f7_1, f7_2, 0, 1, 4, 5) + const f9: v128 = v128.shuffle(f9_1, f9_2, 0, 1, 4, 5) + + // Load all 10 limbs of each `g` const g_a_0123: v128 = v128.load(changetype(g_a), 0 << 4) const g_a_4567: v128 = v128.load(changetype(g_a), 1 << 4) const g_a_89xx: v128 = v128.load64_zero(changetype(g_a), 2 << 4) @@ -899,13 +947,14 @@ export function fe_mul2_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEl const g_b_4567: v128 = v128.load(changetype(g_b), 1 << 4) const g_b_89xx: v128 = v128.load64_zero(changetype(g_b), 2 << 4) + // Blend each `g` index in alternating lanes const g01: v128 = v128.shuffle(g_a_0123, g_b_0123, 0, 4, 1, 5) const g23: v128 = v128.shuffle(g_a_0123, g_b_0123, 2, 6, 3, 7) const g45: v128 = v128.shuffle(g_a_4567, g_b_4567, 0, 4, 1, 5) const g67: v128 = v128.shuffle(g_a_4567, g_b_4567, 2, 6, 3, 7) const g89: v128 = v128.shuffle(g_a_89xx, g_b_89xx, 0, 4, 1, 5) - // factor of 19 for wrap from h9 to h0 + // Multiply by 19 for wrap from h9 to h0 since 2²⁵⁵ ≡ 19 (mod `p`) const g01_19: v128 = v128.add(g01, v128.add(v128.add(g01, g01), v128.shl(g01, 4))) const g23_19: v128 = v128.add(g23, v128.add(v128.add(g23, g23), v128.shl(g23, 4))) const g45_19: v128 = v128.add(g45, v128.add(v128.add(g45, g45), v128.shl(g45, 4))) @@ -1542,6 +1591,7 @@ export function fe_sq (h: FieldElement, f: FieldElement): void { * @param f_b FieldElement B multiplicand source */ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElement, f_b: FieldElement): void { + // Load all 10 limbs of each `f` const f_a_0123: v128 = v128.load(changetype(f_a), 0 << 4) const f_a_4567: v128 = v128.load(changetype(f_a), 1 << 4) const f_a_89xx: v128 = v128.load64_zero(changetype(f_a), 2 << 4) @@ -1550,27 +1600,32 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem const f_b_4567: v128 = v128.load(changetype(f_b), 1 << 4) const f_b_89xx: v128 = v128.load64_zero(changetype(f_b), 2 << 4) + // Blend each `f` index in alternating lanes const f01: v128 = v128.shuffle(f_a_0123, f_b_0123, 0, 4, 1, 5) const f23: v128 = v128.shuffle(f_a_0123, f_b_0123, 2, 6, 3, 7) const f45: v128 = v128.shuffle(f_a_4567, f_b_4567, 0, 4, 1, 5) const f67: v128 = v128.shuffle(f_a_4567, f_b_4567, 2, 6, 3, 7) const f89: v128 = v128.shuffle(f_a_89xx, f_b_89xx, 0, 4, 1, 5) + // Double for square odd-odd and non-square even-even limbs const f01_2: v128 = v128.add(f01, f01) const f23_2: v128 = v128.add(f23, f23) const f45_2: v128 = v128.add(f45, f45) const f67_2: v128 = v128.add(f67, f67) const f89_2: v128 = v128.add(f89, f89) + // Swap doubled limbs to align with even-odd and odd-even limbs const f10_2: v128 = v128.shuffle(f01_2, f01_2, 2, 3, 0, 1) const f32_2: v128 = v128.shuffle(f23_2, f23_2, 2, 3, 0, 1) const f54_2: v128 = v128.shuffle(f45_2, f45_2, 2, 3, 0, 1) const f76_2: v128 = v128.shuffle(f67_2, f67_2, 2, 3, 0, 1) const f98_2: v128 = v128.shuffle(f89_2, f89_2, 2, 3, 0, 1) - // factor of 19 for wrap from h9 to h0 + // Multiply by 19 for wrap from h9 to h0 since 2²⁵⁵ ≡ 19 (mod `p`) const f67_19: v128 = v128.mul(f67, i32x4.splat(19)) const f89_19: v128 = v128.mul(f89, i32x4.splat(19)) + + // Multiply by 38 for odd-odd limb wrapping const f45_38: v128 = v128.mul(f45, i32x4.splat(38)) const f67_38: v128 = v128.add(f67_19, f67_19) const f89_38: v128 = v128.add(f89_19, f89_19) @@ -1757,6 +1812,7 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem * @param f_b FieldElement B base source */ export function fe_sq2_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElement, f_b: FieldElement): void { + // Load all 10 limbs of each `f` const f_a_0123: v128 = v128.load(changetype(f_a), 0 << 4) const f_a_4567: v128 = v128.load(changetype(f_a), 1 << 4) const f_a_89xx: v128 = v128.load64_zero(changetype(f_a), 2 << 4) @@ -1765,27 +1821,32 @@ export function fe_sq2_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldEle const f_b_4567: v128 = v128.load(changetype(f_b), 1 << 4) const f_b_89xx: v128 = v128.load64_zero(changetype(f_b), 2 << 4) + // Blend each `f` index in alternating lanes const f01: v128 = v128.shuffle(f_a_0123, f_b_0123, 0, 4, 1, 5) const f23: v128 = v128.shuffle(f_a_0123, f_b_0123, 2, 6, 3, 7) const f45: v128 = v128.shuffle(f_a_4567, f_b_4567, 0, 4, 1, 5) const f67: v128 = v128.shuffle(f_a_4567, f_b_4567, 2, 6, 3, 7) const f89: v128 = v128.shuffle(f_a_89xx, f_b_89xx, 0, 4, 1, 5) + // Double for square odd-odd and non-square even-even limbs const f01_2: v128 = v128.add(f01, f01) const f23_2: v128 = v128.add(f23, f23) const f45_2: v128 = v128.add(f45, f45) const f67_2: v128 = v128.add(f67, f67) const f89_2: v128 = v128.add(f89, f89) + // Swap doubled limbs to align with even-odd and odd-even limbs const f10_2: v128 = v128.shuffle(f01_2, f01_2, 2, 3, 0, 1) const f32_2: v128 = v128.shuffle(f23_2, f23_2, 2, 3, 0, 1) const f54_2: v128 = v128.shuffle(f45_2, f45_2, 2, 3, 0, 1) const f76_2: v128 = v128.shuffle(f67_2, f67_2, 2, 3, 0, 1) const f98_2: v128 = v128.shuffle(f89_2, f89_2, 2, 3, 0, 1) - // factor of 19 for wrap from h9 to h0 + // Multiply by 19 for wrap from h9 to h0 since 2²⁵⁵ ≡ 19 (mod `p`) const f67_19: v128 = v128.mul(f67, i32x4.splat(19)) const f89_19: v128 = v128.mul(f89, i32x4.splat(19)) + + // Multiply by 38 for odd-odd limb wrapping const f45_38: v128 = v128.mul(f45, i32x4.splat(38)) const f67_38: v128 = v128.add(f67_19, f67_19) const f89_38: v128 = v128.add(f89_19, f89_19) -- 2.52.0