-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr0.py
359 lines (271 loc) · 10.3 KB
/
r0.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from z3 import *
set_param(unsat_core=True)
# Helpers
predicates = {}
my_proofs = {}
def require(s, assertion):
# black magic introspection shit
import traceback,ast
frame = traceback.extract_stack()[-2]
code = frame.line
yea = ast.parse(code)
yea = list(ast.iter_child_nodes(next(ast.iter_child_nodes(next(ast.iter_child_nodes(yea))))))[2]
yea = ast.unparse(yea)
p = FreshBool()
predicates[p] = (yea, frame.lineno, frame.name)
s.assert_and_track(assertion, p)
def print_unsat_core(s):
for p in s.unsat_core():
code, lineno, name = predicates[p]
print(f'* {str(p):5} {"line " + str(lineno):9} {name:16} {code}')
def my_proof(s, name=None):
def decorating_function(user_function):
if name is None:
assert(user_function.__name__.startswith('proof_'))
_name = user_function.__name__[6:]
else:
_name = name # shadowing bullshit
def decorated_function(*args, **kwargs):
s.push()
user_function(*args, **kwargs)
# s.check()
# print(s.assertions())
# print(s.model())
assert(s.check() == unsat)
print('Unsat core:')
print_unsat_core(s)
s.pop()
my_proofs[_name] = decorated_function
return decorated_function
return decorating_function
def run_proof(name):
func = my_proofs[name]
print(name)
func()
print('-> ok')
def run_proofs():
for name in my_proofs:
run_proof(name)
# Modeling
EscrowState = Datatype('EscrowStateEnum')
EscrowState.declare('OPEN')
EscrowState.declare('SUCCESS')
EscrowState.declare('REFUND')
EscrowState = EscrowState.create()
## addresses are 160bits
AddressSort = BitVecSort(160)
Address = lambda x: BitVecVal(x, AddressSort)
UintSort = BitVecSort(256)
Uint = lambda x: BitVecVal(x, UintSort)
MAX_ETH = Uint(120000000e18)
GOAL = Uint(10000e18)
# [ASSUMPTION] the instance of escrow in crowdsale use 1234e8 as eb address
ESCROW_BENEFICIARY = Address(1234e8)
# represent the address of Crodsale Contract
OWNER = BitVec("owner", AddressSort)
ENDTIME = 30
Escrow_Address = BitVec('Escrow_Address', AddressSort)
# States:
# investor_deposits :: mapping(address => uint256)
# eth_balances :: mapping(address => uint256)
# raised :: Uint
# escrowstate :: Symbolic STATE
def initial_state():
s = Solver()
myUser = Const('myUser', AddressSort)
# initialDeposit = Const('initialDeposit', UintSort)
investor_deposits = Array('investor_deposits', AddressSort, UintSort)
eth_balances = Array('eth_balances', AddressSort, UintSort)
# raised = Const('raised', UintSort)
# Arbitrary escrow state
escrowstate = Const('escrowstate', EscrowState)
# TODO
# - time
# Borrow from blog post
# reasonable constraints.
a = Const('a', AddressSort)
require(s, ForAll([a], ULE(investor_deposits[a], eth_balances[Escrow_Address])))
require(s, ForAll([a], ULE(eth_balances[a], MAX_ETH)))
# [ASSUMPTION]
# - ether balance of escrow and sum of all deposits is equall at the beginning
# , here assume both to be 0
# raised, eth_balance[escrow_address], all elem of deposits = 0
# - only one depositor
investor_deposits = Store(investor_deposits, myUser, Uint(0))
eth_balances = Store(eth_balances, Escrow_Address, Uint(0))
raised = eth_balances[Escrow_Address]
# require(s, myUser != ESCROW_BENEFICIARY)
# require(s, myUser != Escrow_Address)
# require(s, ForAll([a], allowance[myUser][a] == 0))
# starting_balance = eth_balances[myUser]
escrow_state = (investor_deposits, escrowstate)
state = (eth_balances, raised, escrow_state)
# state = deposit(s, state, myUser, initialDeposit)
return s, state, myUser
## Trans Rules
# modifier
def escrow_onlyOwner(s, msg_sender):
# SIDE-EFFECT: add new assertion into solver
require(s, msg_sender == OWNER)
def escrow_close(s, state, msg_sender, msg_value):
#onlyOwner
escrow_onlyOwner(s, msg_sender)
_, _, escrow_state = state
_, escrowstate = escrow_state
require(s, escrowstate == EscrowState.SUCCESS)
return state
def escrow_refund(s, state, msg_sender, msg_value):
#onlyOwner
escrow_onlyOwner(s, msg_sender)
_, _, escrow_state = state
_, escrowstate = escrow_state
require(s, escrowstate == EscrowState.REFUND)
return state
# payable, explicit arg
def escrow_deposit(s, state, msg_sender, msg_value, p):
# onlyOwner
escrow_onlyOwner(s, msg_sender)
eth_balances, raised, escrow_state = state
inv_deposits, escrowstate = escrow_state
# implicit from how EVM works
require(s, UGE(eth_balances[p], msg_value))
eth_balances = Store(eth_balances, p, eth_balances[p] - msg_value)
eth_balances = Store(eth_balances, Escrow_Address, eth_balances[Escrow_Address] + msg_value)
# deposits[p] = deposits[p] + msg.value;
inv_deposits = Store(inv_deposits, p, inv_deposits[p] + msg_value)
return (eth_balances, raised, (inv_deposits, escrowstate))
def escrow_withdraw(s, state, msg_sender, msg_value):
eth_balances, raised, escrow_state = state
inv_deposits, escrowstate = escrow_state
# require(state == State.SUCCESS);
require(s, escrowstate == EscrowState.SUCCESS)
# beneficiary.transfer(address(this).balance);
balance = eth_balances[Escrow_Address]
# Always true
require(s, UGE(balance, balance))
eth_balances = Store(eth_balances, ESCROW_BENEFICIARY, eth_balances[ESCROW_BENEFICIARY] + balance)
eth_balances = Store(eth_balances, Escrow_Address, balance - balance)
return (eth_balances, raised, (inv_deposits, escrowstate))
# explicit args payable
def escrow_claimRefund(s, state, msg_sender, msg_value, p):
eth_balances, raised, escrow_state = state
inv_deposits, escrowstate = escrow_state
# require(state == State.REFUND);
require(s, escrowstate == EscrowState.REFUND)
# uint256 amount = deposits[p];
amount = inv_deposits[p]
# deposits[p] = 0;
inv_deposits = Store(inv_deposits, p, 0)
# p.transfer(amount);
require(s, UGE(eth_balances[Escrow_Address], amount))
eth_balances = Store(eth_balances, p, amount)
eth_balances = Store(eth_balances, Escrow_Address, eth_balances[Escrow_Address] - amount)
return (eth_balances, raised, (inv_deposits, escrowstate))
# For Crowdsale contract
def invest(s, state, msg_sender, msg_value):
eth_balances, raised, escrow_state = state
inv_deposits, escrowstate = escrow_state
# require(now<=closeTime)
require(s, raised < GOAL)
# escrow.deposit.value(msg.value)(msg.sender);
eth_balances, raised, escrow_state = escrow_deposit(s, state, OWNER, msg_value, msg_sender)
raised += msg_value
return (eth_balances, raised, (inv_deposits, escrowstate))
def close(s, state, msg_sender, msg_value):
_, raised, escrow_state = state
_, escrowstate = escrow_state
# time can be omitted(?)
# require(now > closeTime || raised >= goal);
# if int(str(raised)) >= int(str(GOAL)): # escrow.close();
# state = escrow_close(s, state, msg_sender, msg_value)
# else: # escrow.refund();
# state = escrow_refund(s, state, msg_sender, msg_value)
p = If(raised >= GOAL,
escrowstate == EscrowState.SUCCESS,
escrowstate == EscrowState.REFUND)
require(s, p)
return state
## External call to TRs
## - Wrap the TRs around abstraction to symbolize the params
## - Computation with onlyOwner modifier can be temporary ignored
def symbolic_invest(s, state, myUser):
user = myUser
value = FreshConst(UintSort, 'value')
# arbitary user except Escrow and Crowdsale
require(s, user != Escrow_Address)
require(s, user != OWNER)
state = invest(s, state, user, value)
return state
def symbolic_close(s, state, myUser):
user = myUser
value = FreshConst(UintSort, 'value')
# arbitary user except Escrow and Crowdsale
require(s, user != Escrow_Address)
require(s, user != OWNER)
state = close(s, state, user, value)
return state
def symbolic_escrow_withdraw(s, state, myUser):
user = myUser
value = FreshConst(UintSort, 'value')
require(s, user != Escrow_Address)
require(s, user != OWNER)
state = escrow_withdraw(s, state, user, value)
return state
def symbolic_escrow_claimRefund(s, state, myUser):
user = myUser
value = FreshConst(UintSort, 'value')
require(s, user != Escrow_Address)
require(s, user != OWNER)
state = escrow_claimRefund(s, state, user, value, user)
return state
def is_ok_r0(state, myUser):
# TODO
# is_ok_r0 :: (...) -> predicates
# @predicate: property to be settled
# p = TODO
# new_state = withdraw(s2, state, myUser, initialDeposit)
eth_balances, raised, escrow_state = state
inv_deposits, escrowstate = escrow_state
# p = If(escrowstate != EscrowState.SUCCESS,
# inv_deposits[myUser] == eth_balances[Escrow_Address],
# True)
p = If(escrowstate != EscrowState.SUCCESS,
raised == eth_balances[Escrow_Address],
True)
return p
# --- Proving ---
# Yet another expression problem
# - r0: The sum of all investor deposits equals the ether balance
# of the escrow unless the crowdsale is declared successful
s, state, myUser = initial_state()
def sanity_check(cur_state, myUser):
# sanity check. Let's make sure the initial state is valid
s.push()
s.add(Not(is_ok_r0(cur_state, myUser)))
# print(s.assertions())
# assert(s.check() == unsat)
s.pop()
sanity_check(state, myUser)
def test(s, state):
pass
print("BMC Inductive Proof of property r0|r1")
@my_proof(s)
def proof_invest():
new_state = symbolic_invest(s, state, myUser)
require(s, Not(is_ok_r0(new_state, myUser)))
@my_proof(s)
def proof_escrow_withdraw():
new_state = symbolic_escrow_withdraw(s, state, myUser)
require(s, Not(is_ok_r0(new_state, myUser)))
# print(s.assertions())
@my_proof(s)
def proof_escrow_claimRefund():
new_state = symbolic_escrow_claimRefund(s, state, myUser)
require(s, Not(is_ok_r0(new_state, myUser)))
# print(s.assertions())
@my_proof(s)
def proof_close():
new_state = symbolic_close(s, state, myUser)
require(s, Not(is_ok_r0(new_state, myUser)))
# print(s.assertions())
run_proofs()