From: Chris Duncan Date: Wed, 21 Oct 2026 04:29:44 +0000 (-0700) Subject: initial move of fe_mul X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=c516e955e93f4d8fe31a4b87bfd97cbe8a9cfc29;p=nano25519.git initial move of fe_mul --- diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 2f5c24e..f2c871e 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -268,44 +268,64 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void const m: v128 = i32x4(-1, 0, -1, 0) // mask to double odd-on-odd indexes - const odds: v128 = i32x4(0, -1, 0, -1) - - const g0123: v128 = v128.load(changetype(g), 0) - const g4567: v128 = v128.load(changetype(g), 16) - const g89xx: v128 = v128.load64_zero(changetype(g), 32) + const odds: 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 g0123: v128 = v128.load(changetype(g), 0 << 4) + const g4567: v128 = v128.load(changetype(g), 1 << 4) + const g89xx: v128 = v128.load64_zero(changetype(g), 2 << 4) + + const g9012: v128 = v128.shuffle(g89xx, g0123, 1, 4, 5, 6) + const g3456: v128 = v128.shuffle(g0123, g4567, 3, 4, 5, 6) + const g789x: v128 = v128.shuffle(g4567, g89xx, 3, 4, 5, 6) // 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))) - // f[0] - let f_i: v128 = v128.load_splat(changetype(f), 0 << 2) + const g9012_19: v128 = v128.add(g9012, v128.add(v128.shl(g9012, 1), v128.shl(g9012, 4))) + const g3456_19: v128 = v128.add(g3456, v128.add(v128.shl(g3456, 1), v128.shl(g3456, 4))) + const g789x_19: v128 = v128.add(g789x, v128.add(v128.shl(g789x, 1), v128.shl(g789x, 4))) - let h01: v128 = i64x2.extmul_low_i32x4_s(f_i, g0123) - let h23: v128 = i64x2.extmul_high_i32x4_s(f_i, g0123) - let h45: v128 = i64x2.extmul_low_i32x4_s(f_i, g4567) - let h67: v128 = i64x2.extmul_high_i32x4_s(f_i, g4567) - let h89: v128 = i64x2.extmul_low_i32x4_s(f_i, g89xx) + // f[0] + let h01: v128 = i64x2.extmul_low_i32x4_s(f0, g0123) + let h23: v128 = i64x2.extmul_high_i32x4_s(f0, g0123) + let h45: v128 = i64x2.extmul_low_i32x4_s(f0, g4567) + let h67: v128 = i64x2.extmul_high_i32x4_s(f0, g4567) + let h89: v128 = i64x2.extmul_low_i32x4_s(f0, g89xx) // f[1] - f_i = v128.load_splat(changetype(f), 1 << 2) - f_i = v128.add(f_i, v128.and(f_i, odds)) + f1 = v128.add(f1, v128.and(f1, odds)) - let h12: v128 = i64x2.extmul_low_i32x4_s(f_i, g0123) - let h34: v128 = i64x2.extmul_high_i32x4_s(f_i, g0123) - let h56: v128 = i64x2.extmul_low_i32x4_s(f_i, g4567) - let h78: v128 = i64x2.extmul_high_i32x4_s(f_i, g4567) - let h90: v128 = i64x2.extmul_low_i32x4_s(f_i, v128.bitselect(g89xx, g89xx_19, m)) + let t0: v128 = i64x2.extmul_low_i32x4_s(f1, v128.bitselect(g9012, g9012_19, m)) + let t1: v128 = i64x2.extmul_high_i32x4_s(f1, g9012) + let t2: v128 = i64x2.extmul_low_i32x4_s(f1, g3456) + let t3: v128 = i64x2.extmul_high_i32x4_s(f1, g3456) + let t4: v128 = i64x2.extmul_low_i32x4_s(f1, g789x) - // f[2] - f_i = v128.load_splat(changetype(f), 2 << 2) + h01 = i64x2.add(h01, t0) + h23 = i64x2.add(h23, t1) + h45 = i64x2.add(h45, t2) + h67 = i64x2.add(h67, t3) + h89 = i64x2.add(h89, t4) - let t0: v128 = i64x2.extmul_low_i32x4_s(f_i, g0123) - let t1: v128 = i64x2.extmul_high_i32x4_s(f_i, g0123) - let t2: v128 = i64x2.extmul_low_i32x4_s(f_i, g4567) - let t3: v128 = i64x2.extmul_high_i32x4_s(f_i, g4567) - let t4: v128 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19) + // f[2] + t0 = i64x2.extmul_low_i32x4_s(f2, g0123) + t1 = i64x2.extmul_high_i32x4_s(f2, g0123) + t2 = i64x2.extmul_low_i32x4_s(f2, g4567) + t3 = i64x2.extmul_high_i32x4_s(f2, g4567) + t4 = i64x2.extmul_low_i32x4_s(f2, g89xx_19) h23 = i64x2.add(h23, t0) h45 = i64x2.add(h45, t1) @@ -314,20 +334,19 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void h01 = i64x2.add(h01, t4) // f[3] - f_i = v128.load_splat(changetype(f), 3 << 2) - f_i = v128.add(f_i, v128.and(f_i, odds)) - - t0 = i64x2.extmul_low_i32x4_s(f_i, g0123) - t1 = i64x2.extmul_high_i32x4_s(f_i, g0123) - t2 = i64x2.extmul_low_i32x4_s(f_i, g4567) - t3 = i64x2.extmul_high_i32x4_s(f_i, v128.bitselect(g4567, g4567_19, m)) - t4 = i64x2.extmul_low_i32x4_s(f_i, g89xx_19) + f3 = v128.add(f3, v128.and(f3, odds)) - h34 = i64x2.add(h34, t0) - h56 = i64x2.add(h56, t1) - h78 = i64x2.add(h78, t2) - h90 = i64x2.add(h90, t3) - h12 = i64x2.add(h12, t4) + t0 = i64x2.extmul_low_i32x4_s(f3, g0123) + t1 = i64x2.extmul_high_i32x4_s(f3, g0123) + t2 = i64x2.extmul_low_i32x4_s(f3, g4567) + t3 = i64x2.extmul_high_i32x4_s(f3, v128.bitselect(g4567, g4567_19, m)) + t4 = i64x2.extmul_low_i32x4_s(f3, g89xx_19) + + h01 = i64x2.add(h01, t0) + h23 = i64x2.add(h23, t1) + h45 = i64x2.add(h45, t2) + h67 = i64x2.add(h67, t3) + h89 = i64x2.add(h89, t4) // f[4] f_i = v128.load_splat(changetype(f), 4 << 2)