arnaucube
arnaucube/sonobePublic Self-hosted repository
sonobe/folding-schemes/src/folding/nova/decider_circuits.rs
folding-schemes/src/folding/nova/decider_circuits.rsRaw
  1/// This file implements the offchain decider circuit. For ethereum use cases, use the
  2/// DeciderEthCircuit.
  3/// More details can be found at the documentation page:
  4/// https://privacy-scaling-explorations.github.io/sonobe-docs/design/nova-decider-offchain.html
  5use ark_crypto_primitives::sponge::{
  6    constraints::CryptographicSpongeVar,
  7    poseidon::{constraints::PoseidonSpongeVar, PoseidonConfig, PoseidonSponge},
  8    Absorb, CryptographicSponge,
  9};
 10use ark_ec::{CurveGroup, Group};
 11use ark_ff::{BigInteger, PrimeField};
 12use ark_poly::Polynomial;
 13use ark_r1cs_std::{
 14    alloc::AllocVar,
 15    boolean::Boolean,
 16    eq::EqGadget,
 17    fields::{fp::FpVar, FieldVar},
 18    groups::GroupOpsBounds,
 19    prelude::CurveVar,
 20    ToConstraintFieldGadget,
 21};
 22use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystemRef, SynthesisError};
 23use ark_std::Zero;
 24use core::marker::PhantomData;
 25
 26use super::{
 27    circuits::{ChallengeGadget, CommittedInstanceVar},
 28    decider_eth_circuit::{KZGChallengesGadget, R1CSVar, RelaxedR1CSGadget, WitnessVar},
 29    nifs::NIFS,
 30    CommittedInstance, Nova, Witness,
 31};
 32use crate::arith::r1cs::R1CS;
 33use crate::commitment::CommitmentScheme;
 34use crate::folding::circuits::{
 35    cyclefold::{
 36        CycleFoldCommittedInstance, CycleFoldCommittedInstanceVar, CycleFoldConfig,
 37        CycleFoldWitness,
 38    },
 39    nonnative::{affine::NonNativeAffineVar, uint::NonNativeUintVar},
 40    CF1, CF2,
 41};
 42use crate::folding::nova::NovaCycleFoldConfig;
 43use crate::folding::traits::{CommittedInstanceVarOps, Dummy};
 44use crate::frontend::FCircuit;
 45use crate::utils::vec::poly_from_vec;
 46use crate::Error;
 47
 48/// Circuit that implements part of the in-circuit checks needed for the offchain verification over
 49/// the Curve2's BaseField (=Curve1's ScalarField).
 50#[derive(Clone, Debug)]
 51pub struct DeciderCircuit1<C1, C2, GC2>
 52where
 53    C1: CurveGroup,
 54    C2: CurveGroup,
 55    GC2: CurveVar<C2, CF2<C2>>,
 56{
 57    _c1: PhantomData<C1>,
 58    _c2: PhantomData<C2>,
 59    _gc2: PhantomData<GC2>,
 60
 61    /// E vector's length of the Nova instance witness
 62    pub E_len: usize,
 63    /// E vector's length of the CycleFold instance witness
 64    pub cf_E_len: usize,
 65    /// R1CS of the Augmented Function circuit
 66    pub r1cs: R1CS<C1::ScalarField>,
 67    pub poseidon_config: PoseidonConfig<CF1<C1>>,
 68    /// public params hash
 69    pub pp_hash: Option<C1::ScalarField>,
 70    pub i: Option<CF1<C1>>,
 71    /// initial state
 72    pub z_0: Option<Vec<C1::ScalarField>>,
 73    /// current i-th state
 74    pub z_i: Option<Vec<C1::ScalarField>>,
 75    /// Nova instances
 76    pub u_i: Option<CommittedInstance<C1>>,
 77    pub w_i: Option<Witness<C1>>,
 78    pub U_i: Option<CommittedInstance<C1>>,
 79    pub W_i: Option<Witness<C1>>,
 80    pub U_i1: Option<CommittedInstance<C1>>,
 81    pub W_i1: Option<Witness<C1>>,
 82    pub cmT: Option<C1>,
 83    pub r: Option<C1::ScalarField>,
 84    /// CycleFold running instance
 85    pub cf_U_i: Option<CycleFoldCommittedInstance<C2>>,
 86
 87    /// KZG challenges
 88    pub kzg_c_W: Option<C1::ScalarField>,
 89    pub kzg_c_E: Option<C1::ScalarField>,
 90    pub eval_W: Option<C1::ScalarField>,
 91    pub eval_E: Option<C1::ScalarField>,
 92}
 93impl<C1, C2, GC2> DeciderCircuit1<C1, C2, GC2>
 94where
 95    C1: CurveGroup,
 96    <C1 as CurveGroup>::BaseField: PrimeField,
 97    <C1 as Group>::ScalarField: Absorb,
 98    C2: CurveGroup,
 99    GC2: CurveVar<C2, CF2<C2>> + ToConstraintFieldGadget<CF2<C2>>,
