arnaucube
arnaucube/sonobe-docsPublic Self-hosted repository
sonobe-docs/src/usage/frontend.md
src/usage/frontend.mdRaw
 1# Frontend
 2
 3The frontend interface allows to define the circuit to be folded. The currently available frontends are [circom](https://github.com/iden3/circom) and [arkworks](https://github.com/arkworks-rs/r1cs-std). We will show here how to define a circuit using `arkworks`.
 4
 5# The `FCircuit` trait
 6
 7To be folded with sonobe, a circuit needs to implement the [`FCircuit` trait](https://github.com/privacy-scaling-explorations/sonobe/blob/main/sonobe/src/frontend/mod.rs). This trait defines the methods that sonobe expects from the circuit to be folded. It corresponds to the $F$ function that is being folded. The trait has the following methods:
 8
 9```rust
10/// FCircuit defines the trait of the circuit of the F function, which is the one being folded (ie.
11/// inside the agmented F' function).
12/// The parameter z_i denotes the current state, and z_{i+1} denotes the next state after applying
13/// the step.
14pub trait FCircuit<F: PrimeField>: Clone + Debug {
15    type Params: Debug;
16
17    /// Returns a new FCircuit instance
18    fn new(params: Self::Params) -> Self;
19
20    /// Returns the number of elements in the state of the FCircuit, which corresponds to the
21    /// FCircuit inputs.
22    fn state_len(&self) -> usize;
23    
24    /// returns the number of elements in the external inputs used by the FCircuit. External inputs
25    /// are optional, and in case no external inputs are used, this method should return 0.
26    fn external_inputs_len(&self) -> usize;
27
28    /// Computes the next state values in place, assigning z_{i+1} into z_i, and computing the new
29    /// z_{i+1}
30    fn step_native(
31        // this method uses self, so that each FCircuit implementation (and different frontends)
32        // can hold a state if needed to store data to compute the next state.
33        &self,
34        i: usize,
35        z_i: Vec<F>,
36        external_inputs: Vec<F>, // inputs that are not part of the state
37    ) -> Result<Vec<F>, Error>;
38
39    /// Generates the constraints for the step of F for the given z_i
40    fn generate_step_constraints(
41        // this method uses self, so that each FCircuit implementation (and different frontends)
42        // can hold a state if needed to store data to generate the constraints.
43        &self,
44        cs: ConstraintSystemRef<F>,
45        i: usize,
46        z_i: Vec<FpVar<F>>,
47        external_inputs: Vec<FpVar<F>>, // inputs that are not part of the state
48    ) -> Result<Vec<FpVar<F>>, SynthesisError>;
49}
50```
51