Skip to content

Commit

Permalink
Move compare circuit to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
r0qs committed Aug 2, 2022
1 parent a19de61 commit a20080a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 72 deletions.
67 changes: 67 additions & 0 deletions circuits/compare.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
pragma circom 2.0.4;

include "../node_modules/circomlib/circuits/mux3.circom";
include "../node_modules/circomlib/circuits/comparators.circom";

// Compare compares a with b based on the given operator op
// Operator is represented as a 3 bits bitmap.
// Currently supported operations are:
// 000: ==
// 001: !=
// 010: >
// 011: >=
// 100: <
// 101: <=
// 110: not_implemented
// 111: not_implemented
// It returns out with 1 if the condition is satisfied or 0 otherwise.
template Compare() {
signal input a;
signal input b;
signal input op;
signal output out;

component validOp = LessThan(252);
validOp.in[0] <== op;
validOp.in[1] <== 6;
// Restrict to supported operators
assert(validOp.out);

component eq = IsEqual();
eq.in[0] <== a;
eq.in[1] <== b;

component gt = GreaterThan(252);
gt.in[0] <== a;
gt.in[1] <== b;

component gte = GreaterEqThan(252);
gte.in[0] <== a;
gte.in[1] <== b;

component lt = LessThan(252);
lt.in[0] <== a;
lt.in[1] <== b;

component lte = LessEqThan(252);
lte.in[0] <== a;
lte.in[1] <== b;

component mux = Mux3();
component n2b = Num2Bits(3);
n2b.in <== op;
mux.s[0] <== n2b.out[0];
mux.s[1] <== n2b.out[1];
mux.s[2] <== n2b.out[2];

mux.c[0] <== eq.out;
mux.c[1] <== 1 - eq.out;
mux.c[2] <== gt.out;
mux.c[3] <== gte.out;
mux.c[4] <== lt.out;
mux.c[5] <== lte.out;
mux.c[6] <== 0;
mux.c[7] <== 0;

mux.out ==> out;
}
12 changes: 6 additions & 6 deletions circuits/presentationFieldsAuth.circom
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ template VerifyCredentialMultiField(cdl, ctl) {
signal input certreeRoot;
signal input nullifierHash;

var n = 1 << cdl;
signal input fields[n][3];
signal input pathFieldElements[n];
signal input fieldIndices[n];
var m = 1 << cdl;
signal input fields[m][3];
signal input pathFieldElements[m];
signal input fieldIndices[m];

signal input credentialRoot;
signal input subject;
Expand All @@ -98,9 +98,9 @@ template VerifyCredentialMultiField(cdl, ctl) {
certree.root === certreeRoot;

// Verify whether the fields exists in the credential tree
component fieldHasher[n];
component fieldHasher[m];
component credtree = MerkleMultiProof(cdl);
for (var i = 0; i < n; i++) {
for (var i = 0; i < m; i++) {
fieldHasher[i] = CredentialLeafHasher();
fieldHasher[i].key <== fields[i][0];
fieldHasher[i].value <== fields[i][1];
Expand Down
66 changes: 1 addition & 65 deletions circuits/queryField.circom
Original file line number Diff line number Diff line change
@@ -1,72 +1,8 @@
pragma circom 2.0.4;

include "../node_modules/circomlib/circuits/mux3.circom";
include "../node_modules/circomlib/circuits/comparators.circom";
include "compare.circom";
include "presentationFieldsAuth.circom";

// Compare compares a with b based on the given operator op
// Operator is represented as a 3 bits bitmap.
// Currently supported operations are:
// 000: ==
// 001: !=
// 010: >
// 011: >=
// 100: <
// 101: <=
// 110: not_implemented
// 111: not_implemented
// It returns out with 1 if the condition is satisfied or 0 otherwise.
template Compare() {
signal input a;
signal input b;
signal input op;
signal output out;

component validOp = LessThan(252);
validOp.in[0] <== op;
validOp.in[1] <== 6;
// Restrict to supported operators
assert(validOp.out);

component eq = IsEqual();
eq.in[0] <== a;
eq.in[1] <== b;

component gt = GreaterThan(252);
gt.in[0] <== a;
gt.in[1] <== b;

component gte = GreaterEqThan(252);
gte.in[0] <== a;
gte.in[1] <== b;

component lt = LessThan(252);
lt.in[0] <== a;
lt.in[1] <== b;

component lte = LessEqThan(252);
lte.in[0] <== a;
lte.in[1] <== b;

component mux = Mux3();
component n2b = Num2Bits(3);
n2b.in <== op;
mux.s[0] <== n2b.out[0];
mux.s[1] <== n2b.out[1];
mux.s[2] <== n2b.out[2];

mux.c[0] <== eq.out;
mux.c[1] <== 1 - eq.out;
mux.c[2] <== gt.out;
mux.c[3] <== gte.out;
mux.c[4] <== lt.out;
mux.c[5] <== lte.out;
mux.c[6] <== 0;
mux.c[7] <== 0;

mux.out ==> out;
}

// Verifies whether a field in a credential met a conditional
// @param `cdl` is the level of the credential tree
// @param `ctl` is the level of the certree
Expand Down
2 changes: 1 addition & 1 deletion test/circom/circuits/compare_test.circom
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pragma circom 2.0.4;

include "../../../circuits/queryField.circom";
include "../../../circuits/compare.circom";

component main = Compare();

0 comments on commit a20080a

Please sign in to comment.