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 FS = Nova<G1, G2, SHA256FCircuit<Fr>, Pedersen<G1>, Pedersen<G2>, false>;
10
11let num_steps = 10;
12let initial_state = vec![Fr::from(1_u32)];
13
14let F_circuit = Sha256FCircuit::<Fr>::new(());
15
16let poseidon_config = poseidon_canonical_config::<Fr>();
17let mut rng = rand::rngs::OsRng;
18
19println!("Prepare Nova ProverParams & VerifierParams");
20let nova_preprocess_params = PreprocessorParam::new(poseidon_config, F_circuit);
21let nova_params = FS::preprocess(&mut rng, &nova_preprocess_params)?;
22
23
24println!("Initialize FoldingScheme");
25let mut folding_scheme = FS::init(&nova_params, F_circuit, initial_state.clone())?;
26
27
28// compute a step of the IVC
29for i in 0..num_steps {
30    let start = Instant::now();
31    // - 2nd parameter: here we pass an empty vec since the FCircuit that we're
32    // using does not use external_inputs
33    // - 3rd parameter: is for schemes that support folding more than 2
34    // instances at each fold, such as HyperNova. Since we're using Nova, we just
35    // set it to 'None'
36    folding_scheme.prove_step(rng, (), None)?;
37    println!("Nova::prove_step {}: {:?}", i, start.elapsed());
38}
39
40let ivc_proof: FS::IVCProof = folding_scheme.ivc_proof();
41
42println!("Run the Nova's IVC verifier");
43FS::verify(nova_params.1, ivc_proof)?;
44```
45
46<br>
47
48Observe that we define the folding scheme to be used with the following line:
49```rust
50type FS = Nova<G1, G2, FC, Pedersen<G1>, Pedersen<G2>, false>;
51```
52
53(where `FC` is the `FCircuit` that we want to fold).
54
55Now imagine that we want to switch the folding scheme being used. Is as simple as replacing `FS` by:
56
57```rust
58type FS = HyperNova< G1, G2, FC, Pedersen<G1>, Pedersen<G2>, 1, 1, false>;
59```
60and then adapting the `folding_scheme.prove_step(...)` call accordingly.
61
62Similarly for using ProtoGalaxy folding schemes:
63```rust
64type FS = ProtoGalaxy<G1, G2, FC, Pedersen<G1>, Pedersen<G2>>;
65```
66
67
68<br><br>
69
70As 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).