1use core::ops::{AddAssign, Mul};
2
3use p3_dft::TwoAdicSubgroupDft;
4use p3_field::{Algebra, 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 let freq_column: [F; N] = matrix.try_into().unwrap();
79
80 apply_circulant_fft_precomputed(fft, &freq_column, input)
81}
82
83#[inline]
91pub fn apply_circulant_fft_precomputed<
92 F: TwoAdicField,
93 const N: usize,
94 FFT: TwoAdicSubgroupDft<F>,
95>(
96 fft: &FFT,
97 freq_column: &[F; N],
98 input: &[F; N],
99) -> [F; N] {
100 let input = fft.dft(input.to_vec());
102
103 let product = freq_column.iter().zip(input).map(|(&x, y)| x * y).collect();
105
106 let output = fft.idft(product);
108 output.try_into().unwrap()
109}
110
111#[inline]
129pub fn mds_multiply<F, A, const WIDTH: usize>(state: &mut [A; WIDTH], matrix: &[[F; WIDTH]; WIDTH])
130where
131 F: PrimeCharacteristicRing,
132 A: Algebra<F>,
133{
134 let input = state.clone();
136
137 for (out, row) in state.iter_mut().zip(matrix.iter()) {
139 *out = A::mixed_dot_product(&input, row);
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use p3_baby_bear::BabyBear;
146 use p3_dft::NaiveDft;
147 use p3_field::PrimeCharacteristicRing;
148 use proptest::prelude::*;
149
150 use super::*;
151
152 type F = BabyBear;
153
154 fn arb_f() -> impl Strategy<Value = F> {
155 prop::num::u32::ANY.prop_map(F::from_u32)
156 }
157
158 #[test]
159 fn first_row_to_first_col_even_length() {
160 let input = [0, 1, 2, 3, 4, 5];
161 assert_eq!(first_row_to_first_col(&input), [0, 5, 4, 3, 2, 1]);
162 }
163
164 #[test]
165 fn first_row_to_first_col_odd_length() {
166 let input = [10, 20, 30, 40, 50];
167 assert_eq!(first_row_to_first_col(&input), [10, 50, 40, 30, 20]);
168 }
169
170 #[test]
171 fn first_row_to_first_col_single_element() {
172 assert_eq!(first_row_to_first_col(&[42]), [42]);
173 }
174
175 #[test]
176 fn first_row_to_first_col_two_elements() {
177 assert_eq!(first_row_to_first_col(&[1, 2]), [1, 2]);
178 }
179
180 #[test]
181 fn apply_circulant_identity() {
182 let identity_row: [u64; 4] = [1, 0, 0, 0];
184 let input: [F; 4] = [5, 10, 15, 20].map(F::from_u32);
185 assert_eq!(apply_circulant(&identity_row, &input), input);
186 }
187
188 #[test]
189 fn apply_circulant_all_ones() {
190 let ones: [u64; 4] = [1, 1, 1, 1];
192 let input: [F; 4] = [1, 2, 3, 4].map(F::from_u32);
193 let sum = F::from_u32(10);
194 assert_eq!(apply_circulant(&ones, &input), [sum; 4]);
195 }
196
197 #[test]
198 fn apply_circulant_scalar() {
199 let row: [u64; 4] = [7, 0, 0, 0];
201 let input: [F; 4] = [1, 2, 3, 4].map(F::from_u32);
202 let expected: [F; 4] = [7, 14, 21, 28].map(F::from_u32);
203 assert_eq!(apply_circulant(&row, &input), expected);
204 }
205
206 #[test]
207 fn apply_circulant_size_1() {
208 let row: [u64; 1] = [5];
210 let input: [F; 1] = [F::from_u32(3)];
211 assert_eq!(apply_circulant(&row, &input), [F::from_u32(15)]);
212 }
213
214 #[test]
215 fn apply_circulant_fft_matches_naive_4() {
216 let row: [u64; 4] = [2, 3, 5, 7];
218 let col = first_row_to_first_col(&row);
219 let input: [F; 4] = [1, 2, 3, 4].map(F::from_u32);
220
221 let naive = apply_circulant(&row, &input);
222 let fft_result = apply_circulant_fft(&NaiveDft, col, &input);
223 assert_eq!(naive, fft_result);
224 }
225
226 #[test]
227 fn apply_circulant_fft_identity() {
228 let row: [u64; 4] = [1, 0, 0, 0];
230 let col = first_row_to_first_col(&row);
231 let input: [F; 4] = [5, 10, 15, 20].map(F::from_u32);
232 assert_eq!(apply_circulant_fft(&NaiveDft, col, &input), input);
233 }
234
235 proptest! {
236 #[test]
237 fn first_row_to_first_col_involution(v in prop::array::uniform4(0u64..1000)) {
238 let col = first_row_to_first_col(&v);
239 let back = first_row_to_first_col(&col);
240 prop_assert_eq!(back, v);
241 }
242
243 #[test]
244 fn apply_circulant_fft_matches_naive(
245 row in prop::array::uniform4(0u64..1000),
246 input in prop::array::uniform4(arb_f()),
247 ) {
248 let col = first_row_to_first_col(&row);
249 let naive = apply_circulant(&row, &input);
250 let fft_result = apply_circulant_fft(&NaiveDft, col, &input);
251 prop_assert_eq!(naive, fft_result);
252 }
253
254 #[test]
255 fn apply_circulant_linearity(
256 row in prop::array::uniform4(0u64..100),
257 a in prop::array::uniform4(arb_f()),
258 b in prop::array::uniform4(arb_f()),
259 ) {
260 let sum_input: [F; 4] = core::array::from_fn(|i| a[i] + b[i]);
261 let ca = apply_circulant(&row, &a);
262 let cb = apply_circulant(&row, &b);
263 let c_sum = apply_circulant(&row, &sum_input);
264 for i in 0..4 {
265 prop_assert_eq!(c_sum[i], ca[i] + cb[i]);
266 }
267 }
268
269 #[test]
270 fn apply_circulant_zero_matrix(input in prop::array::uniform4(arb_f())) {
271 let zeros: [u64; 4] = [0; 4];
272 let result = apply_circulant(&zeros, &input);
273 prop_assert_eq!(result, [F::ZERO; 4]);
274 }
275 }
276}