arnaucube
arnaucube/grapevine-sonobePublic Self-hosted repository
grapevine-sonobe/circom/node_modules/circomlib/circuits/gates.circom
circom/node_modules/circomlib/circuits/gates.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 XOR() {
22    signal input a;
23    signal input b;
24    signal output out;
25
26    out <== a + b - 2*a*b;
27}
28
29template AND() {
30    signal input a;
31    signal input b;
32    signal output out;
33
34    out <== a*b;
35}
36
37template OR() {
38    signal input a;
39    signal input b;
40    signal output out;
41
42    out <== a + b - a*b;
43}
44
45template NOT() {
46    signal input in;
47    signal output out;
48
49    out <== 1 + in - 2*in;
50}
51
52template NAND() {
53    signal input a;
54    signal input b;
55    signal output out;
56
57    out <== 1 - a*b;
58}
59
60template NOR() {
61    signal input a;
62    signal input b;
63    signal output out;
64
65    out <== a*b + 1 - a - b;
66}
67
68template MultiAND(n) {
69    signal input in[n];
70    signal output out;
71    component and1;
72    component and2;
73    component ands[2];
74    if (n==1) {
75        out <== in[0];
76    } else if (n==2) {
77        and1 = AND();
78        and1.a <== in[0];
79        and1.b <== in[1];
80        out <== and1.out;
81    } else {
82        and2 = AND();
83        var n1 = n\2;
84        var n2 = n-n\2;
85        ands[0] = MultiAND(n1);
86        ands[1] = MultiAND(n2);
87        var i;
88        for (i=0; i<n1; i++) ands[0].in[i] <== in[i];
89        for (i=0; i<n2; i++) ands[1].in[i] <== in[n1+i];
90        and2.a <== ands[0].out;
91        and2.b <== ands[1].out;
92        out <== and2.out;
93    }
94}
95
96