arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/circuits/comparators.circom
circuits/comparators.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
20include "bitify.circom";
21include "binsum.circom";
22
23template IsZero() {
24    signal input in;
25    signal output out;
26
27    signal inv;
28
29    inv <-- in!=0 ? 1/in : 0;
30
31    out <== -in*inv +1;
32    in*out === 0;
33}
34
35
36template IsEqual() {
37    signal input in[2];
38    signal output out;
39
40    component isz = IsZero();
41
42    in[1] - in[0] ==> isz.in;
43
44    isz.out ==> out;
45}
46
47
48// N is the number of bits the input  have.
49// The MSF is the sign bit.
50template LessThan(n) {
51    signal input in[2];
52    signal output out;
53
54    component num2Bits0;
55    component num2Bits1;
56
57    component adder;
58
59    adder = BinSum(n, 2);
60
61    num2Bits0 = Num2Bits(n);
62    num2Bits1 = Num2BitsNeg(n);
63
64    in[0] ==> num2Bits0.in;
65    in[1] ==> num2Bits1.in;
66
67    var i;
68    for (i=0;i<n;i++) {
69        num2Bits0.out[i] ==> adder.in[0][i];
70        num2Bits1.out[i] ==> adder.in[1][i];
71    }
72
73    adder.out[n-1] ==> out;
74}