diff --git a/circom_circuits/misc/comparator.circom b/circom_circuits/misc/comparator.circom index e04a72a..4f5ce28 100644 --- a/circom_circuits/misc/comparator.circom +++ b/circom_circuits/misc/comparator.circom @@ -57,4 +57,24 @@ template SafeFullLessThan() { intermediate_results[3] <== B.out * (intermediate_results[0] + intermediate_results[2]); out <== (1 - A.out) * ((1 - B.out) + intermediate_results[3]); +} + +// Safely compare two n-bit numbers +// Performs range checks on the inputs to avoid overflow. Range is n <= 252 +template SafeLessThan(n) { + assert(n <= 252); + signal input in[2]; + signal output out; + + component aInRange = Num2Bits(n); + aInRange.in <== in[0]; + component bInRange = Num2Bits(n); + bInRange.in <== in[1]; + + component lt = LessThan(n); + + lt.in[0] <== in[0]; + lt.in[1] <== in[1]; + + out <== lt.out; } \ No newline at end of file