Skip to content

Commit 6483c8c

Browse files
authored
fix: comparison; cairo syntax (#135)
1 parent e9b013e commit 6483c8c

File tree

1 file changed

+17
-17
lines changed
  • not-so-smart-contracts/cairo/incorrect_felt_comparison

1 file changed

+17
-17
lines changed
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
# Incorrect Felt Comparison
22

3-
In cairo, there are two methods for comparison, in particular for the less than or equal to operator we have the methods `assert_le` and `assert_nn_le`. `assert_le` asserts that a number a is less than or equal to b, regardless of the size of a, while `assert_nn_le` will also assert that a is non-negative, ie not greater than the `RANGE_CHECK_BOUND` value of `2^128`: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/math.cairo#L66-L67
3+
In Cairo, there are two builtin methods for the less than or equal to comparison operator: `assert_le` and `assert_nn_le`. `assert_le` asserts that a number `a` is less than or equal to `b`, regardless of the size of `a`, while `assert_nn_le` additionally asserts that `a` is non-negative, i.e. not greater than or equal to the `RANGE_CHECK_BOUND` value of `2^128`: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/math.cairo#L66-L67
44

55
# Example
66

7-
Suppose that a codebase uses the following checks regarding a hypothetical ERC20 token. In the first function, it may be possible that `value` is in fact greater than `max_supply`, yet because the function does not verify `value <0` the assertion will incorrectly pass. The second function, however, asserts that `0 < value < max_supply`, which will correctly not let an incorrect `value` go through the assertion.
7+
Suppose that a codebase uses the following checks regarding a hypothetical ERC20 token. In the first function, it may be possible that `value` is in fact greater than `max_supply`, yet because the function does not verify `value >= 0` the assertion will incorrectly pass. The second function, however, asserts that `0 <= value <= max_supply`, which will correctly not let an incorrect `value` go through the assertion.
88

99
```cairo
1010
@storage_var
11-
func max_supply() -> (res: felt):
12-
end
11+
func max_supply() -> (res: felt) {
12+
}
1313
1414
@external
15-
func bad_comparison{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():
16-
let (value: felt) = ERC20.total_supply()
17-
assert_le{range_check_ptr=range_check_ptr}(value, max_supply.read())
15+
func bad_comparison{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() {
16+
let (value: felt) = ERC20.total_supply();
17+
assert_le{range_check_ptr=range_check_ptr}(value, max_supply.read());
1818
19-
# do something...
19+
// do something...
2020
21-
return ()
22-
end
21+
return ();
22+
}
2323
2424
@external
25-
func better_comparison{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():
26-
let (value: felt) = ERC20.total_supply()
27-
assert_nn_le{range_check_ptr=range_check_ptr}(value, max_supply.read())
25+
func better_comparison{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() {
26+
let (value: felt) = ERC20.total_supply();
27+
assert_nn_le{range_check_ptr=range_check_ptr}(value, max_supply.read());
2828
29-
# do something...
29+
// do something...
3030
31-
return ()
31+
return ();
3232
3333
34-
end
34+
}
3535
```
3636

3737

3838

3939
# Mitigations
40-
Review all felt comparisons closely. Determine what sort of behavior the comparison should have, and if `assert_nn_le` is more appropriate.
40+
Review all felt comparisons closely. Determine what sort of behavior the comparison should have, and if `assert_nn_le` is more appropriate.

0 commit comments

Comments
 (0)