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