From: Chris Duncan Date: Mon, 19 Oct 2026 20:06:47 +0000 (-0700) Subject: Add parallel multiplication function. Performance is even at this point with main. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=95b578c964394b01cfa903d320aa61a879bded9e;p=nano25519.git Add parallel multiplication function. Performance is even at this point with main. --- diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 484379f..3921285 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -507,6 +507,280 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void store(changetype(h), h9, 36) } +/** + * For two pairs of FieldElements, multiply pairwise in parallel and store the + * product. + * + * @param h_a FieldElement A product destination + * @param f_a FieldElement A multiplicand source + * @param g_a FieldElement A multiplicand source + * @param h_b FieldElement B product destination + * @param f_b FieldElement B multiplicand source + * @param g_b FieldElement B multiplicand source + */ +export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldElement, h_b: FieldElement, f_b: FieldElement, g_b: FieldElement): void { + // mask to double odd-on-odd indexes + const odds: v128 = i32x4(0, 0, -1, -1) + + // pack each f index twice + const f_a_0123: v128 = v128.load(changetype(f_a), 0) + const f_b_0123: v128 = v128.load(changetype(f_b), 0) + const f0: v128 = v128.shuffle(f_a_0123, f_b_0123, 0, 4, 0, 4) + let f1: v128 = v128.shuffle(f_a_0123, f_b_0123, 1, 5, 1, 5) + const f2: v128 = v128.shuffle(f_a_0123, f_b_0123, 2, 6, 2, 6) + let f3: v128 = v128.shuffle(f_a_0123, f_b_0123, 3, 7, 3, 7) + + const f_a_4567: v128 = v128.load(changetype(f_a), 16) + const f_b_4567: v128 = v128.load(changetype(f_b), 16) + const f4: v128 = v128.shuffle(f_a_4567, f_b_4567, 0, 4, 0, 4) + let f5: v128 = v128.shuffle(f_a_4567, f_b_4567, 1, 5, 1, 5) + const f6: v128 = v128.shuffle(f_a_4567, f_b_4567, 2, 6, 2, 6) + let f7: v128 = v128.shuffle(f_a_4567, f_b_4567, 3, 7, 3, 7) + + const f_a_89xx: v128 = v128.load(changetype(f_a), 32) + const f_b_89xx: v128 = v128.load(changetype(f_b), 32) + const f8: v128 = v128.shuffle(f_a_89xx, f_b_89xx, 0, 4, 0, 4) + let f9: v128 = v128.shuffle(f_a_89xx, f_b_89xx, 1, 5, 1, 5) + + // pack each g index once + const g_a_0123: v128 = v128.load(changetype(g_a), 0) + const g_b_0123: v128 = v128.load(changetype(g_b), 0) + const g01: v128 = v128.shuffle(g_a_0123, g_b_0123, 0, 4, 1, 5) + const g23: v128 = v128.shuffle(g_a_0123, g_b_0123, 2, 6, 3, 7) + + const g_a_4567: v128 = v128.load(changetype(g_a), 16) + const g_b_4567: v128 = v128.load(changetype(g_b), 16) + const g45: v128 = v128.shuffle(g_a_4567, g_b_4567, 0, 4, 1, 5) + const g67: v128 = v128.shuffle(g_a_4567, g_b_4567, 2, 6, 3, 7) + + const g_a_89xx: v128 = v128.load(changetype(g_a), 32) + const g_b_89xx: v128 = v128.load(changetype(g_b), 32) + 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) + + // f[0] + let h0: v128 = v128.extmul_low(f0, g01) + let h1: v128 = v128.extmul_high(f0, g01) + let h2: v128 = v128.extmul_low(f0, g23) + let h3: v128 = v128.extmul_high(f0, g23) + let h4: v128 = v128.extmul_low(f0, g45) + let h5: v128 = v128.extmul_high(f0, g45) + let h6: v128 = v128.extmul_low(f0, g67) + let h7: v128 = v128.extmul_high(f0, g67) + let h8: v128 = v128.extmul_low(f0, g89) + let h9: v128 = v128.extmul_high(f0, g89) + + // f[1] + f1 = v128.add(f1, v128.and(f1, odds)) + + h1 = v128.add(h1, v128.extmul_low(f1, g01)) + h2 = v128.add(h2, v128.extmul_high(f1, g01)) + h3 = v128.add(h3, v128.extmul_low(f1, g23)) + h4 = v128.add(h4, v128.extmul_high(f1, g23)) + h5 = v128.add(h5, v128.extmul_low(f1, g45)) + h6 = v128.add(h6, v128.extmul_high(f1, g45)) + h7 = v128.add(h7, v128.extmul_low(f1, g67)) + h8 = v128.add(h8, v128.extmul_high(f1, g67)) + h9 = v128.add(h9, v128.extmul_low(f1, g89)) + h0 = v128.add(h0, v128.extmul_high(f1, g89_19)) + + // f[2] + h2 = v128.add(h2, v128.extmul_low(f2, g01)) + h3 = v128.add(h3, v128.extmul_high(f2, g01)) + h4 = v128.add(h4, v128.extmul_low(f2, g23)) + h5 = v128.add(h5, v128.extmul_high(f2, g23)) + h6 = v128.add(h6, v128.extmul_low(f2, g45)) + h7 = v128.add(h7, v128.extmul_high(f2, g45)) + h8 = v128.add(h8, v128.extmul_low(f2, g67)) + h9 = v128.add(h9, v128.extmul_high(f2, g67)) + h0 = v128.add(h0, v128.extmul_low(f2, g89_19)) + h1 = v128.add(h1, v128.extmul_high(f2, g89_19)) + + // f[3] + f3 = v128.add(f3, v128.and(f3, odds)) + + h3 = v128.add(h3, v128.extmul_low(f3, g01)) + h4 = v128.add(h4, v128.extmul_high(f3, g01)) + h5 = v128.add(h5, v128.extmul_low(f3, g23)) + h6 = v128.add(h6, v128.extmul_high(f3, g23)) + h7 = v128.add(h7, v128.extmul_low(f3, g45)) + h8 = v128.add(h8, v128.extmul_high(f3, g45)) + h9 = v128.add(h9, v128.extmul_low(f3, g67)) + h0 = v128.add(h0, v128.extmul_high(f3, g67_19)) + h1 = v128.add(h1, v128.extmul_low(f3, g89_19)) + h2 = v128.add(h2, v128.extmul_high(f3, g89_19)) + + // f[4] + h4 = v128.add(h4, v128.extmul_low(f4, g01)) + h5 = v128.add(h5, v128.extmul_high(f4, g01)) + h6 = v128.add(h6, v128.extmul_low(f4, g23)) + h7 = v128.add(h7, v128.extmul_high(f4, g23)) + h8 = v128.add(h8, v128.extmul_low(f4, g45)) + h9 = v128.add(h9, v128.extmul_high(f4, g45)) + h0 = v128.add(h0, v128.extmul_low(f4, g67_19)) + h1 = v128.add(h1, v128.extmul_high(f4, g67_19)) + h2 = v128.add(h2, v128.extmul_low(f4, g89_19)) + h3 = v128.add(h3, v128.extmul_high(f4, g89_19)) + + // f[5] + f5 = v128.add(f5, v128.and(f5, odds)) + + h5 = v128.add(h5, v128.extmul_low(f5, g01)) + h6 = v128.add(h6, v128.extmul_high(f5, g01)) + h7 = v128.add(h7, v128.extmul_low(f5, g23)) + h8 = v128.add(h8, v128.extmul_high(f5, g23)) + h9 = v128.add(h9, v128.extmul_low(f5, g45)) + h0 = v128.add(h0, v128.extmul_high(f5, g45_19)) + h1 = v128.add(h1, v128.extmul_low(f5, g67_19)) + h2 = v128.add(h2, v128.extmul_high(f5, g67_19)) + h3 = v128.add(h3, v128.extmul_low(f5, g89_19)) + h4 = v128.add(h4, v128.extmul_high(f5, g89_19)) + + // f[6] + h6 = v128.add(h6, v128.extmul_low(f6, g01)) + h7 = v128.add(h7, v128.extmul_high(f6, g01)) + h8 = v128.add(h8, v128.extmul_low(f6, g23)) + h9 = v128.add(h9, v128.extmul_high(f6, g23)) + h0 = v128.add(h0, v128.extmul_low(f6, g45_19)) + h1 = v128.add(h1, v128.extmul_high(f6, g45_19)) + h2 = v128.add(h2, v128.extmul_low(f6, g67_19)) + h3 = v128.add(h3, v128.extmul_high(f6, g67_19)) + h4 = v128.add(h4, v128.extmul_low(f6, g89_19)) + h5 = v128.add(h5, v128.extmul_high(f6, g89_19)) + + // f[7] + f7 = v128.add(f7, v128.and(f7, odds)) + + h7 = v128.add(h7, v128.extmul_low(f7, g01)) + h8 = v128.add(h8, v128.extmul_high(f7, g01)) + h9 = v128.add(h9, v128.extmul_low(f7, g23)) + h0 = v128.add(h0, v128.extmul_high(f7, g23_19)) + h1 = v128.add(h1, v128.extmul_low(f7, g45_19)) + h2 = v128.add(h2, v128.extmul_high(f7, g45_19)) + h3 = v128.add(h3, v128.extmul_low(f7, g67_19)) + h4 = v128.add(h4, v128.extmul_high(f7, g67_19)) + h5 = v128.add(h5, v128.extmul_low(f7, g89_19)) + h6 = v128.add(h6, v128.extmul_high(f7, g89_19)) + + // f[8] + h8 = v128.add(h8, v128.extmul_low(f8, g01)) + h9 = v128.add(h9, v128.extmul_high(f8, g01)) + h0 = v128.add(h0, v128.extmul_low(f8, g23_19)) + h1 = v128.add(h1, v128.extmul_high(f8, g23_19)) + h2 = v128.add(h2, v128.extmul_low(f8, g45_19)) + h3 = v128.add(h3, v128.extmul_high(f8, g45_19)) + h4 = v128.add(h4, v128.extmul_low(f8, g67_19)) + h5 = v128.add(h5, v128.extmul_high(f8, g67_19)) + h6 = v128.add(h6, v128.extmul_low(f8, g89_19)) + h7 = v128.add(h7, v128.extmul_high(f8, g89_19)) + + // f[9] + f9 = v128.add(f9, v128.and(f9, odds)) + + h9 = v128.add(h9, v128.extmul_low(f9, g01)) + h0 = v128.add(h0, v128.extmul_high(f9, g01_19)) + h1 = v128.add(h1, v128.extmul_low(f9, g23_19)) + h2 = v128.add(h2, v128.extmul_high(f9, g23_19)) + h3 = v128.add(h3, v128.extmul_low(f9, g45_19)) + h4 = v128.add(h4, v128.extmul_high(f9, g45_19)) + h5 = v128.add(h5, v128.extmul_low(f9, g67_19)) + h6 = v128.add(h6, v128.extmul_high(f9, g67_19)) + h7 = v128.add(h7, v128.extmul_low(f9, g89_19)) + h8 = v128.add(h8, v128.extmul_high(f9, g89_19)) + + // reduce scalars with carry + const bias25 = i64x2.splat(1 << 25) + const bias24 = i64x2.splat(1 << 24) + let carry0: v128 + let carry1: v128 + let carry2: v128 + let carry3: v128 + let carry4: v128 + let carry5: v128 + let carry6: v128 + let carry7: v128 + let carry8: v128 + let carry9: v128 + + carry0 = v128.shr(v128.add(h0, bias25), 26) + h0 = v128.sub(h0, v128.shl(carry0, 26)) + h1 = v128.add(h1, carry0) + + carry4 = v128.shr(v128.add(h4, bias25), 26) + h4 = v128.sub(h4, v128.shl(carry4, 26)) + h5 = v128.add(h5, carry4) + + carry1 = v128.shr(v128.add(h1, bias24), 25) + h1 = v128.sub(h1, v128.shl(carry1, 25)) + h2 = v128.add(h2, carry1) + + carry5 = v128.shr(v128.add(h5, bias24), 25) + h5 = v128.sub(h5, v128.shl(carry5, 25)) + h6 = v128.add(h6, carry5) + + carry2 = v128.shr(v128.add(h2, bias25), 26) + h2 = v128.sub(h2, v128.shl(carry2, 26)) + h3 = v128.add(h3, carry2) + + carry6 = v128.shr(v128.add(h6, bias25), 26) + h6 = v128.sub(h6, v128.shl(carry6, 26)) + h7 = v128.add(h7, carry6) + + carry3 = v128.shr(v128.add(h3, bias24), 25) + h3 = v128.sub(h3, v128.shl(carry3, 25)) + h4 = v128.add(h4, carry3) + + carry7 = v128.shr(v128.add(h7, bias24), 25) + h7 = v128.sub(h7, v128.shl(carry7, 25)) + h8 = v128.add(h8, carry7) + + carry4 = v128.shr(v128.add(h4, bias25), 26) + h4 = v128.sub(h4, v128.shl(carry4, 26)) + h5 = v128.add(h5, carry4) + + carry8 = v128.shr(v128.add(h8, bias25), 26) + h8 = v128.sub(h8, v128.shl(carry8, 26)) + h9 = v128.add(h9, carry8) + + 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))) + + carry0 = v128.shr(v128.add(h0, bias25), 26) + h0 = v128.sub(h0, v128.shl(carry0, 26)) + h1 = v128.add(h1, carry0) + + // extract and assign results to parallel outputs + store(changetype(h_a), i64x2.extract_lane(h0, 0), 0) + store(changetype(h_a), i64x2.extract_lane(h1, 0), 4) + store(changetype(h_a), i64x2.extract_lane(h2, 0), 8) + store(changetype(h_a), i64x2.extract_lane(h3, 0), 12) + store(changetype(h_a), i64x2.extract_lane(h4, 0), 16) + store(changetype(h_a), i64x2.extract_lane(h5, 0), 20) + store(changetype(h_a), i64x2.extract_lane(h6, 0), 24) + store(changetype(h_a), i64x2.extract_lane(h7, 0), 28) + store(changetype(h_a), i64x2.extract_lane(h8, 0), 32) + store(changetype(h_a), i64x2.extract_lane(h9, 0), 36) + + store(changetype(h_b), i64x2.extract_lane(h0, 1), 0) + store(changetype(h_b), i64x2.extract_lane(h1, 1), 4) + store(changetype(h_b), i64x2.extract_lane(h2, 1), 8) + store(changetype(h_b), i64x2.extract_lane(h3, 1), 12) + store(changetype(h_b), i64x2.extract_lane(h4, 1), 16) + store(changetype(h_b), i64x2.extract_lane(h5, 1), 20) + store(changetype(h_b), i64x2.extract_lane(h6, 1), 24) + store(changetype(h_b), i64x2.extract_lane(h7, 1), 28) + store(changetype(h_b), i64x2.extract_lane(h8, 1), 32) + store(changetype(h_b), i64x2.extract_lane(h9, 1), 36) +} + /** * Negate the values of a FieldElement and store the result. * diff --git a/src/assembly/ed25519/p.ts b/src/assembly/ed25519/p.ts index fe4d4cb..72413ff 100644 --- a/src/assembly/ed25519/p.ts +++ b/src/assembly/ed25519/p.ts @@ -2,7 +2,7 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: ISC -import { FieldElement, fe, fe_0, fe_1, fe_add, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_neg, fe_sq, fe_sq2, fe_sub, fe_tobytes } from './fe' +import { FieldElement, fe, fe_0, fe_1, fe_add, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_dual, fe_neg, fe_sq, fe_sq2, fe_sub, fe_tobytes } from './fe' /** * 2d = −121665 / 60833 @@ -74,16 +74,13 @@ export function ge_precomp_0 (h: ge_precomp): void { * r = p */ export function ge_p1p1_to_p2 (r: ge_p2, p: ge_p1p1): void { - fe_mul(r.X, p.X, p.T) - fe_mul(r.Y, p.Y, p.Z) + fe_mul_dual(r.X, p.X, p.T, r.Y, p.Y, p.Z) fe_mul(r.Z, p.Z, p.T) } export function ge_p1p1_to_p3 (r: ge_p3, p: ge_p1p1): void { - fe_mul(r.X, p.X, p.T) - fe_mul(r.Y, p.Y, p.Z) - fe_mul(r.Z, p.Z, p.T) - fe_mul(r.T, p.X, p.Y) + fe_mul_dual(r.X, p.X, p.T, r.Y, p.Y, p.Z) + fe_mul_dual(r.Z, p.Z, p.T, r.T, p.X, p.Y) } /** @@ -132,10 +129,8 @@ export function ge_p3_dbl (r: ge_p1p1, p: ge_p3, t: FieldElement): void { export function ge_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached, t: FieldElement): void { fe_add(r.X, p.Y, p.X) fe_sub(r.Y, p.Y, p.X) - fe_mul(r.Z, r.X, q.YplusX) - fe_mul(r.Y, r.Y, q.YminusX) - fe_mul(r.T, q.T2d, p.T) - fe_mul(r.X, p.Z, q.Z) + fe_mul_dual(r.Z, r.X, q.YplusX, r.Y, r.Y, q.YminusX) + fe_mul_dual(r.T, q.T2d, p.T, r.X, p.Z, q.Z) fe_dbl(t, r.X) fe_sub(r.X, r.Z, r.Y) fe_add(r.Y, r.Z, r.Y) @@ -152,8 +147,7 @@ export function ge_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached, t: FieldEleme export function ge_add_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void { fe_add(r.X, p.Y, p.X) fe_sub(r.Y, p.Y, p.X) - fe_mul(r.Z, r.X, q.yplusx) - fe_mul(r.Y, r.Y, q.yminusx) + fe_mul_dual(r.Z, r.X, q.yplusx, r.Y, r.Y, q.yminusx) fe_mul(r.T, q.xy2d, p.T) fe_dbl(p.Z, p.Z) fe_sub(r.X, r.Z, r.Y) @@ -183,10 +177,8 @@ export function ge_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_fe: FieldElement, t_g export function ge_sub_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void { fe_add(r.X, p.Y, p.X) fe_sub(r.Y, p.Y, p.X) - fe_mul(r.Z, r.X, q.YminusX) - fe_mul(r.Y, r.Y, q.YplusX) - fe_mul(r.T, q.T2d, p.T) - fe_mul(r.X, p.Z, q.Z) + fe_mul_dual(r.Z, r.X, q.YminusX, r.Y, r.Y, q.YplusX) + fe_mul_dual(r.T, q.T2d, p.T, r.X, p.Z, q.Z) fe_dbl(p.X, r.X) fe_sub(r.X, r.Z, r.Y) fe_add(r.Y, r.Z, r.Y) @@ -203,8 +195,7 @@ export function ge_sub_cached (r: ge_p1p1, p: ge_p3, q: ge_cached): void { export function ge_sub_precomp (r: ge_p1p1, p: ge_p3, q: ge_precomp): void { fe_add(r.X, p.Y, p.X) fe_sub(r.Y, p.Y, p.X) - fe_mul(r.Z, r.X, q.yminusx) - fe_mul(r.Y, r.Y, q.yplusx) + fe_mul_dual(r.Z, r.X, q.yminusx, r.Y, r.Y, q.yplusx) fe_mul(r.T, q.xy2d, p.T) fe_dbl(p.Z, p.Z) fe_sub(r.X, r.Z, r.Y) @@ -218,8 +209,7 @@ export function ge_p3_tobytes (s: StaticArray, h: ge_p3, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_fe_3: FieldElement ): void { fe_invert(reciprocal, h.Z, t_fe_0, t_fe_1, t_fe_2, t_fe_3) - fe_mul(x, h.X, reciprocal) - fe_mul(y, h.Y, reciprocal) + fe_mul_dual(x, h.X, reciprocal, y, h.Y, reciprocal) fe_tobytes(s, y, t_fe_0) s[31] ^= fe_isnegative(x, t_fe_0) << 7 } diff --git a/test/index.html b/test/index.html index 569dca9..8e9f4db 100644 --- a/test/index.html +++ b/test/index.html @@ -37,8 +37,7 @@ SPDX-License-Identifier: GPL-3.0-or-later "https://unpkg.com/nano25519@2.0.0/dist/browser.js": "sha512-bJnXSxESvcTNkzXTtqJThciGM7+TbjpSWzGN5bsVQk6CylYm1PO6lCNb/Nh7xvO2VwP4BNqRaHCOVF/m+oKOSA==", "https://unpkg.com/tweetnacl@1.0.3/nacl-fast.js": "sha512-vtXgmExiD+YrImVtOeCfoGiVKgouA3SgPHZbLQKcC972FeFo5i881KTOMxtK7dsZa2z5vjpyCpDSd1lGvJY3bg==", "https://git.codecow.com/?p=nano25519.git;a=blob_plain;f=test/vectors.mjs;hb=refs/tags/v2.0.0": "sha512-8da7GVIhpvrVoF7TE9RWpNoPIh2SC0Co+QoBCn0scO3FeQbV1e4yfuijFahSMTHaUx2/wlNm0b5yNRyiqsMehQ==", - "./vectors/mjs": "sha512-8da7GVIhpvrVoF7TE9RWpNoPIh2SC0Co+QoBCn0scO3FeQbV1e4yfuijFahSMTHaUx2/wlNm0b5yNRyiqsMehQ==", - "../dist/browser.js": "sha512-bJnXSxESvcTNkzXTtqJThciGM7+TbjpSWzGN5bsVQk6CylYm1PO6lCNb/Nh7xvO2VwP4BNqRaHCOVF/m+oKOSA==" + "./vectors/mjs": "sha512-8da7GVIhpvrVoF7TE9RWpNoPIh2SC0Co+QoBCn0scO3FeQbV1e4yfuijFahSMTHaUx2/wlNm0b5yNRyiqsMehQ==" } }