From: Chris Duncan Date: Wed, 21 Oct 2026 13:42:53 +0000 (-0700) Subject: Reduce multiplicand loading and use shuffles to select lanes so that doubling can... X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=b0a579717e06bd443c3ad23235a56902cabe7abb;p=nano25519.git Reduce multiplicand loading and use shuffles to select lanes so that doubling can take place up front. --- diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index bf2f0ad..416fc3a 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -264,19 +264,24 @@ export function fe_iszero (f: FieldElement, t_fe: FieldElement, t_u8x32: StaticA * @param g FieldElement multiplicand source */ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void { - // mask to select (v1, v0, v1, v0) - const m: v128 = i32x4(-1, 0, -1, 0) - - const f0: v128 = v128.load_splat(changetype(f), 0 << 2) - let f1: v128 = v128.load_splat(changetype(f), 1 << 2) - const f2: v128 = v128.load_splat(changetype(f), 2 << 2) - let f3: v128 = v128.load_splat(changetype(f), 3 << 2) - const f4: v128 = v128.load_splat(changetype(f), 4 << 2) - let f5: v128 = v128.load_splat(changetype(f), 5 << 2) - const f6: v128 = v128.load_splat(changetype(f), 6 << 2) - let f7: v128 = v128.load_splat(changetype(f), 7 << 2) - const f8: v128 = v128.load_splat(changetype(f), 8 << 2) - let f9: v128 = v128.load_splat(changetype(f), 9 << 2) + const f0123: v128 = v128.load(changetype(f), 0 << 4) + const f4567: v128 = v128.load(changetype(f), 1 << 4) + const f89xx: v128 = v128.load64_zero(changetype(f), 2 << 4) + + const f0123_2: v128 = v128.add(f0123, f0123) + const f4567_2: v128 = v128.add(f4567, f4567) + const f89xx_2: v128 = v128.add(f89xx, f89xx) + + const f0: v128 = v128.shuffle(f0123, f0123, 0, 0, 0, 0) + const f1: v128 = v128.shuffle(f0123_2, f0123, 1, 5, 1, 5) + const f2: v128 = v128.shuffle(f0123, f0123, 2, 2, 2, 2) + const f3: v128 = v128.shuffle(f0123_2, f0123, 3, 7, 3, 7) + const f4: v128 = v128.shuffle(f4567, f4567, 0, 0, 0, 0) + const f5: v128 = v128.shuffle(f4567_2, f4567, 1, 5, 1, 5) + const f6: v128 = v128.shuffle(f4567, f4567, 2, 2, 2, 2) + const f7: v128 = v128.shuffle(f4567_2, f4567, 3, 7, 3, 7) + const f8: v128 = v128.shuffle(f89xx, f89xx, 0, 0, 0, 0) + const f9: v128 = v128.shuffle(f89xx_2, f89xx, 1, 5, 1, 5) const g0123: v128 = v128.load(changetype(g), 0 << 4) const g4567: v128 = v128.load(changetype(g), 1 << 4) @@ -287,13 +292,13 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void const g78xx: v128 = v128.shuffle(g4567, g89xx, 3, 4, 6, 7) // factor of 19 for wrap from h9 to h0 - const g0123_19: v128 = v128.add(g0123, v128.add(v128.shl(g0123, 1), v128.shl(g0123, 4))) - const g4567_19: v128 = v128.add(g4567, v128.add(v128.shl(g4567, 1), v128.shl(g4567, 4))) - const g89xx_19: v128 = v128.add(g89xx, v128.add(v128.shl(g89xx, 1), v128.shl(g89xx, 4))) + 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 g9012_19: v128 = v128.bitselect(v128.add(g9012, v128.add(v128.shl(g9012, 1), v128.shl(g9012, 4))), g9012, i32x4(-1, 0, -1, -1)) - const g3456_19: v128 = v128.add(g3456, v128.add(v128.shl(g3456, 1), v128.shl(g3456, 4))) - const g78xx_19: v128 = v128.add(g78xx, v128.add(v128.shl(g78xx, 1), v128.shl(g78xx, 4))) + 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))) // f[0] let h01: v128 = i64x2.extmul_low_i32x4_s(f0, g0123) @@ -303,8 +308,6 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void let h89: v128 = i64x2.extmul_low_i32x4_s(f0, g89xx) // f[1] - f1 = v128.add(f1, v128.and(f1, m)) - let t0: v128 = i64x2.extmul_low_i32x4_s(f1, g9012_19) let t1: v128 = i64x2.extmul_high_i32x4_s(f1, g9012) let t2: v128 = i64x2.extmul_low_i32x4_s(f1, g3456) @@ -331,8 +334,6 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void h01 = i64x2.add(h01, t4) // f[3] - f3 = v128.add(f3, v128.and(f3, m)) - t0 = i64x2.extmul_low_i32x4_s(f3, g9012_19) t1 = i64x2.extmul_high_i32x4_s(f3, g9012) t2 = i64x2.extmul_low_i32x4_s(f3, g3456) @@ -359,8 +360,6 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void h23 = i64x2.add(h23, t4) // f[5] - f5 = v128.add(f5, v128.and(f5, m)) - t0 = i64x2.extmul_low_i32x4_s(f5, g9012_19) t1 = i64x2.extmul_high_i32x4_s(f5, g9012) t2 = i64x2.extmul_low_i32x4_s(f5, g3456) @@ -387,8 +386,6 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void h45 = i64x2.add(h45, t4) // f[7] - f7 = v128.add(f7, v128.and(f7, m)) - t0 = i64x2.extmul_low_i32x4_s(f7, g9012_19) t1 = i64x2.extmul_high_i32x4_s(f7, g9012) t2 = i64x2.extmul_low_i32x4_s(f7, g3456_19) @@ -415,8 +412,6 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void h67 = i64x2.add(h67, t4) // f[9] - f9 = v128.add(f9, v128.and(f9, m)) - t0 = i64x2.extmul_low_i32x4_s(f9, g9012_19) t1 = i64x2.extmul_high_i32x4_s(f9, g9012_19) t2 = i64x2.extmul_low_i32x4_s(f9, g3456_19)