arnaucube
arnaucube/sonobe-docsPublic Self-hosted repository
sonobe-docs/src/usage/fold.md
src/usage/fold.mdRaw
 1# Fold
 2
 3We plug our `FCircuit` into the library:
 4
 5```rust
 6// The idea here is that we could replace the next line chunk that defines the
 7// `type NOVA = Nova<...>` by using another folding scheme that fulfills the `FoldingScheme`
 8// trait, and the rest of our code would be working without needing to be updated.
 9type F = Nova<
10    Projective,
11    GVar,
12    Projective2,
13    GVar2,
14    Sha256FCircuit<Fr>,
15    KZG<'static, Bn254>,
16    Pedersen<Projective2>,
17    false,
18>;
19
20let num_steps = 10;
21let initial_state = vec![Fr::from(1_u32)];
22
23let F_circuit = Sha256FCircuit::<Fr>::new(());
24
25let poseidon_config = poseidon_canonical_config::<Fr>();
26let mut rng = rand::rngs::OsRng;
27
28println!("Prepare Nova ProverParams & VerifierParams");
29let nova_preprocess_params = PreprocessorParam::new(poseidon_config, F_circuit);
30let nova_params = FS::preprocess(&mut rng, &nova_preprocess_params).unwrap();
31
32
33println!("Initialize FoldingScheme");
34let mut folding_scheme = FS::init(&nova_params, F_circuit, initial_state.clone()).unwrap();
35
36
37// compute a step of the IVC
38for i in 0..num_steps {
39    let start = Instant::now();
40    // - 2nd parameter: here we pass an empty vec since the FCircuit that we're
41    // using does not use external_inputs
42    // - 3rd parameter: is for schemes that support folding more than 2
43    // instances at each fold, such as HyperNova. Since we're using Nova, we just
44    // set it to 'None'
45    folding_scheme.prove_step(rng, vec![], None).unwrap();
46    println!("Nova::prove_step {}: {:?}", i, start.elapsed());
47}
48
49let (running_instance, incoming_instance, cyclefold_instance) = folding_scheme.instances();
50
51println!("Run the Nova's IVC verifier");
52FS::verify(
53    nova_params.1,
54    initial_state,
55    folding_scheme.state(), // latest state
56    Fr::from(num_steps as u32),
57    running_instance,
58    incoming_instance,
59    cyclefold_instance,
60)
61.unwrap();
62```
63
64<br>
65
66Now imagine that we want to switch the folding scheme being used. Is as simple as replacing `FS` by:
67
68```rust
69type FS = HyperNova<
70    Projective,
71    GVar,
72    Projective2,
73    GVar2,
74    CubicFCircuit<Fr>,
75    KZG<'static, Bn254>,
76    Pedersen<Projective2>,
77    1, 1, false,
78>; 
79```
80and then adapting the `folding_scheme.prove_step(...)` call accordingly.
81
82
83<br><br>
84
85As in the previous sections, you can find a full examples with all the code at [sonobe/examples](https://github.com/privacy-scaling-explorations/sonobe/tree/main/examples).