From 707a32d0d978244a25bbd02e352c9953c78c8712 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 28 Oct 2026 13:28:07 -0700 Subject: [PATCH] Simplify legibility with multiplication instead of add-shift. --- src/assembly/ed25519/fe.ts | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 3f1c178..230c12d 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -403,14 +403,15 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void const g78xx: v128 = v128.shuffle(g4567, g89xx, 3, 4, 6, 7) // 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))) + const w: v128 = i32x4.splat(19) + const g0123_19: v128 = v128.mul(g0123, w) + const g4567_19: v128 = v128.mul(g4567, w) + const g89xx_19: v128 = v128.mul(g89xx, w) // 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))) + const g9012_19: v128 = v128.mul(g9012, i32x4(19, 1, 19, 19)) + const g3456_19: v128 = v128.mul(g3456, w) + const g78xx_19: v128 = v128.mul(g78xx, w) // f[0] let h01: v128 = i64x2.extmul_low_i32x4_s(f0, g0123) @@ -715,11 +716,12 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle const g89: v128 = v128.shuffle(g_a_89xx, g_b_89xx, 0, 4, 1, 5) // 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))) - const g67_19: v128 = v128.add(g67, v128.add(v128.add(g67, g67), v128.shl(g67, 4))) - const g89_19: v128 = v128.add(g89, v128.add(v128.add(g89, g89), v128.shl(g89, 4))) + const w: v128 = i32x4.splat(19) + const g01_19: v128 = v128.mul(g01, w) + const g23_19: v128 = v128.mul(g23, w) + const g45_19: v128 = v128.mul(g45, w) + const g67_19: v128 = v128.mul(g67, w) + const g89_19: v128 = v128.mul(g89, w) // f[0] let h0: v128 = v128.extmul_low(f0, g01) @@ -1005,11 +1007,12 @@ export function fe_mul2_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEl const g89: v128 = v128.shuffle(g_a_89xx, g_b_89xx, 0, 4, 1, 5) // 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))) - const g67_19: v128 = v128.add(g67, v128.add(v128.add(g67, g67), v128.shl(g67, 4))) - const g89_19: v128 = v128.add(g89, v128.add(v128.add(g89, g89), v128.shl(g89, 4))) + const w: v128 = i32x4.splat(19) + const g01_19: v128 = v128.mul(g01, w) + const g23_19: v128 = v128.mul(g23, w) + const g45_19: v128 = v128.mul(g45, w) + const g67_19: v128 = v128.mul(g67, w) + const g89_19: v128 = v128.mul(g89, w) // f[0] let h0: v128 = v128.extmul_low(f0, g01) -- 2.52.0