bellman/gadgets/
multieq.rs1use ff::PrimeField;
2
3use crate::{ConstraintSystem, LinearCombination, SynthesisError, Variable};
4
5pub struct MultiEq<Scalar: PrimeField, CS: ConstraintSystem<Scalar>> {
6 cs: CS,
7 ops: usize,
8 bits_used: usize,
9 lhs: LinearCombination<Scalar>,
10 rhs: LinearCombination<Scalar>,
11}
12
13impl<Scalar: PrimeField, CS: ConstraintSystem<Scalar>> MultiEq<Scalar, CS> {
14 pub fn new(cs: CS) -> Self {
15 MultiEq {
16 cs,
17 ops: 0,
18 bits_used: 0,
19 lhs: LinearCombination::zero(),
20 rhs: LinearCombination::zero(),
21 }
22 }
23
24 fn accumulate(&mut self) {
25 let ops = self.ops;
26 let lhs = self.lhs.clone();
27 let rhs = self.rhs.clone();
28 self.cs.enforce(
29 || format!("multieq {}", ops),
30 |_| lhs,
31 |lc| lc + CS::one(),
32 |_| rhs,
33 );
34 self.lhs = LinearCombination::zero();
35 self.rhs = LinearCombination::zero();
36 self.bits_used = 0;
37 self.ops += 1;
38 }
39
40 pub fn enforce_equal(
41 &mut self,
42 num_bits: usize,
43 lhs: &LinearCombination<Scalar>,
44 rhs: &LinearCombination<Scalar>,
45 ) {
46 if (Scalar::CAPACITY as usize) <= (self.bits_used + num_bits) {
48 self.accumulate();
49 }
50
51 assert!((Scalar::CAPACITY as usize) > (self.bits_used + num_bits));
52
53 let coeff = Scalar::from(2).pow_vartime(&[self.bits_used as u64]);
54 self.lhs = self.lhs.clone() + (coeff, lhs);
55 self.rhs = self.rhs.clone() + (coeff, rhs);
56 self.bits_used += num_bits;
57 }
58}
59
60impl<Scalar: PrimeField, CS: ConstraintSystem<Scalar>> Drop for MultiEq<Scalar, CS> {
61 fn drop(&mut self) {
62 if self.bits_used > 0 {
63 self.accumulate();
64 }
65 }
66}
67
68impl<Scalar: PrimeField, CS: ConstraintSystem<Scalar>> ConstraintSystem<Scalar>
69 for MultiEq<Scalar, CS>
70{
71 type Root = Self;
72
73 fn one() -> Variable {
74 CS::one()
75 }
76
77 fn alloc<F, A, AR>(&mut self, annotation: A, f: F) -> Result<Variable, SynthesisError>
78 where
79 F: FnOnce() -> Result<Scalar, SynthesisError>,
80 A: FnOnce() -> AR,
81 AR: Into<String>,
82 {
83 self.cs.alloc(annotation, f)
84 }
85
86 fn alloc_input<F, A, AR>(&mut self, annotation: A, f: F) -> Result<Variable, SynthesisError>
87 where
88 F: FnOnce() -> Result<Scalar, SynthesisError>,
89 A: FnOnce() -> AR,
90 AR: Into<String>,
91 {
92 self.cs.alloc_input(annotation, f)
93 }
94
95 fn enforce<A, AR, LA, LB, LC>(&mut self, annotation: A, a: LA, b: LB, c: LC)
96 where
97 A: FnOnce() -> AR,
98 AR: Into<String>,
99 LA: FnOnce(LinearCombination<Scalar>) -> LinearCombination<Scalar>,
100 LB: FnOnce(LinearCombination<Scalar>) -> LinearCombination<Scalar>,
101 LC: FnOnce(LinearCombination<Scalar>) -> LinearCombination<Scalar>,
102 {
103 self.cs.enforce(annotation, a, b, c)
104 }
105
106 fn push_namespace<NR, N>(&mut self, name_fn: N)
107 where
108 NR: Into<String>,
109 N: FnOnce() -> NR,
110 {
111 self.cs.get_root().push_namespace(name_fn)
112 }
113
114 fn pop_namespace(&mut self) {
115 self.cs.get_root().pop_namespace()
116 }
117
118 fn get_root(&mut self) -> &mut Self::Root {
119 self
120 }
121}