Skip to main content

log_fft/paper/
ops.rs

1//! The concrete commuting family used for verification.
2//!
3//! Vectors are functions `x : F_2^m → F_p`, stored as slices of length `2^m`.
4//! `Q_j` is translation by the `j`-th basis vector: `(Q_j x)(y) = x(y ⊕ e_j)`.
5//! The `Q_j` are commuting involutions. The Walsh–Hadamard transform `S`
6//! diagonalises all of them at once. At frequency `ν`, `Q_j` has
7//! eigenvalue `(−1)^{ν_j}`, so `k_j(ν) = ν_j`.
8//!
9//! The target is `F = Q_1 ⋯ Q_m`, translation by the all-ones vector.
10//! Its multiplier at `ν` is `z(ν) = (−1)^{|ν|}`.
11//!
12//! Applied directly, `F` is just a permutation, so it costs nothing. The point
13//! here is to check that the paper's predictor-and-repair pipeline rebuilds
14//! `F` exactly, using only linear combinations of single `Q_j` applications
15//! plus a small spectral table. In the paper's recursion, each `Q_j` is an
16//! expensive call: a sub-transform along one axis.
17
18use crate::field::Fp;
19use std::cell::Cell;
20
21/// Counts the operations that the paper's cost model charges for.
22#[derive(Debug, Default)]
23pub struct OpCounter {
24    /// Single-row applications of some `Q_j`.
25    pub q_calls: Cell<u64>,
26    /// Full row conversions by `S` or `S^{-1}` (spectral table).
27    pub spectral_rows: Cell<u64>,
28}
29
30impl OpCounter {
31    pub fn q(&self) -> u64 {
32        self.q_calls.get()
33    }
34    pub fn spectral(&self) -> u64 {
35        self.spectral_rows.get()
36    }
37    fn bump_q(&self) {
38        self.q_calls.set(self.q_calls.get() + 1);
39    }
40    fn bump_spectral(&self) {
41        self.spectral_rows.set(self.spectral_rows.get() + 1);
42    }
43}
44
45/// `Q_j x`: translation by `e_j`.
46pub fn apply_q(j: usize, x: &[Fp], ctr: &OpCounter) -> Vec<Fp> {
47    ctr.bump_q();
48    let bit = 1usize << j;
49    (0..x.len()).map(|y| x[y ^ bit]).collect()
50}
51
52/// `F x`, the reference answer: translation by the all-ones vector.
53pub fn apply_f_reference(m: usize, x: &[Fp]) -> Vec<Fp> {
54    let mask = (1usize << m) - 1;
55    (0..x.len()).map(|y| x[y ^ mask]).collect()
56}
57
58/// In-place unnormalised Walsh–Hadamard transform `S`.
59pub fn wht_in_place(x: &mut [Fp]) {
60    let n = x.len();
61    assert!(n.is_power_of_two());
62    let mut h = 1;
63    while h < n {
64        for i in (0..n).step_by(2 * h) {
65            for k in i..i + h {
66                let (a, b) = (x[k], x[k + h]);
67                x[k] = a + b;
68                x[k + h] = a - b;
69            }
70        }
71        h *= 2;
72    }
73}
74
75/// Forward conversion `S x`.
76pub fn to_spectral(x: &[Fp], ctr: &OpCounter) -> Vec<Fp> {
77    ctr.bump_spectral();
78    let mut y = x.to_vec();
79    wht_in_place(&mut y);
80    y
81}
82
83/// Inverse conversion `S^{-1} x = S x / 2^m`.
84pub fn from_spectral(x: &[Fp], ctr: &OpCounter) -> Vec<Fp> {
85    ctr.bump_spectral();
86    let mut y = x.to_vec();
87    wht_in_place(&mut y);
88    let inv_n = Fp::new(x.len() as u64).inv();
89    for v in &mut y {
90        *v *= inv_n;
91    }
92    y
93}
94
95/// `z(ν) = (−1)^{|ν|}`, the multiplier of `F`.
96pub fn target_multiplier(nu: usize) -> Fp {
97    Fp::sign(nu.count_ones() as i64)
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn q_diagonalised_by_wht() {
106        let m = 4;
107        let ctr = OpCounter::default();
108        let x: Vec<Fp> = (0..1 << m).map(|i| Fp::new(i * i + 3)).collect();
109        let xs = to_spectral(&x, &ctr);
110        for j in 0..m {
111            let qs = to_spectral(&apply_q(j, &x, &ctr), &ctr);
112            for nu in 0..1usize << m {
113                let ev = Fp::sign(((nu >> j) & 1) as i64);
114                assert_eq!(qs[nu], ev * xs[nu]);
115            }
116        }
117        assert_eq!(from_spectral(&xs, &ctr), x);
118    }
119}