]> git.codecow.com Git - nano25519.git/commitdiff
Carry scalars in mul and sq with mask instead of shift.
authorChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 18:35:09 +0000 (11:35 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 21 Oct 2026 18:35:09 +0000 (11:35 -0700)
src/assembly/ed25519/fe.ts

index ec8ff765c917b8940c86e0e05f92e47823175519..c3ad5638bb9ba5edf1128996da1e9fff5aff7a9d 100644 (file)
@@ -437,6 +437,11 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        let h9: i64 = i64x2.extract_lane(h89, 1)
 
        // reduce scalars with carry
+       const s25: i64 = <i64>1 << 25
+       const s24: i64 = <i64>1 << 24
+       const m26 = ~((<i64>1 << 26) - 1)
+       const m25 = ~((<i64>1 << 25) - 1)
+       let c: i64
        let carry0: i64
        let carry1: i64
        let carry2: i64
@@ -448,52 +453,64 @@ export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void
        let carry8: i64
        let carry9: i64
 
-       carry0 = (h0 + (1 << 25)) >> 26
-       h0 -= carry0 << 26
+       c = h0 + s25
+       carry0 = c >> 26
+       h0 -= c & m26
        h1 += carry0
 
-       carry4 = (h4 + (1 << 25)) >> 26
-       h4 -= carry4 << 26
+       c = h4 + s25
+       carry4 = c >> 26
+       h4 -= c & m26
        h5 += carry4
 
-       carry1 = (h1 + (1 << 24)) >> 25
-       h1 -= carry1 << 25
+       c = h1 + s24
+       carry1 = c >> 25
+       h1 -= c & m25
        h2 += carry1
 
-       carry5 = (h5 + (1 << 24)) >> 25
-       h5 -= carry5 << 25
+       c = h5 + s24
+       carry5 = c >> 25
+       h5 -= c & m25
        h6 += carry5
 
-       carry2 = (h2 + (1 << 25)) >> 26
-       h2 -= carry2 << 26
+       c = h2 + s25
+       carry2 = c >> 26
+       h2 -= c & m26
        h3 += carry2
 
-       carry6 = (h6 + (1 << 25)) >> 26
-       h6 -= carry6 << 26
+       c = h6 + s25
+       carry6 = c >> 26
+       h6 -= c & m26
        h7 += carry6
 
-       carry3 = (h3 + (1 << 24)) >> 25
-       h3 -= carry3 << 25
+       c = h3 + s24
+       carry3 = c >> 25
+       h3 -= c & m25
        h4 += carry3
 
-       carry7 = (h7 + (1 << 24)) >> 25
-       h7 -= carry7 << 25
+       c = h7 + s24
+       carry7 = c >> 25
+       h7 -= c & m25
        h8 += carry7
 
-       carry4 = (h4 + (1 << 25)) >> 26
-       h4 -= carry4 << 26
+       c = h4 + s25
+       carry4 = c >> 26
+       h4 -= c & m26
        h5 += carry4
 
-       carry8 = (h8 + (1 << 25)) >> 26
-       h8 -= carry8 << 26
+       c = h8 + s25
+       carry8 = c >> 26
+       h8 -= c & m26
        h9 += carry8
 
-       carry9 = (h9 + (1 << 24)) >> 25
-       h9 -= carry9 << 25
+       c = h9 + s24
+       carry9 = c >> 25
+       h9 -= c & m25
        h0 += carry9 * 19
 
-       carry0 = (h0 + (1 << 25)) >> 26
-       h0 -= carry0 << 26
+       c = h0 + s25
+       carry0 = c >> 26
+       h0 -= c & m26
        h1 += carry0
 
        // assign results to output
@@ -1062,6 +1079,12 @@ export function fe_sq (h: FieldElement, f: FieldElement): void {
 
        h8 += f9 * f9_38
 
+       // reduce scalars with carry
+       const s25: i64 = <i64>1 << 25
+       const s24: i64 = <i64>1 << 24
+       const m26 = ~((<i64>1 << 26) - 1)
+       const m25 = ~((<i64>1 << 25) - 1)
+       let c: i64
        let carry0: i64
        let carry1: i64
        let carry2: i64
@@ -1073,48 +1096,65 @@ export function fe_sq (h: FieldElement, f: FieldElement): void {
        let carry8: i64
        let carry9: i64
 
-       carry0 = (h0 + i64(1 << 25)) >> 26
+       c = h0 + s25
+       carry0 = c >> 26
+       h0 -= c & m26
        h1 += carry0
-       h0 -= carry0 << 26
-       carry4 = (h4 + i64(1 << 25)) >> 26
+
+       c = h4 + s25
+       carry4 = c >> 26
+       h4 -= c & m26
        h5 += carry4
-       h4 -= carry4 << 26
 
-       carry1 = (h1 + i64(1 << 24)) >> 25
+       c = h1 + s24
+       carry1 = c >> 25
+       h1 -= c & m25
        h2 += carry1
-       h1 -= carry1 << 25
-       carry5 = (h5 + i64(1 << 24)) >> 25
+
+       c = h5 + s24
+       carry5 = c >> 25
+       h5 -= c & m25
        h6 += carry5
-       h5 -= carry5 << 25
 
-       carry2 = (h2 + i64(1 << 25)) >> 26
+       c = h2 + s25
+       carry2 = c >> 26
+       h2 -= c & m26
        h3 += carry2
-       h2 -= carry2 << 26
-       carry6 = (h6 + i64(1 << 25)) >> 26
+
+       c = h6 + s25
+       carry6 = c >> 26
+       h6 -= c & m26
        h7 += carry6
-       h6 -= carry6 << 26
 
-       carry3 = (h3 + i64(1 << 24)) >> 25
+       c = h3 + s24
+       carry3 = c >> 25
+       h3 -= c & m25
        h4 += carry3
-       h3 -= carry3 << 25
-       carry7 = (h7 + i64(1 << 24)) >> 25
+
+       c = h7 + s24
+       carry7 = c >> 25
+       h7 -= c & m25
        h8 += carry7
-       h7 -= carry7 << 25
 
-       carry4 = (h4 + i64(1 << 25)) >> 26
+       c = h4 + s25
+       carry4 = c >> 26
+       h4 -= c & m26
        h5 += carry4
-       h4 -= carry4 << 26
-       carry8 = (h8 + i64(1 << 25)) >> 26
+
+       c = h8 + s25
+       carry8 = c >> 26
+       h8 -= c & m26
        h9 += carry8
-       h8 -= carry8 << 26
 
-       carry9 = (h9 + i64(1 << 24)) >> 25
+       c = h9 + s24
+       carry9 = c >> 25
+       h9 -= c & m25
        h0 += carry9 * 19
-       h9 -= carry9 << 25
 
-       carry0 = (h0 + i64(1 << 25)) >> 26
+       c = h0 + s25
+       carry0 = c >> 26
+       h0 -= c & m26
        h1 += carry0
-       h0 -= carry0 << 26
 
        store<i32>(changetype<usize>(h), h0, 0)
        store<i32>(changetype<usize>(h), h1, 4)