circomlib/circuits/mux2.circom
circuits/mux2.circomRaw
1/*
2 Copyright 2018 0KIMS association.
3
4 This file is part of circom (Zero Knowledge Circuit Compiler).
5
6 circom is a free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 circom is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with circom. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20template MultiMux2(n) {
21 signal input c[n][4]; // Constants
22 signal input s[2]; // Selector
23 signal output out[n];
24
25 signal a10[n];
26 signal a1[n];
27 signal a0[n];
28 signal a[n];
29
30 signal s10;
31 s10 <== s[1] * s[0];
32
33 for (var i=0; i<n; i++) {
34
35 a10[i] <== ( c[i][ 3]-c[i][ 2]-c[i][ 1]+c[i][ 0] ) * s10;
36 a1[i] <== ( c[i][ 2]-c[i][ 0] ) * s[1];
37 a0[i] <== ( c[i][ 1]-c[i][ 0] ) * s[0];
38 a[i] <== ( c[i][ 0] )
39
40 out[i] <== ( a10[i] + a1[i] + a0[i] + a[i] );
41
42 }
43}
44
45template Mux2() {
46 var i;
47 signal input c[4]; // Constants
48 signal input s[2]; // Selector
49 signal output out;
50
51 component mux = MultiMux2(1);
52
53 for (i=0; i<4; i++) {
54 mux.c[0][i] <== c[i];
55 }
56
57 for (i=0; i<2; i++) {
58 s[i] ==> mux.s[i];
59 }
60
61 mux.out[0] ==> out;
62}