1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
use bellperson::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable}; use ff::Field; use paired::Engine; use std::cmp::Ordering; #[derive(Clone, Copy)] struct OrderedVariable(Variable); impl Eq for OrderedVariable {} impl PartialEq for OrderedVariable { fn eq(&self, other: &OrderedVariable) -> bool { match (self.0.get_unchecked(), other.0.get_unchecked()) { (Index::Input(ref a), Index::Input(ref b)) => a == b, (Index::Aux(ref a), Index::Aux(ref b)) => a == b, _ => false, } } } impl PartialOrd for OrderedVariable { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) } } impl Ord for OrderedVariable { fn cmp(&self, other: &Self) -> Ordering { match (self.0.get_unchecked(), other.0.get_unchecked()) { (Index::Input(ref a), Index::Input(ref b)) => a.cmp(b), (Index::Aux(ref a), Index::Aux(ref b)) => a.cmp(b), (Index::Input(_), Index::Aux(_)) => Ordering::Less, (Index::Aux(_), Index::Input(_)) => Ordering::Greater, } } } fn eval_lc<E: Engine>(terms: &[(Variable, E::Fr)], inputs: &[E::Fr], aux: &[E::Fr]) -> E::Fr { let mut acc = E::Fr::zero(); for &(var, ref coeff) in terms { let mut tmp = match var.get_unchecked() { Index::Input(index) => inputs[index], Index::Aux(index) => aux[index], }; tmp.mul_assign(&coeff); acc.add_assign(&tmp); } acc } #[derive(Debug)] pub struct BenchCS<E: Engine> { inputs: Vec<E::Fr>, aux: Vec<E::Fr>, a: Vec<E::Fr>, b: Vec<E::Fr>, c: Vec<E::Fr>, } impl<E: Engine> BenchCS<E> { pub fn new() -> Self { BenchCS::default() } pub fn num_constraints(&self) -> usize { self.a.len() } } impl<E: Engine> Default for BenchCS<E> { fn default() -> Self { BenchCS { inputs: vec![E::Fr::one()], aux: vec![], a: vec![], b: vec![], c: vec![], } } } impl<E: Engine> ConstraintSystem<E> for BenchCS<E> { type Root = Self; fn alloc<F, A, AR>(&mut self, _: A, f: F) -> Result<Variable, SynthesisError> where F: FnOnce() -> Result<E::Fr, SynthesisError>, A: FnOnce() -> AR, AR: Into<String>, { self.aux.push(f()?); Ok(Variable::new_unchecked(Index::Aux(self.aux.len() - 1))) } fn alloc_input<F, A, AR>(&mut self, _: A, f: F) -> Result<Variable, SynthesisError> where F: FnOnce() -> Result<E::Fr, SynthesisError>, A: FnOnce() -> AR, AR: Into<String>, { self.inputs.push(f()?); Ok(Variable::new_unchecked(Index::Input(self.inputs.len() - 1))) } fn enforce<A, AR, LA, LB, LC>(&mut self, _: A, a: LA, b: LB, c: LC) where A: FnOnce() -> AR, AR: Into<String>, LA: FnOnce(LinearCombination<E>) -> LinearCombination<E>, LB: FnOnce(LinearCombination<E>) -> LinearCombination<E>, LC: FnOnce(LinearCombination<E>) -> LinearCombination<E>, { self.a.push(eval_lc::<E>( a(LinearCombination::zero()).as_ref(), &self.inputs, &self.aux, )); self.b.push(eval_lc::<E>( b(LinearCombination::zero()).as_ref(), &self.inputs, &self.aux, )); self.c.push(eval_lc::<E>( c(LinearCombination::zero()).as_ref(), &self.inputs, &self.aux, )); } fn push_namespace<NR, N>(&mut self, _: N) where NR: Into<String>, N: FnOnce() -> NR, { } fn pop_namespace(&mut self) {} fn get_root(&mut self) -> &mut Self::Root { self } }