Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Calldata generation for bytes, bytes[] parameters #2265

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ee2036f
Add dynamic array to `TypedArgs`
palinatolmach Dec 1, 2023
c55b808
WIP: add placeholder rules for dynamic arrays
palinatolmach Dec 1, 2023
e8ecb09
Set Version: 1.0.379
Dec 1, 2023
ea017c7
Remove a `TODO` comment
palinatolmach Dec 4, 2023
c8d8ab5
Merge branch 'master' into symbolic-calldata
palinatolmach Dec 6, 2023
9ce9f34
Set Version: 1.0.386
Dec 6, 2023
96de532
Add `abi_symbolic_calldata`
palinatolmach Dec 6, 2023
a75ba38
Add dynamic bytes array production
palinatolmach Dec 10, 2023
74dd5da
Merge branch 'master' into symbolic-calldata
palinatolmach Dec 10, 2023
9f5692a
Set Version: 1.0.392
Dec 10, 2023
d1acd6e
Reformat symbolic calldata
palinatolmach Dec 11, 2023
28671dd
Merge branch 'master' into symbolic-calldata
palinatolmach Dec 18, 2023
f9cebc9
Set Version: 1.0.398
Dec 18, 2023
32c5697
Experimental: build calldata for a specific test
palinatolmach Dec 20, 2023
47eaddc
Fix structured calldata with fixed array lengths
palinatolmach Dec 21, 2023
006b203
Merge branch 'master' into symbolic-calldata
palinatolmach Dec 21, 2023
aed61a1
Set Version: 1.0.406
Dec 21, 2023
692e32a
Merge branch 'master' into symbolic-calldata
palinatolmach Dec 22, 2023
d859002
Set Version: 1.0.407
Dec 22, 2023
6b4f4b3
Move manual calldata construction to functions
palinatolmach Dec 22, 2023
60beec5
Add calldata w/symbolic `bytes data` length
palinatolmach Dec 22, 2023
43a18d0
Use fully structured calldata by default
palinatolmach Dec 22, 2023
7e4409f
Lemmas on Boolean reasoning, set reasoning, map lookup (#2037)
PetarMax Dec 22, 2023
c51e7b6
summarization spec
PetarMax Dec 27, 2023
a9601ac
Set Version: 1.0.408
Dec 27, 2023
a08c113
general spec of copy_memory_to_memory provable
PetarMax Dec 29, 2023
26b3ca2
cleanup
PetarMax Dec 30, 2023
a82a267
preserving definedness
PetarMax Jan 14, 2024
0900c03
Merge branch 'master' into symbolic-calldata
palinatolmach Jan 15, 2024
512f4c7
Set Version: 1.0.416
Jan 15, 2024
3efd0da
merge with master
PetarMax Jan 16, 2024
a1b0660
Set Version: 1.0.418
Jan 16, 2024
57465be
Merge branch 'master' into symbolic-calldata
palinatolmach Jan 17, 2024
d672d4f
Set Version: 1.0.420
Jan 17, 2024
81cb534
Merge branch 'master' into symbolic-calldata
palinatolmach Jan 20, 2024
5cd9cc4
Set Version: 1.0.423
Jan 20, 2024
4ae65b6
Minor cleanup: remove unused hardcoded symbolic calldata
palinatolmach Jan 20, 2024
f5c2051
Add `#bytesArray`; `is_dynamic` to `#tuple`
palinatolmach Jan 21, 2024
33664d9
Add special `#encBytes` functions for encoding `bytes[]` args, remove…
palinatolmach Jan 22, 2024
18680d8
Add 1 Gb bound for `bytes`, `todo` comments for `#struct`
palinatolmach Jan 22, 2024
00b8be0
Set Version: 1.0.424
Jan 22, 2024
b8db13c
redefining #ceil32 to use the compiler-preferred way of rounding up
PetarMax Jan 23, 2024
02fdc42
Merge branch 'master' into symbolic-calldata-gen
PetarMax Jan 23, 2024
7229915
Set Version: 1.0.428
Jan 23, 2024
d497617
add #tuple encoding and remove hardcoded calldata
anvacaru Jan 24, 2024
06adbd8
implement concat for TypedArgs
anvacaru Jan 24, 2024
53833be
fix tuple encoding
anvacaru Jan 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kevm-pyk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "kevm-pyk"
version = "1.0.427"
version = "1.0.428"
description = ""
authors = [
"Runtime Verification, Inc. <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion kevm-pyk/src/kevm_pyk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from typing import Final


VERSION: Final = '1.0.427'
VERSION: Final = '1.0.428'
12 changes: 12 additions & 0 deletions kevm-pyk/src/kevm_pyk/kevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,18 @@ def abi_tuple(values: list[KInner]) -> KApply:
def abi_array(elem_type: KInner, length: KInner, elems: list[KInner]) -> KApply:
return KApply('abi_type_array', [elem_type, length, KEVM.typed_args(elems)])

@staticmethod
def abi_dynamic_array(elem_type: KInner) -> KApply:
return KApply('abi_type_dynamic_array', [elem_type])

# @staticmethod
# def abi_dynamic_bytes_array(elem_type: KInner) -> KApply:
# return KApply('abi_type_dynamic_bytes_array', [elem_type])

@staticmethod
def abi_bytes_array(length: KInner, elems_size: KInner, elems: list[KInner]) -> KApply:
return KApply('abi_type_bytes_array', [length, elems_size, KEVM.typed_args(elems)])

@staticmethod
def empty_typedargs() -> KApply:
return KApply('.List{"_,__EVM-ABI_TypedArgs_TypedArg_TypedArgs"}_TypedArgs')
Expand Down
75 changes: 70 additions & 5 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,16 @@ where `F1 : F2 : F3 : F4` is the (two's complement) byte-array representation of
| #bytes ( Bytes ) [klabel(abi_type_bytes), symbol]
| #string ( String ) [klabel(abi_type_string), symbol]
| #array ( TypedArg , Int , TypedArgs ) [klabel(abi_type_array), symbol]
| #dynArray( TypedArg ) [klabel(abi_type_dynamic_array), symbol]
| #bytesArray ( Int , Int , TypedArgs ) [klabel(abi_type_bytes_array), symbol]
| #tuple ( TypedArgs ) [klabel(abi_type_tuple), symbol]
// ----------------------------------------------------------------------------------------------

syntax TypedArgs ::= List{TypedArg, ","} [klabel(typedArgs)]
// ------------------------------------------------------------
syntax TypedArgs ::= "concat" "(" TypedArgs "," TypedArgs ")" [function, total]
// -------------------------------------------------------------------------------
rule concat(.TypedArgs, TA) => TA
rule concat((A, TA:TypedArgs), TB) => concat(TA, (A, TB))

syntax Bytes ::= #abiCallData ( String , TypedArgs ) [function]
// ---------------------------------------------------------------
Expand Down Expand Up @@ -266,13 +271,17 @@ where `F1 : F2 : F3 : F4` is the (two's complement) byte-array representation of

rule #typeName( #array(T, _, _)) => #typeName(T) +String "[]"

rule #typeName( #tuple(TARGS)) => "(" +String #generateSignatureArgs(TARGS) +String ")"
rule #typeName( #dynArray( T )) => #typeName(T) +String "[]"

rule #typeName( #tuple( ARGS )) => "(" +String #generateSignatureArgs(ARGS) +String ")"

rule #typeName( #bytesArray( _, _, _ )) => "bytes[]"

syntax Bytes ::= #encodeArgs ( TypedArgs ) [function]
syntax Bytes ::= #encodeArgsAux ( TypedArgs , Int , Bytes , Bytes ) [function]
// ------------------------------------------------------------------------------
rule #encodeArgs(ARGS) => #encodeArgsAux(ARGS, #lenOfHeads(ARGS), .Bytes, .Bytes)
rule #encodeArgs(#tuple(ARGS)) => #encodeArgs(ARGS)

rule #encodeArgsAux(.TypedArgs, _:Int, HEADS, TAILS) => HEADS +Bytes TAILS

rule #encodeArgsAux((ARG, ARGS), OFFSET, HEADS, TAILS)
Expand All @@ -283,6 +292,11 @@ where `F1 : F2 : F3 : F4` is the (two's complement) byte-array representation of
=> #encodeArgsAux(ARGS, OFFSET +Int #sizeOfDynamicType(ARG), HEADS +Bytes #enc(#uint256(OFFSET)), TAILS +Bytes #enc(ARG))
requires notBool(#isStaticType(ARG))

syntax Bool ::= #isTuple (TypedArg) [function, total]
// -----------------------------------------------------
rule #isTuple(#tuple(_)) => true
rule #isTuple(_) => false

syntax Int ::= #lenOfHeads ( TypedArgs ) [function, total]
// ----------------------------------------------------------
rule #lenOfHeads(.TypedArgs) => 0
Expand Down Expand Up @@ -397,8 +411,14 @@ where `F1 : F2 : F3 : F4` is the (two's complement) byte-array representation of

rule #lenOfHead( #string( _ )) => 32

rule #lenOfHead( #tuple( ARGS )) => #lenOfHeads(ARGS) requires #isStaticType(#tuple(ARGS))
rule #lenOfHead( #tuple( ARGS )) => 32 requires notBool #isStaticType(#tuple(ARGS))

rule #lenOfHead(#array(_, _, _)) => 32

rule #lenOfHead(#dynArray( _ )) => 32
rule #lenOfHead(#bytesArray( _, _, _ )) => 32

syntax Bool ::= #isStaticType ( TypedArg ) [function, total]
// ------------------------------------------------------------
rule #isStaticType( #address( _ )) => true
Expand Down Expand Up @@ -510,21 +530,44 @@ where `F1 : F2 : F3 : F4` is the (two's complement) byte-array representation of

rule #isStaticType(#array(_, _, _)) => false

rule #isStaticType(#dynArray( _ )) => false

rule #isStaticType(#tuple( ARGS )) => notBool #hasDynamicType(ARGS)

rule #isStaticType(#bytesArray( _ , _ , _)) => false

syntax Bool ::= #hasDynamicType(TypedArgs) [function, total]
// ------------------------------------------------------------
rule #hasDynamicType(.TypedArgs) => false
rule #hasDynamicType(T, TS ) => #hasDynamicType(TS) requires #isStaticType(T)
rule #hasDynamicType(T, _ ) => true requires notBool #isStaticType(T)

syntax Int ::= #sizeOfDynamicType ( TypedArg ) [function]
// ---------------------------------------------------------
rule #sizeOfDynamicType(#bytes(BS)) => 32 +Int #ceil32(lengthBytes(BS))

rule #sizeOfDynamicType(#tuple(ARGS)) => 32 +Int #sizeOfDynamicTypeAux(ARGS)

rule #sizeOfDynamicType(#array(T, N, _)) => 32 *Int (1 +Int N)
requires #isStaticType(T)

rule #sizeOfDynamicType(#array(T, N, ELEMS)) => 32 *Int (1 +Int N +Int #sizeOfDynamicTypeAux(ELEMS))
requires notBool #isStaticType(T)

syntax Int ::= #sizeOfDynamicTypeAux ( TypedArgs ) [function]
// -------------------------------------------------------------
// TODO(palina): placeholder rule for dynamic arrays
rule #sizeOfDynamicType(#dynArray(T)) => 32 *Int (1 +Int #sizeOfDynamicType(T))
requires notBool #isStaticType(T)

rule #sizeOfDynamicType(#bytesArray(N, L, _)) => 32 +Int N *Int (64 +Int #ceil32(L))

syntax Int ::= #sizeOfDynamicTypeAux ( TypedArgs ) [function, total]
// --------------------------------------------------------------------
rule #sizeOfDynamicTypeAux(TARG, TARGS) => #sizeOfDynamicType(TARG) +Int #sizeOfDynamicTypeAux(TARGS)
requires notBool #isStaticType(TARG)

rule #sizeOfDynamicTypeAux(TARG, TARGS) => #lenOfHead(TARG) +Int #sizeOfDynamicTypeAux(TARGS)
requires #isStaticType(TARG)

rule #sizeOfDynamicTypeAux(.TypedArgs) => 0

syntax Bytes ::= #enc ( TypedArg ) [function]
Expand Down Expand Up @@ -632,15 +675,37 @@ where `F1 : F2 : F3 : F4` is the (two's complement) byte-array representation of
rule #enc( #bytes32( DATA )) => #bufStrict(32, #getValue(#bytes32( DATA )))

rule #enc( #bool( DATA )) => #bufStrict(32, #getValue( #bool( DATA )))
rule #enc( #tuple( DATA )) => #encodeArgs(DATA)

// dynamic Type
rule #enc( #bytes(BS)) => #encBytes(lengthBytes(BS), BS)
// length of `bytes` variable is <= 1 Gb
ensures lengthBytes(BS) <=Int 1073741824

rule #enc(#array(_, N, DATA)) => #enc(#uint256(N)) +Bytes #encodeArgs(DATA)
rule #enc( #string(STR)) => #enc(#bytes(String2Bytes(STR)))
rule #enc(#bytesArray(N, L, DATA)) => #enc(#uint256(N)) +Bytes #encodeArgsBytes(DATA, L)

// TODO: placeholder rule for dynamic arrays
rule #enc(#dynArray(T)) => #enc(T)

syntax Bytes ::= #encBytes ( Int , Bytes ) [function]
// -----------------------------------------------------
rule #encBytes(N, BS) => #enc(#uint256(N)) +Bytes BS +Bytes #bufStrict(#ceil32(N) -Int N, 0)

// Auxilliary bytes encoding functions (for elements of `bytes[]`):
syntax Bytes ::= #encBytes ( Int , TypedArg ) [function]
syntax Bytes ::= #encodeArgsBytes( TypedArgs, Int ) [function]
syntax Bytes ::= #encodeArgsAuxBytes ( TypedArgs , Int , Bytes , Bytes, Int ) [function]
// -----------------------------------------------------
rule #encBytes(N, #bytes(BS)) => #enc(#uint256(N)) +Bytes BS +Bytes #bufStrict(#ceil32(N) -Int N, 0)
ensures lengthBytes(BS) ==Int N

rule #encodeArgsBytes(ARGS, N) => #encodeArgsAuxBytes(ARGS, #lenOfHeads(ARGS), .Bytes, .Bytes, N)
rule #encodeArgsAuxBytes(.TypedArgs, _:Int, HEADS, TAILS, _) => HEADS +Bytes TAILS

rule #encodeArgsAuxBytes((ARG, ARGS), OFFSET, HEADS, TAILS, N)
=> #encodeArgsAuxBytes(ARGS, OFFSET +Int #sizeOfDynamicType(ARG), HEADS +Bytes #enc(#uint256(OFFSET)), TAILS +Bytes #encBytes(N, ARG), N)
```

```k
Expand Down
2 changes: 1 addition & 1 deletion kevm-pyk/src/kevm_pyk/kproj/evm-semantics/buf.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Claims should always use `#bufStrict` in LHS and `#buf` in RHS.

syntax Int ::= #ceil32 ( Int ) [macro]
// --------------------------------------
rule #ceil32(N) => (N up/Int 32) *Int 32
rule #ceil32(N) => notMaxUInt5 &Int ( N +Int maxUInt5 )

endmodule

Expand Down
29 changes: 16 additions & 13 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/evm.md
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,23 @@ These are just used by the other operators for shuffling local execution state a
```k
syntax InternalOp ::= "#newAccount" Int
| "#newExistingAccount" Int
| "#newFreshAccount" Int
// --------------------------------------------
rule <k> #newAccount ACCT => #newExistingAccount ACCT ... </k> <account> <acctID> ACCT </acctID> ... </account>
rule <k> #newAccount ACCT => #newFreshAccount ACCT ... </k> [owise]
rule <k> #newAccount ACCT => . ... </k>
<accounts>
( .Bag
=>
<account>
<acctID> ACCT </acctID>
<balance> 0 </balance>
<code> .Bytes:AccountCode </code>
<storage> .Map </storage>
<origStorage> .Map </origStorage>
<nonce> 0 </nonce>
</account>
)
...
</accounts> [owise, preserves-definedness]

rule <k> #newExistingAccount ACCT => #end EVMC_ACCOUNT_ALREADY_EXISTS ... </k>
<account>
Expand All @@ -772,17 +785,6 @@ These are just used by the other operators for shuffling local execution state a
...
</account>
requires lengthBytes(CODE) ==Int 0

rule <k> #newFreshAccount ACCT => . ... </k>
<accounts>
( .Bag
=> <account>
<acctID> ACCT </acctID>
...
</account>
)
...
</accounts>
```

- `#transferFunds` moves money from one account into another, creating the destination account if it doesn't exist.
Expand Down Expand Up @@ -811,6 +813,7 @@ These are just used by the other operators for shuffling local execution state a
...
</account>
requires ACCTFROM =/=K ACCTTO andBool VALUE <=Int ORIGFROM
[preserves-definedness]

rule <k> #transferFunds ACCTFROM _ACCTTO VALUE => #end EVMC_BALANCE_UNDERFLOW ... </k>
<account>
Expand Down
2 changes: 1 addition & 1 deletion package/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.427
1.0.428
Loading