1use core::ops::{AddAssign, Mul};
2
3use p3_dft::TwoAdicSubgroupDft;
4use p3_field::{PrimeCharacteristicRing, TwoAdicField};
5
6#[inline(always)]
8pub fn dot_product<T, const N: usize>(u: [T; N], v: [T; N]) -> T
9where
10 T: Copy + AddAssign + Mul<Output = T>,
11{
12 debug_assert_ne!(N, 0);
13 let mut dp = u[0] * v[0];
14 for i in 1..N {
15 dp += u[i] * v[i];
16 }
17 dp
18}
19
20pub fn apply_circulant<R: PrimeCharacteristicRing, const N: usize>(
28 circ_matrix: &[u64; N],
29 input: &[R; N],
30) -> [R; N] {
31 let matrix = circ_matrix.map(R::from_u64);
32
33 core::array::from_fn(|row| {
34 let rotated: [R; N] = core::array::from_fn(|col| matrix[(N + col - row) % N].clone());
36 R::dot_product(&rotated, input)
37 })
38}
39
40pub const fn first_row_to_first_col<const N: usize, T: Copy>(v: &[T; N]) -> [T; N] {
53 let mut output = *v;
55 let mut i = 1;
56 while i < N {
57 output[i] = v[N - i];
59 i += 1;
60 }
61 output
62}
63
64#[inline]
70pub fn apply_circulant_fft<F: TwoAdicField, const N: usize, FFT: TwoAdicSubgroupDft<F>>(
71 fft: &FFT,
72 column: [u64; N],
73 input: &[F; N],
74) -> [F; N] {
75 let column = column.map(F::from_u64).to_vec();
77 let matrix = fft.dft(column);
78
79 let input = fft.dft(input.to_vec());
81
82 let product = matrix.iter().zip(input).map(|(&x, y)| x * y).collect();
84
85 let output = fft.idft(product);
87 output.try_into().unwrap()
88}
89
90#[cfg(test)]
91mod tests {
92 use p3_baby_bear::BabyBear;
93 use p3_dft::NaiveDft;
94 use p3_field::PrimeCharacteristicRing;
95 use proptest::prelude::*;
96
97 use super::*;
98
99 type F = BabyBear;
100
101 fn arb_f() -> impl Strategy<Value = F> {
102 prop::num::u32::ANY.prop_map(F::from_u32)
103 }
104
105 #[test]
106 fn first_row_to_first_col_even_length() {
107 let input = [0, 1, 2, 3, 4, 5];
108 assert_eq!(first_row_to_first_col(&input), [0, 5, 4, 3, 2, 1]);
109 }
110
111 #[test]
112 fn first_row_to_first_col_odd_length() {
113 let input = [10, 20, 30, 40, 50];
114 assert_eq!(first_row_to_first_col(&input), [10, 50, 40, 30, 20]);
115 }
116
117 #[test]
118 fn first_row_to_first_col_single_element() {
119 assert_eq!(first_row_to_first_col(&[42]), [42]);
120 }
121
122 #[test]
123 fn first_row_to_first_col_two_elements() {
124 assert_eq!(first_row_to_first_col(&[1, 2]), [1, 2]);
125 }
126
127 #[test]
128 fn apply_circulant_identity() {
129 let identity_row: [u64; 4] = [1, 0, 0, 0];
131 let input: [F; 4] = [5, 10, 15, 20].map(F::from_u32);
132 assert_eq!(apply_circulant(&identity_row, &input), input);
133 }
134
135 #[test]
136 fn apply_circulant_all_ones() {
137 let ones: [u64; 4] = [1, 1, 1, 1];
139 let input: [F; 4] = [1, 2, 3, 4].map(F::from_u32);
140 let sum = F::from_u32(10);
141 assert_eq!(apply_circulant(&ones, &input), [sum; 4]);
142 }
143
144 #[test]
145 fn apply_circulant_scalar() {
146 let row: [u64; 4] = [7, 0, 0, 0];
148 let input: [F; 4] = [1, 2, 3, 4].map(F::from_u32);
149 let expected: [F; 4] = [7, 14, 21, 28].map(F::from_u32);
150 assert_eq!(apply_circulant(&row, &input), expected);
151 }
152
153 #[test]
154 fn apply_circulant_size_1() {
155 let row: [u64; 1] = [5];
157 let input: [F; 1] = [F::from_u32(3)];
158 assert_eq!(apply_circulant(&row, &input), [F::from_u32(15)]);
159 }
160
161 #[test]
162 fn apply_circulant_fft_matches_naive_4() {
163 let row: [u64; 4] = [2, 3, 5, 7];
165 let col = first_row_to_first_col(&row);
166 let input: [F; 4] = [1, 2, 3, 4].map(F::from_u32);
167
168 let naive = apply_circulant(&row, &input);
169 let fft_result = apply_circulant_fft(&NaiveDft, col, &input);
170 assert_eq!(naive, fft_result);
171 }
172
173 #[test]
174 fn apply_circulant_fft_identity() {
175 let row: [u64; 4] = [1, 0, 0, 0];
177 let col = first_row_to_first_col(&row);
178 let input: [F; 4] = [5, 10, 15, 20].map(F::from_u32);
179 assert_eq!(apply_circulant_fft(&NaiveDft, col, &input), input);
180 }
181
182 proptest! {
183 #[test]
184 fn first_row_to_first_col_involution(v in prop::array::uniform4(0u64..1000)) {
185 let col = first_row_to_first_col(&v);
186 let back = first_row_to_first_col(&col);
187 prop_assert_eq!(back, v);
188 }
189
190 #[test]
191 fn apply_circulant_fft_matches_naive(
192 row in prop::array::uniform4(0u64..1000),
193 input in prop::array::uniform4(arb_f()),
194 ) {
195 let col = first_row_to_first_col(&row);
196 let naive = apply_circulant(&row, &input);
197 let fft_result = apply_circulant_fft(&NaiveDft, col, &input);
198 prop_assert_eq!(naive, fft_result);
199 }
200
201 #[test]
202 fn apply_circulant_linearity(
203 row in prop::array::uniform4(0u64..100),
204 a in prop::array::uniform4(arb_f()),
205 b in prop::array::uniform4(arb_f()),
206 ) {
207 let sum_input: [F; 4] = core::array::from_fn(|i| a[i] + b[i]);
208 let ca = apply_circulant(&row, &a);
209 let cb = apply_circulant(&row, &b);
210 let c_sum = apply_circulant(&row, &sum_input);
211 for i in 0..4 {
212 prop_assert_eq!(c_sum[i], ca[i] + cb[i]);
213 }
214 }
215
216 #[test]
217 fn apply_circulant_zero_matrix(input in prop::array::uniform4(arb_f())) {
218 let zeros: [u64; 4] = [0; 4];
219 let result = apply_circulant(&zeros, &input);
220 prop_assert_eq!(result, [F::ZERO; 4]);
221 }
222 }
223}