]> git.codecow.com Git - nano25519.git/commitdiff
Adjust dual squaring packing and unpacking.
authorChris Duncan <chris@codecow.com>
Tue, 20 Oct 2026 06:45:02 +0000 (23:45 -0700)
committerChris Duncan <chris@codecow.com>
Tue, 20 Oct 2026 06:45:02 +0000 (23:45 -0700)
src/assembly/ed25519/fe.ts

index 9c71c3644fe69975a0ba8a28708feb885bb19504..236b61db5f0b0b5d20f7ef7e7cd4d4d7d27240fc 100644 (file)
@@ -960,7 +960,7 @@ export function fe_reduce (h: FieldElement, f: FieldElement): void {
  * |f| bounded by 1.65*2²⁶,1.65*2²⁵,1.65*2²⁶,1.65*2²⁵,etc.
  *
  * Postconditions:
- * |h| bounded by 1.01*2²⁵,1.01*2²⁴4,1.01*2²⁵,1.01*2²⁴,etc.
+ * |h| bounded by 1.01*2²⁵,1.01*2²⁴,1.01*2²⁵,1.01*2²⁴,etc.
  *
  * @param h FieldElement power destination
  * @param f FieldElement base source
@@ -1152,7 +1152,7 @@ export function fe_sq (h: FieldElement, f: FieldElement): void {
  * |f| bounded by 1.65*2²⁶,1.65*2²⁵,1.65*2²⁶,1.65*2²⁵,etc.
  *
  * Postconditions:
- * |h| bounded by 1.01*2²⁵,1.01*2²⁴4,1.01*2²⁵,1.01*2²⁴,etc.
+ * |h| bounded by 1.01*2²⁵,1.01*2²⁴,1.01*2²⁵,1.01*2²⁴,etc.
  *
  * @param h_a FieldElement A product destination
  * @param f_a FieldElement A multiplicand source
@@ -1165,36 +1165,39 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        const f_b_0123: v128 = v128.load(changetype<usize>(f_b), 0)
        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 f0: v128 = v128.shuffle<i32>(f01, f01, 0, 4, 0, 4)
-       const f1: v128 = v128.shuffle<i32>(f01, f01, 1, 5, 1, 5)
-       const f2: v128 = v128.shuffle<i32>(f23, f23, 2, 6, 2, 6)
-       const f3: v128 = v128.shuffle<i32>(f23, f23, 3, 7, 3, 7)
+       const f0: v128 = v128.shuffle<i32>(f01, f01, 0, 1, 0, 1)
+       const f1: v128 = v128.shuffle<i32>(f01, f01, 2, 3, 2, 3)
+       const f2: v128 = v128.shuffle<i32>(f23, f23, 0, 1, 0, 1)
+       const f3: v128 = v128.shuffle<i32>(f23, f23, 2, 3, 2, 3)
 
        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 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 f4: v128 = v128.shuffle<i32>(f45, f45, 0, 4, 0, 4)
-       const f5: v128 = v128.shuffle<i32>(f45, f45, 1, 5, 1, 5)
-       const f6: v128 = v128.shuffle<i32>(f67, f67, 2, 6, 2, 6)
-       const f7: v128 = v128.shuffle<i32>(f67, f67, 3, 7, 3, 7)
+       const f4: v128 = v128.shuffle<i32>(f45, f45, 0, 1, 0, 1)
+       const f5: v128 = v128.shuffle<i32>(f45, f45, 2, 3, 2, 3)
+       const f6: v128 = v128.shuffle<i32>(f67, f67, 0, 1, 0, 1)
+       const f7: v128 = v128.shuffle<i32>(f67, f67, 2, 3, 2, 3)
 
        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 f89: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 0, 4, 1, 5)
-       const f8: v128 = v128.shuffle<i32>(f89, f89, 0, 4, 0, 4)
-       const f9: v128 = v128.shuffle<i32>(f89, f89, 1, 5, 1, 5)
+       const f8: v128 = v128.shuffle<i32>(f89, f89, 0, 1, 0, 1)
+       const f9: v128 = v128.shuffle<i32>(f89, f89, 2, 3, 2, 3)
 
        // precomputed doublings for square special as well as odd-on-odd
-       const f01_2: v128 = v128.shl<i32>(f01, 1)
-       const f23_2: v128 = v128.shl<i32>(f23, 1)
-       const f45_2: v128 = v128.shl<i32>(f45, 1)
-       const f67_2: v128 = v128.shl<i32>(f67, 1)
-       const f89_2: v128 = v128.shl<i32>(f89, 1)
+       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)
 
        // 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)))
-       const f6_19: v128 = v128.add<i32>(f6, v128.add<i32>(f67_2, v128.shl<i32>(f67_2, 3)))
+       const f6_19: v128 = v128.add<i32>(f6, v128.add<i32>(f6_2, v128.shl<i32>(f6_2, 3)))
        const f7_19: v128 = v128.add<i32>(f7, v128.add<i32>(f7_2, v128.shl<i32>(f7_2, 3)))
        const f8_19: v128 = v128.add<i32>(f8, v128.add<i32>(v128.shl<i32>(f8, 1), v128.shl<i32>(f8, 4)))
        const f9_19: v128 = v128.add<i32>(f9, v128.add<i32>(v128.shl<i32>(f9, 1), v128.shl<i32>(f9, 4)))
@@ -1204,15 +1207,15 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        const f9_38: v128 = v128.shl<i32>(f9_19, 1)
 
        let h0: v128 = v128.extmul_low<i32>(f0, f01)
-       let h1: v128 = v128.extmul_high<i32>(f01_2, f01)
-       let h2: v128 = v128.extmul_low<i32>(f01_2, f23)
-       let h3: v128 = v128.extmul_high<i32>(f01_2, f23)
-       let h4: v128 = v128.extmul_low<i32>(f01_2, f45)
-       let h5: v128 = v128.extmul_high<i32>(f01_2, f45)
-       let h6: v128 = v128.extmul_low<i32>(f01_2, f67)
-       let h7: v128 = v128.extmul_high<i32>(f01_2, f67)
-       let h8: v128 = v128.extmul_low<i32>(f01_2, f89)
-       let h9: v128 = v128.extmul_high<i32>(f01_2, f89)
+       let h1: v128 = v128.extmul_high<i32>(f0_2, f01)
+       let h2: v128 = v128.extmul_low<i32>(f0_2, f23)
+       let h3: v128 = v128.extmul_high<i32>(f0_2, f23)
+       let h4: v128 = v128.extmul_low<i32>(f0_2, f45)
+       let h5: v128 = v128.extmul_high<i32>(f0_2, f45)
+       let h6: v128 = v128.extmul_low<i32>(f0_2, f67)
+       let h7: v128 = v128.extmul_high<i32>(f0_2, f67)
+       let h8: v128 = v128.extmul_low<i32>(f0_2, f89)
+       let h9: v128 = v128.extmul_high<i32>(f0_2, f89)
 
        h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f1_2, f01))
        h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f1_2, f23))
@@ -1225,13 +1228,13 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f1_2, f9_38))
 
        h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f2, f23))
-       h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f23_2, f23))
-       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f23_2, f45))
-       h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f23_2, f45))
-       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f23_2, f67))
-       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f23_2, f67))
-       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f23_2, f8_19))
-       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f23_2, f9_19))
+       h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f2_2, f23))
+       h6 = v128.add<i64>(h6, v128.extmul_low<i32>(f2_2, f45))
+       h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f2_2, f45))
+       h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f2_2, f67))
+       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f2_2, f67))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f2_2, f8_19))
+       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f2_2, f9_19))
 
        h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f3_2, f23))
        h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f3_2, f45))
@@ -1242,22 +1245,22 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f3_2, f9_38))
 
        h8 = v128.add<i64>(h8, v128.extmul_low<i32>(f4, f45))
-       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f45_2, f45))
-       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f45_2, f6_19))
-       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f45_2, f7_19))
-       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f45_2, f8_19))
-       h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f45_2, f9_19))
+       h9 = v128.add<i64>(h9, v128.extmul_high<i32>(f4_2, f45))
+       h0 = v128.add<i64>(h0, v128.extmul_low<i32>(f4_2, f6_19))
+       h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f4_2, f7_19))
+       h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f4_2, f8_19))
+       h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f4_2, f9_19))
 
        h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f5_2, f5_19))
-       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f5_2, f7_19))
+       h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f5_2, f6_19))
        h2 = v128.add<i64>(h2, v128.extmul_high<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_high<i32>(f5_2, f9_38))
 
        h2 = v128.add<i64>(h2, v128.extmul_low<i32>(f6, f6_19))
-       h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f67_2, f7_19))
-       h4 = v128.add<i64>(h4, v128.extmul_low<i32>(f67_2, f8_19))
-       h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f67_2, f9_19))
+       h3 = v128.add<i64>(h3, v128.extmul_high<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_high<i32>(f6_2, f9_19))
 
        h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f7_2, f7_19))
        h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f7_2, f8_19))
@@ -1331,20 +1334,20 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
        h1 = v128.add<i64>(h1, carry0)
 
        // reduce i64x2 to i32x4 now that only low half has a value and fits into i32
-       const h01: v128 = v128.shuffle<i32>(h0, h1, 0, 4, 2, 6) // h0a, h1a, h0b, h1b
-       const h23: v128 = v128.shuffle<i32>(h2, h3, 0, 4, 2, 6) // h2a, h3a, h2b, h3b
-       const h45: v128 = v128.shuffle<i32>(h4, h5, 0, 4, 2, 6) // h4a, h5a, h4b, h5b
-       const h67: v128 = v128.shuffle<i32>(h6, h7, 0, 4, 2, 6) // h6a, h7a, h6b, h7b
-       const h89: v128 = v128.shuffle<i32>(h8, h9, 0, 4, 2, 6) // h8a, h9a, h8b, h9b
+       const h01: v128 = v128.shuffle<i32>(h0, h1, 0, 2, 4, 6) // h0a, h0b, h1a, h1b
+       const h23: v128 = v128.shuffle<i32>(h2, h3, 0, 2, 4, 6) // h2a, h2b ,h3a, h3b
+       const h45: v128 = v128.shuffle<i32>(h4, h5, 0, 2, 4, 6) // h4a, h4b, h5a, h5b
+       const h67: v128 = v128.shuffle<i32>(h6, h7, 0, 2, 4, 6) // h6a, h6b, h7a, h7b
+       const h89: v128 = v128.shuffle<i32>(h8, h9, 0, 2, 4, 6) // h8a, h8b, h9a, h9b
 
        // assign results to parallel outputs
-       v128.store(changetype<usize>(h_a), v128.shuffle<i32>(h01, h23, 0, 1, 4, 5), 0)
-       v128.store(changetype<usize>(h_a), v128.shuffle<i32>(h45, h67, 0, 1, 4, 5), 16)
-       v128.store_lane<i64>(changetype<usize>(h_a), h89, 0, 32)
+       v128.store(changetype<usize>(h_a), v128.shuffle<i32>(h01, h23, 0, 2, 4, 6), 0)
+       v128.store(changetype<usize>(h_a), v128.shuffle<i32>(h45, h67, 0, 2, 4, 6), 16)
+       v128.store_lane<i64>(changetype<usize>(h_a), v128.shuffle<i32>(h89, h89, 0, 2, 4, 6), 0, 32)
 
-       v128.store(changetype<usize>(h_b), v128.shuffle<i32>(h01, h23, 2, 3, 6, 7), 0)
-       v128.store(changetype<usize>(h_b), v128.shuffle<i32>(h45, h67, 2, 3, 6, 7), 16)
-       v128.store_lane<i64>(changetype<usize>(h_b), h89, 1, 32)
+       v128.store(changetype<usize>(h_b), v128.shuffle<i32>(h01, h23, 1, 3, 5, 7), 0)
+       v128.store(changetype<usize>(h_b), v128.shuffle<i32>(h45, h67, 1, 3, 5, 7), 16)
+       v128.store_lane<i64>(changetype<usize>(h_b), v128.shuffle<i32>(h89, h89, 1, 3, 5, 7), 0, 32)
 }
 
 /**