Files
nightmarket/circuits/util/rangeProof.circom
2022-04-08 18:41:35 +00:00

56 lines
1.5 KiB
Plaintext

pragma circom 2.0.3;
include "../../node_modules/circomlib/circuits/comparators.circom";
include "../../node_modules/circomlib/circuits/bitify.circom";
// NB: RangeProof is inclusive.
// input: field element, whose abs is claimed to be <= than max_abs_value
// output: none
// also checks that both max and abs(in) are expressible in `bits` bits
template RangeProof(bits) {
signal input in;
signal input max_abs_value;
/* check that both max and abs(in) are expressible in `bits` bits */
component n2b1 = Num2Bits(bits+1);
n2b1.in <== in + (1 << bits);
component n2b2 = Num2Bits(bits);
n2b2.in <== max_abs_value;
/* check that in + max is between 0 and 2*max */
component lowerBound = LessThan(bits+1);
component upperBound = LessThan(bits+1);
lowerBound.in[0] <== max_abs_value + in;
lowerBound.in[1] <== 0;
lowerBound.out === 0;
upperBound.in[0] <== 2 * max_abs_value;
upperBound.in[1] <== max_abs_value + in;
upperBound.out === 0;
}
// input: n field elements, whose abs are claimed to be less than max_abs_value
// output: none
template MultiRangeProof(n, bits) {
signal input in[n];
signal input max_abs_value;
component rangeProofs[n];
for (var i = 0; i < n; i++) {
rangeProofs[i] = RangeProof(bits);
rangeProofs[i].in <== in[i];
rangeProofs[i].max_abs_value <== max_abs_value;
}
}
/*
template RPTester() {
signal input a;
component rp = RangeProof(10);
rp.in <== a;
rp.max_abs_value <== 100;
}
component main = RPTester();
*/