-
Notifications
You must be signed in to change notification settings - Fork 0
/
isafe_math.spec
149 lines (111 loc) · 3.5 KB
/
isafe_math.spec
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
definition MAX_INT256() returns mathint = 57896044618658097711785492504343953926634992332820282019728792003956564819967;
definition MIN_INT256() returns mathint = -57896044618658097711785492504343953926634992332820282019728792003956564819968;
definition ABS(int256 x) returns mathint = x < 0 ? -x : x;
// remainder = get_sign(x) * (abs(x) % abs(y))
definition REM(int256 x, int256 y) returns mathint = x < 0 ? -1 * (ABS(x) % ABS(y)) : ABS(x) % ABS(y);
methods {
function safe_add(int256 x, int256 y) external returns(int256) envfree;
function safe_sub(int256 x, int256 y) external returns(int256) envfree;
function safe_div(int256 x, int256 y) external returns(int256) envfree;
function safe_mul(int256 x, int256 y) external returns(int256) envfree;
function safe_modulo(int256 x, int256 y) external returns(int256) envfree;
function safe_exp_base2(int256 y) external returns(int256) envfree;
function safe_abs(int256 y) external returns(int256) envfree;
}
rule safe_add {
int256 a; int256 b;
mathint cres = a + b;
int256 vres = safe_add@withrevert(a, b);
if (cres >= MIN_INT256() && cres <= MAX_INT256()) {
assert vres == assert_int256(cres);
} else {
assert lastReverted;
}
}
rule safe_sub {
int256 a; int256 b;
mathint cres = a - b;
int256 vres = safe_sub@withrevert(a, b);
if (cres >= MIN_INT256() && cres <= MAX_INT256()) {
assert vres == assert_int256(cres);
} else {
assert lastReverted;
}
}
rule safe_div {
int256 a; int256 b;
int256 vres = safe_div@withrevert(a, b);
if (b != 0 && (a != assert_int256(MIN_INT256()) || b != -1)) {
mathint cres = a / b;
mathint remainder = a % b;
mathint adjusted_cres =
(cres == 0) ? 0 : // If the result is 0, don't adjust
((a < 0 && b > 0 && remainder != 0) || (a > 0 && b < 0 && remainder != 0)) ?
cres + 1 : cres;
assert vres == assert_int256(adjusted_cres);
} else {
assert lastReverted;
}
}
/*
rule safe_mul {
int256 a; int256 b;
mathint cres = a * b;
int256 vres = safe_mul@withrevert(a, b);
if (cres >= MIN_INT256() && cres <= MAX_INT256()) {
assert vres == assert_int256(cres);
} else {
assert lastReverted;
}
}
*/
rule safe_mod {
int256 a; int256 b;
int256 vres = safe_modulo@withrevert(a, b);
if (b != 0) {
mathint cres = REM(a, b);
assert vres == assert_int256(cres);
} else {
assert lastReverted;
}
}
rule safe_exp_base2 {
int256 b;
require b >= 0;
mathint cres = 1 << assert_uint256(b);
int256 vres = safe_exp_base2@withrevert(b);
if (cres >= MIN_INT256() && cres <= MAX_INT256()) {
assert vres == assert_int256(cres);
} else {
assert lastReverted;
}
}
rule safe_abs {
int256 b;
require b >= 0;
int256 vres = safe_abs@withrevert(b);
if (b != assert_int256(MIN_INT256())) {
mathint cres = ABS(b);
assert vres == assert_int256(cres);
} else {
assert lastReverted;
}
}
/*
function power(uint256 base, uint256 exp) returns uint256 {
uint256 i, result = 1;
for (i = 0; i < exp; i++)
result *= base;
return result;
}
rule safe_exp_general {
uint256 base, exp;
mathint cres = power(base, exp);
uint256 vres = safe_exp_base2@withrevert(b);
if (cres <= max_uint256) {
assert vres == assert_uint256(cres);
} else {
assert lastReverted;
}
}
*/