forked from Uniswap/v3-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullMathEchidnaTest.sol
67 lines (57 loc) · 1.7 KB
/
FullMathEchidnaTest.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.7.6;
import '../libraries/FullMath.sol';
contract FullMathEchidnaTest {
function checkMulDivRounding(
uint256 x,
uint256 y,
uint256 d
) external pure {
require(d > 0);
uint256 ceiled = FullMath.mulDivRoundingUp(x, y, d);
uint256 floored = FullMath.mulDiv(x, y, d);
if (mulmod(x, y, d) > 0) {
assert(ceiled - floored == 1);
} else {
assert(ceiled == floored);
}
}
function checkMulDiv(
uint256 x,
uint256 y,
uint256 d
) external pure {
require(d > 0);
uint256 z = FullMath.mulDiv(x, y, d);
if (x == 0 || y == 0) {
assert(z == 0);
return;
}
// recompute x and y via mulDiv of the result of floor(x*y/d), should always be less than original inputs by < d
uint256 x2 = FullMath.mulDiv(z, d, y);
uint256 y2 = FullMath.mulDiv(z, d, x);
assert(x2 <= x);
assert(y2 <= y);
assert(x - x2 < d);
assert(y - y2 < d);
}
function checkMulDivRoundingUp(
uint256 x,
uint256 y,
uint256 d
) external pure {
require(d > 0);
uint256 z = FullMath.mulDivRoundingUp(x, y, d);
if (x == 0 || y == 0) {
assert(z == 0);
return;
}
// recompute x and y via mulDiv of the result of floor(x*y/d), should always be less than original inputs by < d
uint256 x2 = FullMath.mulDiv(z, d, y);
uint256 y2 = FullMath.mulDiv(z, d, x);
assert(x2 >= x);
assert(y2 >= y);
assert(x2 - x < d);
assert(y2 - y < d);
}
}