-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add Isabelle proof script on the settlement algo #206
Conversation
definition TAD :: "int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> impl_number" where | ||
"TAD D1 D2 W1 W2 = impl_sub (impl_sub (impl_add (Some D1) (Some D2)) (Some W1)) (Some W2)" | ||
|
||
definition RmaxP1 :: "int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> impl_number" where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines look terrible as ASCII texts. The Isabelle IDE automatically shows this as
definition RmaxP2 :: "int ⇒ int ⇒ int ⇒ int ⇒ int ⇒ int ⇒ int ⇒ int ⇒ impl_number"
I know it's a peculiar engineering choice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Does the order of expressions matter for Isabelle? This is a big point of concern for the solidity code, as each operation may potentially overflow/underflow. For instance, TLmax2 - TLmax1 + D1 - W1
, has to be computed from left to right, otherwise overflow/underflow are possible (e.g. TLmax2 + D1
will overflow). IOW, isn't necessary to have valid
expressions for each intermediary value?
I will stop looking at this now, since I may be making implausible claims, if you find this useful @pirapira let me know and can go through the rest
proofs/Settlement.thy
Outdated
type_synonym impl_number = "int option" | ||
|
||
definition valid :: "int \<Rightarrow> bool" | ||
where "valid a = (0 \<le> a \<and> a < 32)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question, why do you use 0 \<le> a
on the right, but a < 32
on the left?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question, the idea is that if the proof works for the range [0,32] , it should also work for the range [0, 2**256], correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right.
It's [0, 32)
only because Isabelle finds counter-examples faster with the smaller region (but that's relevant only when the proof fails).
I'll try to change this into [0, 2**256)
and see if the proofs still go through.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I replace 32 with 2**256, the proof about S1 goes through but S2 fails. Investigating...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I need (D1 + D2) < 2 ** 256
. Not sure why. Ah, but it's called (12) already in the spec. Yay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done changing into "2 ** 256*.
question, why do you use 0 <le> a on the right, but a < 32 on the left?
Because 0 is a uint256
but 2 ^ 256
is no longer a uint256
.
proofs/Settlement.thy
Outdated
valid W1 \<Longrightarrow> | ||
valid W2 \<Longrightarrow> | ||
valid (T1 + L1) \<Longrightarrow> (* 10 *) | ||
valid (T2 + L2) \<Longrightarrow> (* 10 *) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how valid (T1 + L1)
and valid (T2 + L2)
follows form the rule 10
:
(10) B1 + B2 = TAD, where TAD = D1 + D2 - W1 - W2, TAD >= 0
Perhaps we should add this to the spec?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I'll make a PR to the spec. (edit: in spec it's called (11 R))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is:
(11 R) T1 + L1 < 2^256 ; T2 + L2 < 2^256
What I don't see is the individual constraints - e.g. T1 < 2^256
. I did not add them because this is handled by the EVM, but for the sake of completeness, we can add them. Suggestion: before Protocol Values Constraints
, as Implementation/Solidity-enforced Values Constraints
or similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see I made the numbering mistake. Will fix.
For individual T1
, T2
, etc I'll add comments because of EVM & Solidity types
in the Isabelle script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I have fixed the number valid (T1 + L1) \<Longrightarrow> (* (11 R) *)
.
Also I've added comments to individual variables: because it's uint256
.
proofs/Settlement.thy
Outdated
valid W2 \<Longrightarrow> | ||
valid (T1 + L1) \<Longrightarrow> (* 10 *) | ||
valid (T2 + L2) \<Longrightarrow> (* 10 *) | ||
L1 <= D1 - W1 + T2 - T1 \<Longrightarrow> (* (5) *) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct, however it doesn't seems to be the same things as (5 R) AB1 = D1 - W1 + T2 - T1 - L1; AB1 >= 0, AB1 <= TAD
, and it's more strict than (5.1 R) L1 <= TAD, L1 >= 0
I interpret the above as "a node must not accept a balance proof which would result in a negative available balance, or one larger than the total available deposit". While this rule says "a node must not have a locked amount larger than its total capacity".
Anyways, should we add this to the spec?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the following three are equivalent.
AB1 >= 0
(expanding AB1)
D1 - W1 + T2 - T1 - L1 >= 0
(moving L1)
D1 - W1 + T2 - T1 >= L1
But, I'll change the Isabelle script closer to the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I'll do the same to other conditions. Changing the Isabelle script more literally loyal to the spec.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I have something closer to the spec.
D1 - W1 + T2 - T1 - L1 \<ge> 0 \<Longrightarrow> (* (5 R) *)
D1 - W1 + T2 - T1 - L1 \<le> D1 + D2 - W1 - W2 \<Longrightarrow> (* (5 R) *)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked the forms of the other conditions too.
proofs/Settlement.thy
Outdated
valid (T2 + L2) \<Longrightarrow> (* 10 *) | ||
L1 <= D1 - W1 + T2 - T1 \<Longrightarrow> (* (5) *) | ||
D1 - W1 + T2 - T1 - L1 \<le> D1 + D2 - W1 - W2 \<Longrightarrow> (* (5) *) | ||
-(D1 - W1) <= T2 + L2 - T1 - L1 \<Longrightarrow> (* (7) *) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hummm, the rule ( 7 )
from the spec does not seem to sustain itself on its own, I think it should be broken down into two.
The current statement is (7 R) -(D1 - W1) <= T2 + L2 - T1 - L1 <= D2 - W2
, which boils down to two things (note that there is only one locked amount):
- A node cannot commit/send more than it has
-(D1 - W1) <= T2 - T1 - L1
- A node must not accept from its partner more than he has
T2 + L2 - T1 <= D2 - W2
Having both locked amounts in there would make this valid:
W1=0
, W2=0
, T1=0
, T2=0
, reduces -(D1 - W1) <= T2 + L2 - T1 - L1 <= D2 - W2
to -D1 <= L2 - L1 <= D2
D1 | D2 | L1 | L2 | -D1 <= L2 - L1 <= D2 |
---|---|---|---|---|
1 | 1 | 0 | 0 | true |
1 | 1 | 1 | 0 | true |
1 | 1 | 2 | 1 | true |
The last line above should not be true, however, with -(D1 - W1) <= T2 - T1 - L1
D1 | L1 | -D1 <= - L1 |
---|---|---|
1 | 0 | true |
1 | 1 | true |
1 | 2 | false |
I believe the above worked because of the stricter definition just above L1 <= D1 - W1 + T2 - T1
(but I have not checked this hypothesis)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I tried if Isabelle understands 0 <= 1 <= 2
; no it doesn't. It has to be 0 <= 1 /\ 1 <= 2
.
I guess
-(D1 - W1) <= T2 + L2 - T1 - L1 /\ T2 + L2 - T1 - L1 <= D2 - W2 (7 R)
is the best approximation. I'll use ^ unless I hear otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[update - yes, I was missing something, disregard the following]
The 3rd case seems weird:
D1 | D2 | L1 | L2 | -D1 <= L2 - L1 <= D2 |
---|---|---|---|---|
1 | 1 | 2 | 1 | true |
because to get to that case, we need a state of:
D1 | D2 | L1 | L2 |
---|---|---|---|
1 | 1 | 1 | 1 |
Here, both available balances are 0
: AB1 = 1 - 0 + 0 - 0 - 1 = 0
& AB2 = 1 - 0 + 0 - 0 - 1 = 0
. So, none of the participants can make transfers -> under (5 R)
you can't have L1 == 2
.
Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this discussion is about changing the condition in the spec, please take it there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated my comment - I missed the point first time I read it.
So, just to conclude:
So from -(D1 - W1) <= T2 + L2 - T1 - L1 <= D2 - W2
only the second part is useful in the actual demonstration: T2 + L2 - T1 - L1 <= D2 - W2
- this is used solve the + D1
overflow from (T2 + L2) - (T1 + L1) + D1 - W1
.
Agreed that the first part -(D1 - W1) <= T2 + L2 - T1 - L1
is less strict than needed to sustain itself. This was added after this discussion: #119 (comment) and I think it was good to add it.
They can be separated if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I dropped the first part and the proof still works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I opened an issue about changing the spec #209 .
No, So I defined |
I assume the order is actually enforced by doing The order can be important if our Solidity implementation detects an underflow while the Isabelle proof does not, because it uses another order that neutralizes it. But I don't think it's the case here. |
The result so far confirms two things: | ||
lemma s1_correct: The value calculated as 'S1 = RmaxP1 - SL2' is equal to 'S1 = D1 - W1 + T2 - T1 - L1'. | ||
lemma s2_correct: Similarly for 'S2 = RmaxP2 - SL1' and 'S2 = D2 - W2 + T1 - T2 - L2'. | ||
The required conditions appear in the statements of the lemma. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the notes that I already made in person:
s1_correct
&s2_correct
- stand only if both balance proofs arevalid
&last
; indeed, it should be always correct in this case.- for cases where at least one balance proof is
old
, we need to move the check down the channel lifecycle: after bothunlock
s are done. In these cases, after settlement you can frequently have a differentS1
orS2
value in the contract thanS1 = D1 - W1 + T2 - T1 - L1
, due to under/overflows. We need to make sure this difference balances out with theunlock
s.- Python tests reference: https://github.com/raiden-network/raiden-contracts/blob/76758d271059c96a8781754df3c599c4c948a2de/raiden_contracts/tests/test_channel_settle_unlock_state.py#L232-L244 (actually the entire
test_channel_settle_unlock_state.py::test_settlement_outcome
was made to only check the final, post-unlocks state, because this is our protocol guarantee)
- Python tests reference: https://github.com/raiden-network/raiden-contracts/blob/76758d271059c96a8781754df3c599c4c948a2de/raiden_contracts/tests/test_channel_settle_unlock_state.py#L232-L244 (actually the entire
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have an issue about extending the Isabelle proof for older balance proofs #207 .
Your assumption is correct. |
@hackaugusto @loredanacirstea this PR is ready for review again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I don't know if loredana has anything to add
@loredanacirstea has the Isabelle IDE on her machine, so can also edit the script. |
Nope, I'm good with this PR. 🥇 |
This is how I checked claims in the spec about the settlement algorithms.