From d727f84a56f373fdb09b5ab07b1c1378da1efb78 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Tue, 8 Jun 2021 11:18:21 +0200 Subject: [PATCH] Working test --- src/field/extension_field/algebra.rs | 4 +- src/gates/interpolation.rs | 68 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/field/extension_field/algebra.rs b/src/field/extension_field/algebra.rs index db80ce75..7a4f43a3 100644 --- a/src/field/extension_field/algebra.rs +++ b/src/field/extension_field/algebra.rs @@ -38,7 +38,7 @@ impl, const D: usize> Display for ExtensionAlgebra { for i in 1..D - 1 { write!(f, "({})*b^{} + ", self.0[i], i)?; } - write!(f, "{}*b^{}", self.0[D - 1], D - 1) + write!(f, "({})*b^{}", self.0[D - 1], D - 1) } } @@ -112,7 +112,7 @@ impl, const D: usize> Mul for ExtensionAlgebra { res[(i + j) % D] += if i + j < D { self.0[i] * rhs.0[j] } else { - w * self.0[i] * rhs.0[i] + w * self.0[i] * rhs.0[j] } } } diff --git a/src/gates/interpolation.rs b/src/gates/interpolation.rs index 3872b9ee..804b9310 100644 --- a/src/gates/interpolation.rs +++ b/src/gates/interpolation.rs @@ -282,9 +282,14 @@ mod tests { use std::marker::PhantomData; use crate::field::crandall_field::CrandallField; + use crate::field::extension_field::quartic::QuarticCrandallField; + use crate::field::extension_field::FieldExtension; + use crate::field::field::Field; use crate::gates::gate::Gate; use crate::gates::gate_testing::test_low_degree; use crate::gates::interpolation::InterpolationGate; + use crate::polynomial::polynomial::PolynomialCoeffs; + use crate::vars::EvaluationVars; #[test] fn wire_indices() { @@ -311,4 +316,67 @@ mod tests { type F = CrandallField; test_low_degree(InterpolationGate::::new(4)); } + + #[test] + fn test_gate_constraint() { + type F = CrandallField; + type FF = QuarticCrandallField; + const D: usize = 4; + + /// Returns the local wires for an interpolation gate for given coeffs, points and eval point. + fn get_wires( + num_points: usize, + coeffs: PolynomialCoeffs, + points: Vec, + eval_point: FF, + ) -> Vec { + let mut v = vec![F::ZERO; num_points * 5 + (coeffs.len() + 3) * D]; + for j in 0..num_points { + v[j] = points[j]; + } + for j in 0..num_points { + for i in 0..D { + v[num_points + D * j + i] = >::to_basefield_array( + &coeffs.eval(points[j].into()), + )[i]; + } + } + for i in 0..D { + v[num_points * 5 + i] = + >::to_basefield_array(&eval_point)[i]; + } + for i in 0..D { + v[num_points * 5 + D + i] = + >::to_basefield_array(&coeffs.eval(eval_point))[i]; + } + for i in 0..coeffs.len() { + for (j, input) in + (0..D).zip(num_points * 5 + (2 + i) * D..num_points * 5 + (3 + i) * D) + { + v[input] = >::to_basefield_array(&coeffs.coeffs[i])[j]; + } + } + v.iter().map(|&x| x.into()).collect::>() + } + + /// Get a working row for InterpolationGate. + let coeffs = PolynomialCoeffs::new(vec![FF::rand(), FF::rand()]); + let points = vec![F::rand(), F::rand()]; + let eval_point = FF::rand(); + let gate = InterpolationGate:: { + num_points: 2, + _phantom: PhantomData, + }; + let vars = EvaluationVars { + local_constants: &[], + local_wires: &get_wires(2, coeffs, points, eval_point), + }; + + assert!( + gate.eval_unfiltered(vars.clone()) + .iter() + .all(|x| x.is_zero()), + "Gate constraints are not satisfied." + ); + } }