ark_r1cs_std/poly/polynomial/univariate/
dense.rs

1use ark_ff::PrimeField;
2use ark_relations::r1cs::SynthesisError;
3
4use crate::fields::{fp::FpVar, FieldVar};
5use ark_std::vec::Vec;
6
7/// Stores a polynomial in coefficient form, where coeffcient is represented by
8/// a list of `Fpvar<F>`.
9pub struct DensePolynomialVar<F: PrimeField> {
10    /// The coefficient of `x^i` is stored at location `i` in `self.coeffs`.
11    pub coeffs: Vec<FpVar<F>>,
12}
13
14impl<F: PrimeField> DensePolynomialVar<F> {
15    /// Constructs a new polynomial from a list of coefficients.
16    pub fn from_coefficients_slice(coeffs: &[FpVar<F>]) -> Self {
17        Self::from_coefficients_vec(coeffs.to_vec())
18    }
19
20    /// Constructs a new polynomial from a list of coefficients.
21    pub fn from_coefficients_vec(coeffs: Vec<FpVar<F>>) -> Self {
22        Self { coeffs }
23    }
24
25    /// Evaluates `self` at the given `point` and just gives you the gadget for
26    /// the result. Caution for use in holographic lincheck: The output has
27    /// 2 entries in one matrix
28    pub fn evaluate(&self, point: &FpVar<F>) -> Result<FpVar<F>, SynthesisError> {
29        let mut result: FpVar<F> = FpVar::zero();
30        // current power of point
31        let mut curr_pow_x: FpVar<F> = FpVar::one();
32        for i in 0..self.coeffs.len() {
33            let term = &curr_pow_x * &self.coeffs[i];
34            result += &term;
35            curr_pow_x *= point;
36        }
37
38        Ok(result)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use crate::{
45        alloc::AllocVar, fields::fp::FpVar,
46        poly::polynomial::univariate::dense::DensePolynomialVar, R1CSVar,
47    };
48    use ark_poly::{polynomial::univariate::DensePolynomial, DenseUVPolynomial, Polynomial};
49    use ark_relations::r1cs::ConstraintSystem;
50    use ark_std::{test_rng, vec::Vec, UniformRand};
51    use ark_test_curves::bls12_381::Fr;
52
53    #[test]
54    fn test_evaluate() {
55        let mut rng = test_rng();
56        for _ in 0..100 {
57            let cs = ConstraintSystem::new_ref();
58            let poly: DensePolynomial<Fr> = DensePolynomial::rand(10, &mut rng);
59            let poly_var = {
60                let coeff: Vec<_> = poly
61                    .coeffs
62                    .iter()
63                    .map(|&x| FpVar::new_witness(ns!(cs, "coeff"), || Ok(x)).unwrap())
64                    .collect();
65                DensePolynomialVar::from_coefficients_vec(coeff)
66            };
67            let point = Fr::rand(&mut rng);
68            let point_var = FpVar::new_witness(ns!(cs, "point"), || Ok(point)).unwrap();
69
70            let expected = poly.evaluate(&point);
71            let actual = poly_var.evaluate(&point_var).unwrap();
72
73            assert_eq!(actual.value().unwrap(), expected);
74            assert!(cs.is_satisfied().unwrap());
75        }
76    }
77}