Booleans
Booleans are a good example of a snarky variable.
#![allow(unused)] fn main() { pub struct Boolean<F: PrimeField>(CVar<F>); impl<F> SnarkyType<F> for Boolean<F> where F: PrimeField, { type Auxiliary = (); type OutOfCircuit = bool; const SIZE_IN_FIELD_ELEMENTS: usize = 1; fn to_cvars(&self) -> (Vec<CVar<F>>, Self::Auxiliary) { (vec![self.0.clone()], ()) } fn from_cvars_unsafe(cvars: Vec<CVar<F>>, _aux: Self::Auxiliary) -> Self { assert_eq!(cvars.len(), Self::SIZE_IN_FIELD_ELEMENTS); Self(cvars[0].clone()) } fn check(&self, cs: &mut RunState<F>) { // TODO: annotation? cs.assert_(Some("boolean check"), vec![BasicSnarkyConstraint::Boolean(self.0.clone())]); } fn deserialize(&self) -> (Self::OutOfCircuit, Self::Auxiliary) { todo!() } fn serialize(out_of_circuit: Self::OutOfCircuit, aux: Self::Auxiliary) -> Self { todo!() } fn constraint_system_auxiliary() -> Self::Auxiliary { todo!() } fn value_to_field_elements(x: &Self::OutOfCircuit) -> (Vec<F>, Self::Auxiliary) { todo!() } fn value_of_field_elements(x: (Vec<F>, Self::Auxiliary)) -> Self::OutOfCircuit { todo!() } } }
Check
The check()
function is simply constraining the CVar
to be either or using the following constraint:
It is trivial to use the double generic gate for this.
And
Not