miden-crypto/src/gkr/utils/mod.rs
src/gkr/utils/mod.rsRaw
1use winter_math::{FieldElement, batch_inversion};
2
3
4pub fn barycentric_weights<E: FieldElement>(points: &[(E, E)]) -> Vec<E> {
5 let n = points.len();
6 let tmp = (0..n)
7 .map(|i| (0..n).filter(|&j| j != i).fold(E::ONE, |acc, j| acc * (points[i].0 - points[j].0)))
8 .collect::<Vec<_>>();
9 batch_inversion(&tmp)
10}
11
12pub fn evaluate_barycentric<E: FieldElement>(
13 points: &[(E, E)],
14 x: E,
15 barycentric_weights: &[E],
16) -> E {
17 for &(x_i, y_i) in points {
18 if x_i == x {
19 return y_i;
20 }
21 }
22
23 let l_x: E = points.iter().fold(E::ONE, |acc, &(x_i, _y_i)| acc * (x - x_i));
24
25 let sum = (0..points.len()).fold(E::ZERO, |acc, i| {
26 let x_i = points[i].0;
27 let y_i = points[i].1;
28 let w_i = barycentric_weights[i];
29 acc + (w_i / (x - x_i) * y_i)
30 });
31
32 l_x * sum
33}