From: Chris Duncan Date: Wed, 30 Sep 2026 15:30:58 +0000 (-0700) Subject: Add field element doubling template. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=948bcaf141071aa765fb634c123159faac2b6fb8;p=nano25519.git Add field element doubling template. --- diff --git a/scripts/fe.gen.mjs b/scripts/fe.gen.mjs index 3f62a70..5bd688b 100644 --- a/scripts/fe.gen.mjs +++ b/scripts/fe.gen.mjs @@ -32,6 +32,7 @@ export function fe_add (h, f, g) { * @param {string} h Variable name of FieldElement difference destination * @param {string} f Variable name of FieldElement minuend source * @param {string} g Variable name of FieldElement subtrahend source + * @returns {string} Three vector subtraction commands */ export function fe_sub (h, f, g) { return ` @@ -42,6 +43,22 @@ export function fe_sub (h, f, g) { ` } +/** + * Add a FieldElement to itself and store the sum. + * + * @param {string} h Variable name of FieldElement sum destination + * @param {string} f Variable name of FieldElement summand source + * @returns {string} Three vector doubling commands + */ +export function fe_dbl (h, f) { + return ` + // ${h} = 2*${f} + ${h}0 = v128.shl(${f}0, 1) + ${h}1 = v128.shl(${f}1, 1) + ${h}2 = v128.shl(${f}2, 1) +` +} + export const FE = `//! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later /** @@ -172,9 +189,19 @@ export function fe_copy (h: FieldElement, f: FieldElement): void { export function fe_dbl (h: FieldElement, f: FieldElement): void { const h_ptr = changetype(h) const f_ptr = changetype(f) - v128.store(h_ptr, v128.shl(v128.load(f_ptr, 0), 1), 0) - v128.store(h_ptr, v128.shl(v128.load(f_ptr, 16), 1), 16) - v128.store_lane(h_ptr, v128.shl(v128.load(f_ptr, 32), 1), 0, 32) + + let h0: v128, h1: v128, h2: v128 + + let f0 = v128.load(f_ptr, 0) + let f1 = v128.load(f_ptr, 16) + let f2 = v128.load(f_ptr, 32) + + ${fe_dbl('h', 'f')} + + // store h + v128.store(h_ptr, h0, 0) + v128.store(h_ptr, h1, 16) + v128.store_lane(h_ptr, h2, 0, 32) } /** diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index fc46ae7..93e443a 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -131,9 +131,22 @@ export function fe_copy (h: FieldElement, f: FieldElement): void { export function fe_dbl (h: FieldElement, f: FieldElement): void { const h_ptr = changetype(h) const f_ptr = changetype(f) - v128.store(h_ptr, v128.shl(v128.load(f_ptr, 0), 1), 0) - v128.store(h_ptr, v128.shl(v128.load(f_ptr, 16), 1), 16) - v128.store_lane(h_ptr, v128.shl(v128.load(f_ptr, 32), 1), 0, 32) + + let h0: v128, h1: v128, h2: v128 + + let f0 = v128.load(f_ptr, 0) + let f1 = v128.load(f_ptr, 16) + let f2 = v128.load(f_ptr, 32) + + // h = 2*f + h0 = v128.shl(f0, 1) + h1 = v128.shl(f1, 1) + h2 = v128.shl(f2, 1) + + // store h + v128.store(h_ptr, h0, 0) + v128.store(h_ptr, h1, 16) + v128.store_lane(h_ptr, h2, 0, 32) } /**