compconstant.circom
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 pragma circom 2.0.0; 20 21 include "bitify.circom"; 22 23 // Returns 1 if in (in binary) > ct 24 25 template CompConstant(ct) { 26 signal input in[254]; 27 signal output out; 28 29 signal parts[127]; 30 signal sout; 31 32 var clsb; 33 var cmsb; 34 var slsb; 35 var smsb; 36 37 var sum=0; 38 39 var b = (1 << 128) -1; 40 var a = 1; 41 var e = 1; 42 var i; 43 44 for (i=0;i<127; i++) { 45 clsb = (ct >> (i*2)) & 1; 46 cmsb = (ct >> (i*2+1)) & 1; 47 slsb = in[i*2]; 48 smsb = in[i*2+1]; 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 }