arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/circuits/multiplexer.circom
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// --> Assignation without constraint
21// <-- Assignation without constraint
22// === Constraint
23// <== Assignation with constraint
24// ==> Assignation with constraint
25// All variables are members of the field F[p]
26// https://github.com/zcash-hackworks/sapling-crypto
27// https://github.com/ebfull/bellman
28
29/*
30function log2(a) {
31    if (a==0) {
32        return 0;
33    }
34    let n = 1;
35    let r = 1;
36    while (n<a) {
37        r++;
38        n *= 2;
39    }
40    return r;
41}
42*/
43
44template EscalarProduct(w) {
45    signal input in1[w];
46    signal input in2[w];
47    signal output out;
48    signal aux[w];
49    var lc = 0;
50    for (var i=0; i<w; i++) {
51        aux[i] <== in1[i]*in2[i];
52        lc = lc + aux[i];
53    }
54    out <== lc;
55}
56
57template Decoder(w) {
58    signal input inp;
59    signal output out[w];
60    signal output success;
61    var lc=0;
62
63    for (var i=0; i<w; i++) {
64        out[i] <-- (inp == i) ? 1 : 0;
65        out[i] * (inp-i) === 0;
66        lc = lc + out[i];
67    }
68
69    lc ==> success;
70    success * (success -1) === 0;
71}
72
73
74template Multiplexor(wIn, nIn) {
75    signal input inp[nIn][wIn];
76    signal input sel;
77    signal output out[wIn];
78    component Decoder(nIn) dec;
79    component EscalarProduct(nIn) ep[wIn];
80    sel ==> dec.inp;
81    for (var j=0; j<wIn; j++) {
82        for (var k=0; k<nIn; k++) {
83            inp[k][j] ==> ep[j].in1[k];
84            dec.out[k] ==> ep[j].in2[k];
85        }
86        ep[j].out ==> out[j];
87    }
88    dec.success === 1;
89}
90
91component Multiplexor(8,3) main;
92
93