sonobe/folding-schemes/src/folding/nova/mod.rs
folding-schemes/src/folding/nova/mod.rsRaw
1/// Implements the scheme described in [Nova](https://eprint.iacr.org/2021/370.pdf) and
2/// [CycleFold](https://eprint.iacr.org/2023/1192.pdf).
3use ark_crypto_primitives::{
4 crh::{poseidon::CRH, CRHScheme},
5 sponge::{poseidon::PoseidonConfig, Absorb},
6};
7use ark_ec::{AffineRepr, CurveGroup, Group};
8use ark_ff::{BigInteger, PrimeField};
9use ark_r1cs_std::{groups::GroupOpsBounds, prelude::CurveVar};
10use ark_std::fmt::Debug;
11use ark_std::{One, Zero};
12use core::marker::PhantomData;
13
14use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystem};
15
16use crate::ccs::r1cs::{extract_r1cs, extract_w_x, R1CS};
17use crate::commitment::CommitmentProver;
18use crate::folding::circuits::nonnative::point_to_nonnative_limbs;
19use crate::frontend::FCircuit;
20use crate::utils::vec::is_zero_vec;
21use crate::Error;
22use crate::FoldingScheme;
23
24pub mod circuits;
25pub mod cyclefold;
26pub mod decider_eth;
27pub mod decider_eth_circuit;
28pub mod nifs;
29pub mod traits;
30
31use circuits::{AugmentedFCircuit, ChallengeGadget, CF2};
32use cyclefold::{CycleFoldChallengeGadget, CycleFoldCircuit};
33use nifs::NIFS;
34use traits::NovaR1CS;
35
36#[cfg(test)]
37use cyclefold::CF_IO_LEN;
38
39#[derive(Debug, Clone, Eq, PartialEq)]
40pub struct CommittedInstance<C: CurveGroup> {
41 pub cmE: C,
42 pub u: C::ScalarField,
43 pub cmW: C,
44 pub x: Vec<C::ScalarField>,
45}
46
47impl<C: CurveGroup> CommittedInstance<C> {
48 pub fn dummy(io_len: usize) -> Self {
49 Self {
50 cmE: C::zero(),
51 u: C::ScalarField::zero(),
52 cmW: C::zero(),
53 x: vec![C::ScalarField::zero(); io_len],
54 }
55 }
56}
57
58impl<C: CurveGroup> CommittedInstance<C>
59where
60 <C as Group>::ScalarField: Absorb,
61 <C as ark_ec::CurveGroup>::BaseField: ark_ff::PrimeField,
62{
63 /// hash implements the committed instance hash compatible with the gadget implemented in
64 /// nova/circuits.rs::CommittedInstanceVar.hash.
65 /// Returns `H(i, z_0, z_i, U_i)`, where `i` can be `i` but also `i+1`, and `U_i` is the
66 /// `CommittedInstance`.
67 pub fn hash(
68 &self,
69 poseidon_config: &PoseidonConfig<C::ScalarField>,
70 i: C::ScalarField,
71 z_0: Vec<C::ScalarField>,
72 z_i: Vec<C::ScalarField>,
73 ) -> Result<C::ScalarField, Error> {
74 let (cmE_x, cmE_y) = point_to_nonnative_limbs::<C>(self.cmE)?;
75 let (cmW_x, cmW_y) = point_to_nonnative_limbs::<C>(self.cmW)?;
76
77 CRH::<C::ScalarField>::evaluate(
78 poseidon_config,
79 vec![
80 vec![i],
81 z_0,
82 z_i,
83 vec![self.u],
84 self.x.clone(),
85 cmE_x,
86 cmE_y,
87 cmW_x,
88 cmW_y,
89 ]
90 .concat(),
91 )
92 .map_err(|e| Error::Other(e.to_string()))
93 }
94}
95
96#[derive(Debug, Clone, Eq, PartialEq)]
97pub struct Witness<C: CurveGroup> {
98 pub E: Vec<C::ScalarField>,
99 pub rE: C::ScalarField,
100 pub W: Vec<C::ScalarField>,
101 pub rW: C::ScalarField,
102}
103
104impl<C: CurveGroup> Witness<C>
105where
106 <C as Group>::ScalarField: Absorb,
107{
108 pub fn new(w: Vec<C::ScalarField>, e_len: usize) -> Self {
109 // note: at the current version, we don't use the blinding factors and we set them to 0
110 // always.
111 Self {
112 E: vec![C::ScalarField::zero(); e_len],
113 rE: C::ScalarField::zero(),
114 W: w,
115 rW: C::ScalarField::zero(),
116 }
117 }
118 pub fn commit<CP: CommitmentProver<C>>(
119 &self,
120 params: &CP::Params,
121 x: Vec<C::ScalarField>,
122 ) -> Result<CommittedInstance<C>, Error> {
123 let mut cmE = C::zero();
124 if !is_zero_vec::<C::ScalarField>(&self.E) {
125 cmE = CP::commit(params, &self.E, &self.rE)?;
126 }
127 let cmW = CP::commit(params, &self.W, &self.rW)?;
128 Ok(CommittedInstance {
129 cmE,
130 u: C::ScalarField::one(),
131 cmW,
132 x,
133 })
134 }
135}
136
137#[derive(Debug, Clone)]
138pub struct ProverParams<C1, C2, CP1, CP2>
139where
140 C1: CurveGroup,
141 C2: CurveGroup,
142 CP1: CommitmentProver<C1>,
143 CP2: CommitmentProver<C2>,
144{
145 pub poseidon_config: PoseidonConfig<C1::ScalarField>,
146 pub cm_params: CP1::Params,
147 pub cf_cm_params: CP2::Params,
148}
149
150#[derive(Debug, Clone)]
151pub struct VerifierParams<C1: CurveGroup, C2: CurveGroup> {
152 pub poseidon_config: PoseidonConfig<C1::ScalarField>,
153 pub r1cs: R1CS<C1::ScalarField>,
154 pub cf_r1cs: R1CS<C2::ScalarField>,
155}
156
157/// Implements Nova+CycleFold's IVC, described in [Nova](https://eprint.iacr.org/2021/370.pdf) and
158/// [CycleFold](https://eprint.iacr.org/2023/1192.pdf), following the FoldingScheme trait
159#[derive(Clone, Debug)]
160pub struct Nova<C1, GC1, C2, GC2, FC, CP1, CP2>
161where
162 C1: CurveGroup,
163 GC1: CurveVar<C1, CF2<C1>>,
164 C2: CurveGroup,
165 GC2: CurveVar<C2, CF2<C2>>,
166 FC: FCircuit<C1::ScalarField>,
167 CP1: CommitmentProver<C1>,
168 CP2: CommitmentProver<C2>,
169{
170 _gc1: PhantomData<GC1>,
171 _c2: PhantomData<C2>,
172 _gc2: PhantomData<GC2>,
173 /// R1CS of the Augmented Function circuit
174 pub r1cs: R1CS<C1::ScalarField>,
175 /// R1CS of the CycleFold circuit
176 pub cf_r1cs: R1CS<C2::ScalarField>,
177 pub poseidon_config: PoseidonConfig<C1::ScalarField>,
178 /// CommitmentProver::Params over C1
179 pub cm_params: CP1::Params,
180 /// CycleFold CommitmentProver::Params, over C2
181 pub cf_cm_params: CP2::Params,
182 /// F circuit, the circuit that is being folded
183 pub F: FC,
184 pub i: C1::ScalarField,
185 /// initial state
186 pub z_0: Vec<C1::ScalarField>,
187 /// current i-th state
188 pub z_i: Vec<C1::ScalarField>,
189 /// Nova instances
190 pub w_i: Witness<C1>,
191 pub u_i: CommittedInstance<C1>,
192 pub W_i: Witness<C1>,
193 pub U_i: CommittedInstance<C1>,
194
195 /// CycleFold running instance
196 pub cf_W_i: Witness<C2>,
197 pub cf_U_i: CommittedInstance<C2>,
198}
199
200impl<C1, GC1, C2, GC2, FC, CP1, CP2> FoldingScheme<C1, C2, FC>
201 for Nova<C1, GC1, C2, GC2, FC, CP1, CP2>
202where
203 C1: CurveGroup,
204 GC1: CurveVar<C1, CF2<C1>>,
205 C2: CurveGroup,
206 GC2: CurveVar<C2, CF2<C2>>,
207 FC: FCircuit<C1::ScalarField>,
208 CP1: CommitmentProver<C1>,
209 CP2: CommitmentProver<C2>,
210 <C1 as CurveGroup>::BaseField: PrimeField,
211 <C2 as CurveGroup>::BaseField: PrimeField,
212 <C1 as Group>::ScalarField: Absorb,
213 <C2 as Group>::ScalarField: Absorb,
214 C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
215 for<'a> &'a GC1: GroupOpsBounds<'a, C1, GC1>,
216 for<'a> &'a GC2: GroupOpsBounds<'a, C2, GC2>,
217{
218 type PreprocessorParam = (Self::ProverParam, FC);
219 type ProverParam = ProverParams<C1, C2, CP1, CP2>;
220 type VerifierParam = VerifierParams<C1, C2>;
221 type CommittedInstanceWithWitness = (CommittedInstance<C1>, Witness<C1>);
222 type CFCommittedInstanceWithWitness = (CommittedInstance<C2>, Witness<C2>);
223
224 fn preprocess(
225 prep_param: &Self::PreprocessorParam,
226 ) -> Result<(Self::ProverParam, Self::VerifierParam), Error> {
227 let (prover_params, F_circuit) = prep_param;
228
229 let (r1cs, cf_r1cs) =
230 get_r1cs::<C1, GC1, C2, GC2, FC>(&prover_params.poseidon_config, *F_circuit)?;
231
232 let verifier_params = VerifierParams::<C1, C2> {
233 poseidon_config: prover_params.poseidon_config.clone(),
234 r1cs,
235 cf_r1cs,
236 };
237 Ok((prover_params.clone(), verifier_params))
238 }
239
240 /// Initializes the Nova+CycleFold's IVC for the given parameters and initial state `z_0`.
241 fn init(pp: &Self::ProverParam, F: FC, z_0: Vec<C1::ScalarField>) -> Result<Self, Error> {
242 // prepare the circuit to obtain its R1CS
243 let cs = ConstraintSystem::<C1::ScalarField>::new_ref();
244 let cs2 = ConstraintSystem::<C1::BaseField>::new_ref();
245
246 let augmented_F_circuit =
247 AugmentedFCircuit::<C1, C2, GC2, FC>::empty(&pp.poseidon_config, F);
248 let cf_circuit = CycleFoldCircuit::<C1, GC1>::empty();
249
250 augmented_F_circuit.generate_constraints(cs.clone())?;
251 cs.finalize();
252 let cs = cs.into_inner().ok_or(Error::NoInnerConstraintSystem)?;
253 let r1cs = extract_r1cs::<C1::ScalarField>(&cs);
254
255 cf_circuit.generate_constraints(cs2.clone())?;
256 cs2.finalize();
257 let cs2 = cs2.into_inner().ok_or(Error::NoInnerConstraintSystem)?;
258 let cf_r1cs = extract_r1cs::<C1::BaseField>(&cs2);
259
260 // setup the dummy instances
261 let (w_dummy, u_dummy) = r1cs.dummy_instance();
262 let (cf_w_dummy, cf_u_dummy) = cf_r1cs.dummy_instance();
263
264 // W_dummy=W_0 is a 'dummy witness', all zeroes, but with the size corresponding to the
265 // R1CS that we're working with.
266 Ok(Self {
267 _gc1: PhantomData,
268 _c2: PhantomData,
269 _gc2: PhantomData,
270 r1cs,
271 cf_r1cs,
272 poseidon_config: pp.poseidon_config.clone(),
273 cm_params: pp.cm_params.clone(),
274 cf_cm_params: pp.cf_cm_params.clone(),
275 F,
276 i: C1::ScalarField::zero(),
277 z_0: z_0.clone(),
278 z_i: z_0,
279 w_i: w_dummy.clone(),
280 u_i: u_dummy.clone(),
281 W_i: w_dummy,
282 U_i: u_dummy,
283 // cyclefold running instance
284 cf_W_i: cf_w_dummy.clone(),
285 cf_U_i: cf_u_dummy.clone(),
286 })
287 }
288
289 /// Implements IVC.P of Nova+CycleFold
290 fn prove_step(&mut self) -> Result<(), Error> {
291 let augmented_F_circuit: AugmentedFCircuit<C1, C2, GC2, FC>;
292 let cfW_circuit: CycleFoldCircuit<C1, GC1>;
293 let cfE_circuit: CycleFoldCircuit<C1, GC1>;
294
295 let z_i1 = self.F.step_native(self.z_i.clone())?;
296
297 // compute T and cmT for AugmentedFCircuit
298 let (T, cmT) = self.compute_cmT()?;
299
300 // r_bits is the r used to the RLC of the F' instances
301 let r_bits = ChallengeGadget::<C1>::get_challenge_native(
302 &self.poseidon_config,
303 self.U_i.clone(),
304 self.u_i.clone(),
305 cmT,
306 )?;
307 let r_Fr = C1::ScalarField::from_bigint(BigInteger::from_bits_le(&r_bits))
308 .ok_or(Error::OutOfBounds)?;
309
310 // fold Nova instances
311 let (W_i1, U_i1): (Witness<C1>, CommittedInstance<C1>) = NIFS::<C1, CP1>::fold_instances(
312 r_Fr, &self.W_i, &self.U_i, &self.w_i, &self.u_i, &T, cmT,
313 )?;
314
315 // folded instance output (public input, x)
316 // u_{i+1}.x = H(i+1, z_0, z_{i+1}, U_{i+1})
317 let u_i1_x = U_i1.hash(
318 &self.poseidon_config,
319 self.i + C1::ScalarField::one(),
320 self.z_0.clone(),
321 z_i1.clone(),
322 )?;
323
324 if self.i == C1::ScalarField::zero() {
325 // base case
326 augmented_F_circuit = AugmentedFCircuit::<C1, C2, GC2, FC> {
327 _gc2: PhantomData,
328 poseidon_config: self.poseidon_config.clone(),
329 i: Some(C1::ScalarField::zero()), // = i=0
330 z_0: Some(self.z_0.clone()), // = z_i
331 z_i: Some(self.z_i.clone()),
332 u_i: Some(self.u_i.clone()), // = dummy
333 U_i: Some(self.U_i.clone()), // = dummy
334 U_i1: Some(U_i1.clone()),
335 cmT: Some(cmT),
336 F: self.F,
337 x: Some(u_i1_x),
338 cf1_u_i: None,
339 cf2_u_i: None,
340 cf_U_i: None,
341 cf1_U_i1: None,
342 cf_U_i1: None,
343 cf1_cmT: None,
344 cf2_cmT: None,
345 cf1_r_nonnat: None,
346 cf2_r_nonnat: None,
347 };
348
349 #[cfg(test)]
350 NIFS::<C1, CP1>::verify_folded_instance(r_Fr, &self.U_i, &self.u_i, &U_i1, &cmT)?;
351 } else {
352 // CycleFold part:
353 // get the vector used as public inputs 'x' in the CycleFold circuit
354 // cyclefold circuit for cmW
355 let cfW_u_i_x = [
356 get_cm_coordinates(&self.U_i.cmW),
357 get_cm_coordinates(&self.u_i.cmW),
358 get_cm_coordinates(&U_i1.cmW),
359 ]
360 .concat();
361 // cyclefold circuit for cmE
362 let cfE_u_i_x = [
363 get_cm_coordinates(&self.U_i.cmE),
364 get_cm_coordinates(&self.u_i.cmE),
365 get_cm_coordinates(&U_i1.cmE),
366 ]
367 .concat();
368
369 cfW_circuit = CycleFoldCircuit::<C1, GC1> {
370 _gc: PhantomData,
371 r_bits: Some(r_bits.clone()),
372 p1: Some(self.U_i.clone().cmW),
373 p2: Some(self.u_i.clone().cmW),
374 p3: Some(U_i1.clone().cmW),
375 x: Some(cfW_u_i_x.clone()),
376 };
377 cfE_circuit = CycleFoldCircuit::<C1, GC1> {
378 _gc: PhantomData,
379 r_bits: Some(r_bits.clone()),
380 p1: Some(self.U_i.clone().cmE),
381 p2: Some(cmT),
382 p3: Some(U_i1.clone().cmE),
383 x: Some(cfE_u_i_x.clone()),
384 };
385
386 // fold self.cf_U_i + cfW_U -> folded running with cfW
387 let (_cfW_w_i, cfW_u_i, cfW_W_i1, cfW_U_i1, cfW_cmT, cfW_r1_Fq) = self
388 .fold_cyclefold_circuit(
389 self.cf_W_i.clone(), // CycleFold running instance witness
390 self.cf_U_i.clone(), // CycleFold running instance
391 cfW_u_i_x,
392 cfW_circuit,
393 )?;
394 // fold [the output from folding self.cf_U_i + cfW_U] + cfE_U = folded_running_with_cfW + cfE
395 let (_cfE_w_i, cfE_u_i, cf_W_i1, cf_U_i1, cf_cmT, cf_r2_Fq) =
396 self.fold_cyclefold_circuit(cfW_W_i1, cfW_U_i1.clone(), cfE_u_i_x, cfE_circuit)?;
397
398 augmented_F_circuit = AugmentedFCircuit::<C1, C2, GC2, FC> {
399 _gc2: PhantomData,
400 poseidon_config: self.poseidon_config.clone(),
401 i: Some(self.i),
402 z_0: Some(self.z_0.clone()),
403 z_i: Some(self.z_i.clone()),
404 u_i: Some(self.u_i.clone()),
405 U_i: Some(self.U_i.clone()),
406 U_i1: Some(U_i1.clone()),
407 cmT: Some(cmT),
408 F: self.F,
409 x: Some(u_i1_x),
410 // cyclefold values
411 cf1_u_i: Some(cfW_u_i.clone()),
412 cf2_u_i: Some(cfE_u_i.clone()),
413 cf_U_i: Some(self.cf_U_i.clone()),
414 cf1_U_i1: Some(cfW_U_i1.clone()),
415 cf_U_i1: Some(cf_U_i1.clone()),
416 cf1_cmT: Some(cfW_cmT),
417 cf2_cmT: Some(cf_cmT),
418 cf1_r_nonnat: Some(cfW_r1_Fq),
419 cf2_r_nonnat: Some(cf_r2_Fq),
420 };
421
422 self.cf_W_i = cf_W_i1.clone();
423 self.cf_U_i = cf_U_i1.clone();
424
425 #[cfg(test)]
426 {
427 self.cf_r1cs.check_instance_relation(&_cfW_w_i, &cfW_u_i)?;
428 self.cf_r1cs.check_instance_relation(&_cfE_w_i, &cfE_u_i)?;
429 self.cf_r1cs
430 .check_relaxed_instance_relation(&self.cf_W_i, &self.cf_U_i)?;
431 }
432 }
433
434 let cs = ConstraintSystem::<C1::ScalarField>::new_ref();
435
436 augmented_F_circuit.generate_constraints(cs.clone())?;
437
438 let cs = cs.into_inner().ok_or(Error::NoInnerConstraintSystem)?;
439 let (w_i1, x_i1) = extract_w_x::<C1::ScalarField>(&cs);
440 if x_i1[0] != u_i1_x {
441 return Err(Error::NotEqual);
442 }
443
444 #[cfg(test)]
445 if x_i1.len() != 1 {
446 return Err(Error::NotExpectedLength(x_i1.len(), 1));
447 }
448
449 // set values for next iteration
450 self.i += C1::ScalarField::one();
451 self.z_i = z_i1.clone();
452 self.w_i = Witness::<C1>::new(w_i1, self.r1cs.A.n_rows);
453 self.u_i = self.w_i.commit::<CP1>(&self.cm_params, vec![u_i1_x])?;
454 self.W_i = W_i1.clone();
455 self.U_i = U_i1.clone();
456
457 #[cfg(test)]
458 {
459 self.r1cs.check_instance_relation(&self.w_i, &self.u_i)?;
460 self.r1cs
461 .check_relaxed_instance_relation(&self.W_i, &self.U_i)?;
462 }
463
464 Ok(())
465 }
466
467 fn state(&self) -> Vec<C1::ScalarField> {
468 self.z_i.clone()
469 }
470 fn instances(
471 &self,
472 ) -> (
473 Self::CommittedInstanceWithWitness,
474 Self::CommittedInstanceWithWitness,
475 Self::CFCommittedInstanceWithWitness,
476 ) {
477 (
478 (self.U_i.clone(), self.W_i.clone()),
479 (self.u_i.clone(), self.w_i.clone()),
480 (self.cf_U_i.clone(), self.cf_W_i.clone()),
481 )
482 }
483
484 /// Implements IVC.V of Nova+CycleFold
485 fn verify(
486 vp: Self::VerifierParam,
487 z_0: Vec<C1::ScalarField>, // initial state
488 z_i: Vec<C1::ScalarField>, // last state
489 num_steps: C1::ScalarField,
490 running_instance: Self::CommittedInstanceWithWitness,
491 incoming_instance: Self::CommittedInstanceWithWitness,
492 cyclefold_instance: Self::CFCommittedInstanceWithWitness,
493 ) -> Result<(), Error> {
494 let (U_i, W_i) = running_instance;
495 let (u_i, w_i) = incoming_instance;
496 let (cf_U_i, cf_W_i) = cyclefold_instance;
497
498 if u_i.x.len() != 1 || U_i.x.len() != 1 {
499 return Err(Error::IVCVerificationFail);
500 }
501
502 // check that u_i's output points to the running instance
503 // u_i.X == H(i, z_0, z_i, U_i)
504 let expected_u_i_x = U_i.hash(&vp.poseidon_config, num_steps, z_0, z_i.clone())?;
505 if expected_u_i_x != u_i.x[0] {
506 return Err(Error::IVCVerificationFail);
507 }
508
509 // check u_i.cmE==0, u_i.u==1 (=u_i is a un-relaxed instance)
510 if !u_i.cmE.is_zero() || !u_i.u.is_one() {
511 return Err(Error::IVCVerificationFail);
512 }
513
514 // check R1CS satisfiability
515 vp.r1cs.check_instance_relation(&w_i, &u_i)?;
516 // check RelaxedR1CS satisfiability
517 vp.r1cs.check_relaxed_instance_relation(&W_i, &U_i)?;
518
519 // check CycleFold RelaxedR1CS satisfiability
520 vp.cf_r1cs
521 .check_relaxed_instance_relation(&cf_W_i, &cf_U_i)?;
522
523 Ok(())
524 }
525}
526
527impl<C1, GC1, C2, GC2, FC, CP1, CP2> Nova<C1, GC1, C2, GC2, FC, CP1, CP2>
528where
529 C1: CurveGroup,
530 GC1: CurveVar<C1, CF2<C1>>,
531 C2: CurveGroup,
532 GC2: CurveVar<C2, CF2<C2>>,
533 FC: FCircuit<C1::ScalarField>,
534 CP1: CommitmentProver<C1>,
535 CP2: CommitmentProver<C2>,
536 <C2 as CurveGroup>::BaseField: PrimeField,
537 <C1 as Group>::ScalarField: Absorb,
538 <C2 as Group>::ScalarField: Absorb,
539 C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
540{
541 // computes T and cmT for the AugmentedFCircuit
542 fn compute_cmT(&self) -> Result<(Vec<C1::ScalarField>, C1), Error> {
543 NIFS::<C1, CP1>::compute_cmT(
544 &self.cm_params,
545 &self.r1cs,
546 &self.w_i,
547 &self.u_i,
548 &self.W_i,
549 &self.U_i,
550 )
551 }
552 // computes T* and cmT* for the CycleFoldCircuit
553 fn compute_cf_cmT(
554 &self,
555 cf_w_i: &Witness<C2>,
556 cf_u_i: &CommittedInstance<C2>,
557 cf_W_i: &Witness<C2>,
558 cf_U_i: &CommittedInstance<C2>,
559 ) -> Result<(Vec<C2::ScalarField>, C2), Error> {
560 NIFS::<C2, CP2>::compute_cyclefold_cmT(
561 &self.cf_cm_params,
562 &self.cf_r1cs,
563 cf_w_i,
564 cf_u_i,
565 cf_W_i,
566 cf_U_i,
567 )
568 }
569}
570
571impl<C1, GC1, C2, GC2, FC, CP1, CP2> Nova<C1, GC1, C2, GC2, FC, CP1, CP2>
572where
573 C1: CurveGroup,
574 GC1: CurveVar<C1, CF2<C1>>,
575 C2: CurveGroup,
576 GC2: CurveVar<C2, CF2<C2>>,
577 FC: FCircuit<C1::ScalarField>,
578 CP1: CommitmentProver<C1>,
579 CP2: CommitmentProver<C2>,
580 <C1 as CurveGroup>::BaseField: PrimeField,
581 <C2 as CurveGroup>::BaseField: PrimeField,
582 <C1 as Group>::ScalarField: Absorb,
583 <C2 as Group>::ScalarField: Absorb,
584 C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
585 for<'a> &'a GC1: GroupOpsBounds<'a, C1, GC1>,
586 for<'a> &'a GC2: GroupOpsBounds<'a, C2, GC2>,
587{
588 // folds the given cyclefold circuit and its instances
589 #[allow(clippy::type_complexity)]
590 fn fold_cyclefold_circuit(
591 &self,
592 cf_W_i: Witness<C2>, // witness of the running instance
593 cf_U_i: CommittedInstance<C2>, // running instance
594 cf_u_i_x: Vec<C2::ScalarField>,
595 cf_circuit: CycleFoldCircuit<C1, GC1>,
596 ) -> Result<
597 (
598 Witness<C2>,
599 CommittedInstance<C2>, // u_i
600 Witness<C2>, // W_i1
601 CommittedInstance<C2>, // U_i1
602 C2, // cmT
603 C2::ScalarField, // r_Fq
604 ),
605 Error,
606 > {
607 let cs2 = ConstraintSystem::<C1::BaseField>::new_ref();
608 cf_circuit.generate_constraints(cs2.clone())?;
609
610 let cs2 = cs2.into_inner().ok_or(Error::NoInnerConstraintSystem)?;
611 let (cf_w_i, cf_x_i) = extract_w_x::<C1::BaseField>(&cs2);
612 if cf_x_i != cf_u_i_x {
613 return Err(Error::NotEqual);
614 }
615
616 #[cfg(test)]
617 if cf_x_i.len() != CF_IO_LEN {
618 return Err(Error::NotExpectedLength(cf_x_i.len(), CF_IO_LEN));
619 }
620
621 // fold cyclefold instances
622 let cf_w_i = Witness::<C2>::new(cf_w_i.clone(), self.cf_r1cs.A.n_rows);
623 let cf_u_i: CommittedInstance<C2> =
624 cf_w_i.commit::<CP2>(&self.cf_cm_params, cf_x_i.clone())?;
625
626 // compute T* and cmT* for CycleFoldCircuit
627 let (cf_T, cf_cmT) = self.compute_cf_cmT(&cf_w_i, &cf_u_i, &cf_W_i, &cf_U_i)?;
628
629 let cf_r_bits = CycleFoldChallengeGadget::<C2, GC2>::get_challenge_native(
630 &self.poseidon_config,
631 cf_U_i.clone(),
632 cf_u_i.clone(),
633 cf_cmT,
634 )?;
635 let cf_r_Fq = C1::BaseField::from_bigint(BigInteger::from_bits_le(&cf_r_bits))
636 .ok_or(Error::OutOfBounds)?;
637
638 let (cf_W_i1, cf_U_i1) = NIFS::<C2, CP2>::fold_instances(
639 cf_r_Fq, &cf_W_i, &cf_U_i, &cf_w_i, &cf_u_i, &cf_T, cf_cmT,
640 )?;
641 Ok((cf_w_i, cf_u_i, cf_W_i1, cf_U_i1, cf_cmT, cf_r_Fq))
642 }
643}
644
645/// helper method to get the r1cs from the ConstraintSynthesizer
646pub fn get_r1cs_from_cs<F: PrimeField>(
647 circuit: impl ConstraintSynthesizer<F>,
648) -> Result<R1CS<F>, Error> {
649 let cs = ConstraintSystem::<F>::new_ref();
650 circuit.generate_constraints(cs.clone())?;
651 cs.finalize();
652 let cs = cs.into_inner().ok_or(Error::NoInnerConstraintSystem)?;
653 let r1cs = extract_r1cs::<F>(&cs);
654 Ok(r1cs)
655}
656
657/// helper method to get the R1CS for both the AugmentedFCircuit and the CycleFold circuit
658#[allow(clippy::type_complexity)]
659pub fn get_r1cs<C1, GC1, C2, GC2, FC>(
660 poseidon_config: &PoseidonConfig<C1::ScalarField>,
661 F_circuit: FC,
662) -> Result<(R1CS<C1::ScalarField>, R1CS<C2::ScalarField>), Error>
663where
664 C1: CurveGroup,
665 GC1: CurveVar<C1, CF2<C1>>,
666 C2: CurveGroup,
667 GC2: CurveVar<C2, CF2<C2>>,
668 FC: FCircuit<C1::ScalarField>,
669 <C1 as CurveGroup>::BaseField: PrimeField,
670 <C2 as CurveGroup>::BaseField: PrimeField,
671 <C1 as Group>::ScalarField: Absorb,
672 <C2 as Group>::ScalarField: Absorb,
673 C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
674 for<'a> &'a GC1: GroupOpsBounds<'a, C1, GC1>,
675 for<'a> &'a GC2: GroupOpsBounds<'a, C2, GC2>,
676{
677 let augmented_F_circuit =
678 AugmentedFCircuit::<C1, C2, GC2, FC>::empty(poseidon_config, F_circuit);
679 let cf_circuit = CycleFoldCircuit::<C1, GC1>::empty();
680 let r1cs = get_r1cs_from_cs::<C1::ScalarField>(augmented_F_circuit)?;
681 let cf_r1cs = get_r1cs_from_cs::<C2::ScalarField>(cf_circuit)?;
682 Ok((r1cs, cf_r1cs))
683}
684
685/// helper method to get the pedersen params length for both the AugmentedFCircuit and the
686/// CycleFold circuit
687pub fn get_pedersen_params_len<C1, GC1, C2, GC2, FC>(
688 poseidon_config: &PoseidonConfig<C1::ScalarField>,
689 F_circuit: FC,
690) -> Result<(usize, usize), Error>
691where
692 C1: CurveGroup,
693 GC1: CurveVar<C1, CF2<C1>>,
694 C2: CurveGroup,
695 GC2: CurveVar<C2, CF2<C2>>,
696 FC: FCircuit<C1::ScalarField>,
697 <C1 as CurveGroup>::BaseField: PrimeField,
698 <C2 as CurveGroup>::BaseField: PrimeField,
699 <C1 as Group>::ScalarField: Absorb,
700 <C2 as Group>::ScalarField: Absorb,
701 C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
702 for<'a> &'a GC1: GroupOpsBounds<'a, C1, GC1>,
703 for<'a> &'a GC2: GroupOpsBounds<'a, C2, GC2>,
704{
705 let (r1cs, cf_r1cs) = get_r1cs::<C1, GC1, C2, GC2, FC>(poseidon_config, F_circuit)?;
706 Ok((r1cs.A.n_rows, cf_r1cs.A.n_rows))
707}
708
709pub(crate) fn get_cm_coordinates<C: CurveGroup>(cm: &C) -> Vec<C::BaseField> {
710 let zero = (&C::BaseField::zero(), &C::BaseField::one());
711 let cm = cm.into_affine();
712 let (cm_x, cm_y) = cm.xy().unwrap_or(zero);
713 vec![*cm_x, *cm_y]
714}
715
716#[cfg(test)]
717pub mod tests {
718 use super::*;
719 use ark_pallas::{constraints::GVar, Fr, Projective};
720 use ark_vesta::{constraints::GVar as GVar2, Projective as Projective2};
721
722 use crate::commitment::pedersen::Pedersen;
723 use crate::frontend::tests::CubicFCircuit;
724 use crate::transcript::poseidon::poseidon_test_config;
725
726 /// This test tests the Nova+CycleFold IVC, and by consequence it is also testing the
727 /// AugmentedFCircuit
728 #[test]
729 fn test_ivc() {
730 type NOVA = Nova<
731 Projective,
732 GVar,
733 Projective2,
734 GVar2,
735 CubicFCircuit<Fr>,
736 Pedersen<Projective>,
737 Pedersen<Projective2>,
738 >;
739
740 let mut rng = ark_std::test_rng();
741 let poseidon_config = poseidon_test_config::<Fr>();
742
743 let F_circuit = CubicFCircuit::<Fr>::new(());
744 let z_0 = vec![Fr::from(3_u32)];
745
746 let (cm_len, cf_cm_len) =
747 get_pedersen_params_len::<Projective, GVar, Projective2, GVar2, CubicFCircuit<Fr>>(
748 &poseidon_config,
749 F_circuit,
750 )
751 .unwrap();
752 let pedersen_params = Pedersen::<Projective>::new_params(&mut rng, cm_len);
753 let cf_pedersen_params = Pedersen::<Projective2>::new_params(&mut rng, cf_cm_len);
754
755 let prover_params =
756 ProverParams::<Projective, Projective2, Pedersen<Projective>, Pedersen<Projective2>> {
757 poseidon_config: poseidon_config.clone(),
758 cm_params: pedersen_params,
759 cf_cm_params: cf_pedersen_params,
760 };
761
762 let mut nova = NOVA::init(&prover_params, F_circuit, z_0.clone()).unwrap();
763
764 let num_steps: usize = 3;
765 for _ in 0..num_steps {
766 nova.prove_step().unwrap();
767 }
768 assert_eq!(Fr::from(num_steps as u32), nova.i);
769
770 let verifier_params = VerifierParams::<Projective, Projective2> {
771 poseidon_config,
772 r1cs: nova.clone().r1cs,
773 cf_r1cs: nova.clone().cf_r1cs,
774 };
775 let (running_instance, incoming_instance, cyclefold_instance) = nova.instances();
776 NOVA::verify(
777 verifier_params,
778 z_0,
779 nova.z_i,
780 nova.i,
781 running_instance,
782 incoming_instance,
783 cyclefold_instance,
784 )
785 .unwrap();
786 }
787}