arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/src/traits/circuit.rs
src/traits/circuit.rsRaw
 1//! This module defines traits that a step function must implement
 2use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
 3use core::marker::PhantomData;
 4use ff::PrimeField;
 5
 6/// A helper trait for a step of the incremental computation (i.e., circuit for F)
 7pub trait StepCircuit<F: PrimeField>: Send + Sync + Clone {
 8  /// Return the the number of inputs or outputs of each step
 9  /// (this method is called only at circuit synthesis time)
10  /// `synthesize` and `output` methods are expected to take as
11  /// input a vector of size equal to arity and output a vector of size equal to arity  
12  fn arity(&self) -> usize;
13
14  /// Sythesize the circuit for a computation step and return variable
15  /// that corresponds to the output of the step z_{i+1}
16  fn synthesize<CS: ConstraintSystem<F>>(
17    &self,
18    cs: &mut CS,
19    z: &[AllocatedNum<F>],
20  ) -> Result<Vec<AllocatedNum<F>>, SynthesisError>;
21
22  /// return the output of the step when provided with the step's input
23  fn output(&self, z: &[F]) -> Vec<F>;
24}
25
26/// A trivial step circuit that simply returns the input
27#[derive(Clone, Debug, Default)]
28pub struct TrivialTestCircuit<F: PrimeField> {
29  _p: PhantomData<F>,
30}
31
32impl<F> StepCircuit<F> for TrivialTestCircuit<F>
33where
34  F: PrimeField,
35{
36  fn arity(&self) -> usize {
37    1
38  }
39
40  fn synthesize<CS: ConstraintSystem<F>>(
41    &self,
42    _cs: &mut CS,
43    z: &[AllocatedNum<F>],
44  ) -> Result<Vec<AllocatedNum<F>>, SynthesisError> {
45    Ok(z.to_vec())
46  }
47
48  fn output(&self, z: &[F]) -> Vec<F> {
49    z.to_vec()
50  }
51}