forked from a16z/halmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.sol
37 lines (30 loc) · 1.01 KB
/
Example.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// SPDX-License-Identifier: AGPL-3.0
pragma solidity >=0.8.0 <0.9.0;
contract Example {
function totalPriceBuggy(uint96 price, uint32 quantity) public pure returns (uint128) {
unchecked {
return uint120(price) * quantity;
}
}
function totalPriceFixed(uint96 price, uint32 quantity) public pure returns (uint128) {
unchecked {
return uint128(price) * quantity;
}
}
function totalPriceConservative(uint96 price, uint32 quantity) public pure returns (uint128) {
unchecked {
return uint128(uint(price) * uint(quantity));
}
}
function isPowerOfTwo(uint x) public pure returns (bool) {
unchecked {
return x != 0 && (x & (x - 1)) == 0;
}
}
function isPowerOfTwoIter(uint x) public pure returns (bool) {
unchecked {
while (x != 0 && (x & 1) == 0) x >>= 1; // NOTE: `--loop 256` option needed for complete verification
return x == 1;
}
}
}