1use binius_field::field::FieldOps;
7
8#[inline]
20pub fn extrapolate_line<F: FieldOps>(x0: F, x1: F, z: F) -> F {
21 x0.clone() + (x1 - x0) * z
23}
24
25#[cfg(test)]
26mod tests {
27 use binius_field::{Field, Random, field::FieldOps};
28 use rand::prelude::*;
29
30 use super::*;
31 use crate::test_utils::{B128, Packed128b};
32
33 type P = Packed128b;
34 type F = B128;
35
36 #[test]
37 fn extrapolate_line_reads_the_endpoints_at_zero_and_one() {
38 let mut rng = StdRng::seed_from_u64(0);
39
40 let x0 = F::random(&mut rng);
45 let x1 = F::random(&mut rng);
46 assert_eq!(extrapolate_line(x0, x1, F::ZERO), x0);
47 assert_eq!(extrapolate_line(x0, x1, F::ONE), x1);
48
49 let x0_packed = P::random(&mut rng);
51 let x1_packed = P::random(&mut rng);
52 assert_eq!(extrapolate_line(x0_packed, x1_packed, P::zero()), x0_packed);
53 assert_eq!(extrapolate_line(x0_packed, x1_packed, P::one()), x1_packed);
54 }
55
56 #[test]
57 fn extrapolate_line_matches_the_closed_form() {
58 let mut rng = StdRng::seed_from_u64(0);
59
60 for _ in 0..10 {
63 let x0 = F::random(&mut rng);
64 let x1 = F::random(&mut rng);
65 let z = F::from(rng.next_u64() as u128);
66 assert_eq!(extrapolate_line(x0, x1, z), x0 + (x1 - x0) * z);
67 }
68 }
69}