@@ -2,27 +2,26 @@ pragma circom 2.0.0;
22
33include " ../../node_modules/circomlib/circuits/comparators.circom" ;
44
5+ // Set membership gadget is handled with a multiplicative trick.
6+ //
7+ // For a given set of elements, a prover first computes the difference between
8+ // each element in the set and the element they are proving knowledge of. We
9+ // constrain this operation accordingly. We then multiply all differences and constrain
10+ // this value by zero. If the prover actually knows an element in the set then for that
11+ // element, it must hold that the difference is 0. Therefore, the product of 0 and
12+ // anything else should be 0. The prove can't lie by adding a zero into the diffs set
13+ // because we constrain those to match all elements in the set respectively.
514template SetMembership (length ) {
6- signal input element;
7- signal input set[length];
8-
9- signal product[length + 1 ];
10- product[0 ] <== 1 ;
11-
12- component isEqualChecker[length];
13- component isZeroChecker[length];
14-
15- for (var i = 0 ; i < length; i++ ) {
16- isEqualChecker[i] = IsEqual();
17- isZeroChecker[i] = IsZero();
18-
19- isEqualChecker[i].in [0 ] <== element;
20- isEqualChecker[i].in [1 ] <== set[i];
21-
22- isZeroChecker[i].in <== isEqualChecker[i].out;
23-
24- product[i + 1 ] <== product[i] * isZeroChecker[i].out;
25- }
26-
27- product[length] === 0 ;
28- }
15+ signal input element;
16+ signal input set[length];
17+ signal input diffs[length];
18+
19+ signal product[length + 1 ];
20+ product[0 ] <== element;
21+ for (var i = 0 ; i < length; i++ ) {
22+ set[i] === diffs[i] + element;
23+ product[i + 1 ] <== product[i] * diffs[i];
24+ }
25+
26+ product[length] === 0 ;
27+ }
0 commit comments