]> git.codecow.com Git - nano25519.git/commitdiff
Draft looping square helper.
authorChris Duncan <chris@codecow.com>
Sat, 31 Oct 2026 17:32:05 +0000 (10:32 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 31 Oct 2026 17:32:05 +0000 (10:32 -0700)
src/assembly/ed25519/fe.ts

index 606cd93ecf059a9d6de54da5d8564a82d1811e60..d62281d0883e535fd7f8a5a385b7553eb2d1c7e8 100644 (file)
@@ -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<usize>(a)
        const a0: i64 = i64(load<i32>(a_ptr, 0 << 2))
        const a1: i64 = i64(load<i32>(a_ptr, 1 << 2))