]> git.codecow.com Git - nano25519.git/commitdiff
Replace shift doublings with addition doublings.
authorChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 15:55:55 +0000 (08:55 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 15:55:55 +0000 (08:55 -0700)
src/assembly/ed25519/fe.ts

index 143123f23f744d1a00b0f5593063766fe075d544..2e5462631f7ca588e23fd50411f844c56ecbd106 100644 (file)
@@ -103,7 +103,7 @@ export function fe_copy (h: FieldElement, f: FieldElement): void {
  */
 export function fe_dbl (h: FieldElement, f: FieldElement): void {
        const f0 = v128.load(changetype<usize>(f), 0), f1 = v128.load(changetype<usize>(f), 16), f2 = v128.load(changetype<usize>(f), 32)
-       const h0 = v128.shl<i32>(f0, 1), h1 = v128.shl<i32>(f1, 1), h2 = v128.shl<i32>(f2, 1)
+       const h0 = v128.add<i32>(f0, f0), h1 = v128.add<i32>(f1, f1), h2 = v128.add<i32>(f2, f2)
        v128.store(changetype<usize>(h), h0, 0)
        v128.store(changetype<usize>(h), h1, 16)
        v128.store_lane<i64>(changetype<usize>(h), h2, 0, 32)
@@ -752,7 +752,7 @@ export function fe_mul_dual (h_a: FieldElement, f_a: FieldElement, g_a: FieldEle
 
        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))))
+       h0 = v128.add<i64>(h0, v128.add<i64>(carry9, v128.add<i64>(v128.add<i64>(carry9, carry9), v128.shl<i64>(carry9, 4))))
 
        carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias25)
        h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
@@ -1351,7 +1351,7 @@ export function fe_sq_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldElem
 
        carry9 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h9, bias24), 25), unbias25)
        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))))
+       h0 = v128.add<i64>(h0, v128.add<i64>(carry9, v128.add<i64>(v128.add<i64>(carry9, carry9), v128.shl<i64>(carry9, 4))))
 
        carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias26)
        h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))
@@ -1612,16 +1612,16 @@ export function fe_sq2_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldEle
        const f9: v128 = v128.shuffle<i32>(f_a_89xx, f_b_89xx, 1, 5, 3, 7)
 
        // 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 f0_2: v128 = v128.add<i32>(f0, f0)
+       const f1_2: v128 = v128.add<i32>(f1, f1)
+       const f2_2: v128 = v128.add<i32>(f2, f2)
+       const f3_2: v128 = v128.add<i32>(f3, f3)
+       const f4_2: v128 = v128.add<i32>(f4, f4)
+       const f5_2: v128 = v128.add<i32>(f5, f5)
+       const f6_2: v128 = v128.add<i32>(f6, f6)
+       const f7_2: v128 = v128.add<i32>(f7, f7)
+       const f8_2: v128 = v128.add<i32>(f8, f8)
+       const f9_2: v128 = v128.add<i32>(f9, f9)
 
        // 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)))
@@ -1631,8 +1631,8 @@ export function fe_sq2_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldEle
        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)
 
        let h0: v128 = v128.extmul_low<i32>(f0, f0)
        let h1: v128 = v128.extmul_low<i32>(f0_2, f1)
@@ -1776,7 +1776,7 @@ export function fe_sq2_dual (h_a: FieldElement, f_a: FieldElement, h_b: FieldEle
 
        carry9 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h9, bias24), 25), unbias25)
        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))))
+       h0 = v128.add<i64>(h0, v128.add<i64>(carry9, v128.add<i64>(v128.add<i64>(carry9, carry9), v128.shl<i64>(carry9, 4))))
 
        carry0 = v128.sub<i64>(v128.shr<u64>(v128.add<i64>(h0, bias25), 26), unbias26)
        h0 = v128.sub<i64>(h0, v128.shl<i64>(carry0, 26))