arnaucube
arnaucube/sonobePublic Self-hosted repository
sonobe/folding-schemes/src/folding/hypernova/mod.rs
folding-schemes/src/folding/hypernova/mod.rsRaw
  1/// Implements the scheme described in [HyperNova](https://eprint.iacr.org/2023/573.pdf)
  2use ark_crypto_primitives::sponge::{poseidon::PoseidonConfig, Absorb};
  3use ark_ec::{CurveGroup, Group};
  4use ark_ff::{BigInteger, PrimeField};
  5use ark_r1cs_std::{groups::GroupOpsBounds, prelude::CurveVar, ToConstraintFieldGadget};
  6use ark_std::rand::RngCore;
  7use ark_std::{One, Zero};
  8use core::marker::PhantomData;
  9use std::fmt::Debug;
 10
 11pub mod cccs;
 12pub mod circuits;
 13use circuits::AugmentedFCircuit;
 14pub mod lcccs;
 15pub mod nimfs;
 16pub mod utils;
 17use cccs::CCCS;
 18use lcccs::LCCCS;
 19use nimfs::NIMFS;
 20
 21use crate::commitment::CommitmentScheme;
 22use crate::folding::circuits::{
 23    cyclefold::{fold_cyclefold_circuit, CycleFoldCircuit},
 24    CF2,
 25};
 26use crate::folding::nova::{
 27    get_r1cs_from_cs, traits::NovaR1CS, CommittedInstance, PreprocessorParam,
 28    Witness as NovaWitness,
 29};
 30use crate::frontend::FCircuit;
 31use crate::utils::{get_cm_coordinates, pp_hash};
 32use crate::Error;
 33use crate::FoldingScheme;
 34use crate::{
 35    arith::{
 36        ccs::CCS,
 37        r1cs::{extract_w_x, R1CS},
 38    },
 39    transcript::{poseidon::PoseidonTranscript, Transcript},
 40};
 41
 42/// Witness for the LCCCS & CCCS, containing the w vector, and the r_w used as randomness in the Pedersen commitment.
 43#[derive(Debug, Clone, Eq, PartialEq)]
 44pub struct Witness<F: PrimeField> {
 45    pub w: Vec<F>,
 46    pub r_w: F,
 47}
 48
 49impl<F: PrimeField> Witness<F> {
 50    pub fn new(w: Vec<F>) -> Self {
 51        // note: at the current version, we don't use the blinding factors and we set them to 0
 52        // always.
 53        Self { w, r_w: F::zero() }
 54    }
 55    pub fn dummy(ccs: &CCS<F>) -> Self {
 56        Witness::<F>::new(vec![F::zero(); ccs.n - ccs.l - 1])
 57    }
 58}
 59
 60#[derive(Debug, Clone)]
 61pub struct ProverParams<C1, C2, CS1, CS2>
 62where
 63    C1: CurveGroup,
 64    C2: CurveGroup,
 65    CS1: CommitmentScheme<C1>,
 66    CS2: CommitmentScheme<C2>,
 67{
 68    pub poseidon_config: PoseidonConfig<C1::ScalarField>,
 69    pub cs_params: CS1::ProverParams,
 70    pub cf_cs_params: CS2::ProverParams,
 71    // if ccs is set, it will be used, if not, it will be computed at runtime
 72    pub ccs: Option<CCS<C1::ScalarField>>,
 73}
 74
 75#[derive(Debug, Clone)]
 76pub struct VerifierParams<
 77    C1: CurveGroup,
 78    C2: CurveGroup,
 79    CS1: CommitmentScheme<C1>,
 80    CS2: CommitmentScheme<C2>,
 81> {
 82    pub poseidon_config: PoseidonConfig<C1::ScalarField>,
 83    pub ccs: CCS<C1::ScalarField>,
 84    pub cf_r1cs: R1CS<C2::ScalarField>,
 85    pub cs_vp: CS1::VerifierParams,
 86    pub cf_cs_vp: CS2::VerifierParams,
 87}
 88
 89impl<C1, C2, CS1, CS2> VerifierParams<C1, C2, CS1, CS2>
 90where
 91    C1: CurveGroup,
 92    C2: CurveGroup,
 93    CS1: CommitmentScheme<C1>,
 94    CS2: CommitmentScheme<C2>,
 95{
 96    /// returns the hash of the public parameters of HyperNova
 97    pub fn pp_hash(&self) -> Result<C1::ScalarField, Error> {
 98        pp_hash::<C1, C2, CS1, CS2>(
 99            &self.ccs,
100            &self.cf_r1cs,
101            &self.cs_vp,
102            &self.cf_cs_vp,
103            &self.poseidon_config,
104        )
105    }
106}
107
108/// Implements HyperNova+CycleFold's IVC, described in
109/// [HyperNova](https://eprint.iacr.org/2023/573.pdf) and
110/// [CycleFold](https://eprint.iacr.org/2023/1192.pdf), following the FoldingScheme trait
111#[derive(Clone, Debug)]
112pub struct HyperNova<C1, GC1, C2, GC2, FC, CS1, CS2>
113where
114    C1: CurveGroup,
115    GC1: CurveVar<C1, CF2<C1>> + ToConstraintFieldGadget<CF2<C1>>,
116    C2: CurveGroup,
117    GC2: CurveVar<C2, CF2<C2>>,
118    FC: FCircuit<C1::ScalarField>,
119    CS1: CommitmentScheme<C1>,
120    CS2: CommitmentScheme<C2>,
121{
122    _gc1: PhantomData<GC1>,
123    _c2: PhantomData<C2>,
124    _gc2: PhantomData<GC2>,
125
126    /// CCS of the Augmented Function circuit
127    pub ccs: CCS<C1::ScalarField>,
128    /// R1CS of the CycleFold circuit
129    pub cf_r1cs: R1CS<C2::ScalarField>,
130    pub poseidon_config: PoseidonConfig<C1::ScalarField>,
131    /// CommitmentScheme::ProverParams over C1
132    pub cs_params: CS1::ProverParams,
133    /// CycleFold CommitmentScheme::ProverParams, over C2
134    pub cf_cs_params: CS2::ProverParams,
135    /// F circuit, the circuit that is being folded
136    pub F: FC,
137    /// public params hash
138    pub pp_hash: C1::ScalarField,
139    pub i: C1::ScalarField,
140    /// initial state
141    pub z_0: Vec<C1::ScalarField>,
142    /// current i-th state
143    pub z_i: Vec<C1::ScalarField>,
144    /// HyperNova instances
145    pub W_i: Witness<C1::ScalarField>,
146    pub U_i: LCCCS<C1>,
147    pub w_i: Witness<C1::ScalarField>,
148    pub u_i: CCCS<C1>,
149
150    /// CycleFold running instance
151    pub cf_W_i: NovaWitness<C2>,
152    pub cf_U_i: CommittedInstance<C2>,
153}
154
155impl<C1, GC1, C2, GC2, FC, CS1, CS2> FoldingScheme<C1, C2, FC>
156    for HyperNova<C1, GC1, C2, GC2, FC, CS1, CS2>
157where
158    C1: CurveGroup,
159    GC1: CurveVar<C1, CF2<C1>> + ToConstraintFieldGadget<CF2<C1>>,
160    C2: CurveGroup,
161    GC2: CurveVar<C2, CF2<C2>> + ToConstraintFieldGadget<CF2<C2>>,
162    FC: FCircuit<C1::ScalarField>,
163    CS1: CommitmentScheme<C1>,
164    CS2: CommitmentScheme<C2>,
165    <C1 as CurveGroup>::BaseField: PrimeField,
166    <C2 as CurveGroup>::BaseField: PrimeField,
167    <C1 as Group>::ScalarField: Absorb,
168    <C2 as Group>::ScalarField: Absorb,
169    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
170    for<'a> &'a GC1: GroupOpsBounds<'a, C1, GC1>,
171    for<'a> &'a GC2: GroupOpsBounds<'a, C2, GC2>,
172{
173    type PreprocessorParam = PreprocessorParam<C1, C2, FC, CS1, CS2>;
174    type ProverParam = ProverParams<C1, C2, CS1, CS2>;
175    type VerifierParam = VerifierParams<C1, C2, CS1, CS2>;
176    type RunningInstance = (LCCCS<C1>, Witness<C1::ScalarField>);
177    type IncomingInstance = (CCCS<C1>, Witness<C1::ScalarField>);
178    type CFInstance = (CommittedInstance<C2>, NovaWitness<C2>);
179
180    fn preprocess(
181        mut rng: impl RngCore,
182        prep_param: &Self::PreprocessorParam,
183    ) -> Result<(Self::ProverParam, Self::VerifierParam), Error> {
184        let augmented_f_circuit = AugmentedFCircuit::<C1, C2, GC2, FC>::empty(
185            &prep_param.poseidon_config,
186            prep_param.F.clone(),
187            None,
188        )?;
189        let ccs = augmented_f_circuit.ccs.clone();
190
191        let cf_circuit = CycleFoldCircuit::<C1, GC1>::empty();
192        let cf_r1cs = get_r1cs_from_cs::<C2::ScalarField>(cf_circuit)?;
193
194        // if cs params exist, use them, if not, generate new ones
195        let cs_pp: CS1::ProverParams;
196        let cs_vp: CS1::VerifierParams;
197        let cf_cs_pp: CS2::ProverParams;
198        let cf_cs_vp: CS2::VerifierParams;
199        if prep_param.cs_pp.is_some()
200            && prep_param.cf_cs_pp.is_some()
201            && prep_param.cs_vp.is_some()
202            && prep_param.cf_cs_vp.is_some()
203        {
204            cs_pp = prep_param.clone().cs_pp.unwrap();
205            cs_vp = prep_param.clone().cs_vp.unwrap();
206            cf_cs_pp = prep_param.clone().cf_cs_pp.unwrap();
207            cf_cs_vp = prep_param.clone().cf_cs_vp.unwrap();
208        } else {
209            (cs_pp, cs_vp) = CS1::setup(&mut rng, ccs.n - ccs.l - 1)?;
210            (cf_cs_pp, cf_cs_vp) = CS2::setup(&mut rng, cf_r1cs.A.n_cols - cf_r1cs.l - 1)?;
211        }
212
213        let pp = ProverParams::<C1, C2, CS1, CS2> {
214            poseidon_config: prep_param.poseidon_config.clone(),
215            cs_params: cs_pp.clone(),
216            cf_cs_params: cf_cs_pp.clone(),
217            ccs: Some(ccs.clone()),
218        };
219        let vp = VerifierParams::<C1, C2, CS1, CS2> {
220            poseidon_config: prep_param.poseidon_config.clone(),
221            ccs,
222            cf_r1cs,
223            cs_vp: cs_vp.clone(),
224            cf_cs_vp: cf_cs_vp.clone(),
225        };
226        Ok((pp, vp))
227    }
228
229    /// Initializes the HyperNova+CycleFold's IVC for the given parameters and initial state `z_0`.
230    fn init(
231        params: (Self::ProverParam, Self::VerifierParam),
232        F: FC,
233        z_0: Vec<C1::ScalarField>,
234    ) -> Result<Self, Error> {
235        let (pp, vp) = params;
236
237        // prepare the HyperNova's AugmentedFCircuit and CycleFold's circuits and obtain its CCS
238        // and R1CS respectively
239        let augmented_f_circuit = AugmentedFCircuit::<C1, C2, GC2, FC>::empty(
240            &pp.poseidon_config,
241            F.clone(),
242            pp.ccs.clone(),
243        )?;
244        let ccs = augmented_f_circuit.ccs.clone();
245
246        let cf_circuit = CycleFoldCircuit::<C1, GC1>::empty();
247        let cf_r1cs = get_r1cs_from_cs::<C2::ScalarField>(cf_circuit)?;
248
249        // compute the public params hash
250        let pp_hash = vp.pp_hash()?;
251
252        // setup the dummy instances
253        let W_dummy = Witness::<C1::ScalarField>::dummy(&ccs);
254        let U_dummy = LCCCS::<C1>::dummy(ccs.l, ccs.t, ccs.s);
255        let w_dummy = W_dummy.clone();
256        let mut u_dummy = CCCS::<C1>::dummy(ccs.l);
257        let (cf_W_dummy, cf_U_dummy): (NovaWitness<C2>, CommittedInstance<C2>) =
258            cf_r1cs.dummy_instance();
259        u_dummy.x = vec![
260            U_dummy.hash(
261                &pp.poseidon_config,
262                pp_hash,
263                C1::ScalarField::zero(),
264                z_0.clone(),
265                z_0.clone(),
266            )?,
267            cf_U_dummy.hash_cyclefold(&pp.poseidon_config, pp_hash)?,
268        ];
269
270        // W_dummy=W_0 is a 'dummy witness', all zeroes, but with the size corresponding to the
271        // R1CS that we're working with.
272        Ok(Self {
273            _gc1: PhantomData,
274            _c2: PhantomData,
275            _gc2: PhantomData,
276            ccs,
277            cf_r1cs,
278            poseidon_config: pp.poseidon_config.clone(),
279            cs_params: pp.cs_params.clone(),
280            cf_cs_params: pp.cf_cs_params.clone(),
281            F,
282            pp_hash,
283            i: C1::ScalarField::zero(),
284            z_0: z_0.clone(),
285            z_i: z_0,
286            W_i: W_dummy,
287            U_i: U_dummy,
288            w_i: w_dummy,
289            u_i: u_dummy,
290            // cyclefold running instance
291            cf_W_i: cf_W_dummy,
292            cf_U_i: cf_U_dummy,
293        })
294    }
295
296    /// Implements IVC.P of HyperNova+CycleFold
297    fn prove_step(
298        &mut self,
299        mut rng: impl RngCore,
300        external_inputs: Vec<C1::ScalarField>,
301    ) -> Result<(), Error> {
302        let augmented_f_circuit: AugmentedFCircuit<C1, C2, GC2, FC>;
303
304        if self.z_i.len() != self.F.state_len() {
305            return Err(Error::NotSameLength(
306                "z_i.len()".to_string(),
307                self.z_i.len(),
308                "F.state_len()".to_string(),
309                self.F.state_len(),
310            ));
311        }
312        if external_inputs.len() != self.F.external_inputs_len() {
313            return Err(Error::NotSameLength(
314                "F.external_inputs_len()".to_string(),
315                self.F.external_inputs_len(),
316                "external_inputs.len()".to_string(),
317                external_inputs.len(),
318            ));
319        }
320
321        if self.i > C1::ScalarField::from_le_bytes_mod_order(&usize::MAX.to_le_bytes()) {
322            return Err(Error::MaxStep);
323        }
324
325        let mut i_bytes: [u8; 8] = [0; 8];
326        i_bytes.copy_from_slice(&self.i.into_bigint().to_bytes_le()[..8]);
327        let i_usize: usize = usize::from_le_bytes(i_bytes);
328
329        let z_i1 = self
330            .F
331            .step_native(i_usize, self.z_i.clone(), external_inputs.clone())?;
332
333        // u_{i+1}.x[1] = H(cf_U_{i+1})
334        let cf_u_i1_x: C1::ScalarField;
335        let (U_i1, W_i1);
336
337        if self.i == C1::ScalarField::zero() {
338            W_i1 = Witness::<C1::ScalarField>::dummy(&self.ccs);
339            U_i1 = LCCCS::dummy(self.ccs.l, self.ccs.t, self.ccs.s);
340
341            let u_i1_x = U_i1.hash(
342                &self.poseidon_config,
343                self.pp_hash,
344                C1::ScalarField::one(),
345                self.z_0.clone(),
346                z_i1.clone(),
347            )?;
348
349            // hash the initial (dummy) CycleFold instance, which is used as the 2nd public
350            // input in the AugmentedFCircuit
351            cf_u_i1_x = self
352                .cf_U_i
353                .hash_cyclefold(&self.poseidon_config, self.pp_hash)?;
354
355            augmented_f_circuit = AugmentedFCircuit::<C1, C2, GC2, FC> {
356                _c2: PhantomData,
357                _gc2: PhantomData,
358                poseidon_config: self.poseidon_config.clone(),
359                ccs: self.ccs.clone(),
360                pp_hash: Some(self.pp_hash),
361                i: Some(C1::ScalarField::zero()),
362                i_usize: Some(0),
363                z_0: Some(self.z_0.clone()),
364                z_i: Some(self.z_i.clone()),
365                external_inputs: Some(external_inputs.clone()),
366                u_i_C: Some(self.u_i.C),
367                U_i: Some(self.U_i.clone()),
368                U_i1_C: Some(U_i1.C),
369                F: self.F.clone(),
370                x: Some(u_i1_x),
371                nimfs_proof: None,
372
373                // cyclefold values
374                cf_u_i_cmW: None,
375                cf_U_i: None,
376                cf_x: Some(cf_u_i1_x),
377                cf_cmT: None,
378            };
379        } else {
380            let mut transcript_p: PoseidonTranscript<C1> =
381                PoseidonTranscript::<C1>::new(&self.poseidon_config);
382            transcript_p.absorb(&self.pp_hash);
383            let (rho_bits, nimfs_proof);
384            (nimfs_proof, U_i1, W_i1, rho_bits) = NIMFS::<C1, PoseidonTranscript<C1>>::prove(
385                &mut transcript_p,
386                &self.ccs,
387                &[self.U_i.clone()],
388                &[self.u_i.clone()],
389                &[self.W_i.clone()],
390                &[self.w_i.clone()],
391            )?;
392
393            // sanity check: check the folded instance relation
394            #[cfg(test)]
395            U_i1.check_relation(&self.ccs, &W_i1)?;
396
397            let u_i1_x = U_i1.hash(
398                &self.poseidon_config,
399                self.pp_hash,
400                self.i + C1::ScalarField::one(),
401                self.z_0.clone(),
402                z_i1.clone(),
403            )?;
404
405            let rho_Fq = C2::ScalarField::from_bigint(BigInteger::from_bits_le(&rho_bits))
406                .ok_or(Error::OutOfBounds)?;
407            // CycleFold part:
408            // get the vector used as public inputs 'x' in the CycleFold circuit
409            // cyclefold circuit for cmW
410            let cf_u_i_x = [
411                vec![rho_Fq],
412                get_cm_coordinates(&self.U_i.C),
413                get_cm_coordinates(&self.u_i.C),
414                get_cm_coordinates(&U_i1.C),
415            ]
416            .concat();
417
418            let cf_circuit = CycleFoldCircuit::<C1, GC1> {
419                _gc: PhantomData,
420                r_bits: Some(rho_bits.clone()),
421                p1: Some(self.U_i.clone().C),
422                p2: Some(self.u_i.clone().C),
423                x: Some(cf_u_i_x.clone()),
424            };
425
426            let (_cf_w_i, cf_u_i, cf_W_i1, cf_U_i1, cf_cmT, _) =
427                fold_cyclefold_circuit::<C1, GC1, C2, GC2, FC, CS1, CS2>(
428                    &self.poseidon_config,
429                    self.cf_r1cs.clone(),
430                    self.cf_cs_params.clone(),
431                    self.pp_hash,
432                    self.cf_W_i.clone(), // CycleFold running instance witness
433                    self.cf_U_i.clone(), // CycleFold running instance
434                    cf_u_i_x,
435                    cf_circuit,
436                )?;
437
438            cf_u_i1_x = cf_U_i1.hash_cyclefold(&self.poseidon_config, self.pp_hash)?;
439
440            augmented_f_circuit = AugmentedFCircuit::<C1, C2, GC2, FC> {
441                _c2: PhantomData,
442                _gc2: PhantomData,
443                poseidon_config: self.poseidon_config.clone(),
444                ccs: self.ccs.clone(),
445                pp_hash: Some(self.pp_hash),
446                i: Some(self.i),
447                i_usize: Some(i_usize),
448                z_0: Some(self.z_0.clone()),
449                z_i: Some(self.z_i.clone()),
450                external_inputs: Some(external_inputs),
451                u_i_C: Some(self.u_i.C),
452                U_i: Some(self.U_i.clone()),
453                U_i1_C: Some(U_i1.C),
454                F: self.F.clone(),
455                x: Some(u_i1_x),
456                nimfs_proof: Some(nimfs_proof),
457
458                // cyclefold values
459                cf_u_i_cmW: Some(cf_u_i.cmW),
460                cf_U_i: Some(self.cf_U_i.clone()),
461                cf_x: Some(cf_u_i1_x),
462                cf_cmT: Some(cf_cmT),
463            };
464
465            // assign the next round instances
466            self.cf_W_i = cf_W_i1;
467            self.cf_U_i = cf_U_i1;
468        }
469
470        let (cs, _) = augmented_f_circuit.compute_cs_ccs()?;
471
472        #[cfg(test)]
473        assert!(cs.is_satisfied()?);
474
475        let (r1cs_w_i1, r1cs_x_i1) = extract_w_x::<C1::ScalarField>(&cs); // includes 1 and public inputs
476
477        let r1cs_z = [
478            vec![C1::ScalarField::one()],
479            r1cs_x_i1.clone(),
480            r1cs_w_i1.clone(),
481        ]
482        .concat();
483        // compute committed instances, w_{i+1}, u_{i+1}, which will be used as w_i, u_i, so we
484        // assign them directly to w_i, u_i.
485        let (u_i, w_i) = self
486            .ccs
487            .to_cccs::<_, C1, CS1>(&mut rng, &self.cs_params, &r1cs_z)?;
488        self.u_i = u_i.clone();
489        self.w_i = w_i.clone();
490
491        // set values for next iteration
492        self.i += C1::ScalarField::one();
493        // assign z_{i+1} into z_i
494        self.z_i = z_i1.clone();
495        self.U_i = U_i1.clone();
496        self.W_i = W_i1.clone();
497
498        #[cfg(test)]
499        {
500            // check the new LCCCS instance relation
501            self.U_i.check_relation(&self.ccs, &self.W_i)?;
502            // check the new CCCS instance relation
503            self.u_i.check_relation(&self.ccs, &self.w_i)?;
504        }
505
506        Ok(())
507    }
508
509    fn state(&self) -> Vec<C1::ScalarField> {
510        self.z_i.clone()
511    }
512
513    fn instances(
514        &self,
515    ) -> (
516        Self::RunningInstance,
517        Self::IncomingInstance,
518        Self::CFInstance,
519    ) {
520        (
521            (self.U_i.clone(), self.W_i.clone()),
522            (self.u_i.clone(), self.w_i.clone()),
523            (self.cf_U_i.clone(), self.cf_W_i.clone()),
524        )
525    }
526
527    /// Implements IVC.V of HyperNova+CycleFold. Notice that this method does not include the
528    /// commitments verification, which is done in the Decider.
529    fn verify(
530        vp: Self::VerifierParam,
531        z_0: Vec<C1::ScalarField>, // initial state
532        z_i: Vec<C1::ScalarField>, // last state
533        num_steps: C1::ScalarField,
534        running_instance: Self::RunningInstance,
535        incoming_instance: Self::IncomingInstance,
536        cyclefold_instance: Self::CFInstance,
537    ) -> Result<(), Error> {
538        if num_steps == C1::ScalarField::zero() {
539            if z_0 != z_i {
540                return Err(Error::IVCVerificationFail);
541            }
542            return Ok(());
543        }
544
545        let (U_i, W_i) = running_instance;
546        let (u_i, w_i) = incoming_instance;
547        let (cf_U_i, cf_W_i) = cyclefold_instance;
548        if u_i.x.len() != 2 || U_i.x.len() != 2 {
549            return Err(Error::IVCVerificationFail);
550        }
551
552        let pp_hash = vp.pp_hash()?;
553
554        // check that u_i's output points to the running instance
555        // u_i.X[0] == H(i, z_0, z_i, U_i)
556        let expected_u_i_x = U_i.hash(&vp.poseidon_config, pp_hash, num_steps, z_0, z_i.clone())?;
557        if expected_u_i_x != u_i.x[0] {
558            return Err(Error::IVCVerificationFail);
559        }
560        // u_i.X[1] == H(cf_U_i)
561        let expected_cf_u_i_x = cf_U_i.hash_cyclefold(&vp.poseidon_config, pp_hash)?;
562        if expected_cf_u_i_x != u_i.x[1] {
563            return Err(Error::IVCVerificationFail);
564        }
565
566        // check LCCCS satisfiability
567        U_i.check_relation(&vp.ccs, &W_i)?;
568        // check CCCS satisfiability
569        u_i.check_relation(&vp.ccs, &w_i)?;
570
571        // check CycleFold's RelaxedR1CS satisfiability
572        vp.cf_r1cs
573            .check_relaxed_instance_relation(&cf_W_i, &cf_U_i)?;
574
575        Ok(())
576    }
577}
578
579#[cfg(test)]
580mod tests {
581    use crate::commitment::kzg::KZG;
582    use ark_bn254::{constraints::GVar, Bn254, Fr, G1Projective as Projective};
583    use ark_grumpkin::{constraints::GVar as GVar2, Projective as Projective2};
584
585    use super::*;
586    use crate::commitment::pedersen::Pedersen;
587    use crate::frontend::tests::CubicFCircuit;
588    use crate::transcript::poseidon::poseidon_canonical_config;
589
590    #[test]
591    pub fn test_ivc() {
592        let poseidon_config = poseidon_canonical_config::<Fr>();
593
594        let F_circuit = CubicFCircuit::<Fr>::new(()).unwrap();
595
596        // run the test using Pedersen commitments on both sides of the curve cycle
597        test_ivc_opt::<Pedersen<Projective>, Pedersen<Projective2>>(
598            poseidon_config.clone(),
599            F_circuit,
600        );
601        // run the test using KZG for the commitments on the main curve, and Pedersen for the
602        // commitments on the secondary curve
603        test_ivc_opt::<KZG<Bn254>, Pedersen<Projective2>>(poseidon_config, F_circuit);
604    }
605
606    // test_ivc allowing to choose the CommitmentSchemes
607    fn test_ivc_opt<CS1: CommitmentScheme<Projective>, CS2: CommitmentScheme<Projective2>>(
608        poseidon_config: PoseidonConfig<Fr>,
609        F_circuit: CubicFCircuit<Fr>,
610    ) {
611        let mut rng = ark_std::test_rng();
612
613        type HN<CS1, CS2> =
614            HyperNova<Projective, GVar, Projective2, GVar2, CubicFCircuit<Fr>, CS1, CS2>;
615
616        let prep_param =
617            PreprocessorParam::<Projective, Projective2, CubicFCircuit<Fr>, CS1, CS2>::new(
618                poseidon_config.clone(),
619                F_circuit,
620            );
621        let (prover_params, verifier_params) = HN::preprocess(&mut rng, &prep_param).unwrap();
622
623        let z_0 = vec![Fr::from(3_u32)];
624        let mut hypernova = HN::init(
625            (prover_params, verifier_params.clone()),
626            F_circuit,
627            z_0.clone(),
628        )
629        .unwrap();
630
631        let num_steps: usize = 3;
632        for _ in 0..num_steps {
633            hypernova.prove_step(&mut rng, vec![]).unwrap();
634        }
635        assert_eq!(Fr::from(num_steps as u32), hypernova.i);
636
637        let (running_instance, incoming_instance, cyclefold_instance) = hypernova.instances();
638        HN::verify(
639            verifier_params,
640            z_0,
641            hypernova.z_i,
642            hypernova.i,
643            running_instance,
644            incoming_instance,
645            cyclefold_instance,
646        )
647        .unwrap();
648    }
649}