From 112d0bd6a65f75d83556dbd5ced073466e745f35 Mon Sep 17 00:00:00 2001 From: enricobottazzi <85900164+enricobottazzi@users.noreply.github.com> Date: Tue, 17 Jan 2023 16:34:05 +0100 Subject: [PATCH 1/3] add `SafeLessThan` template to comparators --- circuits/comparators.circom | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/circuits/comparators.circom b/circuits/comparators.circom index bfed0320..6ea44b9c 100644 --- a/circuits/comparators.circom +++ b/circuits/comparators.circom @@ -98,6 +98,23 @@ template LessThan(n) { out <== 1-n2b.out[n]; } +// Do Range checks on the inputs to avoid overflow +template SafeLessThan(n) { + assert(n <= 252); + signal input in[2]; + signal output out; + + component aInRange = Num2Bits(252); + aInRange.in <== in[0]; + component bInRange = Num2Bits(252); + bInRange.in <== in[1]; + + component n2b = Num2Bits(n+1); + + n2b.in <== in[0]+ (1< Date: Wed, 18 Jan 2023 13:45:57 +0100 Subject: [PATCH 2/3] use `LessThan` component inside `SafeLessThan` to save compatibility --- circuits/comparators.circom | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/circuits/comparators.circom b/circuits/comparators.circom index 6ea44b9c..b327d42d 100644 --- a/circuits/comparators.circom +++ b/circuits/comparators.circom @@ -109,11 +109,12 @@ template SafeLessThan(n) { component bInRange = Num2Bits(252); bInRange.in <== in[1]; - component n2b = Num2Bits(n+1); + component lt = LessThan(n); - n2b.in <== in[0]+ (1< Date: Wed, 18 Jan 2023 13:54:43 +0100 Subject: [PATCH 3/3] Restrict `Num2Bits` component instantiation to n bits instead of 252 --- circuits/comparators.circom | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/circuits/comparators.circom b/circuits/comparators.circom index b327d42d..3977d246 100644 --- a/circuits/comparators.circom +++ b/circuits/comparators.circom @@ -98,15 +98,16 @@ template LessThan(n) { out <== 1-n2b.out[n]; } -// Do Range checks on the inputs to avoid overflow +// 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(252); + component aInRange = Num2Bits(n); aInRange.in <== in[0]; - component bInRange = Num2Bits(252); + component bInRange = Num2Bits(n); bInRange.in <== in[1]; component lt = LessThan(n);