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 page on formal verification for smart contracts #6394

Merged
merged 21 commits into from
Aug 4, 2022

Conversation

emmanuel-awosika
Copy link
Contributor

@emmanuel-awosika emmanuel-awosika commented May 19, 2022

This page offers a high-level introduction to the concept of formal verification of Ethereum smart contracts.

Preview deploy:
https://ethereumorgwebsitedev01-emmanuelawosikaethereumor06662.gtsb.io/en/developers/docs/smart-contracts/formal-verification

Description

@samajammin asked for contributions to a new documentation page on formal verification for smart contracts, so I put together a high-level introduction on the topic. The guide explains what formal verification means, how it works, and tools developers can use to conduct formal verification for Ethereum smart contracts.

Related Issue

#6194

This page offers a high-level introduction to the concept of formal verification of Ethereum smart contracts.
@gatsby-cloud
Copy link

gatsby-cloud bot commented May 19, 2022

Gatsby Cloud Build Report

ethereum-org-website-dev

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 13m

@samajammin
Copy link
Member

FYI I edited the PR description to link to the corresponding issue (#6194).

@emmanuel-awosika
Copy link
Contributor Author

FYI I edited the PR description to link to the corresponding issue (#6194).

Oh, I thought they were separate pages?

Fixed internal link issues.
---


## A brief introduction {#introduction-to-formal-verification}
Copy link
Member

Choose a reason for hiding this comment

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

@samajammin
Copy link
Member

@emmanuel-awosika haven't been able to review this in detail yet but at first glance it looks like a great start! I'll share this around with some folks to get some eyes & input on it. Thanks again!

FYI I just pushed a couple commits to rename the file (index.md in a directory is our typical pattern) & added the page to the docs navigation.


Formal verification is grounded in [formal methods](https://en.wikipedia.org/wiki/Formal_methods), which use mathematically rigorous methods for specifying, developing, and verifying software. Formal verification allows developers to mathematically model software programs and create formalized specifications, making testing more precise and robust.

Specifications are properties or used to describe the intended behavior of a smart contract. Formal verification can prove that a contract behaves exactly as described in the specification. Unlike testing based on informal assumptions, formal verification provides mathematical proof of a smart contract's correctness.
Copy link
Member

Choose a reason for hiding this comment

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

Specifications are properties or used to

This sentence doesn't make sense to me. Should it be "Specifications are properties used to", or something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a typo. The sentence should read "specifications are properties used to...". I'll edit the content to fix it.

emmanuel-awosika and others added 2 commits May 22, 2022 06:49
Fixed typos and added new information (about the Parity multisig wallet hack) to demonstrate the value of formal verification. The edit also includes the addition of formal verification projects focused on Ethereum.
Copy link
Member

@wackerow wackerow left a comment

Choose a reason for hiding this comment

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

@emmanuel-awosika This is something I am not well versed in, so I would love to get some more eyes on it, but overall I felt like this read well and made sense to read through.
Left just a couple small notes/adjustments, but overall 👍 from me =)


Specifications are a collection of properties used to describe the intended behavior of a smart contract. Formal verification can prove that a contract behaves exactly as described in the specification. Unlike testing based on informal assumptions, formal verification provides mathematical proof of a smart contract's correctness.

Formal verification is critical for smart contracts because minor vulnerabilities can lead to massive losses. For example, the [Parity Multisig wallet hack](https://www.freecodecamp.org/news/a-hacker-stole-31m-of-ether-how-it-happened-and-what-it-means-for-ethereum-9e5dc29e33ce/amp/) was the result of an unintended function that allowed the hacker to override owner settings and drain the contract of up to $31 million in ETH.
Copy link
Member

Choose a reason for hiding this comment

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

Curious, would this have helped if the contract was formally verified?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The consensus is that formal verification can help detect unintended code executions in smart contracts, so formal verification would probably have helped in the Parity case. More to the point, the Parity hack was mentioned in a couple of academic research and articles on formal verification, so I thought it'd be a good addition.


Reliability is a highly desired quality of smart contracts, and both users and developers want assurances that a smart contract will work as intended. This is important because code in the [Ethereum Virtual Machine (EVM)](/developers/docs/evm/) is immutable, which rules out the possibility of fixing bugs or retrieving funds lost to malicious attacks.

Formal verification is one of the recommended techniques for [testing smart contracts](/developers/docs/smart-contracts/testing/) and improving [smart contract security](/developers/docs/smart-contracts/security/). Formal verification involves testing a program (a smart contract in this case) to ensure it behaves as expected during execution.
Copy link
Member

Choose a reason for hiding this comment

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

Formal verification involves testing a program

This part isn't really precise, since FV is not about testing. I would actually recommend removing this line completely, since line 16 below already introduces FV well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing it out. I mixed up testing contracts (to check results) and evaluating contracts to see if they satisfy specific criteria (which is what I believe FV does). I'm working on rewriting certain page of the page to remove the inaccuracy.


Formal verification can help determine the correctness of a contract's compiled bytecode. This requires creating bytecode-based, low-level specifications that define how a smart contract should behave in the EVM environment.

Low-level formal verification uses techniques like static analysis and fuzzing to ensure that contract bytecode satisfies all properties. This assures developers that the business logic specified in the source code is what will run in the context of the EVM.
Copy link
Member

Choose a reason for hiding this comment

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

This is not 100% precise. There are also several tools that apply symbolic execution and model checking to EVM verification, which are FV techniques.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. I did a bit more research and found some more FV techniques (e.g., symbolic execution, model checking, and theorem proving), which are not mentioned in the page. I'll add those details while reworking the page, though.


### 1. Comprehensiveness {#comprehensiveness}

Formal verification uses automated tests, which can be run many times over. A formal verification assessment will likely cover the entire contract, reducing edge cases and hidden vulnerabilities.
Copy link
Member

Choose a reason for hiding this comment

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

Not precise, FV doesn't use tests. It mathematically abstracts the software behavior in a way that specialized procedures can guarantee correctness for every possible input case, without having to exhaustively go through each one of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just figured this out, too, after reading your comment and researching existing literature. I'll add this to the page while editing. Your comment may also be useful in highlighting the difference between FV and regular testing (i.e., that regular testing can only cover a small number of input cases).

Edits to rework the page.
@emmanuel-awosika
Copy link
Contributor Author

Hey @leonardoalt and @corwintines, I made a couple of substantial changes to the page on formal verification for Ethereum contracts. Feedback on the new version would be great. Also, @leonardoalt, I tried to rework the tools section a bit; I'm not totally familiar with them, so if any inputs you have on how to structure that section is welcome.

Edits to add new detail.
@emmanuel-awosika
Copy link
Contributor Author

emmanuel-awosika commented Jul 23, 2022

@leonardoalt, @corwintines; here are a couple more things I wanted to note:

  • Do I create a different section for logical frameworks (e.g., Isabelle/HOL and Coq) used in theorem proving? I'm not totally sure of the appropriate category here. I understand these are used as part of a larger verification framework, but I'm still trying to wrap my head around them--so your expert input would be welcome here.

Another suggestion for the above problem would be to create a section for theorem provers and place them together. Open to your thoughts on this.

  • Some tools, especially the ones based on symbolic execution (e.g., Manticore, Mythril, etc.), also feature on the smart contract testing page. Is it okay to leave them there, or do we separate them to avoid confusion?

  • The testing page also has a section for formal verification (this was when I thought the two were similar). What do you think about taking it out? (Since we clearly say FV is not testing on this page).

  • I added some academic papers to provide more context on formal verification since the articles gloss over many details. Open to suggestions on leaving this in or taking it out.

@leonardoalt
Copy link
Member

Hi @emmanuel-awosika

Another suggestion for the above problem would be to create a section for theorem provers and place them together.

I think they do belong in a different section, since they're not really specific tools used for smart contracts, but may be used for it too.

Some tools, especially the ones based on symbolic execution

IMO it's fine to leave them there too. Although I don't find Manticore that useful for quick testing as Mythril/hevm/fuzzers. But that's just my personal opinion.

What do you think about taking it out? (Since we clearly say FV is not testing on this page).

It could make sense to say it exists and point to this new page for details, without giving details there.

I added some academic papers to provide more context on formal verification since the articles gloss over many details.

I think I wouldn't add them. It's just too broad to be able to do justice to all relevant papers, plus even in FV of smart contracts alone there's too many papers.


- [How Formal Verification of Smart Contracts Works](https://runtimeverification.com/blog/how-formal-verification-of-smart-contracts-works/) [How Formal Verification Can Ensure Flawless Smart Contracts](https://media.consensys.net/how-formal-verification-can-ensure-flawless-smart-contracts-cbda8ad99bd1)
- [An Overview of Formal Verification Projects in the Ethereum Ecosystem](https://github.com/leonardoalt/ethereum_formal_verification_overview)
- [An Introduction to Formal Verification of Ethereum Contracts](https://github.com/pirapira/Ethereum-Formal-Verification-Overview)
Copy link
Member

Choose a reason for hiding this comment

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

This repo hasn't been updated in 4 years, so might be a bit misleading.

- [An Overview of Formal Verification Projects in the Ethereum Ecosystem](https://github.com/leonardoalt/ethereum_formal_verification_overview)
- [An Introduction to Formal Verification of Ethereum Contracts](https://github.com/pirapira/Ethereum-Formal-Verification-Overview)
- [SMTChecker and Formal Verification](https://docs.soliditylang.org/en/v0.8.15/smtchecker.html)

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this too.

**Oyente** - *_Static analysis tool for analyzing vulnerabilities in EVM bytecode with symbolic execution._*

- [GitHub](https://github.com/melonproject/oyente)

Copy link
Member

Choose a reason for hiding this comment

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

I'd remove Oyente and Securify.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed them now.

**Harvey** - *_Harvey is an automated fuzzing tool useful for detecting property violations in smart contract code._*

- [Website](https://consensys.net/diligence/fuzzing/)

Copy link
Member

Choose a reason for hiding this comment

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

Echidna and Harvey are fuzzers, so maybe it'd make sense to have a fuzzers section even though this is an FV page? Or maybe move that to the testing page actually?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. I took them out now.


- [GitHub](https://github.com/SRI-CSL/solidity)

**hevm** - *_hevm is a symbolic execution engine and equivalence checker for EVM bytecode._*
Copy link
Member

Choose a reason for hiding this comment

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

I think hevm fits the symbolic execution section better.

Edits to incorporate feedback.
@emmanuel-awosika
Copy link
Contributor Author

emmanuel-awosika commented Aug 1, 2022

Hi @leonardoalt. Based on your feedback, I made the following changes:

  • Added a new section for theorem proving tools
  • Removed Yoichi's GitHub repo and section on academic papers
  • Removed fuzzers from tooling section (they're on the testing on the page already)
  • Opened a new PR to tweak the formal verification section on the smart contract testing page

Let me know if there's any other change you want to see.

@minimalsm
Copy link
Contributor

Reminder that we should link to this page on the testing smart contracts page once this PR is ready.

@corwintines
Copy link
Member

Reminder that we should link to this page on the testing smart contracts page once this PR is ready.

@emmanuel-awosika @minimalsm brought in this link

Copy link
Member

@corwintines corwintines left a comment

Choose a reason for hiding this comment

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

This looks great. Thanks, @emmanuel-awosika, great work! I left one nitpick that isn't a blocker. I pinged @leonardoalt to see if they want to do one last pass, but I approve this PR :)

@emmanuel-awosika
Copy link
Contributor Author

Reminder that we should link to this page on the testing smart contracts page once this PR is ready.

@emmanuel-awosika @minimalsm brought in this link

Oh, yeah, I'd forgotten @samajammin added the file to the docs nav. Thanks for adding it.

@corwintines corwintines merged commit 91ddd6f into ethereum:dev Aug 4, 2022
@corwintines corwintines mentioned this pull request Aug 4, 2022
@emmanuel-awosika emmanuel-awosika deleted the patch-6 branch August 5, 2022 07:10
@leonardoalt
Copy link
Member

This looks great. Thanks, @emmanuel-awosika, great work! I left one nitpick that isn't a blocker. I pinged @leonardoalt to see if they want to do one last pass, but I approve this PR :)

Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content 🖋️ This involves copy additions or edits Status: Blocked 🛑 This is blocked
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants