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

Wrc20 balance fixes #310

Merged
merged 31 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
180cf4f
Add #wrap rules corresponding to modInt Rules
hjorthjort Mar 24, 2020
af90492
Fix existential variable
hjorthjort Mar 24, 2020
645ef9e
More wrapping rules
hjorthjort Mar 24, 2020
ae5e70f
WIP: Add speed-up lemma
hjorthjort Mar 24, 2020
1d5ebd1
Merge branch 'master' into small-fixes
hjorthjort Apr 1, 2020
98d0fef
Clean up general lemmas
hjorthjort Apr 1, 2020
872f368
Start moving exact values in simplifications to side conditions
hjorthjort Apr 1, 2020
fc838b4
Simplify modInts to #wraps
hjorthjort Apr 1, 2020
319b6f1
Bugfix: missing syntax production
hjorthjort Apr 2, 2020
9088ebb
Make setRange total
hjorthjort Apr 2, 2020
eae41fa
New #getRange lemmas
hjorthjort Apr 2, 2020
b48a3b2
Formatting
hjorthjort Apr 9, 2020
5d9b041
Remove the loop-lemma, not giving any speed-up
hjorthjort Apr 9, 2020
75ce6eb
Formatting
hjorthjort Apr 9, 2020
5b31383
Documentation update
hjorthjort Apr 15, 2020
5be32b9
Merge branch 'master' into wrc20-balance-fixes
hjorthjort Apr 15, 2020
53032a4
Remove unused and bad lemma, caused branching
hjorthjort Apr 15, 2020
cf57722
Nits
hjorthjort Apr 15, 2020
9e584ab
Update lemmas
hjorthjort Apr 16, 2020
03cfb32
Fix side condition
hjorthjort Apr 16, 2020
8ef1f61
Merge branch 'master' into wrc20-balance-fixes
hjorthjort Apr 16, 2020
013e6c4
Remove commented out lemma
hjorthjort Apr 16, 2020
3172a96
Specilalize #getRange lemma
hjorthjort Apr 21, 2020
62a6c3a
Fix getRange side conditions for when WIDTH <= 0
hjorthjort Apr 21, 2020
dac0971
Merge remote-tracking branch 'origin/master' into wrc20-balance-fixes
hjorthjort Apr 21, 2020
e9e90b7
Typo in annotation
hjorthjort Apr 21, 2020
1d6c9fa
Merge branch 'master' into wrc20-balance-fixes
hjorthjort Apr 21, 2020
b44aa92
Merge branch 'master' into wrc20-balance-fixes
hjorthjort Apr 21, 2020
54c9830
Merge branch 'master' into wrc20-balance-fixes
ehildenb Apr 22, 2020
3026fc8
Merge branch 'master' into wrc20-balance-fixes
hjorthjort Apr 23, 2020
e61e2cf
Merge branch 'master' into wrc20-balance-fixes
hjorthjort Apr 28, 2020
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
4 changes: 2 additions & 2 deletions data.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@ However, `ByteMap` is just a wrapper around regular `Map`s.
`#setRange(BM, START, VAL, WIDTH)` writes the integer `I` to memory as bytes (little-endian), starting at index `N`.

```k
syntax ByteMap ::= #setRange(ByteMap, Int, Int, Int) [function]
// ---------------------------------------------------------------
syntax ByteMap ::= #setRange(ByteMap, Int, Int, Int) [function, functional, smtlib(setRange)]
// ---------------------------------------------------------------------------------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We technically shouldn't do this because #set is not functional. We should actually remove functional from the #set function.

But we have an issue tracking it, and I guess it should be OK because once #set is fixed, we should be OK here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like the right thing to do. Thanks

rule #setRange(BM, _, _, WIDTH) => BM
requires notBool (WIDTH >Int 0)
rule #setRange(BM, IDX, VAL, WIDTH) => #setRange(#set(BM, IDX, VAL modInt 256), IDX +Int 1, VAL /Int 256, WIDTH -Int 1)
Expand Down
58 changes: 52 additions & 6 deletions kwasm-lemmas.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Not however that K defines `X modInt N ==Int X modInt (-N)`.

#### Rules for Expressions With Only Modulus

These are given in pure modulus form, and in form with `#wrap`, which is modulus with a power of 2 for positive `N`.

```k
rule X modInt N => X
requires 0 <=Int X
Expand All @@ -57,6 +59,10 @@ Not however that K defines `X modInt N ==Int X modInt (-N)`.
requires M >Int 0
andBool M <=Int N
[simplification]

rule #wrap(N, #wrap(M, X)) => #wrap(M, X)
requires M <=Int N
[simplification]
```

Proof:
Expand All @@ -71,6 +77,10 @@ Since 0 <= x mod m < m <= n, (x mod m) mod n = x mod m
andBool N >Int 0
andBool M modInt N ==Int 0
[simplification]

rule #wrap(N, #wrap(M, X)) => #wrap(N, X)
requires notBool (M <=Int N)
[simplification]
```
ehildenb marked this conversation as resolved.
Show resolved Hide resolved

Proof:
Expand Down Expand Up @@ -101,12 +111,12 @@ x = m * q + r, for a unique q and r s.t. 0 <= r < m
[simplification]

rule #wrap(N, (X <<Int M) +Int Y) => #wrap(N, Y)
requires 0 <=Int N
requires 0 <=Int M
andBool N <=Int M
[simplification]

rule #wrap(N, Y +Int (X <<Int M)) => #wrap(N, Y)
requires 0 <=Int N
requires 0 <=Int M
andBool N <=Int M
[simplification]
ehildenb marked this conversation as resolved.
Show resolved Hide resolved
```
Expand All @@ -130,6 +140,14 @@ x * m + y mod n = x * (k * n) + y mod n = y mod n
andBool N >Int 0
andBool M modInt N ==Int 0
[simplification]

rule #wrap(N, #wrap(M, X) +Int Y) => #wrap(N, X +Int Y)
requires N <=Int M
[simplification]

rule #wrap(N, X +Int #wrap(M, Y)) => #wrap(N, X +Int Y)
requires N <=Int M
[simplification]
ehildenb marked this conversation as resolved.
Show resolved Hide resolved
```

Proof:
Expand All @@ -150,8 +168,12 @@ x mod m + y = r + y
We want K to understand what a bit-shift is.

```k
rule (X <<Int N) modInt M ==Int 0 => true
requires M modInt (2 ^Int N) ==Int 0
rule (X <<Int N) modInt M => 0
requires (2 ^Int N) modInt M ==Int 0
[simplification]

rule #wrap(M, X <<Int N) => 0
requires M <=Int N
[simplification]

rule (X >>Int N) => 0 requires X <Int 2 ^Int N [simplification]
Expand Down Expand Up @@ -324,8 +346,15 @@ They are non-trivial in their implementation, but the following should obviously
requires WIDTH ==Int 1
[simplification]

rule #getRange(BM, ADDR, WIDTH) modInt 256 => #get(BM, ADDR)
requires notBool (WIDTH ==Int 0)
rule #getRange(BM, ADDR, WIDTH) modInt BYTE_SIZE => #get(BM, ADDR)
requires BYTE_SIZE ==Int 256
ehildenb marked this conversation as resolved.
Show resolved Hide resolved
andBool notBool (WIDTH <=Int 0)
andBool #isByteMap(BM)
[simplification]

rule #wrap(N, #getRange(BM, ADDR, WIDTH)) => #get(BM, ADDR)
requires N ==Int 8
ehildenb marked this conversation as resolved.
Show resolved Hide resolved
andBool notBool (WIDTH <=Int 0)
andBool #isByteMap(BM)
[simplification]

Expand All @@ -334,6 +363,23 @@ They are non-trivial in their implementation, but the following should obviously
[simplification]
```

`#getRange` over `#setRange`

```k
rule #getRange(#setRange(BM, EA, VALUE, SET_WIDTH), EA, GET_WIDTH)
=> #wrap(GET_WIDTH *Int 8, VALUE)
requires GET_WIDTH <=Int SET_WIDTH
[simplification]

rule #getRange(#setRange(BM, EA, VALUE, SET_WIDTH), EA, GET_WIDTH)
=> #wrap(SET_WIDTH *Int 8, VALUE)
requires (notBool GET_WIDTH <=Int SET_WIDTH)
andBool #getRange(BM, EA +Int SET_WIDTH, GET_WIDTH -Int SET_WIDTH) ==Int 0
[simplificaton]

rule #getRange(ByteMap <| .Map |>, _, _) => 0 [simplification]
```

`#get` over `#setRange`.

```k
Expand Down
6 changes: 0 additions & 6 deletions tests/proofs/wrc20-spec.k
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ requires "wrc20.k"
module WRC20-SPEC
imports WRC20-LEMMAS

// Main spec.
// There is no specified behavior yet, so this claim is commented out.
//
// rule <k> #wrc20 </k>

// Reverse bytes spec.

rule <k> #wrc20ReverseBytes // TODO: Have this pre-loaded in the store.
Expand Down Expand Up @@ -44,7 +39,6 @@ module WRC20-SPEC
requires notBool unnameFuncType(asFuncType(#wrc20ReverseBytesTypeDecls)) in values(TYPES)
andBool ADDR +Int #numBytes(i64) <=Int SIZE *Int #pageSize()
andBool #isByteMap(BM)
// andBool #inUnsignedRange(i64, X) X is not bound in the LHS
andBool #inUnsignedRange(i32, ADDR)
ensures #get(BM, ADDR +Int 0) ==Int #get(?BM', ADDR +Int 7 )
andBool #get(BM, ADDR +Int 1) ==Int #get(?BM', ADDR +Int 6 )
Expand Down
30 changes: 25 additions & 5 deletions wrc20.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,34 @@ module WRC20-LEMMAS [symbolic]
imports KWASM-LEMMAS
```

This conversion turns out to be helpful in this particular proof, but we don't want to apply it on all KWasm proofs.
These conversions turns out to be helpful in this particular proof, but we don't want to apply it on all KWasm proofs.

```k
rule X /Int 256 => X >>Int 8
rule X /Int N => X >>Int 8 requires N ==Int 256 [simplification]
```

TODO: The two `#get` theorems below theorems handle special cases in this proof, but we should be able to use some more general theorems to prove them.
TODO: The `#get` theorems below theorems handle special cases in this proof, but we should be able to use some more general theorems to prove them.

```k
rule (#get(BM, ADDR) +Int (X +Int Y)) modInt 256 => (#get(BM, ADDR) +Int ((X +Int Y) modInt 256)) modInt 256 [simplification]
rule (#get(BM, ADDR) +Int X) >>Int 8 => X >>Int 8 requires X modInt 256 ==Int 0 andBool #isByteMap(BM) [simplification]
rule (#get(BM, ADDR) +Int (X +Int Y)) modInt N => (#get(BM, ADDR) +Int ((X +Int Y) modInt 256)) modInt 256
requires N ==Int 256
[simplification]

rule #wrap(N, #get(BM, ADDR) +Int (X +Int Y)) => #wrap(8, #get(BM, ADDR) +Int #wrap(8, X +Int Y))
requires N ==Int 8
[simplification]

rule (#get(BM, ADDR) +Int X) >>Int N => X >>Int 8
requires N ==Int 8
andBool X modInt 256 ==Int 0
andBool #isByteMap(BM)
[simplification]
```

TODO: The following theorems should be generalized and proven, and moved to the set of general lemmas.
Perhaps using `requires N ==Int 2 ^Int log2Int(N)`?
Also, some of these have concrete integers on the LHS.
It may be better to use a symbolic value as a side condition, e.g. `rule N => foo requires N ==Int 8`, because simplifications rely on exact matching of the LHS.

```k
rule X *Int 256 >>Int N => (X >>Int (N -Int 8)) requires N >=Int 8 [simplification]
Expand All @@ -54,6 +67,13 @@ Perhaps using `requires N ==Int 2 ^Int log2Int(N)`?
andBool X <Int 256
andBool M >=Int 8
[simplification]

rule #wrap(N, (X +Int (Y <<Int M))) => X +Int (#wrap(N, Y <<Int M))
requires N >=Int 8
andBool 0 <=Int X
andBool X <Int 256
andBool M >=Int 8
[simplification]
```

TODO: The following theorem should be proven, and moved to the set of general lemmas.
Expand Down