]> git.codecow.com Git - nano25519.git/commitdiff
Improve parallel multiplication store to memory.
authorChris Duncan <chris@codecow.com>
Mon, 19 Oct 2026 20:27:53 +0000 (13:27 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 19 Oct 2026 20:27:53 +0000 (13:27 -0700)
src/assembly/ed25519/fe.ts

index 3921285f6d3bef78cea67e30d530dd7a219a70d6..27d63e06a06e2d56ebaecbdf86ee4c2e84ee665a 100644 (file)
@@ -757,28 +757,21 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
        h1 = v128.add<i64>(h1, carry0)
 
-       // extract and assign results to parallel outputs
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h0, 0), 0)
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h1, 0), 4)
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h2, 0), 8)
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h3, 0), 12)
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h4, 0), 16)
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h5, 0), 20)
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h6, 0), 24)
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h7, 0), 28)
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h8, 0), 32)
-       store<i32>(changetype<usize>(h_a), i64x2.extract_lane(h9, 0), 36)
-
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h0, 1), 0)
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h1, 1), 4)
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h2, 1), 8)
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h3, 1), 12)
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h4, 1), 16)
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h5, 1), 20)
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h6, 1), 24)
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h7, 1), 28)
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h8, 1), 32)
-       store<i32>(changetype<usize>(h_b), i64x2.extract_lane(h9, 1), 36)
+       // reduce i64x2 to i32x4 now that only low half has a value and fits into i32
+       const h01: v128 = v128.shuffle<i32>(h0, h1, 0, 4, 2, 6) // h0a, h1a, h0b, h1b
+       const h23: v128 = v128.shuffle<i32>(h2, h3, 0, 4, 2, 6) // h2a, h3a, h2b, h3b
+       const h45: v128 = v128.shuffle<i32>(h4, h5, 0, 4, 2, 6) // h4a, h5a, h4b, h5b
+       const h67: v128 = v128.shuffle<i32>(h6, h7, 0, 4, 2, 6) // h6a, h7a, h6b, h7b
+       const h89: v128 = v128.shuffle<i32>(h8, h9, 0, 4, 2, 6) // h8a, h9a, h8b, h9b
+
+       // assign results to parallel outputs
+       v128.store(changetype<usize>(h_a), v128.shuffle<i32>(h01, h23, 0, 1, 4, 5), 0)
+       v128.store(changetype<usize>(h_a), v128.shuffle<i32>(h45, h67, 0, 1, 4, 5), 16)
+       v128.store_lane<i64>(changetype<usize>(h_a), h89, 0, 32)
+
+       v128.store(changetype<usize>(h_b), v128.shuffle<i32>(h01, h23, 2, 3, 6, 7), 0)
+       v128.store(changetype<usize>(h_b), v128.shuffle<i32>(h45, h67, 2, 3, 6, 7), 16)
+       v128.store_lane<i64>(changetype<usize>(h_b), h89, 1, 32)
 }
 
 /**