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