]> git.codecow.com Git - nano25519.git/commitdiff
Rearrange dual square loading.
authorChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 14:01:52 +0000 (07:01 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 14:01:52 +0000 (07:01 -0700)
src/assembly/ed25519/fe.ts

index dc034809a145bb3b1f22f83033a23209ec09be99..d26bb606198535ce96644a788eb857195d78caa7 100644 (file)
@@ -1116,17 +1116,16 @@ export function fe_sq (h: FieldElement, f: FieldElement): void {
        h1 += carry0
        h0 -= carry0 << 26
 
-       const h_ptr: usize = changetype<usize>(h)
-       store<i32>(h_ptr, h0, 0)
-       store<i32>(h_ptr, h1, 4)
-       store<i32>(h_ptr, h2, 8)
-       store<i32>(h_ptr, h3, 12)
-       store<i32>(h_ptr, h4, 16)
-       store<i32>(h_ptr, h5, 20)
-       store<i32>(h_ptr, h6, 24)
-       store<i32>(h_ptr, h7, 28)
-       store<i32>(h_ptr, h8, 32)
-       store<i32>(h_ptr, h9, 36)
+       store<i32>(changetype<usize>(h), h0, 0)
+       store<i32>(changetype<usize>(h), h1, 4)
+       store<i32>(changetype<usize>(h), h2, 8)
+       store<i32>(changetype<usize>(h), h3, 12)
+       store<i32>(changetype<usize>(h), h4, 16)
+       store<i32>(changetype<usize>(h), h5, 20)
+       store<i32>(changetype<usize>(h), h6, 24)
+       store<i32>(changetype<usize>(h), h7, 28)
+       store<i32>(changetype<usize>(h), h8, 32)
+       store<i32>(changetype<usize>(h), h9, 36)
 }
 
 /**
@@ -1162,37 +1161,49 @@ export function fe_sq (h: FieldElement, f: FieldElement): void {
  * @param f_b FieldElement B multiplicand source
  */
 export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElement, f_b: FieldElement): void {
-       // pack each f index in low lanes, high lanes are ignored
-       const f_a_0123: v128 = v128.load(changetype<usize>(f_a), 0)
-       const f_b_0123: v128 = v128.load(changetype<usize>(f_b), 0)
-       const f0: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 0, 4, 0, 4)
-       const f1: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 1, 5, 1, 5)
-       const f2: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 2, 6, 2, 6)
-       const f3: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 3, 7, 3, 7)
+       const f_a_0123: v128 = v128.load(changetype<usize>(f_a), 0 << 4)
+       const f_a_4567: v128 = v128.load(changetype<usize>(f_a), 1 << 4)
+       const f_a_89xx: v128 = v128.load64_zero(changetype<usize>(f_a), 2 << 4)
 
-       const f_a_4567: v128 = v128.load(changetype<usize>(f_a), 16)
-       const f_b_4567: v128 = v128.load(changetype<usize>(f_b), 16)
-       const f4: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 0, 4, 0, 4)
-       const f5: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 1, 5, 1, 5)
-       const f6: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 2, 6, 2, 6)
-       const f7: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 3, 7, 3, 7)
+       const f_b_0123: v128 = v128.load(changetype<usize>(f_b), 0 << 4)
+       const f_b_4567: v128 = v128.load(changetype<usize>(f_b), 1 << 4)
+       const f_b_89xx: v128 = v128.load64_zero(changetype<usize>(f_b), 2 << 4)
 
-       const f_a_89xx: v128 = v128.load64_zero(changetype<usize>(f_a), 32)
-       const f_b_89xx: v128 = v128.load64_zero(changetype<usize>(f_b), 32)
-       const f8: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 0, 4, 2, 6)
-       const f9: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 1, 5, 3, 7)
+       const f01: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 0, 4, 1, 5)
+       const f23: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 2, 6, 3, 7)
+       const f45: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 0, 4, 1, 5)
+       const f67: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 2, 6, 3, 7)
+       const f89: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 0, 4, 1, 5)
 
-       // precomputed doublings for square special as well as odd-on-odd
-       const f0_2: v128 = v128.shl<i32>(f0, 1)
-       const f1_2: v128 = v128.shl<i32>(f1, 1)
-       const f2_2: v128 = v128.shl<i32>(f2, 1)
-       const f3_2: v128 = v128.shl<i32>(f3, 1)
-       const f4_2: v128 = v128.shl<i32>(f4, 1)
-       const f5_2: v128 = v128.shl<i32>(f5, 1)
-       const f6_2: v128 = v128.shl<i32>(f6, 1)
-       const f7_2: v128 = v128.shl<i32>(f7, 1)
-       const f8_2: v128 = v128.shl<i32>(f8, 1)
-       const f9_2: v128 = v128.shl<i32>(f9, 1)
+       const f01_2: v128 = v128.add<i32>(f01, f01)
+       const f23_2: v128 = v128.add<i32>(f23, f23)
+       const f45_2: v128 = v128.add<i32>(f45, f45)
+       const f67_2: v128 = v128.add<i32>(f67, f67)
+       const f89_2: v128 = v128.add<i32>(f89, f89)
+
+       // pack each f index in low lanes, high lanes are ignored
+       const f0: v128 = v128.shuffle<i32>(f01, f01, 0, 1, 0, 1)
+       const f1: v128 = v128.shuffle<i32>(f01, f01, 2, 3, 6, 7)
+       const f2: v128 = v128.shuffle<i32>(f23, f23, 0, 1, 0, 1)
+       const f3: v128 = v128.shuffle<i32>(f23, f23, 2, 3, 6, 7)
+       const f4: v128 = v128.shuffle<i32>(f45, f45, 0, 1, 0, 1)
+       const f5: v128 = v128.shuffle<i32>(f45, f45, 2, 3, 6, 7)
+       const f6: v128 = v128.shuffle<i32>(f67, f67, 0, 1, 0, 1)
+       const f7: v128 = v128.shuffle<i32>(f67, f67, 2, 3, 6, 7)
+       const f8: v128 = v128.shuffle<i32>(f89, f89, 0, 1, 0, 1)
+       const f9: v128 = v128.shuffle<i32>(f89, f89, 2, 3, 6, 7)
+
+       // precomputed doublings for square special and odd-on-odd
+       const f0_2: v128 = v128.shuffle<i32>(f01_2, f01_2, 0, 1, 0, 1)
+       const f1_2: v128 = v128.shuffle<i32>(f01_2, f01_2, 2, 3, 6, 7)
+       const f2_2: v128 = v128.shuffle<i32>(f23_2, f23_2, 0, 1, 0, 1)
+       const f3_2: v128 = v128.shuffle<i32>(f23_2, f23_2, 2, 3, 6, 7)
+       const f4_2: v128 = v128.shuffle<i32>(f45_2, f45_2, 0, 1, 0, 1)
+       const f5_2: v128 = v128.shuffle<i32>(f45_2, f45_2, 2, 3, 6, 7)
+       const f6_2: v128 = v128.shuffle<i32>(f67_2, f67_2, 0, 1, 0, 1)
+       const f7_2: v128 = v128.shuffle<i32>(f67_2, f67_2, 2, 3, 6, 7)
+       const f8_2: v128 = v128.shuffle<i32>(f89_2, f89_2, 0, 1, 0, 1)
+       const f9_2: v128 = v128.shuffle<i32>(f89_2, f89_2, 2, 3, 6, 7)
 
        // factor of 19 for wrap from h9 to h0
        const f5_19: v128 = v128.add<i32>(f5, v128.add<i32>(f5_2, v128.shl<i32>(f5_2, 3)))
@@ -1202,9 +1213,10 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        const f9_19: v128 = v128.add<i32>(f9, v128.add<i32>(f9_2, v128.shl<i32>(f9_2, 3)))
 
        // doubled odd-on-odd factor of 19
-       const f7_38: v128 = v128.shl<i32>(f7_19, 1)
-       const f9_38: v128 = v128.shl<i32>(f9_19, 1)
+       const f7_38: v128 = v128.add<i32>(f7_19, f7_19)
+       const f9_38: v128 = v128.add<i32>(f9_19, f9_19)
 
+       // f[0]
        let h0: v128 = v128.extmul_low<i32>(f0, f0)
        let h1: v128 = v128.extmul_low<i32>(f0_2, f1)
        let h2: v128 = v128.extmul_low<i32>(f0_2, f2)
@@ -1216,6 +1228,7 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        let h8: v128 = v128.extmul_low<i32>(f0_2, f8)
        let h9: v128 = v128.extmul_low<i32>(f0_2, f9)
 
+       // f[1]
        h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f1_2, f1))
        h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f1_2, f2))
        h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f1_2, f3_2))
@@ -1226,6 +1239,7 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f1_2, f8))
        h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f1_2, f9_38))
 
+       // f[2]
        h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f2, f2))
        h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f2_2, f3))
        h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f2_2, f4))
@@ -1235,6 +1249,7 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f2_2, f8_19))
        h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f2_2, f9_19))
 
+       // f[3]
        h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f3_2, f3))
        h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f3_2, f4))
        h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f3_2, f5_2))
@@ -1243,6 +1258,7 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f3_2, f8_19))
        h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f3_2, f9_38))
 
+       // f[4]
        h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f4, f4))
        h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f4_2, f5))
        h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f4_2, f6_19))
@@ -1250,24 +1266,29 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f4_2, f8_19))
        h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f4_2, f9_19))
 
+       // f[5]
        h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f5_2, f5_19))
        h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f5_2, f6_19))
        h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f5_2, f7_38))
        h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f5_2, f8_19))
        h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f5_2, f9_38))
 
+       // f[6]
        h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f6, f6_19))
        h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f6_2, f7_19))
        h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f6_2, f8_19))
        h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f6_2, f9_19))
 
+       // f[7]
        h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f7_2, f7_19))
        h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f7_2, f8_19))
        h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f7_2, f9_38))
 
+       // f[8]
        h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f8, f8_19))
        h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f8_2, f9_19))
 
+       // f[9]
        h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f9_2, f9_19))
 
        // reduce scalars with carry