plonky2/field/src/interpolation.rs

146 lines
4.2 KiB
Rust
Raw Normal View History

use plonky2_util::log2_ceil;
use crate::fft::ifft;
2021-11-30 20:17:34 +01:00
use crate::polynomial::{PolynomialCoeffs, PolynomialValues};
use crate::types::Field;
2021-04-24 20:11:00 -07:00
/// Computes the unique degree < n interpolant of an arbitrary list of n (point, value) pairs.
///
/// Note that the implementation assumes that `F` is two-adic, in particular that
/// `2^{F::TWO_ADICITY} >= points.len()`. This leads to a simple FFT-based implementation.
pub fn interpolant<F: Field>(points: &[(F, F)]) -> PolynomialCoeffs<F> {
let n = points.len();
let n_log = log2_ceil(n);
2021-06-16 17:43:41 +02:00
let subgroup = F::two_adic_subgroup(n_log);
2021-04-24 20:11:00 -07:00
let barycentric_weights = barycentric_weights(points);
let subgroup_evals = subgroup
.into_iter()
2021-04-24 20:11:00 -07:00
.map(|x| interpolate(points, x, &barycentric_weights))
.collect();
let mut coeffs = ifft(PolynomialValues::new(subgroup_evals));
coeffs.trim();
coeffs
}
/// Interpolate the polynomial defined by an arbitrary set of (point, value) pairs at the given
/// point `x`.
2021-04-26 18:24:57 +02:00
pub fn interpolate<F: Field>(points: &[(F, F)], x: F, barycentric_weights: &[F]) -> F {
2021-04-24 20:11:00 -07:00
// If x is in the list of points, the Lagrange formula would divide by zero.
for &(x_i, y_i) in points {
if x_i == x {
return y_i;
}
}
2021-04-26 18:24:57 +02:00
let l_x: F = points.iter().map(|&(x_i, _y_i)| x - x_i).product();
2021-04-24 20:11:00 -07:00
let sum = (0..points.len())
.map(|i| {
2021-04-24 20:11:00 -07:00
let x_i = points[i].0;
let y_i = points[i].1;
2021-04-24 20:11:00 -07:00
let w_i = barycentric_weights[i];
w_i / (x - x_i) * y_i
})
2021-04-24 20:11:00 -07:00
.sum();
l_x * sum
}
2021-04-26 18:24:57 +02:00
pub fn barycentric_weights<F: Field>(points: &[(F, F)]) -> Vec<F> {
let n = points.len();
F::batch_multiplicative_inverse(
&(0..n)
.map(|i| {
(0..n)
.filter(|&j| j != i)
.map(|j| points[i].0 - points[j].0)
.product::<F>()
})
.collect::<Vec<_>>(),
)
}
2021-06-18 11:10:33 +02:00
/// Interpolate the linear polynomial passing through `points` on `x`.
pub fn interpolate2<F: Field>(points: [(F, F); 2], x: F) -> F {
// a0 -> a1
// b0 -> b1
// x -> a1 + (x-a0)*(b1-a1)/(b0-a0)
let (a0, a1) = points[0];
let (b0, b1) = points[1];
assert_ne!(a0, b0);
a1 + (x - a0) * (b1 - a1) / (b0 - a0)
}
#[cfg(test)]
mod tests {
2021-06-18 11:10:33 +02:00
use super::*;
use crate::extension::quartic::QuarticExtension;
use crate::goldilocks_field::GoldilocksField;
2021-11-30 20:17:34 +01:00
use crate::polynomial::PolynomialCoeffs;
use crate::types::Field;
#[test]
fn interpolant_random() {
2021-11-02 12:04:42 -07:00
type F = GoldilocksField;
for deg in 0..10 {
2021-05-06 15:14:43 +02:00
let domain = F::rand_vec(deg);
let coeffs = F::rand_vec(deg);
let coeffs = PolynomialCoeffs { coeffs };
2021-04-24 20:11:00 -07:00
let points = eval_naive(&coeffs, &domain);
assert_eq!(interpolant(&points), coeffs);
}
}
#[test]
fn interpolant_random_roots_of_unity() {
2021-11-02 12:04:42 -07:00
type F = GoldilocksField;
2021-04-24 20:11:00 -07:00
for deg_log in 0..4 {
let deg = 1 << deg_log;
2021-06-16 17:43:41 +02:00
let domain = F::two_adic_subgroup(deg_log);
2021-05-06 15:14:43 +02:00
let coeffs = F::rand_vec(deg);
2021-04-24 20:11:00 -07:00
let coeffs = PolynomialCoeffs { coeffs };
let points = eval_naive(&coeffs, &domain);
assert_eq!(interpolant(&points), coeffs);
}
}
#[test]
fn interpolant_random_overspecified() {
2021-11-02 12:04:42 -07:00
type F = GoldilocksField;
for deg in 0..10 {
let points = deg + 5;
2021-05-06 15:14:43 +02:00
let domain = F::rand_vec(points);
let coeffs = F::rand_vec(deg);
let coeffs = PolynomialCoeffs { coeffs };
let points = eval_naive(&coeffs, &domain);
assert_eq!(interpolant(&points), coeffs);
}
}
fn eval_naive<F: Field>(coeffs: &PolynomialCoeffs<F>, domain: &[F]) -> Vec<(F, F)> {
domain.iter().map(|&x| (x, coeffs.eval(x))).collect()
}
2021-06-18 11:10:33 +02:00
#[test]
fn test_interpolate2() {
2021-11-02 12:04:42 -07:00
type F = QuarticExtension<GoldilocksField>;
2021-06-18 11:10:33 +02:00
let points = [(F::rand(), F::rand()), (F::rand(), F::rand())];
let x = F::rand();
let ev0 = interpolant(&points).eval(x);
let ev1 = interpolate(&points, x, &barycentric_weights(&points));
let ev2 = interpolate2(points, x);
2021-06-18 11:10:33 +02:00
assert_eq!(ev0, ev1);
assert_eq!(ev0, ev2);
2021-06-18 11:10:33 +02:00
}
}