From a8c835e1475a2654895036ca759ad93f36830f67 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 29 Oct 2026 12:41:38 -0700 Subject: [PATCH] Create wrappers for fe_mul to enable clean multiplication of more than two operands. --- src/assembly/ed25519/fe.ts | 57 +++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 76e20df..2484ced 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -354,11 +354,13 @@ export function fe_iszero (f: FieldElement, t_fe: FieldElement, t_u8x32: StaticA /** * Multiply two FieldElements and store the product. * - * @param h FieldElement product destination + * @param f FieldElement product destination * @param a FieldElement multiplicand source * @param b FieldElement multiplicand source */ -export function fe_mul_ab (h: FieldElement, a: FieldElement, b: FieldElement): void { +//@ts-expect-error +@inline +function fe_mul (f: FieldElement, a: FieldElement, b: FieldElement): void { // Load all 10 limbs of `a` const a_ptr: usize = changetype(a) const a0123: v128 = v128.load(a_ptr, 0 << 4) @@ -397,12 +399,12 @@ export function fe_mul_ab (h: FieldElement, a: FieldElement, b: FieldElement): v const b4567: v128 = v128.load(b_ptr, 1 << 4) const b89xx: v128 = v128.load64_zero(b_ptr, 2 << 4) - // Shift limbs to align with even-odd `f` limbs + // Shift limbs to align with even-odd `a` limbs const b9012: v128 = v128.shuffle(b89xx, b0123, 1, 4, 5, 6) const b3456: v128 = v128.shuffle(b0123, b4567, 3, 4, 5, 6) const b78xx: v128 = v128.shuffle(b4567, b89xx, 3, 4, 6, 7) - // Multiply by 19 for wrap from h9 to h0 since 2²⁵⁵ ≡ 19 (mod `p`) + // Multiply by 19 for wrap from f9 to f0 since 2²⁵⁵ ≡ 19 (mod `p`) const w: v128 = i32x4.splat(19) const b0123_19: v128 = v128.mul(b0123, w) const b4567_19: v128 = v128.mul(b4567, w) @@ -549,7 +551,42 @@ export function fe_mul_ab (h: FieldElement, a: FieldElement, b: FieldElement): v let f8: i64 = i64x2.extract_lane(f89, 0) let f9: i64 = i64x2.extract_lane(f89, 1) - carry_i64(changetype(h), f0, f1, f2, f3, f4, f5, f6, f7, f8, f9) + carry_i64(changetype(f), f0, f1, f2, f3, f4, f5, f6, f7, f8, f9) +} + +/** + * Multiply two FieldElements and store the product. + * + * @param f FieldElement product destination + * @param a FieldElement multiplicand source + * @param b FieldElement multiplicand source + */ +export function fe_mul_ab (f: FieldElement, a: FieldElement, b: FieldElement): void { + fe_mul(f, a, b) +} + +/** + * Multiply three FieldElements and store the product. + * + * @param f FieldElement product destination + * @param a FieldElement multiplicand source + * @param b FieldElement multiplicand source + * @param c FieldElement multiplicand source + */ +export function fe_mul_abc (f: FieldElement, a: FieldElement, b: FieldElement, c: FieldElement): void { + fe_mul(f, a, b) + fe_mul(f, f, c) +} + +/** + * Multiply two FieldElements with precomputed constant and store the product. + * + * @param f FieldElement product destination + * @param a FieldElement multiplicand source + * @param b FieldElement multiplicand source + */ +export function fe_mul_ab_2d (f: FieldElement, a: FieldElement, b: FieldElement): void { + fe_mul(f, a, b) } /** @@ -564,7 +601,7 @@ export function fe_mul_ab (h: FieldElement, a: FieldElement, b: FieldElement): v * @param b_1 FieldElement B multiplicand source */ export function fe_mul_ab_pair (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement, b_1: FieldElement): void { - // Load all 10 limbs of each `f` + // Load all 10 limbs of each `a` const a_0_ptr: usize = changetype(a_0) const a_0_0123: v128 = v128.load(a_0_ptr, 0 << 4) const a_0_4567: v128 = v128.load(a_0_ptr, 1 << 4) @@ -575,7 +612,7 @@ export function fe_mul_ab_pair (f_0: FieldElement, a_0: FieldElement, b_0: Field const a_1_4567: v128 = v128.load(a_1_ptr, 1 << 4) const a_1_89xx: v128 = v128.load64_zero(a_1_ptr, 2 << 4) - // Blend each `f` index in alternating lanes + // Blend each `a` index in alternating lanes const a01: v128 = v128.shuffle(a_0_0123, a_1_0123, 0, 4, 1, 5) const a23: v128 = v128.shuffle(a_0_0123, a_1_0123, 2, 6, 3, 7) const a45: v128 = v128.shuffle(a_0_4567, a_1_4567, 0, 4, 1, 5) @@ -608,7 +645,7 @@ export function fe_mul_ab_pair (f_0: FieldElement, a_0: FieldElement, b_0: Field const a7: v128 = v128.shuffle(a7_1, a7_2, 0, 1, 4, 5) const a9: v128 = v128.shuffle(a9_1, a9_2, 0, 1, 4, 5) - // Load all 10 limbs of each `g` + // Load all 10 limbs of each `b` const b_0_ptr: usize = changetype(b_0) const b_0_0123: v128 = v128.load(b_0_ptr, 0 << 4) const b_0_4567: v128 = v128.load(b_0_ptr, 1 << 4) @@ -619,14 +656,14 @@ export function fe_mul_ab_pair (f_0: FieldElement, a_0: FieldElement, b_0: Field const b_1_4567: v128 = v128.load(b_1_ptr, 1 << 4) const b_1_89xx: v128 = v128.load64_zero(b_1_ptr, 2 << 4) - // Blend each `g` index in alternating lanes + // Blend each `b` index in alternating lanes const b01: v128 = v128.shuffle(b_0_0123, b_1_0123, 0, 4, 1, 5) const b23: v128 = v128.shuffle(b_0_0123, b_1_0123, 2, 6, 3, 7) const b45: v128 = v128.shuffle(b_0_4567, b_1_4567, 0, 4, 1, 5) const b67: v128 = v128.shuffle(b_0_4567, b_1_4567, 2, 6, 3, 7) const b89: v128 = v128.shuffle(b_0_89xx, b_1_89xx, 0, 4, 1, 5) - // Multiply by 19 for wrap from h9 to h0 since 2²⁵⁵ ≡ 19 (mod `p`) + // Multiply by 19 for wrap from f9 to f0 since 2²⁵⁵ ≡ 19 (mod `p`) const w: v128 = i32x4.splat(19) const b01_19: v128 = v128.mul(b01, w) const b23_19: v128 = v128.mul(b23, w) -- 2.52.0