Cointime

Download App
iOS & Android

What is Formal Verification?

Validated Project

Formal verification is a method of mathematically proving that a computer program functions as intended. It involves expressing the program's properties and expected behavior as mathematical formulas, and then using automated tools to check that these formulas hold true. This process helps ensure that the program meets its desired specifications.

Formal verification is a tool that can be applied to many systems, including:

  • Computer hardware design: Ensuring that integrated circuits and digital systems meet their desired specifications and behave correctly.
  • Software engineering: Verifying the correctness and reliability of software systems, especially in mission-critical applications such as aviation, medical devices, and financial systems.
  • Cybersecurity: Evaluating the security of cryptographic algorithms and protocols, and identifying vulnerabilities in security-sensitive systems.
  • Artificial intelligence and machine learning: Verifying the properties and behavior of AI and ML models, ensuring that they operate as intended and make accurate predictions.
  • Automated theorem proving: Verifying mathematical theorems and proving mathematical conjectures, which has applications in fields such as mathematics, physics, and computer science.
  • Blockchain and smart contracts: Ensuring the correctness, security, and reliability of blockchain systems and smart contracts.

Formal Verification of Smart Contracts

Formal verification of smart contracts works by representing the logic and desired behavior of smart contracts as mathematical statements, and then using automated tools to check if these statements are correct.

The process involves:

  1. Defining the specifications and desired properties of a contract in a formal language.
  2. Translating the code of the contract into a formal representation, such as mathematical logic or models.
  3. Using automated theorem provers or model checkers to validate that the specifications and properties of the contract hold true.
  4. Repeating the verification process to find and fix any errors or deviations from the desired properties.

Sometimes the automated theorem provers or model checkers cannot prove or disprove that a property holds true. In this case, the specifications and desired properties may need to be refined and the verification process repeated.

The specifications and desired properties can be refined by applying more specifications to smaller pieces of code or making the specifications more detailed. This can make it easier for the theorem provers and model checkers to validate that specifications and properties hold true.

Formal verification can be applied to one contract or to multiple contracts at a time. Web3 projects often use multiple contracts, and it is important to make sure that the contracts work together and implement the desired project functionality correctly.

This use of mathematical reasoning helps to ensure that smart contracts are free from bugs, vulnerabilities, and other unintended behavior. It also helps to increase trust and confidence in the contract, as its properties have been rigorously proven to be correct.

Translating code into a formal representation

Code Snippet 1 shows a simplified program that implements a token transfer function. There are two users that each have a balance of tokens (balance and balance2). The function transferFromUser1 transfers tokens from User 1 to User 2. The program has an invariant that the total supply of tokens always equals the sum of the balances.

uint balance1;
uint balance2;
uint totalSupply;

// Transfer money from User 1 to User 2.
function transferFromUser1(uint amount) {
    balance1 = balance1 - amount;
    balance2 = balance2 + amount;
}

Code Snippet 1: A simple program illustrating a transfer

We represent the invariant as a mathematical formula. We number formulas to keep track of them. Because formulas are mathematical, = means “equals”, not assignment, in them:

Formula 1: totalSupply =  balance1 + balance2     // sum of balances

Code Snippet 2 shows how we can add logical formulas representing what is true at each point in the function (for simplicity, we ignore the possibility of integer overflow. Handling that would make the formulas much longer).

function transferFromUser1(uint amount) {

  // Formula 1: totalSupply = balance1 + balance2

  balance1 = balance1 - amount;

  // old(balance1) represents the value of balance1 when entering the function.
  // Formula 2: totalSupply = old(balance1) + balance2
  // Formula 3: balance1 = old(balance1) - amount // implied by the assignment
  // Formula 4: Formula 2 ^ Formula 3

  balance2 = balance2 + amount;

  // old(balance2) represents the value of balance2 when entering the function.
  // Substitute old(balance2) into Formula 4, replacing balance2.
  // Formula 5: (totalSupply = old(balance1) + old(balance2)) ^
  //            (balance1 = old(balance1) - amount)
  // Formula 6: balance2 = old(balance2) + amount   // implied by the assignment
  // Formula 7: Formula 5 ^ Formal 6.
  // Formula 7 expands to:
  //            (totalSupply = old(balance1) + old(balance2)) ^
  //            (balance1 = old(balance1) - amount) ^
  //            (balance2 = old(balance2) + amount)
}

Code Snippet 2: Function with logical formulas representing the meaning of the code.

If we want to check that transferFromUser1 maintains the program invariant, we can check that Formula 7 (at the end of the function) implies the invariant (Formula 1). Here is the proof, using high-school algebra simplification rules.

Assume Formula 7 is true 
Solve for old(balance1) and old(balance2) using the last 2 clauses of Formula 7:
  old(balance1) = balance1 + amount
  old(balance2) = balance2 - amount
Substitute that into the first clause of Formula 7:
  totalSupply = (balance1 + amount) + (balance2 - amount)
Cancel addition and subtraction of amount:
   totalSupply = balance1 + balance2

How formal verification and manual auditing work together

Formal verification and manual auditing complement each other in ensuring the security of smart contracts.

Formal verification provides a systematic and automated way to check the contract's logic and behavior against its desired properties, making it easier to identify and fix any potential errors or bugs. It is especially useful for finding complex and subtle issues that may be difficult to detect through manual inspection. When dealing with complex or multiple contracts, it can become difficult for a human to reason about all the possible combinations and cases that need to be checked. Machines, however, are well-suited to this task.

Manual auditing provides a human expert review of the contract's code, design, and deployment. The auditor can use their experience and expertise to identify potential security risks and evaluate the contract's overall security posture. They can also verify that the formal verification process was performed correctly, and check for issues that may not be detectable with automated tools. Their expert insight helps ensure that the specifications and desired properties used in formal verification are indeed the right ones.

Combining formal verification and manual auditing provides a comprehensive and thorough evaluation of a smart contract's security, increasing the chances of finding and fixing any vulnerabilities. The result is a defense-in-depth approach to security that leverages the unique capabilities of both humans and machines.

Conclusion

This is an overview of what formal verification is and how it can be applied to increase the security of smart contracts and decentralized applications. Stay tuned for a forthcoming technical deep dive into the formal verification of ERC-20 tokens.

Read more: https://www.certik.com/resources/blog/3UDUMVAMia8ZibM7EmPf9f-what-is-formal-verification

Comments

All Comments

Recommended for you

  • Trump: Details of US-Iran Agreement to Be Released After Signing on 19th

    On June 16, during the G7 summit in Évian-les-Bains, France, U.S. President Trump stated that the details of the US-Iran agreement will be made public after its official signing on the 19th. (Xinhua News Agency)

  • Iranian Foreign Minister Announces Memorandum Signing on June 19

    On June 15, Iranian Foreign Minister Amir-Abdollahian stated that a meeting between the heads of the Iranian and American negotiating delegations is expected to take place in Switzerland on June 19, during which a memorandum of understanding between Iran and the United States will be signed, followed by the first round of subsequent negotiations. (CCTV International News)

  • U.S. Senior Officials: U.S. and Iran Sign Memorandum of Understanding

    On June 16, a senior U.S. official stated that the United States has signed a memorandum of understanding with Iran. U.S. President Trump and Vice President Pence signed the memorandum, and the Speaker of the Iranian Islamic Consultative Assembly also signed the document. The official also mentioned that the agreement stipulates the immediate opening of the Strait of Hormuz and the lifting of U.S. sanctions on Iran. Traffic in the strait will significantly increase starting immediately.

  • BTC Surpasses $67,000

    Market data shows that BTC has surpassed $67,000, currently priced at $67,197.47, with a 24-hour increase of 4.94%. The market is highly volatile, so please ensure proper risk management.

  • Musk's Wealth Reaches $1.2 Trillion as SpaceX Surpasses TSMC in Valuation

    On June 15, according to the latest global billionaire rankings released by Forbes, Elon Musk, the head of Tesla and SpaceX, has seen his personal wealth soar to an astonishing $1.2 trillion, setting a historical record. He became the world's first 'trillionaire' in the previous trading day. This wealth phenomenon is primarily attributed to the strong performance of his two flagship companies. Recent market data shows that SpaceX (SPCX) has reached a total valuation of $2.28 trillion (approximately $2.28 trillion), surging 8% in a single day, officially surpassing semiconductor giant TSMC (TSM), which has a market value of $2.26 trillion, and entering the top tier of U.S. stock market valuations, ranking sixth. Currently, the top three in the U.S. stock market by total market value are Nvidia ($5.05 trillion), Google, and Apple. SpaceX, with its absolute dominance in the commercial space and Starlink sectors, continues to see its valuation skyrocket, becoming the core pillar of Musk's trillion-dollar fortune.

  • Philadelphia Semiconductor Index Soars 4.7% in Early Trading

    On June 15, the Philadelphia Semiconductor Index opened high, rising by 4.7%. Nvidia's stock price increased by 2.67%, TSMC's stock price rose by 3.76%, Broadcom's stock price went up by 3.37%, Micron Technology's stock price surged by 9.31%, Advanced Micro Devices' stock price climbed by 6.61%, and ASML's stock price gained 1.47%.

  • SpaceX Raises Approximately $85.7 Billion in Initial Public Offering

    On June 15, SpaceX announced that underwriters have fully exercised their over-allotment option in the IPO, purchasing an additional 83.33 million shares. SpaceX has raised approximately $85.7 billion through the IPO.

  • Nasdaq Golden Dragon China Index Rises Over 1%

    On June 15, the Nasdaq Golden Dragon China Index rose over 1%. Canaan Inc. increased by 13.84%, EHang soared by 10.86%, Zai Lab gained 5.59%, Xunlei rose by 5.16%, and Kingsoft Cloud climbed by 5.31%.

  • Anthropic Sued by User for Allegedly Inflating Subscription Usage Limits

    On June 15, according to The Wall Street Journal, a consumer is seeking compensation from Anthropic for its highest-tier subscription plan and has accused the company of exaggerating the usage limits provided. The lawsuit claims that Anthropic misled consumers regarding the usage restrictions of its Max 5x and Max 20x subscription plans. The cheapest Pro subscription for individual users costs between $17 and $20 per month, while the Max 5x costs $100 per month and the Max 20x costs $200 per month. The lawsuit alleges that Anthropic advertised the Max 5x and Max 20x plans as having 5 times and 20 times the usage limits of the Pro plan, respectively, but the actual limits are difficult to determine and appear to be far below the advertised levels. The lawsuit seeks to qualify for a class action on behalf of users who purchased these packages since April of last year.

  • ETH Surpasses $1800

    Market data shows that ETH has surpassed $1800, currently priced at $1804.82, with a 24-hour increase of 8.2%. The market is experiencing significant fluctuations, so please ensure proper risk management.