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 if ((cmsb==0)&&(clsb==0)) {
50 parts[i] <== -b*smsb*slsb + b*smsb + b*slsb;
51 } else if ((cmsb==0)&&(clsb==1)) {
52 parts[i] <== a*smsb*slsb - a*slsb + b*smsb - a*smsb + a;
53 } else if ((cmsb==1)&&(clsb==0)) {
54 parts[i] <== b*smsb*slsb - a*smsb + a;
55 } else {
56 parts[i] <== -a*smsb*slsb + a;
57 }
58
59 sum = sum + parts[i];
60
61 b = b -e;
62 a = a +e;
63 e = e*2;
64 }
65
66 sout <== sum;
67
68 component num2bits = Num2Bits(135);
69
70 num2bits.in <== sout;
71
72 out <== num2bits.out[127];
73}