From: Chris Duncan Date: Mon, 19 Oct 2026 21:10:16 +0000 (-0700) Subject: Use shifts and adds instead of potentially slower multiplication by constant. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=55fc722b42c61d90eaa2417ef53b1037cdcfe8e0;p=nano25519.git Use shifts and adds instead of potentially slower multiplication by constant. --- diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 4d867f3..7c485aa 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -558,12 +558,11 @@ 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) // factor of 19 for wrap from h9 to h0 - const v19: v128 = i32x4.splat(19) - const g01_19: v128 = v128.mul(g01, v19) - const g23_19: v128 = v128.mul(g23, v19) - const g45_19: v128 = v128.mul(g45, v19) - const g67_19: v128 = v128.mul(g67, v19) - const g89_19: v128 = v128.mul(g89, v19) + const g01_19: v128 = v128.add(g01, v128.add(v128.shl(g01, 1), v128.shl(g01, 4))) + const g23_19: v128 = v128.add(g23, v128.add(v128.shl(g23, 1), v128.shl(g23, 4))) + const g45_19: v128 = v128.add(g45, v128.add(v128.shl(g45, 1), v128.shl(g45, 4))) + const g67_19: v128 = v128.add(g67, v128.add(v128.shl(g67, 1), v128.shl(g67, 4))) + const g89_19: v128 = v128.add(g89, v128.add(v128.shl(g89, 1), v128.shl(g89, 4))) // f[0] let h0: v128 = v128.extmul_low(f0, g01) @@ -751,7 +750,7 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle carry9 = v128.shr(v128.add(h9, bias24), 25) h9 = v128.sub(h9, v128.shl(carry9, 25)) - h0 = v128.add(h0, v128.mul(carry9, i64x2.splat(19))) + h0 = v128.add(h0, v128.add(carry9, v128.add(v128.shl(carry9, 1), v128.shl(carry9, 4)))) carry0 = v128.shr(v128.add(h0, bias25), 26) h0 = v128.sub(h0, v128.shl(carry0, 26))