* @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 `
`
}
+/**
+ * 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<i32>(${f}0, 1)
+ ${h}1 = v128.shl<i32>(${f}1, 1)
+ ${h}2 = v128.shl<i32>(${f}2, 1)
+`
+}
+
export const FE = `//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! SPDX-License-Identifier: GPL-3.0-or-later
/**
export function fe_dbl (h: FieldElement, f: FieldElement): void {
const h_ptr = changetype<usize>(h)
const f_ptr = changetype<usize>(f)
- v128.store(h_ptr, v128.shl<i32>(v128.load(f_ptr, 0), 1), 0)
- v128.store(h_ptr, v128.shl<i32>(v128.load(f_ptr, 16), 1), 16)
- v128.store_lane<i64>(h_ptr, v128.shl<i32>(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<u64>(h_ptr, h2, 0, 32)
}
/**
export function fe_dbl (h: FieldElement, f: FieldElement): void {
const h_ptr = changetype<usize>(h)
const f_ptr = changetype<usize>(f)
- v128.store(h_ptr, v128.shl<i32>(v128.load(f_ptr, 0), 1), 0)
- v128.store(h_ptr, v128.shl<i32>(v128.load(f_ptr, 16), 1), 16)
- v128.store_lane<i64>(h_ptr, v128.shl<i32>(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<i32>(f0, 1)
+ h1 = v128.shl<i32>(f1, 1)
+ h2 = v128.shl<i32>(f2, 1)
+
+ // store h
+ v128.store(h_ptr, h0, 0)
+ v128.store(h_ptr, h1, 16)
+ v128.store_lane<u64>(h_ptr, h2, 0, 32)
}
/**