p3_interpolation/
lib.rs

1//! Tools for Lagrange interpolation.
2
3#![no_std]
4
5extern crate alloc;
6
7use alloc::vec::Vec;
8
9use p3_field::coset::TwoAdicMultiplicativeCoset;
10use p3_field::{
11    ExtensionField, TwoAdicField, batch_multiplicative_inverse, scale_slice_in_place_single_core,
12};
13use p3_matrix::Matrix;
14use p3_maybe_rayon::prelude::*;
15use p3_util::log2_strict_usize;
16
17/// Given evaluations of a batch of polynomials over the canonical power-of-two subgroup, evaluate
18/// the polynomials at `point`.
19///
20/// This assumes the point is not in the subgroup, otherwise the behavior is undefined.
21pub fn interpolate_subgroup<F, EF, Mat>(subgroup_evals: &Mat, point: EF) -> Vec<EF>
22where
23    F: TwoAdicField,
24    EF: ExtensionField<F>,
25    Mat: Matrix<F>,
26{
27    interpolate_coset(subgroup_evals, F::ONE, point)
28}
29
30/// Given evaluations of a batch of polynomials over the given coset of the canonical power-of-two
31/// subgroup, evaluate the polynomials at `point`.
32///
33/// This assumes the point is not in the coset, otherwise the behavior is undefined.
34///
35/// The `coset_evals` must be given in standard (not bit-reversed) order.
36pub fn interpolate_coset<F, EF, Mat>(coset_evals: &Mat, shift: F, point: EF) -> Vec<EF>
37where
38    F: TwoAdicField,
39    EF: ExtensionField<F>,
40    Mat: Matrix<F>,
41{
42    let height = coset_evals.height();
43    let log_height = log2_strict_usize(height);
44
45    let coset = TwoAdicMultiplicativeCoset::new(shift, log_height)
46        .unwrap()
47        .iter()
48        .collect();
49
50    // Compute `1/(z - gh^i)` for each element of the coset.
51    let diffs: Vec<_> = coset.par_iter().map(|&g| point - g).collect();
52    let diff_invs = batch_multiplicative_inverse(&diffs);
53
54    interpolate_coset_with_precomputation(coset_evals, shift, point, &coset, &diff_invs)
55}
56
57/// Given evaluations of a batch of polynomials over the given coset of the
58/// canonical power-of-two subgroup, evaluate the polynomials at `point`.
59///
60/// This assumes the point is not in the coset, otherwise the behavior is undefined.
61///
62/// This function takes the precomputed `subgroup` points and `diff_invs` (the
63/// inverses of the differences between the evaluation point and each shifted
64/// subgroup element), and should be preferred over `interpolate_coset` when
65/// repeatedly called with the same subgroup and/or point.
66///
67/// Unlike `interpolate_coset`, the parameters `subgroup`, `coset_evals`, and
68/// `diff_invs` may use any indexing scheme, as long as they are all consistent.
69pub fn interpolate_coset_with_precomputation<F, EF, Mat>(
70    coset_evals: &Mat,
71    shift: F,
72    point: EF,
73    coset: &[F],
74    diff_invs: &[EF],
75) -> Vec<EF>
76where
77    F: TwoAdicField,
78    EF: ExtensionField<F>,
79    Mat: Matrix<F>,
80{
81    // Slight variation of this approach: https://hackmd.io/@vbuterin/barycentric_evaluation
82    debug_assert_eq!(coset.len(), diff_invs.len());
83    debug_assert_eq!(coset.len(), coset_evals.height());
84
85    // We start with the evaluations of a polynomial `f` over a coset `gH` of size `N` and want to compute `f(z)`.
86    // Observe that `z^N - g^N` is equal to `0` at all points in the coset.
87    // Thus `(z^N - g^N)/(z - gh^i)` is equal to `0` at all points except for `gh^i` where it is equal to:
88    //          `N * (gh^i)^{N - 1} = N * g^N * (gh^i)^{-1}.`
89    //
90    // Hence `L_i(z) = h^i * (z^N - g^N)/(N * g^{N - 1} * (z - gh^i))` will be equal to `1` at `gh^i` and `0`
91    // at all other points in the coset. This means that we can compute `f(z)` as:
92    //          `\sum_i L_i(z) f(gh^i) = (z^N - g^N)/(N * g^N) * \sum_i gh^i/(z - gh^i) f(gh^i).`
93
94    // TODO: It might be possible to speed this up by refactoring the code to instead compute:
95    //          `((z/g)^N - 1)/N * \sum_i 1/(z/(gh^i) - 1) f(gh^i).`
96    // This would remove the need for the multiplications and collections in `col_scale` and
97    // let us remove one of the `exp_power_of_2` calls (which are somewhat expensive as they are over the
98    // extension field). We could also remove the .inverse() in scale_vec.
99
100    let height = coset_evals.height();
101    let log_height = log2_strict_usize(height);
102
103    // Compute `gh^i/(z - gh^i)` for each i.
104    let col_scale: Vec<_> = coset
105        .par_iter()
106        .zip(diff_invs)
107        .map(|(&sg, &diff_inv)| diff_inv * sg)
108        .collect();
109
110    let point_pow_height = point.exp_power_of_2(log_height);
111    let shift_pow_height = shift.exp_power_of_2(log_height);
112
113    // Compute the vanishing polynomial of the coset: `Z_{sH}(z) = z^N - g^N`.
114    let vanishing_polynomial = point_pow_height - shift_pow_height;
115
116    // Compute N * g^N
117    let denominator = shift_pow_height.mul_2exp_u64(log_height as u64);
118
119    // Scaling factor s = Z_{sH}(z)/(N * g^N)
120    let scaling_factor = vanishing_polynomial * denominator.inverse();
121
122    // For each column polynomial `f_j`, compute `\sum_i h^i/(gh^i - z) * f_j(gh^i)`,
123    // then scale by s.
124    let mut evals = coset_evals.columnwise_dot_product(&col_scale);
125    scale_slice_in_place_single_core(&mut evals, scaling_factor);
126    evals
127}
128
129#[cfg(test)]
130mod tests {
131    use alloc::vec;
132    use alloc::vec::Vec;
133
134    use p3_baby_bear::BabyBear;
135    use p3_field::extension::BinomialExtensionField;
136    use p3_field::{Field, PrimeCharacteristicRing, TwoAdicField, batch_multiplicative_inverse};
137    use p3_matrix::dense::RowMajorMatrix;
138    use p3_util::log2_strict_usize;
139
140    use crate::{interpolate_coset, interpolate_coset_with_precomputation, interpolate_subgroup};
141
142    #[test]
143    fn test_interpolate_subgroup() {
144        // x^2 + 2 x + 3
145        type F = BabyBear;
146        let evals = [
147            6, 886605102, 1443543107, 708307799, 2, 556938009, 569722818, 1874680944,
148        ]
149        .map(F::from_u32);
150        let evals_mat = RowMajorMatrix::new(evals.to_vec(), 1);
151        let point = F::from_u16(100);
152        let result = interpolate_subgroup(&evals_mat, point);
153        assert_eq!(result, vec![F::from_u16(10203)]);
154    }
155
156    #[test]
157    fn test_interpolate_coset() {
158        // x^2 + 2 x + 3
159        type F = BabyBear;
160        let shift = F::GENERATOR;
161        let evals = [
162            1026, 129027310, 457985035, 994890337, 902, 1988942953, 1555278970, 913671254,
163        ]
164        .map(F::from_u32);
165        let evals_mat = RowMajorMatrix::new(evals.to_vec(), 1);
166        let point = F::from_u16(100);
167        let result = interpolate_coset(&evals_mat, shift, point);
168        assert_eq!(result, vec![F::from_u16(10203)]);
169
170        let n = evals.len();
171        let k = log2_strict_usize(n);
172
173        let coset = F::two_adic_generator(k).shifted_powers(shift).collect_n(n);
174
175        let denom: Vec<_> = coset.iter().map(|&w| point - w).collect();
176
177        let denom = batch_multiplicative_inverse(&denom);
178        let result =
179            interpolate_coset_with_precomputation(&evals_mat, shift, point, &coset, &denom);
180        assert_eq!(result, vec![F::from_u16(10203)]);
181    }
182
183    #[test]
184    fn test_interpolate_coset_single_point_identity() {
185        type F = BabyBear;
186
187        // Test a trivial case: constant polynomial f(x) = c
188        // Regardless of x, f(x) = c, so interpolation must always return c
189        let c = F::from_u32(42); // constant polynomial
190        let evals = vec![c; 8];
191        let evals_mat = RowMajorMatrix::new(evals, 1);
192
193        let shift = F::GENERATOR;
194        let point = F::from_u16(1337);
195
196        let result = interpolate_coset(&evals_mat, shift, point);
197        assert_eq!(result, vec![c]); // must recover the constant
198    }
199
200    #[test]
201    fn test_interpolate_subgroup_degree_3_correctness() {
202        type F = BabyBear;
203        type EF4 = BinomialExtensionField<BabyBear, 4>;
204
205        // This test checks that interpolation works for a degree-3 polynomial
206        // when evaluated over 2^2 = 4 subgroup points, which is valid.
207        let poly = |x: EF4| x * x * x + x * x * F::TWO + x * F::from_u32(3) + F::from_u32(4);
208
209        let subgroup = EF4::two_adic_generator(2).powers().collect_n(4);
210        let evals: Vec<_> = subgroup.iter().map(|&x| poly(x)).collect();
211
212        let evals_mat = RowMajorMatrix::new(evals, 1);
213        let point = EF4::from_u16(5);
214
215        let result = interpolate_subgroup(&evals_mat, point);
216        let expected = poly(point);
217
218        assert_eq!(result[0], expected);
219    }
220
221    #[test]
222    fn test_interpolate_coset_multiple_polynomials() {
223        type F = BabyBear;
224        type EF4 = BinomialExtensionField<BabyBear, 4>;
225
226        // We test interpolation of two polynomials evaluated over a coset.
227        // f1(x) = x^2 + 2x + 3
228        // f2(x) = 4x^2 + 5x + 6
229        //
230        // Each is evaluated at the coset and interpolated at the same external point.
231        let shift = EF4::GENERATOR;
232        let coset = EF4::two_adic_generator(3)
233            .shifted_powers(shift)
234            .collect_n(8);
235
236        let f1 = |x: EF4| x * x + x * F::TWO + F::from_u32(3);
237        let f2 = |x: EF4| x * x * F::from_u32(4) + x * F::from_u32(5) + F::from_u32(6);
238
239        let evals: Vec<_> = coset.iter().flat_map(|&x| vec![f1(x), f2(x)]).collect();
240        let evals_mat = RowMajorMatrix::new(evals, 2);
241
242        let point = EF4::from_u32(77);
243        let result = interpolate_coset(&evals_mat, shift, point);
244
245        // Evaluate f1 and f2 at the same point directly
246        let expected_f1 = f1(point);
247        let expected_f2 = f2(point);
248
249        assert_eq!(result[0], expected_f1);
250        assert_eq!(result[1], expected_f2);
251    }
252
253    #[test]
254    fn test_interpolate_subgroup_multiple_columns() {
255        type F = BabyBear;
256        type EF4 = BinomialExtensionField<BabyBear, 4>;
257
258        // Define two polynomials f1(x) = x^2 + 2x + 3 and f2(x) = 4x^2 + 5x + 6
259        let f1 = |x: EF4| x * x + x * F::TWO + F::from_u32(3);
260        let f2 = |x: EF4| x * x * F::from_u32(4) + x * F::from_u32(5) + F::from_u32(6);
261
262        // Evaluation domain: 2^3 = 8-point subgroup
263        let subgroup_iter = EF4::two_adic_generator(3).powers().take(8);
264
265        // Evaluate both polynomials on the subgroup
266        let evals: Vec<_> = subgroup_iter.flat_map(|x| vec![f1(x), f2(x)]).collect();
267
268        // Organize into a 2-column matrix (column-major: 8 rows × 2 columns)
269        let evals_mat = RowMajorMatrix::new(evals, 2);
270
271        // Choose a point outside the subgroup to interpolate at
272        let point = EF4::from_u32(77);
273
274        // Perform interpolation
275        let result = interpolate_subgroup(&evals_mat, point);
276
277        // Expected results: f1(point), f2(point)
278        let expected_f1 = f1(point);
279        let expected_f2 = f2(point);
280
281        assert_eq!(result, vec![expected_f1, expected_f2]);
282    }
283}