]> git.codecow.com Git - nano25519.git/commitdiff
Eliminate extra accumulators in single multiplication.
authorChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 06:24:06 +0000 (23:24 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 06:24:06 +0000 (23:24 -0700)
src/assembly/ed25519/fe.ts

index f2c871e79d604fafc62f212ca09dce4c85daad47..f2a4fea83adecee9c4ea8ad86b3dc9903995150a 100644 (file)
@@ -264,12 +264,9 @@ 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 {
-       // mask to select even lane from first vector and odd lane from second
+       // mask to select (v1, v0, v1, v0)
        const m: v128 = i32x4(-1, 0, -1, 0)
 
-       // mask to double odd-on-odd indexes
-       const odds: v128 = i32x4(-1, 0, -1, 0)
-
        const f0: v128 = v128.load_splat<i32>(changetype<usize>(f), 0 << 2)
        let f1: v128 = v128.load_splat<i32>(changetype<usize>(f), 1 << 2)
        const f2: v128 = v128.load_splat<i32>(changetype<usize>(f), 2 << 2)
@@ -285,18 +282,18 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        const g4567: v128 = v128.load(changetype<usize>(g), 1 << 4)
        const g89xx: v128 = v128.load64_zero(changetype<usize>(g), 2 << 4)
 
-       const g9012: v128 = v128.shuffle(g89xx, g0123, 1, 4, 5, 6)
-       const g3456: v128 = v128.shuffle(g0123, g4567, 3, 4, 5, 6)
-       const g789x: v128 = v128.shuffle(g4567, g89xx, 3, 4, 5, 6)
+       const g9012: v128 = v128.shuffle<i32>(g89xx, g0123, 1, 4, 5, 6)
+       const g3456: v128 = v128.shuffle<i32>(g0123, g4567, 3, 4, 5, 6)
+       const g78xx: v128 = v128.shuffle<i32>(g4567, g89xx, 3, 4, 6, 7)
 
        // factor of 19 for wrap from h9 to h0
        const g0123_19: v128 = v128.add<i32>(g0123, v128.add<i32>(v128.shl<i32>(g0123, 1), v128.shl<i32>(g0123, 4)))
        const g4567_19: v128 = v128.add<i32>(g4567, v128.add<i32>(v128.shl<i32>(g4567, 1), v128.shl<i32>(g4567, 4)))
        const g89xx_19: v128 = v128.add<i32>(g89xx, v128.add<i32>(v128.shl<i32>(g89xx, 1), v128.shl<i32>(g89xx, 4)))
 
-       const g9012_19: v128 = v128.add<i32>(g9012, v128.add<i32>(v128.shl<i32>(g9012, 1), v128.shl<i32>(g9012, 4)))
+       const g9012_19: v128 = v128.bitselect(v128.add<i32>(g9012, v128.add<i32>(v128.shl<i32>(g9012, 1), v128.shl<i32>(g9012, 4))), g9012, i32x4(-1, 0, -1, -1))
        const g3456_19: v128 = v128.add<i32>(g3456, v128.add<i32>(v128.shl<i32>(g3456, 1), v128.shl<i32>(g3456, 4)))
-       const g789x_19: v128 = v128.add<i32>(g789x, v128.add<i32>(v128.shl<i32>(g789x, 1), v128.shl<i32>(g789x, 4)))
+       const g78xx_19: v128 = v128.add<i32>(g78xx, v128.add<i32>(v128.shl<i32>(g78xx, 1), v128.shl<i32>(g78xx, 4)))
 
        // f[0]
        let h01: v128 = i64x2.extmul_low_i32x4_s(f0, g0123)
@@ -306,13 +303,13 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        let h89: v128 = i64x2.extmul_low_i32x4_s(f0, g89xx)
 
        // f[1]
-       f1 = v128.add<i32>(f1, v128.and(f1, odds))
+       f1 = v128.add<i32>(f1, v128.and(f1, m))
 
-       let t0: v128 = i64x2.extmul_low_i32x4_s(f1, v128.bitselect(g9012, g9012_19, m))
+       let t0: v128 = i64x2.extmul_low_i32x4_s(f1, g9012_19)
        let t1: v128 = i64x2.extmul_high_i32x4_s(f1, g9012)
        let t2: v128 = i64x2.extmul_low_i32x4_s(f1, g3456)
        let t3: v128 = i64x2.extmul_high_i32x4_s(f1, g3456)
-       let t4: v128 = i64x2.extmul_low_i32x4_s(f1, g789x)
+       let t4: v128 = i64x2.extmul_low_i32x4_s(f1, g78xx)
 
        h01 = i64x2.add(h01, t0)
        h23 = i64x2.add(h23, t1)
@@ -334,28 +331,26 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        h01 = i64x2.add(h01, t4)
 
        // f[3]
-       f3 = v128.add<i32>(f3, v128.and(f3, odds))
+       f3 = v128.add<i32>(f3, v128.and(f3, m))
 
-       t0 = i64x2.extmul_low_i32x4_s(f3, g0123)
-       t1 = i64x2.extmul_high_i32x4_s(f3, g0123)
-       t2 = i64x2.extmul_low_i32x4_s(f3, g4567)
-       t3 = i64x2.extmul_high_i32x4_s(f3, v128.bitselect(g4567, g4567_19, m))
-       t4 = i64x2.extmul_low_i32x4_s(f3, g89xx_19)
+       t0 = i64x2.extmul_low_i32x4_s(f3, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f3, g9012)
+       t2 = i64x2.extmul_low_i32x4_s(f3, g3456)
+       t3 = i64x2.extmul_high_i32x4_s(f3, g3456)
+       t4 = i64x2.extmul_low_i32x4_s(f3, g78xx_19)
 
-       h01 = i64x2.add(h01, t0)
-       h23 = i64x2.add(h23, t1)
-       h45 = i64x2.add(h45, t2)
-       h67 = i64x2.add(h67, t3)
-       h89 = i64x2.add(h89, t4)
+       h23 = i64x2.add(h23, t0)
+       h45 = i64x2.add(h45, t1)
+       h67 = i64x2.add(h67, t2)
+       h89 = i64x2.add(h89, t3)
+       h01 = i64x2.add(h01, t4)
 
        // f[4]
-       f_i = v128.load_splat<i32>(changetype<usize>(f), 4 << 2)
-
-       t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
-       t1 = i64x2.extmul_high_i32x4_s(f_i, g0123)
-       t2 = i64x2.extmul_low_i32x4_s(f_i, g4567)
-       t3 = i64x2.extmul_high_i32x4_s(f_i, g4567_19)
-       t4 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19)
+       t0 = i64x2.extmul_low_i32x4_s(f4, g0123)
+       t1 = i64x2.extmul_high_i32x4_s(f4, g0123)
+       t2 = i64x2.extmul_low_i32x4_s(f4, g4567)
+       t3 = i64x2.extmul_high_i32x4_s(f4, g4567_19)
+       t4 = i64x2.extmul_low_i32x4_s(f4, g89xx_19)
 
        h45 = i64x2.add(h45, t0)
        h67 = i64x2.add(h67, t1)
@@ -364,29 +359,26 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        h23 = i64x2.add(h23, t4)
 
        // f[5]
-       f_i = v128.load_splat<i32>(changetype<usize>(f), 5 << 2)
-       f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
+       f5 = v128.add<i32>(f5, v128.and(f5, m))
 
-       t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
-       t1 = i64x2.extmul_high_i32x4_s(f_i, g0123)
-       t2 = i64x2.extmul_low_i32x4_s(f_i, v128.bitselect(g4567, g4567_19, m))
-       t3 = i64x2.extmul_high_i32x4_s(f_i, g4567_19)
-       t4 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19)
+       t0 = i64x2.extmul_low_i32x4_s(f5, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f5, g9012)
+       t2 = i64x2.extmul_low_i32x4_s(f5, g3456)
+       t3 = i64x2.extmul_high_i32x4_s(f5, g3456_19)
+       t4 = i64x2.extmul_low_i32x4_s(f5, g78xx_19)
 
