Skip to content

Commit

Permalink
fix(utils): fix(safe-comparators): constrain inputs to n bits and cor…
Browse files Browse the repository at this point in the history
…rect Num2Bits instantiation (#20)

re #15
  • Loading branch information
0x471 authored Feb 10, 2025
1 parent c355c9f commit e6e5355
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/utils/src/safe-comparators.circom
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ template SafeLessThan(n) {
signal input in[2];
signal output out;

// Constrain inputs to n bits
component range_check[2];
for (var i = 0; i < 2; i++) {
range_check[i] = Num2Bits(n);
range_check[i].in <== in[i];
}

// Additional conversion to handle arithmetic operation and capture the comparison result.
var n2b[254];
n2b = Num2Bits_strict()(in[0] + (1<<n) - in[1]);
component n2b = Num2Bits(n + 1);
n2b.in <== in[0] + (1<<n) - in[1];

// Determine if in[0] is less than in[1] based on the most significant bit.
out <== 1 - n2b[n];
out <== 1 - n2b.out[n];
}

// Template to check if one input is less than or equal to another.
Expand Down

0 comments on commit e6e5355

Please sign in to comment.