*/
import { load_3, load_4 } from './utils'
+/**
+ * Ed25519 curve constant 2d
+ * = −121665 / 60833
+ * = 16295367250680780974490674513165176452449235426866156013048779062215315747161
+ * = [-21827239, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, 29715968, 9444199]
+ *
+ * Shuffled for multiplication as the following, where `x` is a `FieldElement` placeholder for 0:
+ *
+ * `0123, 4567, 89xx, 9012, 3456, 78xx`
+ *
+ * Shuffles are also multiplied by 19 for limb wrapping, except limb 0 which does not wrap.
+ *
+ */
+const d2_0123 = i32x4(-21827239, -5839606, -30745221, 13898782)
+const d2_4567 = i32x4(229458, 15978800, -12551817, -6495438)
+const d2_89xx = i32x4(29715968, 9444199, 0, 0)
+const d2_9012 = i32x4(9444199, -21827239, -5839606, -30745221)
+const d2_3456 = i32x4(13898782, 229458, 15978800, -12551817)
+const d2_78xx = i32x4(-6495438, 29715968, 0, 0)
+const d2_0123_19 = i32x4(-414717541, -110952514, -584159199, 264076858)
+const d2_4567_19 = i32x4(4359702, 303597200, -238484523, -123413322)
+const d2_89xx_19 = i32x4(564603392, 179439781, 0, 0)
+const d2_9012_19 = i32x4(179439781, -21827239, -110952514, -584159199)
+const d2_3456_19 = i32x4(264076858, 4359702, 303597200, -238484523)
+const d2_78xx_19 = i32x4(-123413322, 564603392, 0, 0)
+
/**
* 10x 25- and 26-bit array of values representing a large integer `n mod p`. In
* this implementation, 12 elements are allocated instead of 10, and this is for
/**
* Multiply two FieldElements and store the product.
*
- * @param f FieldElement product destination
- * @param a FieldElement multiplicand source
- * @param b FieldElement multiplicand source
+ * @param f product destination
+ * @param a multiplicand source
+ * @param b multiplicand source as 12 separate vector shuffles
*/
//@ts-expect-error
@inline
-function fe_mul (f: FieldElement, a: FieldElement, b: FieldElement): void {
+function fe_mul (f: FieldElement, a: FieldElement, b0123: v128, b4567: v128, b89xx: v128, b9012: v128, b3456: v128, b78xx: v128, b0123_19: v128, b4567_19: v128, b89xx_19: v128, b9012_19: v128, b3456_19: v128, b78xx_19: v128): void {
// Load all 10 limbs of `a`
const a_ptr: usize = changetype<usize>(a)
const a0123: v128 = v128.load(a_ptr, 0 << 4)
const a7: v128 = v128.shuffle<i32>(a7_2, a7_1, 0, 5, 0, 5)
const a9: v128 = v128.shuffle<i32>(a9_2, a9_1, 0, 5, 0, 5)
- // Load all 10 limbs of `b`
- const b_ptr: usize = changetype<usize>(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<i32>(b89xx, b0123, 1, 4, 5, 6)
- const b3456: v128 = v128.shuffle<i32>(b0123, b4567, 3, 4, 5, 6)
- const b78xx: v128 = v128.shuffle<i32>(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<i32>(b0123, w)
- const b4567_19: v128 = v128.mul<i32>(b4567, w)
- const b89xx_19: v128 = v128.mul<i32>(b89xx, w)
-
- // Multiply shifts by 19 except limb 0 which never wraps
- const b9012_19: v128 = v128.mul<i32>(b9012, i32x4(19, 1, 19, 19))
- const b3456_19: v128 = v128.mul<i32>(b3456, w)
- const b78xx_19: v128 = v128.mul<i32>(b78xx, w)
-
// f[0]
let f01: v128 = i64x2.extmul_low_i32x4_s(a0, b0123)
let f23: v128 = i64x2.extmul_high_i32x4_s(a0, b0123)
* @param b FieldElement multiplicand source
*/
export function fe_mul_ab (f: FieldElement, a: FieldElement, b: FieldElement): void {
- fe_mul(f, a, b)
+ // Load all 10 limbs of `b`
+ const b_ptr: usize = changetype<usize>(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<i32>(b89xx, b0123, 1, 4, 5, 6)
+ const b3456: v128 = v128.shuffle<i32>(b0123, b4567, 3, 4, 5, 6)
+ const b78xx: v128 = v128.shuffle<i32>(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<i32>(b0123, w)
+ const b4567_19: v128 = v128.mul<i32>(b4567, w)
+ const b89xx_19: v128 = v128.mul<i32>(b89xx, w)
+
+ // Multiply shifts by 19 except limb 0 which never wraps
+ const b9012_19: v128 = v128.mul<i32>(b9012, i32x4(19, 1, 19, 19))
+ const b3456_19: v128 = v128.mul<i32>(b3456, w)
+ const b78xx_19: v128 = v128.mul<i32>(b78xx, w)
+
+ fe_mul(f, a, b0123, b4567, b89xx, b9012, b3456, b78xx, b0123_19, b4567_19, b89xx_19, b9012_19, b3456_19, b78xx_19)
}
/**
* @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)
+ fe_mul_ab(f, a, b)
+ fe_mul_ab(f, f, c)
}
/**
- * Multiply two FieldElements with precomputed constant and store the product.
+ * 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_a_2d (f: FieldElement, a: FieldElement): void {
+ fe_mul(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)
+}
+
+/**
+ * Multiply two FieldElements with the Ed25519 constant `2d` 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)
+ fe_mul_a_2d(f, a)
+ fe_mul_ab(f, f, b)
}
/**
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! SPDX-License-Identifier: ISC
-import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul_ab, fe_mul_ab_pair_dbl, fe_mul_ab_pair, fe_neg, fe_sq_pair_dbl, fe_sq_pair, fe_sub, fe_tobytes } from './fe'
-
-/**
- * 2d = −121665 / 60833
- * = 16295367250680780974490674513165176452449235426866156013048779062215315747161
- */
-const ed25519_d2: FieldElement = StaticArray.fromArray<i32>([
- -21827239, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, 29715968, 9444199, 0, 0
-])
+import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul_ab, fe_mul_a_2d, fe_mul_ab_pair_dbl, fe_mul_ab_pair, fe_neg, fe_sq_pair_dbl, fe_sq_pair, fe_sub, fe_tobytes } from './fe'
/**
* Base class for building branded subclasses of coordinate systems.
export function ge_p3_to_cached (r: ge_cached, p: ge_p3): void {
fe_addsub(r.YplusX, r.YminusX, p.Y, p.X)
fe_copy(r.Z, p.Z)
- fe_mul_ab(r.T2d, p.T, ed25519_d2)
+ fe_mul_a_2d(r.T2d, p.T)
}
/**
export function ge_p1p1_to_cached (r: ge_cached, p: ge_p1p1): void {
fe_mul_ab_pair(r.YminusX, p.X, p.T, r.YplusX, p.Y, p.Z)
fe_mul_ab_pair(r.Z, p.Z, p.T, r.T2d, p.X, p.Y)
- fe_mul_ab(r.T2d, r.T2d, ed25519_d2)
+ fe_mul_a_2d(r.T2d, r.T2d)
fe_addsub(r.YplusX, r.YminusX, r.YplusX, r.YminusX)
}
fe_addsub(r.X, r.Y, p.Y, p.X) // X = Y₁+X₁, Y = Y₁-X₁
fe_addsub(r.Z, r.T, q.Y, q.X) // Z = Y₂+X₂, T = Y₂-X₂
fe_mul_ab_pair(r.X, r.X, r.Z, r.Y, r.Y, r.T) // X = (Y₁+X₁)(Y₂+X₂), Y = (Y₁-X₁)(Y₂-X₂)
- fe_mul_ab(r.T, q.T, ed25519_d2) // T = 2T₂d
+ fe_mul_a_2d(r.T, q.T) // T = 2T₂d
fe_mul_ab_pair_dbl(r.T, r.T, p.T, r.Z, p.Z, q.Z) // T = 2T₁T₂d, Z = 2Z₁Z₂
fe_addsub(r.Y, r.X, r.X, r.Y) // Y = (Y₁+X₁)(Y₂+X₂) + (Y₁-X₁)(Y₂-X₂), X = (Y₁+X₁)(Y₂+X₂) - (Y₁-X₁)(Y₂-X₂)
fe_addsub(r.Z, t_fe, r.Z, r.T) // Z = 2Z₁Z₂ + 2T₁T₂d, t = 2Z₁Z₂ - 2T₁T₂d
// p1p1 -> cached
fe_mul_ab_pair(r.YminusX, r.YminusX, r.T2d, t_fe, r.YminusX, r.YplusX)
fe_mul_ab_pair(r.Z, r.Z, r.T2d, r.YplusX, r.YplusX, r.Z)
- fe_mul_ab(r.T2d, t_fe, ed25519_d2)
+ fe_mul_a_2d(r.T2d, t_fe)
fe_addsub(r.YplusX, r.YminusX, r.YplusX, r.YminusX)
}
fe_mul_ab_pair(r.X, r.X, r.T, r.Y, r.Y, r.Z) // X = (Y₁+X₁)(Y₂-X₂), Y = (Y₁-X₁)(Y₂+X₂)
fe_neg(r.T, q.T) // T = -T₂
- fe_mul_ab(r.T, r.T, ed25519_d2) // T = -2T₂d
+ fe_mul_a_2d(r.T, r.T) // T = -2T₂d
fe_mul_ab_pair_dbl(r.T, r.T, p.T, r.Z, p.Z, q.Z) // T = -2T₁T₂d, Z = 2Z₁Z₂
fe_addsub(r.Y, r.X, r.X, r.Y) // Y = (Y₁+X₁)(Y₂-X₂) + (Y₁-X₁)(Y₂+X₂), X = (Y₁+X₁)(Y₂-X₂) - (Y₁-X₁)(Y₂+X₂)