From: Chris Duncan Date: Thu, 5 Nov 2026 05:37:57 +0000 (-0800) Subject: Use pointer to avoid array accessor with bounds checking. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=9760b383c35a9049f3de1ce42968a37904ce4ea3;p=nano25519.git Use pointer to avoid array accessor with bounds checking. --- diff --git a/src/assembly/ed25519/ge/scalarmult_base.ts b/src/assembly/ed25519/ge/scalarmult_base.ts index a2b95b0..18c02a9 100644 --- a/src/assembly/ed25519/ge/scalarmult_base.ts +++ b/src/assembly/ed25519/ge/scalarmult_base.ts @@ -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 = new StaticArray(64) +const e: usize = changetype(new StaticArray(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, a: StaticArray): 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(e + (i << 1), (a[i] >> 0) & 15) + store(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(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(ei, n) + } - e[63] += carry + // e[63] += carry + store(e + 63, load(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(changetype(e) + i)) + for (let ei = e + 1, bp = base; ei < e + 64; ei += 2, bp += 1152) { + ge_cmov8_base(t, bp, load(ei)) p3_add_precomp(r, h, t) p1p1_to_p3(h, r) } @@ -64,8 +73,8 @@ export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray(changetype(e) + i)) + for (let ei = e, bp = base; ei < e + 64; ei += 2, bp += 1152) { + ge_cmov8_base(t, bp, load(ei)) p3_add_precomp(r, h, t) p1p1_to_p3(h, r) } @@ -77,7 +86,11 @@ export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray