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

Add Isabelle proof script on the settlement algo #206

Merged
merged 4 commits into from
Jan 30, 2019

Conversation

pirapira
Copy link
Contributor

This is how I checked claims in the spec about the settlement algorithms.

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
Copy link
Contributor Author

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.

Copy link
Contributor

@hackaugusto hackaugusto left a 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

type_synonym impl_number = "int option"

definition valid :: "int \<Rightarrow> bool"
where "valid a = (0 \<le> a \<and> a < 32)"
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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...

Copy link
Contributor Author

@pirapira pirapira Jan 30, 2019

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.

Copy link
Contributor Author

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.

valid W1 \<Longrightarrow>
valid W2 \<Longrightarrow>
valid (T1 + L1) \<Longrightarrow> (* 10 *)
valid (T2 + L2) \<Longrightarrow> (* 10 *)
Copy link
Contributor

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?

Copy link
Contributor Author

@pirapira pirapira Jan 30, 2019

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))

Copy link
Contributor

@loredanacirstea loredanacirstea Jan 30, 2019

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

valid W2 \<Longrightarrow>
valid (T1 + L1) \<Longrightarrow> (* 10 *)
valid (T2 + L2) \<Longrightarrow> (* 10 *)
L1 <= D1 - W1 + T2 - T1 \<Longrightarrow> (* (5) *)
Copy link
Contributor

@hackaugusto hackaugusto Jan 29, 2019

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.)

Copy link
Contributor Author

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) *)

Copy link
Contributor Author

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.

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) *)
Copy link
Contributor

@hackaugusto hackaugusto Jan 29, 2019

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)

Copy link
Contributor Author

@pirapira pirapira Jan 30, 2019

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.

Copy link
Contributor

@loredanacirstea loredanacirstea Jan 30, 2019

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@loredanacirstea loredanacirstea Jan 30, 2019

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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 .

@pirapira
Copy link
Contributor Author

pirapira commented Jan 30, 2019

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?

No, a - b + c is always equal to c + a - b and so on.

So I defined impl_add and impl_sub that overflow and underflow into None. S1 and S2 use these overflowing&underflowing operations.

@pirapira pirapira changed the title Add Isabelle proof script on the settlement algo [wip] Add Isabelle proof script on the settlement algo Jan 30, 2019
@loredanacirstea
Copy link
Contributor

loredanacirstea commented Jan 30, 2019

I assume the order is actually enforced by doing impl_sub (impl_add (impl_sub (TLmax2 T2 L2) (TLmax1 T1 L1)) (Some D1)) (Some W1) and from what I see, you closely followed the Solidity implementation.

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.
Copy link
Contributor

@loredanacirstea loredanacirstea Jan 30, 2019

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 are valid & 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 both unlocks are done. In these cases, after settlement you can frequently have a different S1 or S2 value in the contract than S1 = D1 - W1 + T2 - T1 - L1, due to under/overflows. We need to make sure this difference balances out with the unlocks.

Copy link
Contributor Author

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 .

@pirapira
Copy link
Contributor Author

@loredanacirstea

I assume the order is actually enforced by doing impl_sub (impl_add (impl_sub (TLmax2 T2 L2) (TLmax1 T1 L1)) (Some D1)) (Some W1)

Your assumption is correct.

@pirapira pirapira changed the title [wip] Add Isabelle proof script on the settlement algo Add Isabelle proof script on the settlement algo Jan 30, 2019
@pirapira
Copy link
Contributor Author

@hackaugusto @loredanacirstea this PR is ready for review again.

Copy link
Contributor

@hackaugusto hackaugusto left a 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

@pirapira
Copy link
Contributor Author

@loredanacirstea has the Isabelle IDE on her machine, so can also edit the script.

@pirapira pirapira merged commit cc08728 into raiden-network:master Jan 30, 2019
@pirapira pirapira deleted the isabelle branch January 30, 2019 16:29
@loredanacirstea
Copy link
Contributor

Nope, I'm good with this PR. 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants