/**
*
- * `f = a²ⁿ`
+ * `f = a²`
*
* Square a FieldElement and store the result.
*
}
/**
- * f = a²`
+ * 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]`
+ * `a[x]*a[y] + a[y]*f[x] = 2*a[x]*a[y]`
*
* ```
* // non-constant-time example
* @param a FieldElement base source
*/
export function fe_sq_n (f: FieldElement, a: FieldElement, n: i32): void {
+ // Define shifts and masks for carry passes
+ const s25: i64 = <i64>1 << 25
+ const s24: i64 = <i64>1 << 24
+ const m26 = ~((<i64>1 << 26) - 1)
+ const m25 = ~((<i64>1 << 25) - 1)
+
+ // Declare temporary variables for carry passes
+ let c: i64
+ let carry0: i64
+ let carry1: i64
+ let carry2: i64
+ let carry3: i64
+ let carry4: i64
+ let carry5: i64
+ let carry6: i64
+ let carry7: i64
+ let carry8: i64
+ let carry9: i64
+
+ // Declare variables for FieldElement state at the start of each loop
+ let a0: i64
+ let a1: i64
+ let a2: i64
+ let a3: i64
+ let a4: i64
+ let a5: i64
+ let a6: i64
+ let a7: i64
+ let a8: i64
+ let a9: i64
+ let a0_2: i64
+ let a1_2: i64
+ let a2_2: i64
+ let a3_2: i64
+ let a4_2: i64
+ let a5_2: i64
+ let a6_2: i64
+ let a7_2: i64
+ let a6_19: i64
+ let a8_19: i64
+ let a5_38: i64
+ let a7_38: i64
+ let a9_38: i64
+
+ // Initialize `f` with `a` so that, at the start of the loop, `a` is carried over from `f` as if loop just concluded
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))
- const a2: i64 = i64(load<i32>(a_ptr, 2 << 2))
- const a3: i64 = i64(load<i32>(a_ptr, 3 << 2))
- const a4: i64 = i64(load<i32>(a_ptr, 4 << 2))
- const a5: i64 = i64(load<i32>(a_ptr, 5 << 2))
- const a6: i64 = i64(load<i32>(a_ptr, 6 << 2))
- const a7: i64 = i64(load<i32>(a_ptr, 7 << 2))
- const a8: i64 = i64(load<i32>(a_ptr, 8 << 2))
- const a9: i64 = i64(load<i32>(a_ptr, 9 << 2))
-
- const a0_2: i64 = a0 + a0
- const a1_2: i64 = a1 + a1
- const a2_2: i64 = a2 + a2
- const a3_2: i64 = a3 + a3
- const a4_2: i64 = a4 + a4
- const a5_2: i64 = a5 + a5
- const a6_2: i64 = a6 + a6
- const a7_2: i64 = a7 + a7
-
- const a6_19: i64 = a6 * 19 /* 1.959375*2³⁰ */
- const a8_19: i64 = a8 * 19 /* 1.959375*2³⁰ */
-
- const a5_38: i64 = a5 * 38 /* 1.959375*2³⁰ */
- const a7_38: i64 = a7 * 38 /* 1.959375*2³⁰ */
- const a9_38: i64 = a9 * 38 /* 1.959375*2³⁰ */
-
- let f0: i64 = a0 * a0
- let f1: i64 = a0_2 * a1
- let f2: i64 = a0_2 * a2
- let f3: i64 = a0_2 * a3
- let f4: i64 = a0_2 * a4
- let f5: i64 = a0_2 * a5
- let f6: i64 = a0_2 * a6
- let f7: i64 = a0_2 * a7
- let f8: i64 = a0_2 * a8
- let f9: i64 = a0_2 * a9
+ let f0: i64 = i64(load<i32>(a_ptr, 0 << 2))
+ let f1: i64 = i64(load<i32>(a_ptr, 1 << 2))
+ let f2: i64 = i64(load<i32>(a_ptr, 2 << 2))
+ let f3: i64 = i64(load<i32>(a_ptr, 3 << 2))
+ let f4: i64 = i64(load<i32>(a_ptr, 4 << 2))
+ let f5: i64 = i64(load<i32>(a_ptr, 5 << 2))
+ let f6: i64 = i64(load<i32>(a_ptr, 6 << 2))
+ let f7: i64 = i64(load<i32>(a_ptr, 7 << 2))
+ let f8: i64 = i64(load<i32>(a_ptr, 8 << 2))
+ let f9: i64 = i64(load<i32>(a_ptr, 9 << 2))
+
+ // Square `a`, `n` times
+ for (let i = 0; i < n; i++) {
+ a0 = f0
+ a1 = f1
+ a2 = f2
+ a3 = f3
+ a4 = f4
+ a5 = f5
+ a6 = f6
+ a7 = f7
+ a8 = f8
+ a9 = f9
+
+ a0_2 = a0 + a0
+ a1_2 = a1 + a1
+ a2_2 = a2 + a2
+ a3_2 = a3 + a3
+ a4_2 = a4 + a4
+ a5_2 = a5 + a5
+ a6_2 = a6 + a6
+ a7_2 = a7 + a7
+
+ a6_19 = a6 * 19 /* 1.959375*2³⁰ */
+ a8_19 = a8 * 19 /* 1.959375*2³⁰ */
+
+ a5_38 = a5 * 38 /* 1.959375*2³⁰ */
+ a7_38 = a7 * 38 /* 1.959375*2³⁰ */
+ a9_38 = a9 * 38 /* 1.959375*2³⁰ */
+
+ // Start accumulation pass
+ f0 = a0 * a0
+ f1 = a0_2 * a1
+ f2 = a0_2 * a2
+ f3 = a0_2 * a3
+ f4 = a0_2 * a4
+ f5 = a0_2 * a5
+ f6 = a0_2 * a6
+ f7 = a0_2 * a7
+ f8 = a0_2 * a8
+ f9 = a0_2 * a9
f2 += a1_2 * a1
f3 += a1_2 * a2
f8 += a9 * a9_38
- carry_i64(changetype<usize>(f), f0, f1, f2, f3, f4, f5, f6, f7, f8, f9)
+ // Reduce scalar accumulators by carrying coefficients into higher exponents
+ c = h0 + s25
+ carry0 = c >> 26
+ h0 -= c & m26
+ h1 += carry0
+
+ c = h4 + s25
+ carry4 = c >> 26
+ h4 -= c & m26
+ h5 += carry4
+
+ c = h1 + s24
+ carry1 = c >> 25
+ h1 -= c & m25
+ h2 += carry1
+
+ c = h5 + s24
+ carry5 = c >> 25
+ h5 -= c & m25
+ h6 += carry5
+
+ c = h2 + s25
+ carry2 = c >> 26
+ h2 -= c & m26
+ h3 += carry2
+
+ c = h6 + s25
+ carry6 = c >> 26
+ h6 -= c & m26
+ h7 += carry6
+
+ c = h3 + s24
+ carry3 = c >> 25
+ h3 -= c & m25
+ h4 += carry3
+
+ c = h7 + s24
+ carry7 = c >> 25
+ h7 -= c & m25
+ h8 += carry7
+
+ c = h4 + s25
+ carry4 = c >> 26
+ h4 -= c & m26
+ h5 += carry4
+
+ c = h8 + s25
+ carry8 = c >> 26
+ h8 -= c & m26
+ h9 += carry8
+
+ c = h9 + s24
+ carry9 = c >> 25
+ h9 -= c & m25
+ h0 += carry9 * 19
+
+ c = h0 + s25
+ carry0 = c >> 26
+ h0 -= c & m26
+ h1 += carry0
+ }
+
+ // Store final results
+ store<i32>(f, f0, 0)
+ store<i32>(f, f1, 4)
+ store<i32>(f, f2, 8)
+ store<i32>(f, f3, 12)
+ store<i32>(f, f4, 16)
+ store<i32>(f, f5, 20)
+ store<i32>(f, f6, 24)
+ store<i32>(f, f7, 28)
+ store<i32>(f, f8, 32)
+ store<i32>(f, f9, 36)
}
/**
- * `f = a²`
+ * `f = a²; f = a²`
*
* Square two FieldElement values in parallel and store the results.
*
* Can overlap `f` with `a`.
*
* 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
* }
* }
* }
* ```
*
* 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_0 First squaring destination
* @param a_0 First multiplicand source