-       h56 = i64x2.add(h56, t0)
-       h78 = i64x2.add(h78, t1)
-       h90 = i64x2.add(h90, t2)
-       h12 = i64x2.add(h12, t3)
-       h34 = i64x2.add(h34, t4)
+       h45 = i64x2.add(h45, t0)
+       h67 = i64x2.add(h67, t1)
+       h89 = i64x2.add(h89, t2)
+       h01 = i64x2.add(h01, t3)
+       h23 = i64x2.add(h23, t4)
 
        // f[6]
-       f_i = v128.load_splat<i32>(changetype<usize>(f), 6 << 2)
-
-       t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
-       t1 = i64x2.extmul_high_i32x4_s(f_i, g0123)
-       t2 = i64x2.extmul_low_i32x4_s(f_i, g4567_19)
-       t3 = i64x2.extmul_high_i32x4_s(f_i, g4567_19)
-       t4 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19)
+       t0 = i64x2.extmul_low_i32x4_s(f6, g0123)
+       t1 = i64x2.extmul_high_i32x4_s(f6, g0123)
+       t2 = i64x2.extmul_low_i32x4_s(f6, g4567_19)
+       t3 = i64x2.extmul_high_i32x4_s(f6, g4567_19)
+       t4 = i64x2.extmul_low_i32x4_s(f6, g89xx_19)
 
        h67 = i64x2.add(h67, t0)
        h89 = i64x2.add(h89, t1)
