use crate::field::Fp;
use crate::ntt::{series_inverse, Ntt};
#[derive(Clone, Debug)]
pub struct ChirpZ {
n: usize,
m: usize,
pre: Vec<Fp>,
c_hat: Vec<Fp>,
ntt: Ntt,
}
fn chirp_powers(q: Fp, len: usize) -> Vec<Fp> {
let mut out = Vec::with_capacity(len);
let mut cur = Fp::ONE; let mut qt = Fp::ONE; for _ in 0..len {
out.push(cur);
cur *= qt;
qt *= q;
}
out
}
impl ChirpZ {
pub fn new(n: usize, m: usize, q: Fp) -> Self {
assert!(q != Fp::ZERO, "chirp-z ratio must be nonzero");
assert!(n > 0 && m > 0, "chirp-z needs n, m > 0");
let len = n + m - 1;
let size = len.next_power_of_two();
let ntt = Ntt::new(size);
let mut c_hat = chirp_powers(q, len);
c_hat.resize(size, Fp::ZERO);
ntt.forward(&mut c_hat);
let pre = chirp_powers(q.inv(), n.max(m));
ChirpZ { n, m, pre, c_hat, ntt }
}
pub fn input_len(&self) -> usize {
self.n
}
pub fn output_len(&self) -> usize {
self.m
}
pub fn apply(&self, x: &[Fp]) -> Vec<Fp> {
assert_eq!(x.len(), self.n);
let size = self.ntt.len();
let mut buf = vec![Fp::ZERO; size];
for (j, &xj) in x.iter().enumerate() {
buf[self.n - 1 - j] = xj * self.pre[j];
}
self.ntt.forward(&mut buf);
for (b, c) in buf.iter_mut().zip(&self.c_hat) {
*b *= *c;
}
self.ntt.inverse(&mut buf);
(0..self.m).map(|k| buf[self.n - 1 + k] * self.pre[k]).collect()
}
}
#[derive(Clone, Debug)]
pub struct InverseChirpZ {
n: usize,
inv_h: Vec<Fp>,
inv_mid: Vec<Fp>,
s_hat: Vec<Fp>,
ntt: Ntt,
}
impl InverseChirpZ {
pub fn new(n: usize, q: Fp) -> Self {
assert!(n > 0);
assert!(q != Fp::ZERO, "ratio must be nonzero");
let mut h = vec![Fp::ONE; n];
let mut qk = Fp::ONE;
for i in 1..n {
qk *= q;
let f = Fp::ONE - qk;
assert!(f != Fp::ZERO, "nodes not distinct: q^{i} = 1");
h[i] = h[i - 1] * f;
}
let inv_h = batch_inverse(&h);
let cinv = chirp_powers(q.inv(), n);
let inv_mid = (0..n).map(|j| Fp::sign(j as i64) * cinv[j] * h[j]).collect();
let s = series_inverse(&inv_h, n);
let size = (2 * n - 1).next_power_of_two();
let ntt = Ntt::new(size);
let mut s_hat = s;
s_hat.resize(size, Fp::ZERO);
ntt.forward(&mut s_hat);
InverseChirpZ { n, inv_h, inv_mid, s_hat, ntt }
}
pub fn len(&self) -> usize {
self.n
}
pub fn is_empty(&self) -> bool {
self.n == 0
}
fn lower(&self, x: &[Fp]) -> Vec<Fp> {
let mut buf = x.to_vec();
buf.resize(self.ntt.len(), Fp::ZERO);
self.ntt.forward(&mut buf);
for (b, s) in buf.iter_mut().zip(&self.s_hat) {
*b *= *s;
}
self.ntt.inverse(&mut buf);
buf.truncate(self.n);
buf
}
pub fn apply(&self, y: &[Fp]) -> Vec<Fp> {
assert_eq!(y.len(), self.n);
let v: Vec<Fp> = y.iter().zip(&self.inv_h).map(|(a, b)| *a * *b).collect();
let mut v = self.lower(&v);
v.iter_mut().zip(&self.inv_mid).for_each(|(a, b)| *a *= *b);
v.reverse();
let mut v = self.lower(&v);
v.reverse();
v.iter_mut().zip(&self.inv_h).for_each(|(a, b)| *a *= *b);
v
}
}
pub fn batch_inverse(xs: &[Fp]) -> Vec<Fp> {
let mut prefix = Vec::with_capacity(xs.len());
let mut acc = Fp::ONE;
for &x in xs {
assert!(x != Fp::ZERO, "batch_inverse of zero");
prefix.push(acc);
acc *= x;
}
let mut inv = acc.inv();
let mut out = vec![Fp::ZERO; xs.len()];
for i in (0..xs.len()).rev() {
out[i] = prefix[i] * inv;
inv *= xs[i];
}
out
}