From b2321461d2e15481c40cfc53501926611a5c835e Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 19 Oct 2026 13:27:53 -0700 Subject: [PATCH] Improve parallel multiplication store to memory. --- src/assembly/ed25519/fe.ts | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 3921285..27d63e0 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -757,28 +757,21 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle h0 = v128.sub(h0, v128.shl(carry0, 26)) h1 = v128.add(h1, carry0) - // extract and assign results to parallel outputs - store(changetype(h_a), i64x2.extract_lane(h0, 0), 0) - store(changetype(h_a), i64x2.extract_lane(h1, 0), 4) - store(changetype(h_a), i64x2.extract_lane(h2, 0), 8) - store(changetype(h_a), i64x2.extract_lane(h3, 0), 12) - store(changetype(h_a), i64x2.extract_lane(h4, 0), 16) - store(changetype(h_a), i64x2.extract_lane(h5, 0), 20) - store(changetype(h_a), i64x2.extract_lane(h6, 0), 24) - store(changetype(h_a), i64x2.extract_lane(h7, 0), 28) - store(changetype(h_a), i64x2.extract_lane(h8, 0), 32) - store(changetype(h_a), i64x2.extract_lane(h9, 0), 36) - - store(changetype(h_b), i64x2.extract_lane(h0, 1), 0) - store(changetype(h_b), i64x2.extract_lane(h1, 1), 4) - store(changetype(h_b), i64x2.extract_lane(h2, 1), 8) - store(changetype(h_b), i64x2.extract_lane(h3, 1), 12) - store(changetype(h_b), i64x2.extract_lane(h4, 1), 16) - store(changetype(h_b), i64x2.extract_lane(h5, 1), 20) - store(changetype(h_b), i64x2.extract_lane(h6, 1), 24) - store(changetype(h_b), i64x2.extract_lane(h7, 1), 28) - store(changetype(h_b), i64x2.extract_lane(h8, 1), 32) - store(changetype(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(h0, h1, 0, 4, 2, 6) // h0a, h1a, h0b, h1b + const h23: v128 = v128.shuffle(h2, h3, 0, 4, 2, 6) // h2a, h3a, h2b, h3b + const h45: v128 = v128.shuffle(h4, h5, 0, 4, 2, 6) // h4a, h5a, h4b, h5b + const h67: v128 = v128.shuffle(h6, h7, 0, 4, 2, 6) // h6a, h7a, h6b, h7b + const h89: v128 = v128.shuffle(h8, h9, 0, 4, 2, 6) // h8a, h9a, h8b, h9b + + // assign results to parallel outputs + v128.store(changetype(h_a), v128.shuffle(h01, h23, 0, 1, 4, 5), 0) + v128.store(changetype(h_a), v128.shuffle(h45, h67, 0, 1, 4, 5), 16) + v128.store_lane(changetype(h_a), h89, 0, 32) + + v128.store(changetype(h_b), v128.shuffle(h01, h23, 2, 3, 6, 7), 0) + v128.store(changetype(h_b), v128.shuffle(h45, h67, 2, 3, 6, 7), 16) + v128.store_lane(changetype(h_b), h89, 1, 32) } /** -- 2.52.0