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

PLT-5370 - Merkleization formalization #171

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hrajchert
Copy link
Collaborator

@hrajchert hrajchert commented Mar 24, 2023

As part of the Marlowe audit, the following concern was found

Screen Shot 2023-03-24 at 12 53 48

This DRAFT PR is an attempt to formalize merkleized contracts.

Two approaches were considered for this task: modify the contract type in the Core session to include the merkleized case, or create a new Merkle session that reuses everything it can from the Core session and prove that both versions are equivalent. We chose the latter approach for the following reasons.

Modifying the Core session would invalidate all current theories, while adding a new Merkle session does not invalidate anything but adds some duplication. For example, the Merkleized and Unmerkleized versions of reduceContractStep are the same, but we need to have both versions since the types are different.

If we modify the Core session, we would have the Merkleized and Unmerkleized versions under the same type, which can make the lemmas for equivalence harder to express. We would probably need to add an isMerkleized proposition and include it as an assumption in the lemmas, while having separate versions, the proposition is implied by the type.

If we decide to keep this approach, the following tasks need to be accomplished:

  • For all the functions in the MerkleInterpreter theory, we need to prove that if we can unmerkleize the contract then the payments, state, and warnings obtained from the unmerkleized function are equivalent to their merkleized counterpart.
  • Provide a merkleize algorithm (given a hash function with some assumptions).
  • Prove that merkleizing and unmerkleizing a contract yields back the original contract.

@hrajchert hrajchert marked this pull request as draft March 24, 2023 15:46
Comment on lines +85 to +91
fun evalSize :: "State \<Rightarrow> MContract \<Rightarrow> nat" where
"evalSize sta cont = length (accounts sta) + 2 * (size cont)"

lemma reduceMContractStepReducesSize :
"reduceMContractStep env sta c = Reduced twa tef nsta nc \<Longrightarrow>
(evalSize nsta nc) < (evalSize sta c)"
sorry
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was copy pasted and adapted from the evalBound in the Semantic module. This should later be modified to use the mcontract_size function, currently defined in the Unmerkleize module

Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't have any sorrys in main, we should change it to oops and comment out code that depends on it. I guess CI is passing because this is not added to CI yet, otherwise it shouldn't pass

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I left this as sorry on purpose because it is a draft PR, not intended to be merged on main, rather to start the discussion on the direction we should take to formalize merkleization. The most important thing to assess in this PR is which approach of the ones stated in the Readme should be used (or maybe one that I didn't thought of)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

On another note, I think the CI tests are only firing when we make a change in a haskell file, we should check this with the help of @shlevy

| NotReduced
| AmbiguousTimeIntervalReductionError

fun reduceMContractStep :: "Environment \<Rightarrow> State \<Rightarrow> MContract \<Rightarrow> ReduceStepMResult" where
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To improve readability in the future I might rename the functions and type to be the same as the Semantic module, and whenever we need to disambiguate we use the qualified name

@hrajchert hrajchert force-pushed the hrajchert/linorder-simplification branch from ee8c6a7 to 4f4d5f5 Compare March 28, 2023 18:12
Copy link
Collaborator

@palas palas left a comment

Choose a reason for hiding this comment

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

Thanks. I marked a few very concrete disagreements I have, but other than that it looks good

Comment on lines 369 to 373
theorem accountsArePositive :
"valid_state state \<Longrightarrow> (\<forall>x tok. positiveMoneyInAccountOrNoAccount x tok (accounts state)) \<Longrightarrow>
computeTransaction txIn state cont = TransactionOutput txOut \<Longrightarrow>
positiveMoneyInAccountOrNoAccount y tok2 (accounts (txOutState txOut))"
using computeTransaction_gtZero by blast
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why remove this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A couple of notes... Sadly, this change does not belong to the merkleization PR. I had the following dependency

Merkleization PR --depends--> Simplification of some linorder instances --depends--> PLT-3505 small fixes to the isabelle specification --depends--> PLT-4195 - Asset preservation theory

This change actually belongs to PLT-3505 small fixes to the isabelle specification. When I merged the Asset preservation theory I rebased the small fixes and the linorder PR but I forgot to update this one 🤦🏻 .

Regarding the change itself, it was due to this audit observation

Screen Shot 2023-04-07 at 11 04 26

The theorem is a duplicate of computeTransaction_gtZero (and actually is "automatically" solved using that)

@@ -2,9 +2,6 @@ theory QuiescentResult
imports Semantics PositiveAccounts
begin

lemma reduceOne_onlyIfNonEmptyState : "refundOne acc = Some c \<Longrightarrow> acc \<noteq> []"
by auto
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, this is a good theorem to have, not sure why remove it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Screen Shot 2023-04-07 at 11 08 05

The lemma wasn't actually being used and it is trivially solved, so probably any lemma that needs to know that the account isn't empty can use the simplifier directly

isabelle/Core/Semantics.thy Outdated Show resolved Hide resolved
isabelle/Core/Semantics.thy Outdated Show resolved Hide resolved
isabelle/Core/Semantics.thy Outdated Show resolved Hide resolved
isabelle/Core/SemanticsGuarantees.thy Outdated Show resolved Hide resolved
isabelle/Core/SingleInputTransactions.thy Outdated Show resolved Hide resolved
isabelle/Core/Timeout.thy Outdated Show resolved Hide resolved
isabelle/Core/Timeout.thy Outdated Show resolved Hide resolved
Comment on lines +85 to +91
fun evalSize :: "State \<Rightarrow> MContract \<Rightarrow> nat" where
"evalSize sta cont = length (accounts sta) + 2 * (size cont)"

lemma reduceMContractStepReducesSize :
"reduceMContractStep env sta c = Reduced twa tef nsta nc \<Longrightarrow>
(evalSize nsta nc) < (evalSize sta c)"
sorry
Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't have any sorrys in main, we should change it to oops and comment out code that depends on it. I guess CI is passing because this is not added to CI yet, otherwise it shouldn't pass

@hrajchert hrajchert force-pushed the hrajchert/linorder-simplification branch from 4f4d5f5 to 22e262d Compare April 7, 2023 15:01
@hrajchert hrajchert force-pushed the hrajchert/merkleization branch from bf97569 to 7290d26 Compare April 7, 2023 15:06
@hrajchert hrajchert force-pushed the hrajchert/merkleization branch from 7290d26 to d1c19fa Compare April 7, 2023 15:12
Base automatically changed from hrajchert/linorder-simplification to master April 19, 2023 14:14
@palas palas changed the title Merkleization formalization PLT-5370 - Merkleization formalization Apr 30, 2023
@hrajchert hrajchert force-pushed the hrajchert/merkleization branch from e6a8b7a to d1c19fa Compare May 4, 2023 19:21
@bwbush bwbush requested a review from ramsay-t November 15, 2023 14:37
Copy link
Collaborator

@bwbush bwbush left a comment

Choose a reason for hiding this comment

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

I don't have anything to add beyond Pablo's comments. Thanks!

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