grapevine-sonobe/circom/node_modules/circomlib/circuits/mux3.circom
circom/node_modules/circomlib/circuits/mux3.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 MultiMux3(n) {
22 signal input c[n][8]; // Constants
23 signal input s[3]; // Selector
24 signal output out[n];
25
26 signal a210[n];
27 signal a21[n];
28 signal a20[n];
29 signal a2[n];
30
31 signal a10[n];
32 signal a1[n];
33 signal a0[n];
34 signal a[n];
35
36 // 4 constrains for the intermediary variables
37 signal s10;
38 s10 <== s[1] * s[0];
39
40 for (var i=0; i<n; i++) {
41
42 a210[i] <== ( c[i][ 7]-c[i][ 6]-c[i][ 5]+c[i][ 4] - c[i][ 3]+c[i][ 2]+c[i][ 1]-c[i][ 0] ) * s10;
43 a21[i] <== ( c[i][ 6]-c[i][ 4]-c[i][ 2]+c[i][ 0] ) * s[1];
44 a20[i] <== ( c[i][ 5]-c[i][ 4]-c[i][ 1]+c[i][ 0] ) * s[0];
45 a2[i] <== ( c[i][ 4]-c[i][ 0] );
46
47 a10[i] <== ( c[i][ 3]-c[i][ 2]-c[i][ 1]+c[i][ 0] ) * s10;
48 a1[i] <== ( c[i][ 2]-c[i][ 0] ) * s[1];
49 a0[i] <== ( c[i][ 1]-c[i][ 0] ) * s[0];
50 a[i] <== ( c[i][ 0] );
51
52 out[i] <== ( a210[i] + a21[i] + a20[i] + a2[i] ) * s[2] +
53 ( a10[i] + a1[i] + a0[i] + a[i] );
54
55 }
56}
57
58template Mux3() {
59 var i;
60 signal input c[8]; // Constants
61 signal input s[3]; // Selector
62 signal output out;
63
64 component mux = MultiMux3(1);
65
66 for (i=0; i<8; i++) {
67 mux.c[0][i] <== c[i];
68 }
69
70 for (i=0; i<3; i++) {
71 s[i] ==> mux.s[i];
72 }
73
74 mux.out[0] ==> out;
75}