]> git.codecow.com Git - nano25519.git/commitdiff
Simplify bitwise selection with AssemblyScript shift rules.
authorChris Duncan <chris@zoso.dev>
Mon, 28 Sep 2026 05:11:07 +0000 (22:11 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 28 Sep 2026 05:11:07 +0000 (22:11 -0700)
src/assembly/ed25519/ge.ts

index 2c9b06ef0256e844bd6b3c5f31b7045fd188f488..d2b6a233e51448469bd7879748738893b152f4d5 100644 (file)
@@ -41,14 +41,15 @@ const minust = new StaticArray<i32>(36)
 //@ts-expect-error
 @inline
 function ge_cmov8_base (t: ge_precomp, bp: usize, b: i8): void {
-       const bnegative: u8 = u8(b) >> 7 /* b == 0..127: positive; b == 128..255: negative */
-       const babs: u8 = b - (((-bnegative) & b) * (i8(1) << 1))
+       const bnegative: i8 = b >> 7 /* retains only the sign bit of `b` */
+       const babs: i8 = b - (((-bnegative) & b) * (i8(1) << 1))
 
        ge_precomp_0(t)
 
        // base pointer offset += 144n = (4 bytes * 12 values * 3 fields) * n
-       for (let i: u8 = 1; i < 9; i++, bp += 144) {
-               const iEqualsBabs = (u32(babs ^ i) - 1) >> 31 /* 1: yes; 0: no */
+       // for values 1..8, conditionally move if `abs(b)` equals the current index
+       for (let i: i8 = 1; i <= 8; i++, bp += 144) {
+               const iEqualsBabs: i8 = ((babs ^ i) - 1) >> 7 /* xor == 0: yes */
                fe_memcmov(changetype<usize>(t.yplusx), bp, iEqualsBabs)
                fe_memcmov(changetype<usize>(t.yminusx), bp + 48, iEqualsBabs)
                fe_memcmov(changetype<usize>(t.xy2d), bp + 96, iEqualsBabs)