From b2f44ea80639329ab23e6570dcbf7538929bbdc1 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 27 Sep 2026 22:11:07 -0700 Subject: [PATCH] Simplify bitwise selection with AssemblyScript shift rules. --- src/assembly/ed25519/ge.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index 2c9b06e..d2b6a23 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -41,14 +41,15 @@ const minust = new StaticArray(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(t.yplusx), bp, iEqualsBabs) fe_memcmov(changetype(t.yminusx), bp + 48, iEqualsBabs) fe_memcmov(changetype(t.xy2d), bp + 96, iEqualsBabs) -- 2.52.0