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;
 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(2);
218        let w_dummy_native = Witness::<C1>::dummy(
219            self.r1cs.A.n_cols - 3, /* (3=2+1, since u_i.x.len=2) */
220            self.E_len,
221        );
222
223        let u_i = CommittedInstanceVar::<C1>::new_witness(cs.clone(), || {
224            Ok(self.u_i.unwrap_or(u_dummy_native.clone()))
225        })?;
226        let U_i = CommittedInstanceVar::<C1>::new_witness(cs.clone(), || {
227            Ok(self.U_i.unwrap_or(u_dummy_native.clone()))
228        })?;
229        // here (U_i1, W_i1) = NIFS.P( (U_i,W_i), (u_i,w_i))
230        let U_i1 = CommittedInstanceVar::<C1>::new_input(cs.clone(), || {
231            Ok(self.U_i1.unwrap_or(u_dummy_native.clone()))
232        })?;
233        let W_i1 = WitnessVar::<C1>::new_witness(cs.clone(), || {
234            Ok(self.W_i1.unwrap_or(w_dummy_native.clone()))
235        })?;
236
237        // allocate the inputs for the check 6
238        let kzg_c_W = FpVar::<CF1<C1>>::new_input(cs.clone(), || {
239            Ok(self.kzg_c_W.unwrap_or_else(CF1::<C1>::zero))
240        })?;
241        let kzg_c_E = FpVar::<CF1<C1>>::new_input(cs.clone(), || {
242            Ok(self.kzg_c_E.unwrap_or_else(CF1::<C1>::zero))
243        })?;
244        let _eval_W = FpVar::<CF1<C1>>::new_input(cs.clone(), || {
245            Ok(self.eval_W.unwrap_or_else(CF1::<C1>::zero))
246        })?;
247        let _eval_E = FpVar::<CF1<C1>>::new_input(cs.clone(), || {
248            Ok(self.eval_E.unwrap_or_else(CF1::<C1>::zero))
249        })?;
250
251        // `sponge` is for digest computation.
252        let sponge = PoseidonSpongeVar::<C1::ScalarField>::new(cs.clone(), &self.poseidon_config);
253        // `transcript` is for challenge generation.
254        let mut transcript = sponge.clone();
255        // notice that the `pp_hash` is absorbed inside the ChallengeGadget::get_challenge_gadget call
256
257        // 2. u_i.cmE==cm(0), u_i.u==1
258        // Here zero is the x & y coordinates of the zero point affine representation.
259        let zero = NonNativeUintVar::new_constant(cs.clone(), C1::BaseField::zero())?;
260        u_i.cmE.x.enforce_equal_unaligned(&zero)?;
261        u_i.cmE.y.enforce_equal_unaligned(&zero)?;
262        (u_i.u.is_one()?).enforce_equal(&Boolean::TRUE)?;
263
264        // 3.a u_i.x[0] == H(i, z_0, z_i, U_i)
265        let (u_i_x, U_i_vec) = U_i.clone().hash(&sponge, &pp_hash, &i, &z_0, &z_i)?;
266        (u_i.x[0]).enforce_equal(&u_i_x)?;
267
268        // 3.b u_i.x[1] == H(cf_U_i)
269        let cf_u_dummy_native =
270            CycleFoldCommittedInstance::<C2>::dummy(NovaCycleFoldConfig::<C1>::IO_LEN);
271        let cf_U_i = CycleFoldCommittedInstanceVar::<C2, GC2>::new_input(cs.clone(), || {
272            Ok(self.cf_U_i.unwrap_or_else(|| cf_u_dummy_native.clone()))
273        })?;
274        let (cf_u_i_x, _) = cf_U_i.clone().hash(&sponge, pp_hash.clone())?;
275        (u_i.x[1]).enforce_equal(&cf_u_i_x)?;
276
277        // 4. check RelaxedR1CS of U_{i+1}
278        let z_U1: Vec<FpVar<CF1<C1>>> =
279            [vec![U_i1.u.clone()], U_i1.x.to_vec(), W_i1.W.to_vec()].concat();
280        RelaxedR1CSGadget::check_native(r1cs, W_i1.E.clone(), U_i1.u.clone(), z_U1)?;
281
282        // 1.1.a, 5.1 compute NIFS.V and KZG challenges.
283        // We need to ensure the order of challenge generation is the same as
284        // the native counterpart, so we first compute the challenges here and
285        // do the actual checks later.
286        let cmT =
287            NonNativeAffineVar::new_input(cs.clone(), || Ok(self.cmT.unwrap_or_else(C1::zero)))?;
288        let r_bits = ChallengeGadget::<C1>::get_challenge_gadget(
289            &mut transcript,
290            pp_hash,
291            U_i_vec,
292            u_i.clone(),
293            cmT.clone(),
294        )?;
295        // 5.1.
296        let (incircuit_c_W, incircuit_c_E) =
297            KZGChallengesGadget::<C1>::get_challenges_gadget(&mut transcript, U_i1.clone())?;
298        incircuit_c_W.enforce_equal(&kzg_c_W)?;
299        incircuit_c_E.enforce_equal(&kzg_c_E)?;
300
301        // Check 5.2 is temporary disabled due
302        // https://github.com/privacy-scaling-explorations/sonobe/issues/80
303        log::warn!("[WARNING]: issue #80 (https://github.com/privacy-scaling-explorations/sonobe/issues/80) is not resolved yet.");
304        //
305        // 5.2. check eval_W==p_W(c_W) and eval_E==p_E(c_E)
306        // let incircuit_eval_W = evaluate_gadget::<CF1<C1>>(W_i1.W, incircuit_c_W)?;
307        // let incircuit_eval_E = evaluate_gadget::<CF1<C1>>(W_i1.E, incircuit_c_E)?;
308        // incircuit_eval_W.enforce_equal(&eval_W)?;
309        // incircuit_eval_E.enforce_equal(&eval_E)?;
310
311        // 1.1.b check that the NIFS.V challenge matches the one from the public input (so we avoid
312        //   the verifier computing it)
313        let r_Fr = Boolean::le_bits_to_fp_var(&r_bits)?;
314        // check that the in-circuit computed r is equal to the inputted r
315        let r =
316            FpVar::<CF1<C1>>::new_input(cs.clone(), || Ok(self.r.unwrap_or_else(CF1::<C1>::zero)))?;
317        r_Fr.enforce_equal(&r)?;
318
319        Ok(())
320    }
321}
322
323/// Circuit that implements part of the in-circuit checks needed for the offchain verification over
324/// the Curve1's BaseField (=Curve2's ScalarField).
325#[derive(Clone, Debug)]
326pub struct DeciderCircuit2<C1, GC1, C2>
327where
328    C1: CurveGroup,
329    C2: CurveGroup,
330{
331    _c1: PhantomData<C1>,
332    _gc1: PhantomData<GC1>,
333    _c2: PhantomData<C2>,
334
335    /// E vector's length of the CycleFold instance witness
336    pub cf_E_len: usize,
337    /// R1CS of the CycleFold circuit
338    pub cf_r1cs: R1CS<C2::ScalarField>,
339    pub poseidon_config: PoseidonConfig<CF1<C2>>,
340    /// public params hash
341    pub pp_hash: Option<C2::ScalarField>,
342
343    /// CycleFold running instance. Notice that here we use Nova's CommittedInstance (instead of
344    /// CycleFoldCommittedInstance), since we are over C2::Fr, so that the CycleFold instances can
345    /// be computed natively
346    pub cf_U_i: Option<CommittedInstance<C2>>,
347    pub cf_W_i: Option<CycleFoldWitness<C2>>,
348    /// KZG challenges
349    pub kzg_c_W: Option<C2::ScalarField>,
350    pub kzg_c_E: Option<C2::ScalarField>,
351    pub eval_W: Option<C2::ScalarField>,
352    pub eval_E: Option<C2::ScalarField>,
353}
354impl<C1, GC1, C2> DeciderCircuit2<C1, GC1, C2>
355where
356    C1: CurveGroup,
357    C2: CurveGroup,
358    <C1 as CurveGroup>::BaseField: PrimeField,
359    <C1 as Group>::ScalarField: Absorb,
360    <C2 as CurveGroup>::BaseField: PrimeField,
361    <C2 as Group>::ScalarField: Absorb,
362    GC1: CurveVar<C1, CF2<C1>> + ToConstraintFieldGadget<CF2<C1>>,
363{
364    pub fn from_nova<GC2, CS1, CS2, const H: bool, FC: FCircuit<C1::ScalarField>>(
365        nova: Nova<C1, GC1, C2, GC2, FC, CS1, CS2, H>,
366    ) -> Result<Self, Error>
367    where
368        GC2: CurveVar<C2, CF2<C2>> + ToConstraintFieldGadget<CF2<C2>>,
369        CS1: CommitmentScheme<C1, H>,
370        CS2: CommitmentScheme<C2, H>,
371    {
372        // compute the KZG challenges of the CycleFold instance commitments, used as inputs in the
373        // circuit
374        let poseidon_config =
375            crate::transcript::poseidon::poseidon_canonical_config::<C2::ScalarField>();
376        let mut transcript = PoseidonSponge::<C2::ScalarField>::new(&poseidon_config);
377        let pp_hash_Fq =
378            C2::ScalarField::from_le_bytes_mod_order(&nova.pp_hash.into_bigint().to_bytes_le());
379        transcript.absorb(&pp_hash_Fq);
380
381        let (kzg_challenge_W, kzg_challenge_E) =
382            KZGChallengesGadget::<C2>::get_challenges_native(&mut transcript, nova.cf_U_i.clone());
383
384        // get KZG evals
385        let mut W = nova.cf_W_i.W.clone();
386        W.extend(
387            std::iter::repeat(C2::ScalarField::zero())
388                .take(nova.cf_W_i.W.len().next_power_of_two() - nova.cf_W_i.W.len()),
389        );
390        let mut E = nova.cf_W_i.E.clone();
391        E.extend(
392            std::iter::repeat(C2::ScalarField::zero())
393                .take(nova.cf_W_i.E.len().next_power_of_two() - nova.cf_W_i.E.len()),
394        );
395        let p_W = poly_from_vec(W.to_vec())?;
396        let eval_W = p_W.evaluate(&kzg_challenge_W);
397        let p_E = poly_from_vec(E.to_vec())?;
398        let eval_E = p_E.evaluate(&kzg_challenge_E);
399
400        Ok(Self {
401            _c1: PhantomData,
402            _gc1: PhantomData,
403            _c2: PhantomData,
404
405            cf_E_len: nova.cf_W_i.E.len(),
406            cf_r1cs: nova.cf_r1cs,
407            poseidon_config,
408            pp_hash: Some(pp_hash_Fq),
409
410            cf_U_i: Some(nova.cf_U_i),
411            cf_W_i: Some(nova.cf_W_i),
412
413            // CycleFold instance commitments kzg challenges
414            kzg_c_W: Some(kzg_challenge_W),
415            kzg_c_E: Some(kzg_challenge_E),
416            eval_W: Some(eval_W),
417            eval_E: Some(eval_E),
418        })
419    }
420}
421
422impl<C1, GC1, C2> ConstraintSynthesizer<CF1<C2>> for DeciderCircuit2<C1, GC1, C2>
423where
424    C1: CurveGroup,
425    C2: CurveGroup,
426    <C1 as CurveGroup>::BaseField: PrimeField,
427    <C2 as CurveGroup>::BaseField: PrimeField,
428    <C1 as Group>::ScalarField: Absorb,
429    <C2 as Group>::ScalarField: Absorb,
430    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
431    GC1: CurveVar<C1, CF2<C1>> + ToConstraintFieldGadget<CF2<C1>>,
432    for<'a> &'a GC1: GroupOpsBounds<'a, C1, GC1>,
433{
434    fn generate_constraints(self, cs: ConstraintSystemRef<CF1<C2>>) -> Result<(), SynthesisError> {
435        let pp_hash = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
436            Ok(self.pp_hash.unwrap_or_else(CF1::<C2>::zero))
437        })?;
438
439        let cf_u_dummy_native = CommittedInstance::<C2>::dummy(NovaCycleFoldConfig::<C1>::IO_LEN);
440        let w_dummy_native =
441            Witness::<C2>::dummy(self.cf_r1cs.A.n_cols - 1 - self.cf_r1cs.l, self.cf_E_len);
442        let cf_U_i = CommittedInstanceVar::<C2>::new_input(cs.clone(), || {
443            Ok(self.cf_U_i.unwrap_or_else(|| cf_u_dummy_native.clone()))
444        })?;
445        let cf_W_i = WitnessVar::<C2>::new_witness(cs.clone(), || {
446            Ok(self.cf_W_i.unwrap_or(w_dummy_native.clone()))
447        })?;
448
449        let cf_r1cs =
450            R1CSVar::<C2::ScalarField, CF1<C2>, FpVar<CF1<C2>>>::new_witness(cs.clone(), || {
451                Ok(self.cf_r1cs.clone())
452            })?;
453
454        // 6. check RelaxedR1CS of cf_U_i
455        let cf_z_U = [vec![cf_U_i.u.clone()], cf_U_i.x.to_vec(), cf_W_i.W.to_vec()].concat();
456        RelaxedR1CSGadget::check_native(cf_r1cs, cf_W_i.E, cf_U_i.u.clone(), cf_z_U)?;
457
458        // `transcript` is for challenge generation.
459        let mut transcript =
460            PoseidonSpongeVar::<C2::ScalarField>::new(cs.clone(), &self.poseidon_config);
461        transcript.absorb(&pp_hash)?;
462
463        // allocate the inputs for the check 7.1
464        let kzg_c_W = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
465            Ok(self.kzg_c_W.unwrap_or_else(CF1::<C2>::zero))
466        })?;
467        let kzg_c_E = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
468            Ok(self.kzg_c_E.unwrap_or_else(CF1::<C2>::zero))
469        })?;
470        // allocate the inputs for the check 7.2
471        let _eval_W = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
472            Ok(self.eval_W.unwrap_or_else(CF1::<C2>::zero))
473        })?;
474        let _eval_E = FpVar::<CF1<C2>>::new_input(cs.clone(), || {
475            Ok(self.eval_E.unwrap_or_else(CF1::<C2>::zero))
476        })?;
477
478        // 7.1. check the KZG challenges correct computation
479        let (incircuit_c_W, incircuit_c_E) =
480            KZGChallengesGadget::<C2>::get_challenges_gadget(&mut transcript, cf_U_i.clone())?;
481        incircuit_c_W.enforce_equal(&kzg_c_W)?;
482        incircuit_c_E.enforce_equal(&kzg_c_E)?;
483
484        // Check 7.2 is temporary disabled due
485        // https://github.com/privacy-scaling-explorations/sonobe/issues/80
486        log::warn!("[WARNING]: issue #80 (https://github.com/privacy-scaling-explorations/sonobe/issues/80) is not resolved yet.");
487        // 7.2. check eval_W==p_W(c_W) and eval_E==p_E(c_E)
488        // let incircuit_eval_W = evaluate_gadget::<CF1<C1>>(W_i1.W, incircuit_c_W)?;
489        // let incircuit_eval_E = evaluate_gadget::<CF1<C1>>(W_i1.E, incircuit_c_E)?;
490        // incircuit_eval_W.enforce_equal(&eval_W)?;
491        // incircuit_eval_E.enforce_equal(&eval_E)?;
492
493        Ok(())
494    }
495}
496
497#[cfg(test)]
498pub mod tests {
499    use ark_pallas::{constraints::GVar, Fq, Fr, Projective};
500    use ark_relations::r1cs::ConstraintSystem;
501    use ark_std::One;
502    use ark_vesta::{constraints::GVar as GVar2, Projective as Projective2};
503
504    use super::*;
505    use crate::commitment::pedersen::Pedersen;
506    use crate::folding::nova::PreprocessorParam;
507    use crate::frontend::utils::CubicFCircuit;
508    use crate::transcript::poseidon::poseidon_canonical_config;
509    use crate::FoldingScheme;
510
511    #[test]
512    fn test_decider_circuits() {
513        let mut rng = ark_std::test_rng();
514        let poseidon_config = poseidon_canonical_config::<Fr>();
515
516        let F_circuit = CubicFCircuit::<Fr>::new(()).unwrap();
517        let z_0 = vec![Fr::from(3_u32)];
518
519        type N = Nova<
520            Projective,
521            GVar,
522            Projective2,
523            GVar2,
524            CubicFCircuit<Fr>,
525            Pedersen<Projective>,
526            Pedersen<Projective2>,
527            false,
528        >;
529
530        let prep_param = PreprocessorParam::<
531            Projective,
532            Projective2,
533            CubicFCircuit<Fr>,
534            Pedersen<Projective>,
535            Pedersen<Projective2>,
536            false,
537        >::new(poseidon_config, F_circuit);
538        let nova_params = N::preprocess(&mut rng, &prep_param).unwrap();
539
540        // generate a Nova instance and do a step of it
541        let mut nova = N::init(&nova_params, F_circuit, z_0.clone()).unwrap();
542        nova.prove_step(&mut rng, vec![], None).unwrap();
543        let ivc_v = nova.clone();
544        let (running_instance, incoming_instance, cyclefold_instance) = ivc_v.instances();
545        N::verify(
546            nova_params.1, // verifier_params
547            z_0,
548            ivc_v.z_i,
549            Fr::one(),
550            running_instance,
551            incoming_instance,
552            cyclefold_instance,
553        )
554        .unwrap();
555
556        // load the DeciderCircuit 1 & 2 from the Nova instance
557        let decider_circuit1 =
558            DeciderCircuit1::<Projective, Projective2, GVar2>::from_nova(nova.clone()).unwrap();
559        let decider_circuit2 =
560            DeciderCircuit2::<Projective, GVar, Projective2>::from_nova(nova).unwrap();
561
562        // generate the constraints of both circuits and check that are satisfied by the inputs
563        let cs1 = ConstraintSystem::<Fr>::new_ref();
564        decider_circuit1.generate_constraints(cs1.clone()).unwrap();
565        assert!(cs1.is_satisfied().unwrap());
566        let cs2 = ConstraintSystem::<Fq>::new_ref();
567        decider_circuit2.generate_constraints(cs2.clone()).unwrap();
568        assert!(cs2.is_satisfied().unwrap());
569    }
570}