arnaucube
arnaucube/circomPublic Self-hosted repository
circom/circuits/multiplexer.circom
circuits/multiplexer.circomRaw
 1// --> Assignation without constraint
 2// <-- Assignation without constraint
 3// === Constraint
 4// <== Assignation with constraint
 5// ==> Assignation with constraint
 6// All variables are members of the field F[p]
 7// https://github.com/zcash-hackworks/sapling-crypto
 8// https://github.com/ebfull/bellman
 9
10/*
11function log2(a) {
12    if (a==0) {
13        return 0;
14    }
15    let n = 1;
16    let r = 1;
17    while (n<a) {
18        r++;
19        n *= 2;
20    }
21    return r;
22}
23*/
24
25template EscalarProduct(w) {
26    signal input in1[w];
27    signal input in2[w];
28    signal output out;
29    signal aux[w];
30    var lc = 0;
31    for (var i=0; i<w; i++) {
32        aux[i] <== in1[i]*in2[i];
33        lc = lc + aux[i];
34    }
35    out <== lc;
36}
37
38template Decoder(w) {
39    signal input inp;
40    signal output out[w];
41    signal output success;
42    var lc=0;
43
44    for (var i=0; i<w; i++) {
45        out[i] <-- (inp == i) ? 1 : 0;
46        out[i] * (inp-i) === 0;
47        lc = lc + out[i];
48    }
49
50    lc ==> success;
51    success * (success -1) === 0;
52}
53
54
55template Multiplexor(wIn, nIn) {
56    signal input inp[nIn][wIn];
57    signal input sel;
58    signal output out[wIn];
59    component Decoder(nIn) dec;
60    component EscalarProduct(nIn) ep[wIn];
61    sel ==> dec.inp;
62    for (var j=0; j<wIn; j++) {
63        for (var k=0; k<nIn; k++) {
64            inp[k][j] ==> ep[j].in1[k];
65            dec.out[k] ==> ep[j].in2[k];
66        }
67        ep[j].out ==> out[j];
68    }
69    dec.success === 1;
70}
71
72component Multiplexor(8,3) main;
73
74