From 910d735765ed5952991eb1d3f1b1d1578a08d75a Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 30 Oct 2026 10:57:07 -0700 Subject: [PATCH] Extract pairwise multiplication into inlined helper function. --- src/assembly/ed25519/fe.ts | 306 ++++++++++++++++++++----------------- 1 file changed, 170 insertions(+), 136 deletions(-) diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 005129f..606cd93 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -29,9 +29,6 @@ import { load_3, load_4 } from './utils' * * Shuffles are also multiplied by 19 for limb wrapping, except limb 0 which does not wrap. */ -// export const fe_sqrtm1: FieldElement = StaticArray.fromArray([ -// -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482, 0, 0 -// ]) const sqrtm1_0123 = i32x4(-32595792, -7943725, 9377950, 3500415) const sqrtm1_4567 = i32x4(12389472, -272473, -25146209, -2005654) const sqrtm1_89xx = i32x4(326686, 11406482, 0, 0) @@ -612,100 +609,25 @@ function fe_extmul (f: FieldElement, a: FieldElement, b0123: v128, b4567: v128, 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 (f: FieldElement, a: FieldElement, b: FieldElement): void { - // Load all 10 limbs of `b` - const b_ptr: usize = changetype(b) - const b0123: v128 = v128.load(b_ptr, 0 << 4) - 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 `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 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) - const b89xx_19: v128 = v128.mul(b89xx, w) - - // Multiply shifts by 19 except limb 0 which never wraps - const b9012_19: v128 = v128.mul(b9012, i32x4(19, 1, 19, 19)) - const b3456_19: v128 = v128.mul(b3456, w) - const b78xx_19: v128 = v128.mul(b78xx, w) - - fe_extmul(f, a, b0123, b4567, b89xx, b9012, b3456, b78xx, b0123_19, b4567_19, b89xx_19, b9012_19, b3456_19, b78xx_19) -} - -/** - * Multiply a FieldElement with the Ed25519 constant `√-1` and store the product. - * - * @param f FieldElement product destination - * @param a FieldElement multiplicand source - */ -export function fe_mul_sqrtm1 (f: FieldElement, a: FieldElement): void { - fe_extmul(f, a, sqrtm1_0123, sqrtm1_4567, sqrtm1_89xx, sqrtm1_9012, sqrtm1_3456, sqrtm1_78xx, sqrtm1_0123_19, sqrtm1_4567_19, sqrtm1_89xx_19, sqrtm1_9012_19, sqrtm1_3456_19, sqrtm1_78xx_19) -} - -/** - * For two pairs of FieldElements, multiply pairwise in parallel, multiply the - * second lane by the Ed25519 constant `√-1`, and store the product. - * - * @param f_0 FieldElement A product destination - * @param a_0 FieldElement A multiplicand source - * @param b_0 FieldElement A multiplicand source - * @param f_1 FieldElement B product destination - * @param a_1 FieldElement B multiplicand source - * @param b_1 FieldElement B multiplicand source - * - * @param f FieldElement product destination - * @param a FieldElement multiplicand source - */ -export function fe_mul_pair_sqrtm1 (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement): void { - fe_mul(f_0, a_0, b_0) - fe_extmul(f_1, a_1, sqrtm1_0123, sqrtm1_4567, sqrtm1_89xx, sqrtm1_9012, sqrtm1_3456, sqrtm1_78xx, sqrtm1_0123_19, sqrtm1_4567_19, sqrtm1_89xx_19, sqrtm1_9012_19, sqrtm1_3456_19, sqrtm1_78xx_19) -} - -/** - * Multiply a FieldElement with the Ed25519 constant `d` and store the product. - * - * @param f FieldElement product destination - * @param a FieldElement multiplicand source - */ -export function fe_mul_d (f: FieldElement, a: FieldElement): void { - fe_extmul(f, a, d_0123, d_4567, d_89xx, d_9012, d_3456, d_78xx, d_0123_19, d_4567_19, d_89xx_19, d_9012_19, d_3456_19, d_78xx_19) -} - -/** - * Multiply a FieldElement with the Ed25519 constant `2d` and store the product. - * - * @param f FieldElement product destination - * @param a FieldElement multiplicand source - */ -export function fe_mul_2d (f: FieldElement, a: FieldElement): void { - fe_extmul(f, a, d2_0123, d2_4567, d2_89xx, d2_9012, d2_3456, d2_78xx, d2_0123_19, d2_4567_19, d2_89xx_19, d2_9012_19, d2_3456_19, d2_78xx_19) -} - /** * For two pairs of FieldElements, multiply pairwise in parallel and store the * product. * - * @param f_0 FieldElement A product destination - * @param a_0 FieldElement A multiplicand source - * @param b_0 FieldElement A multiplicand source - * @param f_1 FieldElement B product destination - * @param a_1 FieldElement B multiplicand source - * @param b_1 FieldElement B multiplicand source + * Pack two pairs of FieldElements into a series of vectors, multiply pairwise + * in parallel, and store the product. The second operand of the second product + * is passed pre-vectorized so it can accept constants. Inline helper used by + * other `fe_mul_pair` functions; this function need not be exported. + * + * @param f_0 A product destination + * @param a_0 A multiplicand source + * @param b_0 A multiplicand source + * @param f_1 B product destination + * @param a_1 B multiplicand source + * @param b_1 B multiplicand source as 3 separate vectors */ -export function fe_mul_pair (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement, b_1: FieldElement): void { +//@ts-expect-error +@inline +function fe_extmul_pair (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement, b_1_0123: v128, b_1_4567: v128, b_1_89xx: v128): void { // 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) @@ -750,17 +672,12 @@ export function fe_mul_pair (f_0: FieldElement, a_0: FieldElement, b_0: FieldEle 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 `b` + // Load all 10 limbs of first `b`; second `b` is passed as prepacked vectors 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) const b_0_89xx: v128 = v128.load64_zero(b_0_ptr, 2 << 4) - const b_1_ptr: usize = changetype(b_1) - const b_1_0123: v128 = v128.load(b_1_ptr, 0 << 4) - 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 `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) @@ -900,37 +817,23 @@ export function fe_mul_pair (f_0: FieldElement, a_0: FieldElement, b_0: FieldEle } /** - * For two pairs of FieldElements, multiply pairwise in parallel, multiply the - * second lane by the Ed25519 constant `2d`, and store the product. - * - * @param f_0 FieldElement A product destination - * @param a_0 FieldElement A multiplicand source - * @param b_0 FieldElement A multiplicand source - * @param f_1 FieldElement B product destination - * @param a_1 FieldElement B multiplicand source - * @param b_1 FieldElement B multiplicand source - * - * @param f FieldElement product destination - * @param a FieldElement multiplicand source - */ -export function fe_mul_pair_2d (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement, b_1: FieldElement): void { - fe_mul_pair(f_0, a_0, b_0, f_1, a_1, b_1) - fe_extmul(f_1, f_1, d2_0123, d2_4567, d2_89xx, d2_9012, d2_3456, d2_78xx, d2_0123_19, d2_4567_19, d2_89xx_19, d2_9012_19, d2_3456_19, d2_78xx_19) -} - -/** - * For two pairs of FieldElements, multiply pairwise in parallel, double the - * first lane, and store the product. + * Pack two pairs of FieldElements into a series of vectors, multiply pairwise + * in parallel, double the first lane, and store the product. The second operand + * of the second product is passed pre-vectorized so it can accept constants. + * Inline helper used by other `fe_mul_pair` functions; this function need not + * be exported. * - * @param f_0 FieldElement A product destination - * @param a_0 FieldElement A multiplicand source - * @param b_0 FieldElement A multiplicand source - * @param f_1 FieldElement B product destination, doubled - * @param a_1 FieldElement B multiplicand source - * @param b_1 FieldElement B multiplicand source + * @param f_0 A product destination + * @param a_0 A multiplicand source + * @param b_0 A multiplicand source + * @param f_1 B product destination + * @param a_1 B multiplicand source + * @param b_1 B multiplicand source as 3 separate vectors */ -export function fe_mul_pair_dbl (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` +//@ts-expect-error +@inline +function fe_extmul_pair_dbl (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement, b_1_0123: v128, b_1_4567: v128, b_1_89xx: v128): void { + // 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) @@ -941,7 +844,7 @@ export function fe_mul_pair_dbl (f_0: FieldElement, a_0: FieldElement, b_0: Fiel 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) @@ -974,25 +877,20 @@ export function fe_mul_pair_dbl (f_0: FieldElement, a_0: FieldElement, b_0: Fiel 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 first `b`; second `b` is passed as prepacked vectors 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) const b_0_89xx: v128 = v128.load64_zero(b_0_ptr, 2 << 4) - const b_1_ptr: usize = changetype(b_1) - const b_1_0123: v128 = v128.load(b_1_ptr, 0 << 4) - 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) @@ -1136,6 +1034,142 @@ export function fe_mul_pair_dbl (f_0: FieldElement, a_0: FieldElement, b_0: Fiel carry_v128(changetype(f_0), changetype(f_1), 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 (f: FieldElement, a: FieldElement, b: FieldElement): void { + // Load all 10 limbs of `b` + const b_ptr: usize = changetype(b) + const b0123: v128 = v128.load(b_ptr, 0 << 4) + 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 `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 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) + const b89xx_19: v128 = v128.mul(b89xx, w) + + // Multiply shifts by 19 except limb 0 which never wraps + const b9012_19: v128 = v128.mul(b9012, i32x4(19, 1, 19, 19)) + const b3456_19: v128 = v128.mul(b3456, w) + const b78xx_19: v128 = v128.mul(b78xx, w) + + fe_extmul(f, a, b0123, b4567, b89xx, b9012, b3456, b78xx, b0123_19, b4567_19, b89xx_19, b9012_19, b3456_19, b78xx_19) +} + +/** + * Multiply a FieldElement with the Ed25519 constant `√-1` and store the product. + * + * @param f FieldElement product destination + * @param a FieldElement multiplicand source + */ +export function fe_mul_sqrtm1 (f: FieldElement, a: FieldElement): void { + fe_extmul(f, a, sqrtm1_0123, sqrtm1_4567, sqrtm1_89xx, sqrtm1_9012, sqrtm1_3456, sqrtm1_78xx, sqrtm1_0123_19, sqrtm1_4567_19, sqrtm1_89xx_19, sqrtm1_9012_19, sqrtm1_3456_19, sqrtm1_78xx_19) +} + +/** + * For two pairs of FieldElements, multiply pairwise in parallel, multiply the + * second lane by the Ed25519 constant `√-1`, and store the product. + * + * @param f_0 A product destination + * @param a_0 A multiplicand source + * @param b_0 A multiplicand source + * @param f_1 B product destination * √-1 + * @param a_1 B multiplicand source + */ +export function fe_mul_pair_sqrtm1 (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement): void { + fe_extmul_pair(f_0, a_0, b_0, f_1, a_1, sqrtm1_0123, sqrtm1_4567, sqrtm1_89xx) +} + +/** + * Multiply a FieldElement with the Ed25519 constant `d` and store the product. + * + * @param f FieldElement product destination + * @param a FieldElement multiplicand source + */ +export function fe_mul_d (f: FieldElement, a: FieldElement): void { + fe_extmul(f, a, d_0123, d_4567, d_89xx, d_9012, d_3456, d_78xx, d_0123_19, d_4567_19, d_89xx_19, d_9012_19, d_3456_19, d_78xx_19) +} + +/** + * Multiply a FieldElement with the Ed25519 constant `2d` and store the product. + * + * @param f FieldElement product destination + * @param a FieldElement multiplicand source + */ +export function fe_mul_2d (f: FieldElement, a: FieldElement): void { + fe_extmul(f, a, d2_0123, d2_4567, d2_89xx, d2_9012, d2_3456, d2_78xx, d2_0123_19, d2_4567_19, d2_89xx_19, d2_9012_19, d2_3456_19, d2_78xx_19) +} + +/** + * For two pairs of FieldElements, multiply pairwise in parallel and store the + * product. + * + * @param f_0 FieldElement A product destination + * @param a_0 FieldElement A multiplicand source + * @param b_0 FieldElement A multiplicand source + * @param f_1 FieldElement B product destination + * @param a_1 FieldElement B multiplicand source + * @param b_1 FieldElement B multiplicand source + */ +export function fe_mul_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 second `b` to pass to helper function + const b_1_ptr: usize = changetype(b_1) + const b_1_0123: v128 = v128.load(b_1_ptr, 0 << 4) + 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) + fe_extmul_pair(f_0, a_0, b_0, f_1, a_1, b_1_0123, b_1_4567, b_1_89xx) +} + +/** + * For two pairs of FieldElements, multiply pairwise in parallel, multiply the + * second lane by the Ed25519 constant `2d`, and store the product. + * + * @param f_0 FieldElement A product destination + * @param a_0 FieldElement A multiplicand source + * @param b_0 FieldElement A multiplicand source + * @param f_1 FieldElement B product destination + * @param a_1 FieldElement B multiplicand source + * @param b_1 FieldElement B multiplicand source + * + * @param f FieldElement product destination + * @param a FieldElement multiplicand source + */ +export function fe_mul_pair_2d (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement, b_1: FieldElement): void { + fe_mul_pair(f_0, a_0, b_0, f_1, a_1, b_1) + fe_extmul(f_1, f_1, d2_0123, d2_4567, d2_89xx, d2_9012, d2_3456, d2_78xx, d2_0123_19, d2_4567_19, d2_89xx_19, d2_9012_19, d2_3456_19, d2_78xx_19) +} + +/** + * For two pairs of FieldElements, multiply pairwise in parallel, double the + * first lane, and store the product. + * + * @param f_0 FieldElement A product destination + * @param a_0 FieldElement A multiplicand source + * @param b_0 FieldElement A multiplicand source + * @param f_1 FieldElement B product destination, doubled + * @param a_1 FieldElement B multiplicand source + * @param b_1 FieldElement B multiplicand source + */ +export function fe_mul_pair_dbl (f_0: FieldElement, a_0: FieldElement, b_0: FieldElement, f_1: FieldElement, a_1: FieldElement, b_1: FieldElement): void { + // Load all 10 limbs of second `b` to pass to helper function + const b_1_ptr: usize = changetype(b_1) + const b_1_0123: v128 = v128.load(b_1_ptr, 0 << 4) + 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) + fe_extmul_pair_dbl(f_0, a_0, b_0, f_1, a_1, b_1_0123, b_1_4567, b_1_89xx) +} + /** * For two pairs of FieldElements, multiply pairwise in parallel, double the * first lane, multiply the second lane by the Ed25519 constant `2d`, and store -- 2.52.0