]> git.codecow.com Git - nano25519.git/commitdiff
initial move of fe_mul
authorChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 04:29:44 +0000 (21:29 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 04:29:44 +0000 (21:29 -0700)
src/assembly/ed25519/fe.ts

index 2f5c24e53284b60487571c3902dad7679e1a9f11..f2c871e79d604fafc62f212ca09dce4c85daad47 100644 (file)
@@ -268,44 +268,64 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        const m: v128 = i32x4(-1, 0, -1, 0)
 
        // mask to double odd-on-odd indexes
-       const odds: v128 = i32x4(0, -1, 0, -1)
-
-       const g0123: v128 = v128.load(changetype<usize>(g), 0)
-       const g4567: v128 = v128.load(changetype<usize>(g), 16)
-       const g89xx: v128 = v128.load64_zero(changetype<usize>(g), 32)
+       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)
+       let f3: v128 = v128.load_splat<i32>(changetype<usize>(f), 3 << 2)
+       const f4: v128 = v128.load_splat<i32>(changetype<usize>(f), 4 << 2)
+       let f5: v128 = v128.load_splat<i32>(changetype<usize>(f), 5 << 2)
+       const f6: v128 = v128.load_splat<i32>(changetype<usize>(f), 6 << 2)
+       let f7: v128 = v128.load_splat<i32>(changetype<usize>(f), 7 << 2)
+       const f8: v128 = v128.load_splat<i32>(changetype<usize>(f), 8 << 2)
+       let f9: v128 = v128.load_splat<i32>(changetype<usize>(f), 9 << 2)
+
+       const g0123: v128 = v128.load(changetype<usize>(g), 0 << 4)
+       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)
 
        // 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)))
 
-       // f[0]
-       let f_i: v128 = v128.load_splat<i32>(changetype<usize>(f), 0 << 2)
+       const g9012_19: v128 = v128.add<i32>(g9012, v128.add<i32>(v128.shl<i32>(g9012, 1), v128.shl<i32>(g9012, 4)))
+       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)))
 
-       let h01: v128 = i64x2.extmul_low_i32x4_s(f_i, g0123)
-       let h23: v128 = i64x2.extmul_high_i32x4_s(f_i, g0123)
-       let h45: v128 = i64x2.extmul_low_i32x4_s(f_i, g4567)
-       let h67: v128 = i64x2.extmul_high_i32x4_s(f_i, g4567)
-       let h89: v128 = i64x2.extmul_low_i32x4_s(f_i, g89xx)
+       // f[0]
+       let h01: v128 = i64x2.extmul_low_i32x4_s(f0, g0123)
+       let h23: v128 = i64x2.extmul_high_i32x4_s(f0, g0123)
+       let h45: v128 = i64x2.extmul_low_i32x4_s(f0, g4567)
+       let h67: v128 = i64x2.extmul_high_i32x4_s(f0, g4567)
+       let h89: v128 = i64x2.extmul_low_i32x4_s(f0, g89xx)
 
        // f[1]
-       f_i = v128.load_splat<i32>(changetype<usize>(f), 1 << 2)
-       f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
+       f1 = v128.add<i32>(f1, v128.and(f1, odds))
 
-       let h12: v128 = i64x2.extmul_low_i32x4_s(f_i, g0123)
-       let h34: v128 = i64x2.extmul_high_i32x4_s(f_i, g0123)
-       let h56: v128 = i64x2.extmul_low_i32x4_s(f_i, g4567)
-       let h78: v128 = i64x2.extmul_high_i32x4_s(f_i, g4567)
-       let h90: v128 = i64x2.extmul_low_i32x4_s(f_i, v128.bitselect(g89xx, g89xx_19, m))
+       let t0: v128 = i64x2.extmul_low_i32x4_s(f1, v128.bitselect(g9012, g9012_19, m))
+       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)
 
-       // f[2]
-       f_i = v128.load_splat<i32>(changetype<usize>(f), 2 << 2)
+       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)
 
-       let t0: v128 = i64x2.extmul_low_i32x4_s(f_i, g0123)
-       let t1: v128 = i64x2.extmul_high_i32x4_s(f_i, g0123)
-       let t2: v128 = i64x2.extmul_low_i32x4_s(f_i, g4567)
-       let t3: v128 = i64x2.extmul_high_i32x4_s(f_i, g4567)
-       let t4: v128 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19)
+       // f[2]
+       t0 = i64x2.extmul_low_i32x4_s(f2, g0123)
+       t1 = i64x2.extmul_high_i32x4_s(f2, g0123)
+       t2 = i64x2.extmul_low_i32x4_s(f2, g4567)
+       t3 = i64x2.extmul_high_i32x4_s(f2, g4567)
+       t4 = i64x2.extmul_low_i32x4_s(f2, g89xx_19)
 
        h23 = i64x2.add(h23, t0)
        h45 = i64x2.add(h45, t1)
@@ -314,20 +334,19 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        h01 = i64x2.add(h01, t4)
 
        // f[3]
-       f_i = v128.load_splat<i32>(changetype<usize>(f), 3 << 2)
-       f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
-
-       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, v128.bitselect(g4567, g4567_19, m))
-       t4 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19)
+       f3 = v128.add<i32>(f3, v128.and(f3, odds))
 
-       h34 = i64x2.add(h34, t0)
-       h56 = i64x2.add(h56, t1)
-       h78 = i64x2.add(h78, t2)
-       h90 = i64x2.add(h90, t3)
-       h12 = i64x2.add(h12, t4)
+       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)
+
+       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)
 
        // f[4]
        f_i = v128.load_splat<i32>(changetype<usize>(f), 4 << 2)