use crate::{errors::SynthesisError, Index, LinearCombination, Namespace, Variable};
use snarkvm_fields::Field;
use std::marker::PhantomData;
pub trait ConstraintSynthesizer<F: Field>: Sync {
fn generate_constraints<CS: ConstraintSystem<F>>(&self, cs: &mut CS) -> Result<(), SynthesisError>;
}
pub trait ConstraintSystem<F: Field>: Sized {
type Root: ConstraintSystem<F>;
fn one() -> Variable {
Variable::new_unchecked(Index::Public(0))
}
fn alloc<FN, A, AR>(&mut self, annotation: A, f: FN) -> Result<Variable, SynthesisError>
where
FN: FnOnce() -> Result<F, SynthesisError>,
A: FnOnce() -> AR,
AR: AsRef<str>;
fn alloc_input<FN, A, AR>(&mut self, annotation: A, f: FN) -> Result<Variable, SynthesisError>
where
FN: FnOnce() -> Result<F, SynthesisError>,
A: FnOnce() -> AR,
AR: AsRef<str>;
fn enforce<A, AR, LA, LB, LC>(&mut self, annotation: A, a: LA, b: LB, c: LC)
where
A: FnOnce() -> AR,
AR: AsRef<str>,
LA: FnOnce(LinearCombination<F>) -> LinearCombination<F>,
LB: FnOnce(LinearCombination<F>) -> LinearCombination<F>,
LC: FnOnce(LinearCombination<F>) -> LinearCombination<F>;
fn push_namespace<NR, N>(&mut self, name_fn: N)
where
NR: AsRef<str>,
N: FnOnce() -> NR;
fn pop_namespace(&mut self);
fn get_root(&mut self) -> &mut Self::Root;
fn ns<NR, N>(&mut self, name_fn: N) -> Namespace<'_, F, Self::Root>
where
NR: AsRef<str>,
N: FnOnce() -> NR,
{
self.get_root().push_namespace(name_fn);
Namespace(self.get_root(), PhantomData)
}
fn num_constraints(&self) -> usize;
fn num_public_variables(&self) -> usize;
fn num_private_variables(&self) -> usize;
fn is_in_setup_mode(&self) -> bool;
}
impl<F: Field, CS: ConstraintSystem<F>> ConstraintSystem<F> for &mut CS {
type Root = CS::Root;
#[inline]
fn one() -> Variable {
CS::one()
}
#[inline]
fn alloc<FN, A, AR>(&mut self, annotation: A, f: FN) -> Result<Variable, SynthesisError>
where
FN: FnOnce() -> Result<F, SynthesisError>,
A: FnOnce() -> AR,
AR: AsRef<str>,
{
(**self).alloc(annotation, f)
}
#[inline]
fn alloc_input<FN, A, AR>(&mut self, annotation: A, f: FN) -> Result<Variable, SynthesisError>
where
FN: FnOnce() -> Result<F, SynthesisError>,
A: FnOnce() -> AR,
AR: AsRef<str>,
{
(**self).alloc_input(annotation, f)
}
#[inline]
fn enforce<A, AR, LA, LB, LC>(&mut self, annotation: A, a: LA, b: LB, c: LC)
where
A: FnOnce() -> AR,
AR: AsRef<str>,
LA: FnOnce(LinearCombination<F>) -> LinearCombination<F>,
LB: FnOnce(LinearCombination<F>) -> LinearCombination<F>,
LC: FnOnce(LinearCombination<F>) -> LinearCombination<F>,
{
(**self).enforce(annotation, a, b, c)
}
#[inline]
fn push_namespace<NR, N>(&mut self, name_fn: N)
where
NR: AsRef<str>,
N: FnOnce() -> NR,
{
(**self).push_namespace(name_fn)
}
#[inline]
fn pop_namespace(&mut self) {
(**self).pop_namespace()
}
#[inline]
fn get_root(&mut self) -> &mut Self::Root {
(**self).get_root()
}
#[inline]
fn num_constraints(&self) -> usize {
(**self).num_constraints()
}
#[inline]
fn num_public_variables(&self) -> usize {
(**self).num_public_variables()
}
#[inline]
fn num_private_variables(&self) -> usize {
(**self).num_private_variables()
}
#[inline]
fn is_in_setup_mode(&self) -> bool {
(**self).is_in_setup_mode()
}
}