]> git.codecow.com Git - nano25519.git/commitdiff
Preload square root -1 and multiply (not real pair yet, needs work.)
authorChris Duncan <chris@codecow.com>
Fri, 30 Oct 2026 03:32:12 +0000 (20:32 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 30 Oct 2026 03:32:12 +0000 (20:32 -0700)
src/assembly/ed25519/fe.ts
src/assembly/ed25519/ge.ts

index bc9af58aa277e4db289e583e59632195d710ded9..005129f6611afc6c91ac881386036cd345263f65 100644 (file)
 import { load_3, load_4 } from './utils'
 
 /**
- * √-1
+ * Ed25519 curve constant √-1
+ *   = 2⁽ᵖ⁻¹⁾⸍⁴ mod `p`
+ *   = 19681161376707505956807079304988542015446066515923890162744021073123829784752
+ *   = [-32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482]
+ *
+ * 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.
  */
-export const fe_sqrtm1: FieldElement = StaticArray.fromArray<i32>([
-       -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482, 0, 0
-])
+// export const fe_sqrtm1: FieldElement = StaticArray.fromArray<i32>([
+//     -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)
+const sqrtm1_9012 = i32x4(11406482, -32595792, -7943725, 9377950)
+const sqrtm1_3456 = i32x4(3500415, 12389472, -272473, -25146209)
+const sqrtm1_78xx = i32x4(-2005654, 326686, 0, 0)
+const sqrtm1_0123_19 = i32x4(-619320048, -150930775, 178181050, 66507885)
+const sqrtm1_4567_19 = i32x4(235399968, -5176987, -477777971, -38107426)
+const sqrtm1_89xx_19 = i32x4(6207034, 216723158, 0, 0)
+const sqrtm1_9012_19 = i32x4(216723158, -32595792, -150930775, 178181050)
+const sqrtm1_3456_19 = i32x4(66507885, 235399968, -5176987, -477777971)
+const sqrtm1_78xx_19 = i32x4(-38107426, 6207034, 0, 0)
 
 /**
  * Ed25519 curve constant d
@@ -624,6 +645,35 @@ export function fe_mul (f: FieldElement, a: FieldElement, b: FieldElement): void
        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.
  *
index 0b9ead6fc540db564a126f25129d845b1b6d191f..6a67ab346a5c3ba8e7287d418fd04de96d97c18a 100644 (file)
@@ -19,7 +19,7 @@
 */
 import { base } from './base'
 import { base2 } from './base2'
-import { FieldElement, fe, fe_1, fe_add, fe_addsub, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_mul_d, fe_mul_pair, fe_neg, fe_pow22523, fe_sq, fe_sqrtm1, fe_sub } from './fe'
+import { FieldElement, fe, fe_1, fe_add, fe_addsub, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_mul_sqrtm1, fe_mul_d, fe_mul_pair, fe_neg, fe_pow22523, fe_sq, fe_sub, fe_mul_pair_sqrtm1 } from './fe'
 import { p_cached, p1p1, p1p1_0, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_cached, p3_add_cached_to_cached, p3_add_precomp, p3_dbl, p3_dbl_to_p3, p3_sub_cached, p3_sub_precomp, p3_to_cached, p3_tobytes, p_precomp, precomp_0 } from './p'
 
 function ge_cmov8_base (t: p_precomp, bp: usize, b: i8): void {
@@ -132,7 +132,7 @@ export function ge_frombytes_negate_vartime (h: p3, s: StaticArray<u8>,
                if (fe_iszero(p_root_check, t_fe_0, t_u8x32) == 0) {
                        return -1
                }
-               fe_mul(h.X, h.X, fe_sqrtm1)
+               fe_mul_sqrtm1(h.X, h.X)
        }
 
        if (fe_isnegative(h.X, t_fe_0) == (s[31] >> 7)) {
@@ -188,7 +188,7 @@ export function ge_frombytes (h: p3, s: StaticArray<u8>,
        fe_sq(vxx, h.X)
 
        // vxx = vx³, x_sqrtm1 = x√-1
-       fe_mul_pair(vxx, vxx, v, x_sqrtm1, h.X, fe_sqrtm1)
+       fe_mul_pair_sqrtm1(vxx, vxx, v, x_sqrtm1, h.X)
 
        // p = vx²+u
        // m = vx²-u
@@ -229,7 +229,7 @@ function ge_has_small_order (p: p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>
        const x_iszero = fe_iszero(p.X, t_fe, t_u8x32)
        const y_iszero = fe_iszero(p.Y, t_fe, t_u8x32)
 
-       fe_mul(y_sqrtm1, p.Y, fe_sqrtm1)
+       fe_mul_sqrtm1(y_sqrtm1, p.Y)
        fe_sub(c, y_sqrtm1, p.X)
        const y_sqrtm1_minusx = fe_iszero(c, t_fe, t_u8x32)
        fe_add(c, y_sqrtm1, p.X)