arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/circuits/compconstant.circom
circuits/compconstant.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";
21
22// Returns 1 if in (in binary) > ct
23
24template CompConstant(ct) {
25    signal input in[254];
26    signal output out;
27
28    signal parts[127];
29    signal sout;
30
31    var clsb;
32    var cmsb;
33    var slsb;
34    var smsb;
35
36    var sum=0;
37
38    var b = (1 << 128) -1;
39    var a = 1;
40    var e = 1;
41    var i;
42
43    for (i=0;i<127; i++) {
44        clsb = (ct >> (i*2)) & 1;
45        cmsb = (ct >> (i*2+1)) & 1;
46        slsb = in[i*2];
47        smsb = in[i*2+1];
48
49
50        if ((cmsb==0)&(clsb==0)) {
51            parts[i] <== -b*smsb*slsb + b*smsb + b*slsb;
52        } else if ((cmsb==0)&(clsb==1)) {
53            parts[i] <== a*smsb*slsb - a*slsb + b*smsb - a*smsb + a;
54        } else if ((cmsb==1)&(clsb==0)) {
55            parts[i] <== b*smsb*slsb - a*smsb + a;
56        } else {
57            parts[i] <== -a*smsb*slsb + a;
58        }
59
60        sum = sum + parts[i];
61
62        b = b -e;
63        a = a +e;
64        e = e*2;
65    }
66
67    sout <== sum;
68
69    component num2bits = Num2Bits(135);
70
71    num2bits.in <== sout;
72
73    out <== num2bits.out[127];
74}