use crate::frontend::{num::AllocatedNum, ConstraintSystem, SynthesisError};
use core::marker::PhantomData;
use ff::PrimeField;
pub trait StepCircuit<F: PrimeField>: Send + Sync + Clone {
fn arity(&self) -> usize;
fn synthesize<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
z: &[AllocatedNum<F>],
) -> Result<Vec<AllocatedNum<F>>, SynthesisError>;
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TrivialCircuit<F: PrimeField> {
_p: PhantomData<F>,
}
impl<F: PrimeField> StepCircuit<F> for TrivialCircuit<F> {
fn arity(&self) -> usize {
1
}
fn synthesize<CS: ConstraintSystem<F>>(
&self,
_cs: &mut CS,
z: &[AllocatedNum<F>],
) -> Result<Vec<AllocatedNum<F>>, SynthesisError> {
Ok(z.to_vec())
}
}
#[derive(Clone, Debug, Default)]
pub struct NonTrivialCircuit<F: PrimeField> {
num_cons: usize,
_p: PhantomData<F>,
}
impl<F: PrimeField> NonTrivialCircuit<F> {
pub fn new(num_cons: usize) -> Self {
Self {
num_cons,
_p: PhantomData,
}
}
}
impl<F: PrimeField> StepCircuit<F> for NonTrivialCircuit<F> {
fn arity(&self) -> usize {
1
}
fn synthesize<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
z: &[AllocatedNum<F>],
) -> Result<Vec<AllocatedNum<F>>, SynthesisError> {
let mut x = z[0].clone();
let mut y = x.clone();
for i in 0..self.num_cons {
y = x.square(cs.namespace(|| format!("x_sq_{i}")))?;
x = y.clone();
}
Ok(vec![y])
}
}