From 38372a4bf859d607dd89b1f395b91f69477091c9 Mon Sep 17 00:00:00 2001 From: winderica Date: Thu, 3 Oct 2024 21:06:45 +0800 Subject: [PATCH 1/2] Fix panic issues during in-circuit polynomial interpolation --- src/poly/evaluations/univariate/mod.rs | 33 ++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/poly/evaluations/univariate/mod.rs b/src/poly/evaluations/univariate/mod.rs index b6461462..7856b79a 100644 --- a/src/poly/evaluations/univariate/mod.rs +++ b/src/poly/evaluations/univariate/mod.rs @@ -64,8 +64,11 @@ impl EvaluationsVar { /// ready to interpolate pub fn generate_interpolation_cache(&mut self) { if self.domain.offset().is_constant() { - let poly_evaluations_val: Vec<_> = - self.evals.iter().map(|v| v.value().unwrap()).collect(); + let poly_evaluations_val: Vec<_> = self + .evals + .iter() + .map(|v| v.value().unwrap_or_default()) + .collect(); let domain = &self.domain; let lagrange_interpolator = if let &FpVar::Constant(x) = domain.offset() { LagrangeInterpolator::new(x, domain.gen, domain.dim, poly_evaluations_val) @@ -100,7 +103,7 @@ impl EvaluationsVar { .expect("lagrange interpolator has not been initialized. \ Call `self.generate_interpolation_cache` first or set `interpolate` to true in constructor. "); let lagrange_coeffs = - lagrange_interpolator.compute_lagrange_coefficients(t.value().unwrap()); + lagrange_interpolator.compute_lagrange_coefficients(t.value().unwrap_or_default()); let mut lagrange_coeffs_fg = Vec::new(); // Now we convert these lagrange coefficients to gadgets, and then constrain // them. The i-th lagrange coefficients constraint is: @@ -119,13 +122,15 @@ impl EvaluationsVar { Ok(lagrange_coeffs[i]) })?; // Enforce the actual constraint (A_element) * (lagrange_coeff) = 1/Z_I(t) - assert_eq!( - (lagrange_interpolator.v_inv_elems[i] * t.value().unwrap() - - lagrange_interpolator.v_inv_elems[i] - * lagrange_interpolator.all_domain_elems[i]) - * lagrange_coeffs[i], - vp_t.value().unwrap() - ); + if !cs.is_in_setup_mode() { + assert_eq!( + (lagrange_interpolator.v_inv_elems[i] * t.value().unwrap_or_default() + - lagrange_interpolator.v_inv_elems[i] + * lagrange_interpolator.all_domain_elems[i]) + * lagrange_coeffs[i], + vp_t.value().unwrap_or_default() + ); + } a_element.mul_equals(&lag_coeff, &vp_t)?; lagrange_coeffs_fg.push(lag_coeff); } @@ -344,12 +349,16 @@ impl<'a, F: PrimeField> DivAssign<&'a EvaluationsVar> for EvaluationsVar { ); let cs = self.evals[0].cs(); // the prover can generate result = (1 / other) * self offline - let mut result_val: Vec<_> = other.evals.iter().map(|x| x.value().unwrap()).collect(); + let mut result_val: Vec<_> = other + .evals + .iter() + .map(|x| x.value().unwrap_or_default()) + .collect(); batch_inversion(&mut result_val); result_val .iter_mut() .zip(&self.evals) - .for_each(|(a, self_var)| *a *= self_var.value().unwrap()); + .for_each(|(a, self_var)| *a *= self_var.value().unwrap_or_default()); let result_var: Vec<_> = result_val .iter() .map(|x| FpVar::new_witness(ns!(cs, "div result"), || Ok(*x)).unwrap()) From 53e7ad1b8d80edc516d2ce4df0c7d2b12fa0279d Mon Sep 17 00:00:00 2001 From: winderica Date: Thu, 3 Oct 2024 21:19:00 +0800 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9967aa43..e1a5e571 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,8 @@ - [\#145](https://github.com/arkworks-rs/r1cs-std/pull/145) - Avoid deeply nested `LinearCombinations` in `EvaluationsVar::interpolate_and_evaluate` to fix the stack overflow issue when calling `.value()` on the evaluation result. +- [\#148](https://github.com/arkworks-rs/r1cs-std/pull/148) + - Fix panic issues during in-circuit polynomial interpolation. ## 0.4.0