Ultimate magazine theme for WordPress.

10 products and services offered by PancakeSwap

Formal verification is a method of mathematically proving that a computer program works as intended. It involves expressing the properties and expected behavior of the program as mathematical formulas, and then using automated tools to verify that these formulas are correct. This process helps ensure that the program meets the 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 the desired specifications and behave correctly.
  • Software development: Verifying the correctness and reliability of software systems, especially in mission-critical applications such as aviation, medical devices, and financial systems.
  • Internet security: Assessing the security of cryptographic algorithms and protocols and identifying vulnerabilities in security-sensitive systems.
  • Artificial Intelligence and Machine Learning: Reviewing the properties and behavior of AI and ML models to ensure they work as intended and make accurate predictions.
  • Automated theorem proof: Verifying mathematical theorems and proving mathematical conjectures, which has applications in areas 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 smart contract verification works by representing the logic and desired behavior of smart contracts as mathematical statements, and then using automated tools to verify that those statements are correct.

The process includes:

  1. Defining the specifications and desired characteristics of a contract in a formal language.
  2. Translating contract code 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 are met.
  4. Repeating the verification process to find and fix errors or deviations from desired properties.

Sometimes the automated theorem provers or model checkers cannot prove or disprove that a property is 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 by making the specifications more detailed. This can make it easier for theorem provers and model checkers to validate that specifications and properties are true.

The formal review can be applied to one contract or to multiple contracts at the same time. Web3 projects often use multiple contracts and it is important to ensure that the contracts work together and correctly implement the desired project functionality.

This use of mathematical arguments helps ensure that smart contracts are free from bugs, vulnerabilities, and other unintended behavior. It also helps build trust 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 who each have a token balance (balance and balance2). The function transferFromUser1 transfers tokens from user 1 to user 2. The program has an invariant that the total stock of tokens is always equal to the sum of the balances.

uint balance1;
uint balance2;
uint totalSupply;

// Transfer money from user 1 to user 2.
function transferFromUser1(unit amount) {
balance1 = balance1 – amount;
balance2 = balance2 + amount;
}
Code Snippet 1: A simple program to illustrate a transfer

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

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

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

function transferFromUser1(unit amount) {

// Formula 1: totalSupply = balance1 + balance2

balance1 = balance1 – amount;

// old(balance1) represents the value of balance1 when calling 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 calling the function.
// Replace old(balance2) in Formula 4 and replace 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 ^ formula 6.
// Formula 7 expands to:
// (totalSupply = old(balance1) + old(balance2)) ^
// (balance1 = old(balance1) – amount) ^
// (credit2 = old(credit2) + amount)
}

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

If we want to check if transferFromUser1 preserves the program invariant, we can check if formula 7 (at the end of the function) implies the invariant (formula 1). Here’s the proof using high school algebra simplification rules.

Suppose 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:
Total supply = Balance1 + Balance2
How formal verification and manual review work together
Formal verification and manual review complement each other to ensure smart contract security.

Formal verification provides a systematic and automated way to compare the logic and behavior of the contract against desired properties, making it easier to identify and fix potential errors or bugs. It is particularly 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 think through all the possible combinations and cases that need to be checked. However, machines are well suited for this task.

Manual review provides human expert review of contract code, design and deployment. The auditor can use their experience and expertise to identify potential security risks and assess the overall security posture of the contract. You can also verify that the formal verification process has been completed correctly and look for issues that may not be detectable with automated tools. Their expert knowledge helps ensure that the specifications and desired properties used in formal verification are in fact the right ones.

The combination of formal verification and manual testing provides a comprehensive and thorough assessment of a smart contract’s security and increases the chances of finding and fixing vulnerabilities. The result is a defense-in-depth security approach that leverages the unique capabilities of humans and machines.

Diploma

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 an upcoming technical deep dive into ERC-20 token formal verification.

This is a guest post originally published here by CertiK.

What is CertiK:

CertiK is a blockchain security company that helps projects identify and remediate security vulnerabilities in blockchains, smart contracts, and Web3 applications through its services, products, and cybersecurity techniques.

Where to find CertiK:

Website | Twitter | Medium | Telegram | Youtube |

Learn Crypto Trading, Yield Farms, Income strategies and more at CrytoAnswers
https://nov.link/cryptoanswers

Comments are closed.

%d bloggers like this: