]> git.codecow.com Git - nano25519.git/commitdiff
Apply unsigned biasing to dual multiplication.
authorChris Duncan <chris@codecow.com>
Tue, 20 Oct 2026 19:58:16 +0000 (12:58 -0700)
committerChris Duncan <chris@codecow.com>
Tue, 20 Oct 2026 19:58:16 +0000 (12:58 -0700)
src/assembly/ed25519/fe.ts

index b745cbc014cdaf04b07bc3930ba1a4085f9e5cf3..d5b95b6403a355df94a54d6aeee9b464d8677bf7 100644 (file)
@@ -527,20 +527,25 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        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)
        let f1: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 1, 5, 1, 5)
+       f1 = v128.add<i32>(f1, v128.and(f1, odds))
        const f2: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 2, 6, 2, 6)
        let f3: v128 = v128.shuffle<i32>(f_a_0123, f_b_0123, 3, 7, 3, 7)
+       f3 = v128.add<i32>(f3, v128.and(f3, odds))
 
        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)
        let f5: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 1, 5, 1, 5)
+       f5 = v128.add<i32>(f5, v128.and(f5, odds))
        const f6: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 2, 6, 2, 6)
        let f7: v128 = v128.shuffle<i32>(f_a_4567, f_b_4567, 3, 7, 3, 7)
+       f7 = v128.add<i32>(f7, v128.and(f7, odds))
 
        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, 0, 4)
        let f9: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 1, 5, 1, 5)
+       f9 = v128.add<i32>(f9, v128.and(f9, odds))
 
        // pack each g index once
        const g_a_0123: v128 = v128.load(changetype<usize>(g_a), 0)
@@ -577,8 +582,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        let h9: v128 = v128.extmul_high<i32>(f0, g89)
 
        // f[1]
-       f1 = v128.add<i32>(f1, v128.and(f1, odds))
-
        h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f1, g01))
        h2 = v128.add<i64>(h2, v128.extmul_high<i32>(f1, g01))
        h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f1, g23))
@@ -603,8 +606,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        h1 = v128.add<i64>(h1, v128.extmul_high<i32>(f2, g89_19))
 
        // f[3]
-       f3 = v128.add<i32>(f3, v128.and(f3, odds))
-
        h3 = v128.add<i64>(h3, v128.extmul_low<i32>(f3, g01))
        h4 = v128.add<i64>(h4, v128.extmul_high<i32>(f3, g01))
        h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f3, g23))
@@ -629,8 +630,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        h3 = v128.add<i64>(h3, v128.extmul_high<i32>(f4, g89_19))
 
        // f[5]
-       f5 = v128.add<i32>(f5, v128.and(f5, odds))
-
        h5 = v128.add<i64>(h5, v128.extmul_low<i32>(f5, g01))
        h6 = v128.add<i64>(h6, v128.extmul_high<i32>(f5, g01))
        h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f5, g23))
@@ -655,8 +654,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        h5 = v128.add<i64>(h5, v128.extmul_high<i32>(f6, g89_19))
 
        // f[7]
-       f7 = v128.add<i32>(f7, v128.and(f7, odds))
-
        h7 = v128.add<i64>(h7, v128.extmul_low<i32>(f7, g01))
        h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f7, g01))
        h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f7, g23))
@@ -681,8 +678,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        h7 = v128.add<i64>(h7, v128.extmul_high<i32>(f8, g89_19))
 
        // f[9]
-       f9 = v128.add<i32>(f9, v128.and(f9, odds))
-
        h9 = v128.add<i64>(h9, v128.extmul_low<i32>(f9, g01))
        h0 = v128.add<i64>(h0, v128.extmul_high<i32>(f9, g01_19))
        h1 = v128.add<i64>(h1, v128.extmul_low<i32>(f9, g23_19))
@@ -695,8 +690,6 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        h8 = v128.add<i64>(h8, v128.extmul_high<i32>(f9, g89_19))
 
        // reduce scalars with carry
-       const bias25 = i64x2.splat(1 << 25)
-       const bias24 = i64x2.splat(1 << 24)
        let carry0: v128
        let carry1: v128
        let carry2: v128
@@ -708,69 +701,79 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
        let carry8: v128
        let carry9: v128
 
-       carry0 = v128.shr<i64>(v128.add<i64>(h0, bias25), 26)
+       // Signed i64x2 shifts are emulated on x64, unsigned ones are native. Adding
+       // 2⁶² keeps every sum non-negative (|h| ≤ 2⁶⁰·⁴), and the unsigned shift then
+       // floors in fewer instructions. The bias is removed by subtraction, and the
+       // value to remove is the bias minus the original size: 2⁶²⁻²⁶ = 2³⁶ (or
+       // 2⁶²⁻²⁵ = 2³⁷). After unbiasing, the result is the signed carry.
+       const bias24 = v128.splat<i64>((<i64>1 << 24) + (<i64>1 << 62))
+       const bias25 = v128.splat<i64>((<i64>1 << 25) + (<i64>1 << 62))
+       const unbias26 = v128.splat<i64>(<i64>1 << 37)
+       const unbias25 = v128.splat<i64>(<i64>1 << 36)
+
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias25)
        h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
        h1 = v128.add<i64>(h1, carry0)
 
-       carry4 = v128.shr<i64>(v128.add<i64>(h4, bias25), 26)
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias25)
        h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
        h5 = v128.add<i64>(h5, carry4)
 
-       carry1 = v128.shr<i64>(v128.add<i64>(h1, bias24), 25)
+       carry1 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h1, bias24), 25), unbias26)
        h1 = v128.sub<i64>(h1, v128.shl<i64>(carry1, 25))
        h2 = v128.add<i64>(h2, carry1)
 
-       carry5 = v128.shr<i64>(v128.add<i64>(h5, bias24), 25)
+       carry5 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h5, bias24), 25), unbias26)
        h5 = v128.sub<i64>(h5, v128.shl<i64>(carry5, 25))
        h6 = v128.add<i64>(h6, carry5)
 
-       carry2 = v128.shr<i64>(v128.add<i64>(h2, bias25), 26)
+       carry2 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h2, bias25), 26), unbias25)
        h2 = v128.sub<i64>(h2, v128.shl<i64>(carry2, 26))
        h3 = v128.add<i64>(h3, carry2)
 
-       carry6 = v128.shr<i64>(v128.add<i64>(h6, bias25), 26)
+       carry6 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h6, bias25), 26), unbias25)
        h6 = v128.sub<i64>(h6, v128.shl<i64>(carry6, 26))
        h7 = v128.add<i64>(h7, carry6)
 
-       carry3 = v128.shr<i64>(v128.add<i64>(h3, bias24), 25)
+       carry3 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h3, bias24), 25), unbias26)
        h3 = v128.sub<i64>(h3, v128.shl<i64>(carry3, 25))
        h4 = v128.add<i64>(h4, carry3)
 
-       carry7 = v128.shr<i64>(v128.add<i64>(h7, bias24), 25)
+       carry7 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h7, bias24), 25), unbias26)
        h7 = v128.sub<i64>(h7, v128.shl<i64>(carry7, 25))
        h8 = v128.add<i64>(h8, carry7)
 
-       carry4 = v128.shr<i64>(v128.add<i64>(h4, bias25), 26)
+       carry4 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h4, bias25), 26), unbias25)
        h4 = v128.sub<i64>(h4, v128.shl<i64>(carry4, 26))
        h5 = v128.add<i64>(h5, carry4)
 
-       carry8 = v128.shr<i64>(v128.add<i64>(h8, bias25), 26)
+       carry8 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h8, bias25), 26), unbias25)
        h8 = v128.sub<i64>(h8, v128.shl<i64>(carry8, 26))
        h9 = v128.add<i64>(h9, carry8)
 
-       carry9 = v128.shr<i64>(v128.add<i64>(h9, bias24), 25)
+       carry9 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h9, bias24), 25), unbias26)
        h9 = v128.sub<i64>(h9, v128.shl<i64>(carry9, 25))
        h0 = v128.add<i64>(h0, v128.add<i64>(carry9, v128.add<i64>(v128.shl<i64>(carry9, 1), v128.shl<i64>(carry9, 4))))
 
-       carry0 = v128.shr<i64>(v128.add<i64>(h0, bias25), 26)
+       carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias25)
        h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
        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)
 }
 
 /**