]> git.codecow.com Git - nano25519.git/commitdiff
Use pointer to avoid array accessor with bounds checking.
authorChris Duncan <chris@codecow.com>
Thu, 5 Nov 2026 05:37:57 +0000 (21:37 -0800)
committerChris Duncan <chris@codecow.com>
Thu, 5 Nov 2026 05:37:57 +0000 (21:37 -0800)
src/assembly/ed25519/ge/scalarmult_base.ts

index a2b95b0bd067f28af281a28f8175a5337e5d6131..18c02a9f4ebc844bcf0856aaee892d8856b84833 100644 (file)
@@ -6,19 +6,19 @@ import { base } from '../base'
 import { p1p1, p1p1_0, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_precomp, p3_dbl_to_p2, p3_tobytes, p_precomp, precomp_0 } from '../p'
 
 const h: p3 = new p3()
-const e: StaticArray<i8> = new StaticArray<i8>(64)
+const e: usize = changetype<usize>(new StaticArray<i8>(64))
 const r: p1p1 = new p1p1()
 const s: p2 = new p2()
 const t: p_precomp = new p_precomp()
 
 /**
- * `h = a * B` (with precomputation)
+ * `h = a[B]` (with precomputation)
  *
  * where
  *
  * `a = 256⁰a[0] + 256¹a[1] + ... + 256³¹a[31]`
  *
- * B is the Ed25519 base point `(x,⅘)` with `x` positive. As bytes:
+ * `B` is the Ed25519 base point `(x,⅘)` with `x` positive. As bytes:
  *
  * `0x5866666666666666666666666666666666666666666666666666666666666666`
  *
@@ -30,27 +30,36 @@ const t: p_precomp = new p_precomp()
  */
 export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u8>): void {
        for (let i = 0; i < 32; ++i) {
-               e[(i << 1) + 0] = (a[i] >> 0) & 15
-               e[(i << 1) + 1] = (a[i] >> 4) & 15
+               // e[(i << 1) + 0] = (a[i] >> 0) & 15
+               // e[(i << 1) + 1] = (a[i] >> 4) & 15
+               store<i8>(e + (i << 1), (a[i] >> 0) & 15)
+               store<i8>(e + ((i << 1) + 1), (a[i] >> 4) & 15)
        }
        /* each e[i] is between 0 and 15 */
        /* e[63] is between 0 and 7 */
 
        let carry: i8 = 0
-       for (let i = 0; i < 63; ++i) {
-               e[i] += carry
-               carry = e[i] + 8
-               carry >>= 4
-               e[i] -= carry * (i8(1) << 4)
+       for (let ei = e; ei < e + 63; ei++) {
+               let n = load<i8>(ei)
+               // e[i] += carry
+               n += carry
+               // carry = e[i] + 8
+               // carry >>= 4
+               carry = (n + 8) >> 4
+               // e[i] -= carry * (i8(1) << 4)
+               n -= carry * (i8(1) << 4)
+               store<i8>(ei, n)
+
        }
-       e[63] += carry
+       // e[63] += carry
+       store<i8>(e + 63, load<i8>(e + 63) + carry)
        /* each e[i] is between -8 and 8 */
 
        p3_0(h)
 
        // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
-       for (let i = 1, bp = 0; i < 64; i += 2, bp += 1152) {
-               ge_cmov8_base(t, base + bp, load<i8>(changetype<usize>(e) + i))
+       for (let ei = e + 1, bp = base; ei < e + 64; ei += 2, bp += 1152) {
+               ge_cmov8_base(t, bp, load<i8>(ei))
                p3_add_precomp(r, h, t)
                p1p1_to_p3(h, r)
        }
@@ -64,8 +73,8 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
        p1p1_to_p3(h, r)
 
        // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
-       for (let i = 0, bp = 0; i < 64; i += 2, bp += 1152) {
-               ge_cmov8_base(t, base + bp, load<i8>(changetype<usize>(e) + i))
+       for (let ei = e, bp = base; ei < e + 64; ei += 2, bp += 1152) {
+               ge_cmov8_base(t, bp, load<i8>(ei))
                p3_add_precomp(r, h, t)
                p1p1_to_p3(h, r)
        }
@@ -77,7 +86,11 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
        p2_0(s)
        precomp_0(t)
        p3_0(h)
-       e.fill(0)
+       // e.fill(0)
+       v128.store(e, i64x2.splat(0), 0)
+       v128.store(e, i64x2.splat(0), 16)
+       v128.store(e, i64x2.splat(0), 32)
+       v128.store(e, i64x2.splat(0), 48)
 }
 
 function ge_cmov8_base (t: p_precomp, bp: usize, b: i8): void {