Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions circuits/gates.circom
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
*/
pragma circom 2.0.0;

/*
Boolean logic gates.

Inputs generally must be constrained to 0 or 1, using Num2Bits or a*(a-1) === 0 etc.
Most languages extend boolean logic to non boolean types, but using non-booleans here will lead to subtle soundness bugs.
*/

// Requires binary inputs: [2,2] -> -4, should be 0
template XOR() {
signal input a;
signal input b;
Expand All @@ -26,6 +34,7 @@ template XOR() {
out <== a + b - 2*a*b;
}

// Applies to non-binary numbers: gives non-zero output iff both inputs are non-zero because a and b are coprime with p
template AND() {
signal input a;
signal input b;
Expand All @@ -34,6 +43,7 @@ template AND() {
out <== a*b;
}

// Requires binary inputs: [2,2] -> 0, should be non-zero
template OR() {
signal input a;
signal input b;
Expand All @@ -42,13 +52,15 @@ template OR() {
out <== a + b - a*b;
}

// Requires binary inputs: 2 -> -1, should be 0
template NOT() {
signal input in;
signal output out;

out <== 1 + in - 2*in;
}

// Requires binary inputs: [2,1] -> -1, should be 0
template NAND() {
signal input a;
signal input b;
Expand All @@ -57,6 +69,7 @@ template NAND() {
out <== 1 - a*b;
}

// Requires binary inputs: [2,1] -> 0, should be non-zero
template NOR() {
signal input a;
signal input b;
Expand Down