# Mismatching Bit Lengths

{% hint style="info" %}
[**Book an audit with Zokyo**](https://www.zokyo.io/)
{% endhint %}

Many of CircomLib’s circuits take in an expected number of bits. In order for their constraints and output to be accurate, the input parameters need to be constrained to that maximum number of bits outside of the CircomLib circuit. For example, the _LessThan_ circuit expects _n_ number of bits.

```
template LessThan(n) {
    assert(n <= 252);
    signal input in[2];
    signal output out;

    component n2b = Num2Bits(n+1);

    n2b.in <== in[0]+ (1<<n) - in[1];

    out <== 1-n2b.out[n];
}
```

**Attack Scenario**

The _LessThan_ circuit outputs 1 if _in\[0] < in\[1]_, and 0 otherwise. An attacker could use _in\[0]_ as a small number and _in\[1]_ as a number with more than _n_ bits. This would cause the _Num2Bits_ input to underflow, and so the output could be engineered to 0, even though _in\[0] < in\[1]_.

**Preventative Techniques**

In order to prevent this, bit length checks should be done on the inputs as well. This can be done by using CircomLib’s Num2Bits circuit. This circuit is already used in the LessThan circuit, but it is used on _in\[0] + (1 << n) - in\[1]_ instead of on the inputs themselves. So the following Num2Bits constraints should be added as well:

```
signal input1;
signal input2;
signal maxBits;

// Constrain input1 to maxBits
component bitCheck1 = Num2Bits(maxBits);
bitCheck1.in <== input1;

// Constrain input2 to maxBits
component bitCheck2 = Num2Bits(maxBits);
bitCheck2.in <== input2;

// Compare input1 to input2
component lt = LessThan(maxBits);
lt.in[0] <== input1;
lt.in[1] <== input2;

// Ensure input1 < input2
lt.out === 1;
```
