grapevine-sonobe/circom/node_modules/circomlib/circuits/mux2.circom
circom/node_modules/circomlib/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*/
19pragma circom 2.0.0;
20
21template MultiMux2(n) {
22 signal input c[n][4]; // Constants
23 signal input s[2]; // Selector
24 signal output out[n];
25
26 signal a10[n];
27 signal a1[n];
28 signal a0[n];
29 signal a[n];
30
31 signal s10;
32 s10 <== s[1] * s[0];
33
34 for (var i=0; i<n; i++) {
35
36 a10[i] <== ( c[i][ 3]-c[i][ 2]-c[i][ 1]+c[i][ 0] ) * s10;
37 a1[i] <== ( c[i][ 2]-c[i][ 0] ) * s[1];
38 a0[i] <== ( c[i][ 1]-c[i][ 0] ) * s[0];
39 a[i] <== ( c[i][ 0] );
40
41 out[i] <== ( a10[i] + a1[i] + a0[i] + a[i] );
42
43 }
44}
45
46template Mux2() {
47 var i;
48 signal input c[4]; // Constants
49 signal input s[2]; // Selector
50 signal output out;
51
52 component mux = MultiMux2(1);
53
54 for (i=0; i<4; i++) {
55 mux.c[0][i] <== c[i];
56 }
57
58 for (i=0; i<2; i++) {
59 s[i] ==> mux.s[i];
60 }
61
62 mux.out[0] ==> out;
63}