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

  • Musk calls for abolishing the Consumer Financial Protection Bureau

     on November 27th, Musk called for the abolition of the Consumer Financial Protection Bureau (CFPB) on social media platform X, stating that "there are too many redundant regulatory agencies."

  • Binance to Launch MORPHO and CHILLGUY USDT Perpetual Contracts

    Binance futures platform will launch perpetual contracts with a maximum leverage of up to 75 times at the following times:

  • Japanese fintech startup Habitto completes $11.7 million Series A funding

    Japanese fintech startup Habitto announced on Wednesday that it raised $11.7 million in Series A funding led by QED Investors and DG Daiwa Ventures, with participation from Anthemis Group and Scrum Ventures. Existing supporters include Saison Capital, GMO VenturePartners, Cherubic Ventures, and Epic Angels. The funds raised are intended to support Habitto's expansion of its digital banking platform.

  • Blockchain payment company Partior completes $80 million Series B financing, with Deutsche Bank participating

    blockchain payment company Partior has completed an $80 million Series B financing round, with Deutsche Bank joining as a new investor. Previously in July 2024, Partior announced it had completed a $60 million financing round with investors including Peak XV Partners, JPMorgan, Jump Trading Group, Standard Chartered Bank, Temasek, and Valor Capital Group.

  • Andy Ayrey: Truth Terminal treasury funds are being migrated, users do not need to panic

    On November 27th, Truth Terminal founder Andy Ayrey posted on X, stating that the Truth Terminal treasury is undergoing its final migration. There is no need to panic due to changes in funds, as all funds are being transferred to an appropriate, globally distributed multi-signature.

  • U.S. consumer confidence improves again in November, reaching a two-year high

    Dana M. Peterson, Chief Economist of the World Large Enterprises Federation, said, "US consumer confidence continued to improve in November, reaching the highest level in the past two years. The growth in November was mainly due to consumers' more positive assessment of the current situation, especially in the labor market. Compared with October, consumers' optimism about future employment opportunities has also greatly increased, reaching the highest level in nearly three years. At the same time, consumers' expectations for future business conditions have not changed, while their optimism about future income has slightly declined." Earlier, the US Conference Board Consumer Confidence Index for November recorded 111.7, a new high since July 2023.

  • Starknet: Phase 1 of STRK staking is now live on the mainnet

    Starknet announced that the first stage of STRK staking has officially launched on the mainnet.

  • CZ: Not trying to end the meme craze, just encouraging more builders

    CZ posted on X platform today, saying: "I am not against Meme coins, but Meme coins have become 'a little' strange now. Let's use blockchain technology to build practical applications." Some community users said that even Musk is a supporter of Meme coins, and it is very difficult to end this frenzy. CZ responded that "there is no attempt to end anything, everyone has the right to choose to invest or hold what they want. Just encourage more builders."

  • Talus Network Completes $6 Million Strategic Round of Financing with a Valuation of $150 Million

    decentralized AI protocol Talus Network raised $6 million in a strategic financing round led by Polychain Capital, valuing the company at $150 million. This funding will help further develop the Talus ecosystem, including the Protochain, Nexus framework, and "AI dating experience" application.

  • Careers in Crypto: 5 Insights for 2024

    In an overwhelming job market, leaning into personal networks and connections are more important than ever. Emily Landon, CEO of The Crypto Recruiters, outlines what is happening in the crypto job market and how you can position yourself or your company in 2024.