grapevine-sonobe/circom/node_modules/circomlib/circuits/multiplexer.circom
circom/node_modules/circomlib/circuits/multiplexer.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
20/*
21 Copyright 2018 0KIMS association.
22
23 This file is part of circom (Zero Knowledge Circuit Compiler).
24
25 circom is a free software: you can redistribute it and/or modify it
26 under the terms of the GNU General Public License as published by
27 the Free Software Foundation, either version 3 of the License, or
28 (at your option) any later version.
29
30 circom is distributed in the hope that it will be useful, but WITHOUT
31 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
32 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
33 License for more details.
34
35 You should have received a copy of the GNU General Public License
36 along with circom. If not, see <https://www.gnu.org/licenses/>.
37*/
38
39// --> Assignation without constraint
40// <-- Assignation without constraint
41// === Constraint
42// <== Assignation with constraint
43// ==> Assignation with constraint
44// All variables are members of the field F[p]
45// https://github.com/zcash-hackworks/sapling-crypto
46// https://github.com/ebfull/bellman
47
48/*
49function log2(a) {
50 if (a==0) {
51 return 0;
52 }
53 let n = 1;
54 let r = 1;
55 while (n<a) {
56 r++;
57 n *= 2;
58 }
59 return r;
60}
61*/
62
63pragma circom 2.0.0;
64
65template EscalarProduct(w) {
66 signal input in1[w];
67 signal input in2[w];
68 signal output out;
69 signal aux[w];
70 var lc = 0;
71 for (var i=0; i<w; i++) {
72 aux[i] <== in1[i]*in2[i];
73 lc = lc + aux[i];
74 }
75 out <== lc;
76}
77
78template Decoder(w) {
79 signal input inp;
80 signal output out[w];
81 signal output success;
82 var lc=0;
83
84 for (var i=0; i<w; i++) {
85 out[i] <-- (inp == i) ? 1 : 0;
86 out[i] * (inp-i) === 0;
87 lc = lc + out[i];
88 }
89
90 lc ==> success;
91 success * (success -1) === 0;
92}
93
94
95template Multiplexer(wIn, nIn) {
96 signal input inp[nIn][wIn];
97 signal input sel;
98 signal output out[wIn];
99 component dec = Decoder(nIn);
100 component ep[wIn];
101
102 for (var k=0; k<wIn; k++) {
103 ep[k] = EscalarProduct(nIn);
104 }
105
106 sel ==> dec.inp;
107 for (var j=0; j<wIn; j++) {
108 for (var k=0; k<nIn; k++) {
109 inp[k][j] ==> ep[j].in1[k];
110 dec.out[k] ==> ep[j].in2[k];
111 }
112 ep[j].out ==> out[j];
113 }
114 dec.success === 1;
115}