100    for<'b> &'b GC2: GroupOpsBounds<'b, C2, GC2>,
101{
102    pub fn from_nova<GC1, CS1, CS2, const H: bool, FC: FCircuit<C1::ScalarField>>(
103        nova: Nova<C1, GC1, C2, GC2, FC, CS1, CS2, H>,
104    ) -> Result<Self, Error>
105    where
106        C2: CurveGroup,
107        GC1: CurveVar<C1, CF2<C1>> + ToConstraintFieldGadget<CF2<C1>>,
108        GC2: CurveVar<C2, CF2<C2>> + ToConstraintFieldGadget<CF2<C2>>,
109        CS1: CommitmentScheme<C1, H>,
110        CS2: CommitmentScheme<C2, H>,
111    {
112        let mut transcript = PoseidonSponge::<C1::ScalarField>::new(&nova.poseidon_config);
113        // pp_hash is absorbed to transcript at the ChallengeGadget::get_challenge_native call
114
115        // compute the U_{i+1}, W_{i+1}
116        let (T, cmT) = NIFS::<C1, CS1, H>::compute_cmT(
117            &nova.cs_pp,
118            &nova.r1cs.clone(),
119            &nova.w_i.clone(),
120            &nova.u_i.clone(),
121            &nova.W_i.clone(),
122            &nova.U_i.clone(),
123        )?;
124        let r_bits = ChallengeGadget::<C1>::get_challenge_native(
125            &mut transcript,
126            nova.pp_hash,
127            nova.U_i.clone(),
128            nova.u_i.clone(),
129            cmT,
130        );
131        let r_Fr = C1::ScalarField::from_bigint(BigInteger::from_bits_le(&r_bits))
132            .ok_or(Error::OutOfBounds)?;
133        let (W_i1, U_i1) = NIFS::<C1, CS1, H>::fold_instances(
134            r_Fr, &nova.W_i, &nova.U_i, &nova.w_i, &nova.u_i, &T, cmT,
135        )?;
136
137        // compute the KZG challenges used as inputs in the circuit
138        let (kzg_challenge_W, kzg_challenge_E) =
139            KZGChallengesGadget::<C1>::get_challenges_native(&mut transcript, U_i1.clone());
140
141        // get KZG evals
142        let mut W = W_i1.W.clone();
143        W.extend(
144            std::iter::repeat(C1::ScalarField::zero())
145                .take(W_i1.W.len().next_power_of_two() - W_i1.W.len()),
146        );
147        let mut E = W_i1.E.clone();
148        E.extend(
149            std::iter::repeat(C1::ScalarField::zero())
150                .take(W_i1.E.len().next_power_of_two() - W_i1.E.len()),
151        );
152        let p_W = poly_from_vec(W.to_vec())?;
153        let eval_W = p_W.evaluate(&kzg_challenge_W);
154        let p_E = poly_from_vec(E.to_vec())?;
155        let eval_E = p_E.evaluate(&kzg_challenge_E);
156
157        Ok(Self {
158            _c1: PhantomData,
159            _c2: PhantomData,
160            _gc2: PhantomData,
161
162            E_len: nova.W_i.E.len(),
163            cf_E_len: nova.cf_W_i.E.len(),
164            r1cs: nova.r1cs,
165            poseidon_config: nova.poseidon_config,
166            pp_hash: Some(nova.pp_hash),
167            i: Some(nova.i),
168            z_0: Some(nova.z_0),
169            z_i: Some(nova.z_i),
170            u_i: Some(nova.u_i),
171            w_i: Some(nova.w_i),
172            U_i: Some(nova.U_i),
173            W_i: Some(nova.W_i),
174            U_i1: Some(U_i1),
175            W_i1: Some(W_i1),
176            cmT: Some(cmT),
177            r: Some(r_Fr),
178            cf_U_i: Some(nova.cf_U_i),
179            kzg_c_W: Some(kzg_challenge_W),
180            kzg_c_E: Some(kzg_challenge_E),
181            eval_W: Some(eval_W),
182            eval_E: Some(eval_E),
183        })
184    }
185}
186
187impl<C1, C2, GC2> ConstraintSynthesizer<CF1<C1>> for DeciderCircuit1<C1, C2, GC2>
188where
189    C1: CurveGroup,
190    <C1 as CurveGroup>::BaseField: PrimeField,
191    <C1 as Group>::ScalarField: Absorb,
192    C2: CurveGroup,
193    <C2 as CurveGroup>::BaseField: PrimeField,
194    <C2 as Group>::ScalarField: Absorb,
195    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
196    GC2: CurveVar<C2, CF2<C2>> + ToConstraintFieldGadget<CF2<C2>>,
197    for<'b> &'b GC2: GroupOpsBounds<'b, C2, GC2>,
198{
199    fn generate_constraints(self, cs: ConstraintSystemRef<CF1<C1>>) -> Result<(), SynthesisError> {
200        let r1cs =
201            R1CSVar::<C1::ScalarField, CF1<C1>, FpVar<CF1<C1>>>::new_witness(cs.clone(), || {
202                Ok(self.r1cs.clone())
203            })?;
204
205        let pp_hash = FpVar::<CF1<C1>>::new_input(cs.clone(), || {
206            Ok(self.pp_hash.unwrap_or_else(CF1::<C1>::zero))
207        })?;
208        let i =
209            FpVar::<CF1<C1>>::new_input(cs.clone(), || Ok(self.i.unwrap_or_else(CF1::<C1>::zero)))?;
210        let z_0 = Vec::<FpVar<CF1<C1>>>::new_input(cs.clone(), || {
211            Ok(self.z_0.unwrap_or(vec![CF1::<C1>::zero()]))
212        })?;
213        let z_i = Vec::<FpVar<CF1<C1>>>::new_input(cs.clone(), || {
214            Ok(self.z_i.unwrap_or(vec![CF1::<C1>::zero()]))
215        })?;
216
217        let u_dummy_native = CommittedInstance::<C1>::dummy(&self.r1cs);
218        let w_dummy_native = Witness::<C1>::dummy(&self.r1cs);
219
220        let u_i = CommittedInstanceVar::<C1>::new_witness(cs.clone(), || {
221            Ok(self.u_i.unwrap_or(u_dummy_native.clone()))
222        })?;
223        let U_i = CommittedInstanceVar::<C1>::new_witness(cs.clone(), || {
224            Ok(self.U_i.unwrap_or(u_dummy_native.clone()))
225        })?;
226        // here (U_i1, W_i1) = NIFS.P( (U_i,W_i), (u_i,w_i))
227        let U_i1 = CommittedInstanceVar::<C1>::new_input(cs.clone(), || {
228            Ok(self.U_i1.unwrap_or(u_dummy_native.clone()))
229        })?;
230        let W_i1 = WitnessVar::<C1>::new_witness(cs.clone(), || {
231            Ok(self.W_i1.unwrap_or(w_dummy_native.clone()))
232        })?;
233
234        // allocate the inputs for the check 6
235        let kzg_c_W = FpVar::<CF1<C1>>::new_input(cs.clone(), || {
236            Ok(self.kzg_c_W.unwrap_or_else(CF1::<C1>::zero))
237        })?;
238        let kzg_c_E = FpVar::<CF1<C1>>::new_input(cs.clone(), || {
239            Ok(self.kzg_c_E.unwrap_or_else(CF1::<C1>::zero))
240        })?;
241        let _eval_W = FpVar::<CF1<C1>>::new_input(cs.clone(), || {
242            Ok(self.eval_W.unwrap_or_else(CF1::<C1>::zero))
243        })?;
244        let _eval_E = FpVar::<CF1<C1>>::new_input(cs.clone(), || {
245            Ok(self.eval_E.unwrap_or_else(CF1::<C1>::zero))
246        })?;
247
248        // `sponge` is for digest computation.
249        let sponge = PoseidonSpongeVar::<C1::ScalarField>::new(cs.clone(), &self.poseidon_config);
250        // `transcript` is for challenge generation.
251        let mut transcript = sponge.clone();
252        // notice that the `pp_hash` is absorbed inside the ChallengeGadget::get_challenge_gadget call
253
254        // 2. u_i.cmE==cm(0), u_i.u==1
255        // Here zero is the x & y coordinates of the zero point affine representation.
256        let zero = NonNativeUintVar::new_constant(cs.clone(), C1::BaseField::zero())?;
257        u_i.cmE.x.enforce_equal_unaligned(&zero)?;
258        u_i.cmE.y.enforce_equal_unaligned(&zero)?;
259        (u_i.u.is_one()?).enforce_equal(&Boolean::TRUE)?;
260
261        // 3.a u_i.x[0] == H(i, z_0, z_i, U_i)
262        let (u_i_x, U_i_vec) = U_i.clone().hash(&sponge, &pp_hash, &i, &z_0, &z_i)?;
263        (u_i.x[0]).enforce_equal(&u_i_x)?;
264
265        // 3.b u_i.x[1] == H(cf_U_i)
266        let cf_u_dummy_native =
267            CycleFoldCommittedInstance::<C2>::dummy(NovaCycleFoldConfig::<C1>::IO_LEN);
268        let cf_U_i = CycleFoldCommittedInstanceVar::<C2, GC2>::new_input(cs.clone(), || {
269            Ok(self.cf_U_i.unwrap_or_else(|| cf_u_dummy_native.clone()))
270        })?;
271        let (cf_u_i_x, _) = cf_U_i.clone().hash(&sponge, pp_hash.clone())?;
272        (u_i.x[1]).enforce_equal(&cf_u_i_x)?;
273
274        // 4. check RelaxedR1CS of U_{i+1}
275        let z_U1: Vec<FpVar<CF1<C1>>> =
276            [vec![U_i1.u.clone()], U_i1.x.to_vec(), W_i1.W.to_vec()].concat();
277        RelaxedR1CSGadget::check_native(r1cs, W_i1.E.clone(), U_i1.u.clone(), z_U1)?;
278
279        // 1.1.a, 5.1 compute NIFS.V and KZG challenges.
280        // We need to ensure the order of challenge generation is the same as
281        // the native counterpart, so we first compute the challenges here and
282        // do the actual checks later.
283        let cmT =
284            NonNativeAffineVar::new_input(cs.clone(), || Ok(self.cmT.unwrap_or_else(C1::zero)))?;
285        let r_bits = ChallengeGadget::<C1>::get_challenge_gadget(
286            &mut transcript,
287            pp_hash,
288            U_i_vec,
289            u_i.clone(),
290            cmT.clone(),
291        )?;
292        // 5.1.
293        let (incircuit_c_W, incircuit_c_E) =
294            KZGChallengesGadget::<C1>::get_challenges_gadget(&mut transcript, U_i1.clone())?;
295        incircuit_c_W.enforce_equal(&kzg_c_W)?;
296        incircuit_c_E.enforce_equal(&kzg_c_E)?;
297
298        // Check 5.2 is temporary disabled due
299        // https://github.com/privacy-scaling-explorations/sonobe/issues/80
300        log::warn!("[WARNING]: issue #80 (https://github.com/privacy-scaling-explorations/sonobe/issues/80) is not resolved yet.");
301        //
302        // 5.2. check eval_W==p_W(c_W) and eval_E==p_E(c_E)
303        // let incircuit_eval_W = evaluate_gadget::<CF1<C1>>(W_i1.W, incircuit_c_W)?;
304        // let incircuit_eval_E = evaluate_gadget::<CF1<C1>>(W_i1.E, incircuit_c_E)?;
305        // incircuit_eval_W.enforce_equal(&eval_W)?;
306        // incircuit_eval_E.enforce_equal(&eval_E)?;
307
308        // 1.1.b check that the NIFS.V challenge matches the one from the public input (so we avoid
309        //   the verifier computing it)
310        let r_Fr = Boolean::le_bits_to_fp_var(&r_bits)?;
311        // check that the in-circuit computed r is equal to the inputted r
312        let r =
313            FpVar::<CF1<C1>>::new_input(cs.clone(), || Ok(self.r.unwrap_or_else(CF1::<C1>::zero)))?;
314        r_Fr.enforce_equal(&r)?;
315
316        Ok(())
317    }
318}
319
320/// Circuit that implements part of the in-circuit checks needed for the offchain verification over
321/// the Curve1's BaseField (=Curve2's ScalarField).
322#[derive(Clone, Debug)]
323pub struct DeciderCircuit2<C1, GC1, C2>
324where
325    C1: CurveGroup,
326    C2: CurveGroup,
327{
328    _c1: PhantomData<C1>,
329    _gc1: PhantomData<GC1>,
330    _c2: PhantomData<C2>,
331
332    /// E vector's length of the CycleFold instance witness
333    pub cf_E_len: usize,
334    /// R1CS of the CycleFold circuit
335    pub cf_r1cs: R1CS<C2::ScalarField>,
336    pub poseidon_config: PoseidonConfig<CF1<C2>>,
337    /// public params hash
338    pub pp_hash: Option<C2::ScalarField>,
339
340    /// CycleFold running instance. Notice that here we use Nova's CommittedInstance (instead of
341    /// CycleFoldCommittedInstance), since we are over C2::Fr, so that the CycleFold instances can
342    /// be computed natively
343    pub cf_U_i: Option<CommittedInstance<C2>>,
344    pub cf_W_i: Option<CycleFoldWitness<C2>>,
345    /// KZG challenges
346    pub kzg_c_W: Option<C2::ScalarField>,
347    pub kzg_c_E: Option<C2::ScalarField>,
348    pub eval_W: Option<C2::ScalarField>,
349    pub eval_E: Option<C2::ScalarField>,
350}
351impl<C1, GC1, C2> DeciderCircuit2<C1, GC1, C2>
352where
353    C1: CurveGroup,
354    C2: CurveGroup,
355    <C1 as CurveGroup>::BaseField: PrimeField,
356    <C1 as Group>::ScalarField: Absorb,
357    <C2 as CurveGroup>::BaseField: PrimeField,
358    <C2 as Group>::ScalarField: Absorb,
359    GC1: CurveVar<C1, CF2<C1>> + ToConstraintFieldGadget<CF2<C1>>,
360{
361    pub fn from_nova<GC2, CS1, CS2, const H: bool, FC: FCircuit<C1::ScalarField>>(
362        nova: Nova<C1, GC1, C2, GC2, FC, CS1, CS2, H>,
363    ) -> Result<Self, Error>
364    where
365        GC2: CurveVar<C2, CF2<C2>> + ToConstraintFieldGadget<CF2<C2>>,
366        CS1: CommitmentScheme<C1, H>,
367        CS2: CommitmentScheme<C2, H>,
368    {
369        // compute the KZG challenges of the CycleFold instance commitments, used as inputs in the
370        // circuit
371        let poseidon_config =
372            crate::transcript::poseidon::poseidon_canonical_config::<C2::ScalarField>();
373        let mut transcript = PoseidonSponge::<C2::ScalarField>::new(&poseidon_config);
374        let pp_hash_Fq =
375            C2::ScalarField::from_le_bytes_mod_order(&nova.pp_hash.into_bigint().to_bytes_le());
376        transcript.absorb(&pp_hash_Fq);
377
378        let (kzg_challenge_W, kzg_challenge_E) =
379            KZGChallengesGadget::<C2>::get_challenges_native(&mut transcript, nova.cf_U_i.clone());
380
381        // get KZG evals
382        let mut W = nova.cf_W_i.W.clone();
383        W.extend(
384            std::iter::repeat(C2::ScalarField::zero())
385                .take(nova.cf_W_i.W.len().next_power_of_two() - nova.cf_W_i.W.len()),
386        );
387        let mut E = nova.cf_W_i.E.clone();
388        E.extend(
389            std::iter::repeat(C2::ScalarField::zero())
390                .take(nova.cf_W_i.E.len().next_power_of_two() - nova.cf_W_i.E.len()),
391        );
392        let p_W = poly_from_vec(W.to_vec())?;
393        let eval_W = p_W.evaluate(&kzg_challenge_W);
394        let p_E = poly_from_vec(E.to_vec())?;
395        let eval_E = p_E.evaluate(&kzg_challenge_E);
396
397        Ok(Self {
398            _c1: PhantomData,
399            _gc1: PhantomData,
400            _c2: PhantomData,
401
402            cf_E_len: nova.cf_W_i.E.len(),
403            cf_r1cs: nova.cf_r1cs,
404            poseidon_config,
405            pp_hash: Some(pp_hash_Fq),
406
407            cf_U_i: Some(nova.cf_U_i),
408            cf_W_i: Some(nova.cf_W_i),
409
410            // CycleFold instance commitments kzg challenges
411            kzg_c_W: Some(kzg_challenge_W),
412            kzg_c_E: Some(kzg_challenge_E),
413            eval_W: Some(eval_W),
414            eval_E: Some(eval_E),
415        })
416    }
417}
418
419impl<C1, GC1, C2> ConstraintSynthesizer<CF1<C2>> for DeciderCircuit2<C1, GC1, C2>
420where
421    C1: CurveGroup,
422    C2: CurveGroup,
423    <C1 as CurveGroup>::BaseField: PrimeField,
424    <C2 as CurveGroup>::BaseField: PrimeField,
425    <C1 as Group>::ScalarField: Absorb,
426    <C2 as Group>::ScalarField: Absorb,
427    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
428    GC1: CurveVar<C1, CF2<C1>> + ToConstraintFieldGadget<CF2<C1>>,
429    for<'a> &'a GC1: GroupOpsBounds<'a, C1, GC1>,
430{
431    fn generate_constraints(self, cs: ConstraintSystemRef<CF1<C2>>) -> Result<(), SynthesisError> {
432        let pp_hash = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
433            Ok(self.pp_hash.unwrap_or_else(CF1::<C2>::zero))
434        })?;
435
436        let cf_u_dummy_native = CommittedInstance::<C2>::dummy(&self.cf_r1cs);
437        let w_dummy_native = Witness::<C2>::dummy(&self.cf_r1cs);
438        let cf_U_i = CommittedInstanceVar::<C2>::new_input(cs.clone(), || {
439            Ok(self.cf_U_i.unwrap_or_else(|| cf_u_dummy_native.clone()))
440        })?;
441        let cf_W_i = WitnessVar::<C2>::new_witness(cs.clone(), || {
442            Ok(self.cf_W_i.unwrap_or(w_dummy_native.clone()))
443        })?;
444
445        let cf_r1cs =
446            R1CSVar::<C2::ScalarField, CF1<C2>, FpVar<CF1<C2>>>::new_witness(cs.clone(), || {
447                Ok(self.cf_r1cs.clone())
448            })?;
449
450        // 6. check RelaxedR1CS of cf_U_i
451        let cf_z_U = [vec![cf_U_i.u.clone()], cf_U_i.x.to_vec(), cf_W_i.W.to_vec()].concat();
452        RelaxedR1CSGadget::check_native(cf_r1cs, cf_W_i.E, cf_U_i.u.clone(), cf_z_U)?;
453
454        // `transcript` is for challenge generation.
455        let mut transcript =
456            PoseidonSpongeVar::<C2::ScalarField>::new(cs.clone(), &self.poseidon_config);
457        transcript.absorb(&pp_hash)?;
458
459        // allocate the inputs for the check 7.1
460        let kzg_c_W = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
461            Ok(self.kzg_c_W.unwrap_or_else(CF1::<C2>::zero))
462        })?;
463        let kzg_c_E = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
464            Ok(self.kzg_c_E.unwrap_or_else(CF1::<C2>::zero))
465        })?;
466        // allocate the inputs for the check 7.2
467        let _eval_W = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
468            Ok(self.eval_W.unwrap_or_else(CF1::<C2>::zero))
469        })?;
470        let _eval_E = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
471            Ok(self.eval_E.unwrap_or_else(CF1::<C2>::zero))
472        })?;
473
474        // 7.1. check the KZG challenges correct computation
475        let (incircuit_c_W, incircuit_c_E) =
476            KZGChallengesGadget::<C2>::get_challenges_gadget(&mut transcript, cf_U_i.clone())?;
477        incircuit_c_W.enforce_equal(&kzg_c_W)?;
478        incircuit_c_E.enforce_equal(&kzg_c_E)?;
479
480        // Check 7.2 is temporary disabled due
481        // https://github.com/privacy-scaling-explorations/sonobe/issues/80
482        log::warn!("[WARNING]: issue #80 (https://github.com/privacy-scaling-explorations/sonobe/issues/80) is not resolved yet.");
483        // 7.2. check eval_W==p_W(c_W) and eval_E==p_E(c_E)
484        // let incircuit_eval_W = evaluate_gadget::<CF1<C1>>(W_i1.W, incircuit_c_W)?;
485        // let incircuit_eval_E = evaluate_gadget::<CF1<C1>>(W_i1.E, incircuit_c_E)?;
486        // incircuit_eval_W.enforce_equal(&eval_W)?;
487        // incircuit_eval_E.enforce_equal(&eval_E)?;
488
489        Ok(())
490    }
491}
492
493#[cfg(test)]
494pub mod tests {
495    use ark_pallas::{constraints::GVar, Fq, Fr, Projective};
496    use ark_relations::r1cs::ConstraintSystem;
497    use ark_std::One;
498    use ark_vesta::{constraints::GVar as GVar2, Projective as Projective2};
499
500    use super::*;
501    use crate::commitment::pedersen::Pedersen;
502    use crate::folding::nova::PreprocessorParam;
503    use crate::frontend::utils::CubicFCircuit;
504    use crate::transcript::poseidon::poseidon_canonical_config;
505    use crate::FoldingScheme;
506
507    #[test]
508    fn test_decider_circuits() {
509        let mut rng = ark_std::test_rng();
510        let poseidon_config = poseidon_canonical_config::<Fr>();
511
512        let F_circuit = CubicFCircuit::<Fr>::new(()).unwrap();
513        let z_0 = vec![Fr::from(3_u32)];
514
515        type N = Nova<
516            Projective,
517            GVar,
518            Projective2,
519            GVar2,
520            CubicFCircuit<Fr>,
521            Pedersen<Projective>,
522            Pedersen<Projective2>,
523            false,
524        >;
525
526        let prep_param = PreprocessorParam::<
527            Projective,
528            Projective2,
529            CubicFCircuit<Fr>,
530            Pedersen<Projective>,
531            Pedersen<Projective2>,
532            false,
533        >::new(poseidon_config, F_circuit);
534        let nova_params = N::preprocess(&mut rng, &prep_param).unwrap();
535
536        // generate a Nova instance and do a step of it
537        let mut nova = N::init(&nova_params, F_circuit, z_0.clone()).unwrap();
538        nova.prove_step(&mut rng, vec![], None).unwrap();
539        let ivc_v = nova.clone();
540        let (running_instance, incoming_instance, cyclefold_instance) = ivc_v.instances();
541        N::verify(
542            nova_params.1, // verifier_params
543            z_0,
544            ivc_v.z_i,
545            Fr::one(),
546            running_instance,
547            incoming_instance,
548            cyclefold_instance,
549        )
550        .unwrap();
551
552        // load the DeciderCircuit 1 & 2 from the Nova instance
553        let decider_circuit1 =
554            DeciderCircuit1::<Projective, Projective2, GVar2>::from_nova(nova.clone()).unwrap();
555        let decider_circuit2 =
556            DeciderCircuit2::<Projective, GVar, Projective2>::from_nova(nova).unwrap();
557
558        // generate the constraints of both circuits and check that are satisfied by the inputs
559        let cs1 = ConstraintSystem::<Fr>::new_ref();
560        decider_circuit1.generate_constraints(cs1.clone()).unwrap();
561        assert!(cs1.is_satisfied().unwrap());
562        let cs2 = ConstraintSystem::<Fq>::new_ref();
563        decider_circuit2.generate_constraints(cs2.clone()).unwrap();
564        assert!(cs2.is_satisfied().unwrap());
565    }
566}