Skip to content

Commit

Permalink
[poly] Reduce interpolated polynomial
Browse files Browse the repository at this point in the history
So it doesn't start with zero points.
  • Loading branch information
LLFourn committed Jul 26, 2024
1 parent 0e4ba72 commit b331864
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
6 changes: 6 additions & 0 deletions secp256kfun/src/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ pub mod point {
self::add_in_place(&mut interpolating_polynomial, point_scaled_basis_poly);
}

while interpolating_polynomial.len() > 1
&& interpolating_polynomial.last().unwrap().is_zero()
{
interpolating_polynomial.pop();
}

interpolating_polynomial
}

Expand Down
32 changes: 15 additions & 17 deletions secp256kfun/tests/poly.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(feature = "alloc")]
use secp256kfun::{g, marker::*, poly, s, Point, G};
use secp256kfun::{poly, prelude::*};

#[test]
fn test_lagrange_lambda() {
Expand Down Expand Up @@ -90,28 +90,26 @@ fn test_recover_overdetermined_poly() {
let points = indicies
.clone()
.into_iter()
.map(|index| {
(
index,
poly::point::eval(&poly, index.public())
.normalize()
.non_zero()
.unwrap(),
)
})
.map(|index| (index, poly::point::eval(&poly, index.public()).normalize()))
.collect::<Vec<_>>();

let interpolation = poly::point::interpolate(&points);

let (interpolated_coeffs, zero_coeffs) = interpolation.split_at(poly.len());
let n_extra_points = indicies.len() - poly.len();
assert_eq!(interpolation, poly);
}

#[test]
fn test_recover_zero_poly() {
let interpolation = poly::point::interpolate(&[
(s!(1).public(), Point::<Normal, Public, _>::zero()),
(s!(2).public(), Point::<Normal, Public, _>::zero()),
]);

assert_eq!(
(0..n_extra_points)
.map(|_| Point::<Normal, Public, Zero>::zero().public().normalize())
.collect::<Vec<_>>(),
zero_coeffs.to_vec()
interpolation,
vec![Point::<NonNormal, Public, _>::zero()],
"should not be empty vector"
);
assert_eq!(interpolated_coeffs, poly);
}

#[test]
Expand Down

0 comments on commit b331864

Please sign in to comment.