Skip to content

Commit 3cb310e

Browse files
anvacarurv-auditor
andauthored
Add accounts invariants (#2249)
* strenghten invariant * Set Version: 1.0.411 * apply review suggestions * Set Version: 1.0.412 * Set Version: 1.0.412 --------- Co-authored-by: devops <[email protected]>
1 parent 7fa1444 commit 3cb310e

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

kevm-pyk/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "kevm-pyk"
7-
version = "1.0.411"
7+
version = "1.0.412"
88
description = ""
99
authors = [
1010
"Runtime Verification, Inc. <[email protected]>",

kevm-pyk/src/kevm_pyk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
from typing import Final
77

88

9-
VERSION: Final = '1.0.411'
9+
VERSION: Final = '1.0.412'

kevm-pyk/src/kevm_pyk/kevm.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pyk.ktool.kprove import KProve
1313
from pyk.ktool.krun import KRun
1414
from pyk.prelude.kint import intToken, ltInt
15-
from pyk.prelude.ml import mlEqualsTrue
15+
from pyk.prelude.ml import mlEqualsFalse, mlEqualsTrue
1616
from pyk.prelude.string import stringToken
1717
from pyk.proof.reachability import APRBMCProof, APRProof
1818
from pyk.proof.show import APRBMCProofNodePrinter, APRProofNodePrinter
@@ -242,16 +242,39 @@ def short_info(self, cterm: CTerm) -> list[str]:
242242

243243
@staticmethod
244244
def add_invariant(cterm: CTerm) -> CTerm:
245+
def _add_account_invariant(account: KApply) -> list[KApply]:
246+
_account_constraints = []
247+
acct_id, balance, nonce = account.args[0], account.args[1], account.args[5]
248+
249+
if type(acct_id) is KApply and type(acct_id.args[0]) is KVariable:
250+
_account_constraints.append(mlEqualsTrue(KEVM.range_address(acct_id.args[0])))
251+
_account_constraints.append(
252+
mlEqualsFalse(KEVM.is_precompiled_account(acct_id.args[0], cterm.cell('SCHEDULE_CELL')))
253+
)
254+
if type(balance) is KApply and type(balance.args[0]) is KVariable:
255+
_account_constraints.append(mlEqualsTrue(KEVM.range_uint(256, balance.args[0])))
256+
if type(nonce) is KApply and type(nonce.args[0]) is KVariable:
257+
_account_constraints.append(mlEqualsTrue(KEVM.range_nonce(nonce.args[0])))
258+
return _account_constraints
259+
245260
constraints = []
246261
word_stack = cterm.cell('WORDSTACK_CELL')
247262
if type(word_stack) is not KVariable:
248263
word_stack_items = flatten_label('_:__EVM-TYPES_WordStack_Int_WordStack', word_stack)
249264
for i in word_stack_items[:-1]:
250265
constraints.append(mlEqualsTrue(KEVM.range_uint(256, i)))
251266

252-
gas_cell = cterm.cell('GAS_CELL')
253-
if not (type(gas_cell) is KApply and gas_cell.label.name == 'infGas'):
254-
constraints.append(mlEqualsTrue(KEVM.range_uint(256, gas_cell)))
267+
accounts_cell = cterm.cell('ACCOUNTS_CELL')
268+
if type(accounts_cell) is not KApply('.AccountCellMap'):
269+
accounts = flatten_label('_AccountCellMap_', cterm.cell('ACCOUNTS_CELL'))
270+
for wrapped_account in accounts:
271+
if not (type(wrapped_account) is KApply and wrapped_account.label.name == 'AccountCellMapItem'):
272+
continue
273+
274+
account = wrapped_account.args[1]
275+
if type(account) is KApply:
276+
constraints.extend(_add_account_invariant(account))
277+
255278
constraints.append(mlEqualsTrue(KEVM.range_address(cterm.cell('ID_CELL'))))
256279
constraints.append(mlEqualsTrue(KEVM.range_address(cterm.cell('CALLER_CELL'))))
257280
constraints.append(mlEqualsTrue(KEVM.range_address(cterm.cell('ORIGIN_CELL'))))
@@ -315,6 +338,10 @@ def range_bool(i: KInner) -> KApply:
315338
def range_bytes(width: KInner, ba: KInner) -> KApply:
316339
return KApply('#rangeBytes(_,_)_WORD_Bool_Int_Int', [width, ba])
317340

341+
@staticmethod
342+
def range_nonce(i: KInner) -> KApply:
343+
return KApply('#rangeNonce(_)_WORD_Bool_Int', [i])
344+
318345
@staticmethod
319346
def range_blocknum(ba: KInner) -> KApply:
320347
return KApply('#rangeBlockNum(_)_WORD_Bool_Int', [ba])
@@ -343,6 +370,10 @@ def bin_runtime(c: KInner) -> KApply:
343370
def init_bytecode(c: KInner) -> KApply:
344371
return KApply('initBytecode', [c])
345372

373+
@staticmethod
374+
def is_precompiled_account(i: KInner, s: KInner) -> KApply:
375+
return KApply('#isPrecompiledAccount(_,_)_EVM_Bool_Int_Schedule', [i, s])
376+
346377
@staticmethod
347378
def hashed_location(compiler: str, base: KInner, offset: KInner, member_offset: int = 0) -> KApply:
348379
location = KApply(

package/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.411
1+
1.0.412

0 commit comments

Comments
 (0)