From: Chris Duncan Date: Sat, 31 Oct 2026 17:32:05 +0000 (-0700) Subject: Draft looping square helper. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=eb22d2ef5704c1975ff46b6b82c10c05a50842c6;p=nano25519.git Draft looping square helper. --- diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index 606cd93..d62281d 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -1354,36 +1354,73 @@ export function fe_reduce (h: FieldElement, f: FieldElement): void { } /** + * + * `f = a²ⁿ` + * * Square a FieldElement and store the result. * * Some iterations of the inner loop can be skipped since - * `f[x]*f[y] + f[y]*f[x] = 2*f[x]*f[y]` + * `a[x]*a[y] + a[y]*a[x] = 2*a[x]*a[y]` * * ``` * // non-constant-time example * for (let i = 0; i < 10; i++) { * for (let j = i; j < 10; j++) { - * h[i + j] += f[i] * g[j] + * f[i + j] += a[i] * b[j] * if (i < j) { - * h[i + j] *= 2 + * f[i + j] *= 2 * } * } * } * ``` * - * h = f * f - * Can overlap h with f. + * Can overlap `f` with `a`. * * Preconditions: - * |f| bounded by 1.65*2²⁶,1.65*2²⁵,1.65*2²⁶,1.65*2²⁵,etc. + * |a| bounded by 1.65*2²⁶,1.65*2²⁵,1.65*2²⁶,1.65*2²⁵,etc. * * Postconditions: - * |h| bounded by 1.01*2²⁵,1.01*2²⁴,1.01*2²⁵,1.01*2²⁴,etc. + * |f| bounded by 1.01*2²⁵,1.01*2²⁴,1.01*2²⁵,1.01*2²⁴,etc. * * @param f FieldElement power destination * @param a FieldElement base source */ export function fe_sq (f: FieldElement, a: FieldElement): void { + fe_sq_n(f, a, 1) +} + +/** + * f = a²` + * + * Can overlap `f` with `a`. + * + * Square a FieldElement `n` times and store the final result. + * + * Some iterations of the inner loop can be skipped since + * `a[x]*f[y] + a[y]*f[x] = 2*a[x]*a[y]` + * + * ``` + * // non-constant-time example + * for (let i = 0; i < 10; i++) { + * for (let j = i; j < 10; j++) { + * f[i + j] += a[i] * b[j] + * if (i < j) { + * f[i + j] *= 2 + * } + * } + * } + * ``` + * + * Preconditions: + * |a| bounded by 1.65*2²⁶,1.65*2²⁵,1.65*2²⁶,1.65*2²⁵,etc. + * + * Postconditions: + * |f| bounded by 1.01*2²⁵,1.01*2²⁴,1.01*2²⁵,1.01*2²⁴,etc. + * + * @param f FieldElement power destination + * @param a FieldElement base source + */ +export function fe_sq_n (f: FieldElement, a: FieldElement, n: i32): void { const a_ptr: usize = changetype(a) const a0: i64 = i64(load(a_ptr, 0 << 2)) const a1: i64 = i64(load(a_ptr, 1 << 2))