@@ -395,29 +387,26 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        h45 = i64x2.add(h45, t4)
 
        // f[7]
-       f_i = v128.load_splat<i32>(changetype<usize>(f), 7 << 2)
-       f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
+       f7 = v128.add<i32>(f7, v128.and(f7, m))
 
-       t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
-       t1 = i64x2.extmul_high_i32x4_s(f_i, v128.bitselect(g0123, g0123_19, m))
-       t2 = i64x2.extmul_low_i32x4_s(f_i, g4567_19)
-       t3 = i64x2.extmul_high_i32x4_s(f_i, g4567_19)
-       t4 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19)
+       t0 = i64x2.extmul_low_i32x4_s(f7, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f7, g9012)
+       t2 = i64x2.extmul_low_i32x4_s(f7, g3456_19)
+       t3 = i64x2.extmul_high_i32x4_s(f7, g3456_19)
+       t4 = i64x2.extmul_low_i32x4_s(f7, g78xx_19)
 
-       h78 = i64x2.add(h78, t0)
-       h90 = i64x2.add(h90, t1)
-       h12 = i64x2.add(h12, t2)
-       h34 = i64x2.add(h34, t3)
-       h56 = i64x2.add(h56, t4)
+       h67 = i64x2.add(h67, t0)
+       h89 = i64x2.add(h89, t1)
+       h01 = i64x2.add(h01, t2)
+       h23 = i64x2.add(h23, t3)
+       h45 = i64x2.add(h45, t4)
 
        // f[8]
-       f_i = v128.load_splat<i32>(changetype<usize>(f), 8 << 2)
-
-       t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
-       t1 = i64x2.extmul_high_i32x4_s(f_i, g0123_19)
-       t2 = i64x2.extmul_low_i32x4_s(f_i, g4567_19)
-       t3 = i64x2.extmul_high_i32x4_s(f_i, g4567_19)
-       t4 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19)
+       t0 = i64x2.extmul_low_i32x4_s(f8, g0123)
+       t1 = i64x2.extmul_high_i32x4_s(f8, g0123_19)
+       t2 = i64x2.extmul_low_i32x4_s(f8, g4567_19)
+       t3 = i64x2.extmul_high_i32x4_s(f8, g4567_19)
+       t4 = i64x2.extmul_low_i32x4_s(f8, g89xx_19)
 
        h89 = i64x2.add(h89, t0)
        h01 = i64x2.add(h01, t1)
@@ -426,32 +415,31 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        h67 = i64x2.add(h67, t4)
 
        // f[9]
-       f_i = v128.load_splat<i32>(changetype<usize>(f), 9 << 2)
-       f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
+       f9 = v128.add<i32>(f9, v128.and(f9, m))
 
-       t0 = i64x2.extmul_low_i32x4_s(f_i, v128.bitselect(g0123, g0123_19, m))
-       t1 = i64x2.extmul_high_i32x4_s(f_i, g0123_19)
-       t2 = i64x2.extmul_low_i32x4_s(f_i, g4567_19)
-       t3 = i64x2.extmul_high_i32x4_s(f_i, g4567_19)
-       t4 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19)
+       t0 = i64x2.extmul_low_i32x4_s(f9, g9012_19)
+       t1 = i64x2.extmul_high_i32x4_s(f9, g9012_19)
+       t2 = i64x2.extmul_low_i32x4_s(f9, g3456_19)
+       t3 = i64x2.extmul_high_i32x4_s(f9, g3456_19)
+       t4 = i64x2.extmul_low_i32x4_s(f9, g78xx_19)
 
-       h90 = i64x2.add(h90, t0)
-       h12 = i64x2.add(h12, t1)
-       h34 = i64x2.add(h34, t2)
-       h56 = i64x2.add(h56, t3)
-       h78 = i64x2.add(h78, t4)
+       h89 = i64x2.add(h89, t0)
+       h01 = i64x2.add(h01, t1)
+       h23 = i64x2.add(h23, t2)
+       h45 = i64x2.add(h45, t3)
+       h67 = i64x2.add(h67, t4)
 
        // extract scalars from vectors
-       let h0: i64 = i64x2.extract_lane(h90, 1) + i64x2.extract_lane(h01, 0)
-       let h1: i64 = i64x2.extract_lane(h01, 1) + i64x2.extract_lane(h12, 0)
-       let h2: i64 = i64x2.extract_lane(h12, 1) + i64x2.extract_lane(h23, 0)
-       let h3: i64 = i64x2.extract_lane(h23, 1) + i64x2.extract_lane(h34, 0)
-       let h4: i64 = i64x2.extract_lane(h34, 1) + i64x2.extract_lane(h45, 0)
-       let h5: i64 = i64x2.extract_lane(h45, 1) + i64x2.extract_lane(h56, 0)
-       let h6: i64 = i64x2.extract_lane(h56, 1) + i64x2.extract_lane(h67, 0)
-       let h7: i64 = i64x2.extract_lane(h67, 1) + i64x2.extract_lane(h78, 0)
-       let h8: i64 = i64x2.extract_lane(h78, 1) + i64x2.extract_lane(h89, 0)
-       let h9: i64 = i64x2.extract_lane(h89, 1) + i64x2.extract_lane(h90, 0)
+       let h0: i64 = i64x2.extract_lane(h01, 0)
+       let h1: i64 = i64x2.extract_lane(h01, 1)
+       let h2: i64 = i64x2.extract_lane(h23, 0)
+       let h3: i64 = i64x2.extract_lane(h23, 1)
+       let h4: i64 = i64x2.extract_lane(h45, 0)
+       let h5: i64 = i64x2.extract_lane(h45, 1)
+       let h6: i64 = i64x2.extract_lane(h67, 0)
+       let h7: i64 = i64x2.extract_lane(h67, 1)
+       let h8: i64 = i64x2.extract_lane(h89, 0)
+       let h9: i64 = i64x2.extract_lane(h89, 1)
 
        // reduce scalars with carry
        let carry0: i64