Description
Due to the impossibility to represent floating point numbers in Ink, order of multiplications and divisions are important to ensure numerical precision.
Take for example the following function. It is intended to return the percentage of a total profit:
#[ink::message]
pub fn split_profit(uint64 percentage, uint64 total_profit) -> uint64 {
(percentage / 100) * total_profit
}
The result however, will always be zero, given the integer division percentage / 100
yields zero.
Reordering operations to perform multiplications before divisions is a good way to avoid loss of precision:
#[ink::message]
pub fn split_profit(uint64 percentage, uint64 total_profit) -> uint64 {
(percentage * total_profit) / 100
}
Acceptance Criteria
- Add a detector for this vulnerability
- Add to integration tests