From 614e8b9967af313c54ebaf1d8190458dc142c5bd Mon Sep 17 00:00:00 2001 From: cyphersnake Date: Tue, 29 Oct 2024 21:21:11 +0100 Subject: [PATCH] feat(ivc): impl eval_vanishing_poly **Motivation** Part of #361 **Overview** N/A --- src/ivc/protogalaxy/mod.rs | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/ivc/protogalaxy/mod.rs b/src/ivc/protogalaxy/mod.rs index 160cba00..cd6762e4 100644 --- a/src/ivc/protogalaxy/mod.rs +++ b/src/ivc/protogalaxy/mod.rs @@ -576,6 +576,23 @@ mod verify_chip { main_gate.conditional_select(region, &one, &fractional, &is_numerator_denominator_zero) } + /// This fn calculates vanishing polynomial $Z(X)$ from the formula $G(X)=F(\alpha)L_0(X)+K(X)Z(X)$ + /// # Parameters + /// - `log_n` - logarithm of polynomial degree + /// - `point` - `x` - eval Lagrange polynomials at this point + /// # Result - x^n - 1 + /// X^{2^log_n} - 1 + /// -1 * X^0 + 0 * X^1 + ... + a * X^{2^log_n} + pub fn eval_vanish_polynomial( + region: &mut RegionCtx, + main_gate: &MainGate, + degree: usize, + cha: &mut ValuePowers, + ) -> Result, Halo2PlonkError> { + let cha_in_degree = cha.get_or_eval(region, main_gate, degree)?; + main_gate.add_with_const(region, &cha_in_degree, -F::ONE) + } + /// Assigned version of `fn verify` logic from [`crate::nifs::protogalaxy::ProtoGalaxy`]. /// /// # Algorithm @@ -1034,5 +1051,51 @@ mod verify_chip { .verify() .unwrap(); } + + #[traced_test] + #[test] + fn vanishing() { + const DEGREE: usize = 10; + let cha = Base::from_u128(123); + + let off_circuit_vanishing = polynomial::lagrange::eval_vanish_polynomial(DEGREE, cha); + + let (mut wc, main_gate_config) = get_witness_collector(); + + let mut layouter = SingleChipLayouter::new(&mut wc, vec![]).unwrap(); + + let on_circuit_vanishing = layouter + .assign_region( + || "vanishing", + move |region| { + let mut region = RegionCtx::new(region, 0); + let main_gate = MainGate::::new(main_gate_config.clone()); + + let cha = region + .assign_advice(|| "", main_gate_config.state[0], Halo2Value::known(cha)) + .unwrap(); + + let one = region + .assign_advice( + || "", + main_gate_config.state[1], + Halo2Value::known(Base::ONE), + ) + .unwrap(); + + region.next(); + + let mut cha = ValuePowers::new(one, cha); + + eval_vanish_polynomial(&mut region, &main_gate, DEGREE, &mut cha) + }, + ) + .unwrap(); + + assert_eq!( + off_circuit_vanishing, + on_circuit_vanishing.value().unwrap().copied().unwrap() + ); + } } }