circomlib/circuits/compconstant.circom

74 lines
1.8 KiB
Plaintext
Raw Permalink Normal View History

2018-12-16 13:27:29 +03:00
/*
Copyright 2018 0KIMS association.
This file is part of circom (Zero Knowledge Circuit Compiler).
circom is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
circom is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with circom. If not, see <https://www.gnu.org/licenses/>.
*/
2018-12-06 19:32:49 +03:00
include "bitify.circom";
// Returns 1 if in (in binary) > ct
template CompConstant(ct) {
signal input in[254];
signal output out;
signal parts[127];
signal sout;
var clsb;
var cmsb;
var slsb;
var smsb;
var sum=0;
var b = (1 << 128) -1;
var a = 1;
var e = 1;
var i;
for (i=0;i<127; i++) {
clsb = (ct >> (i*2)) & 1;
cmsb = (ct >> (i*2+1)) & 1;
slsb = in[i*2];
smsb = in[i*2+1];
2020-03-16 22:41:11 +03:00
if ((cmsb==0)&&(clsb==0)) {
2018-12-06 19:32:49 +03:00
parts[i] <== -b*smsb*slsb + b*smsb + b*slsb;
2020-03-16 22:41:11 +03:00
} else if ((cmsb==0)&&(clsb==1)) {
2018-12-06 19:32:49 +03:00
parts[i] <== a*smsb*slsb - a*slsb + b*smsb - a*smsb + a;
2020-03-16 22:41:11 +03:00
} else if ((cmsb==1)&&(clsb==0)) {
2018-12-06 19:32:49 +03:00
parts[i] <== b*smsb*slsb - a*smsb + a;
} else {
parts[i] <== -a*smsb*slsb + a;
}
sum = sum + parts[i];
b = b -e;
a = a +e;
e = e*2;
}
sout <== sum;
component num2bits = Num2Bits(135);
num2bits.in <== sout;
out <== num2bits.out[127];
}