Cointime

Download App
iOS & Android

Formally Verifying OpenZeppelin’s ERC-20 Implementation

Validated Project

Open Zeppelin’s ERC-20 reference implementation is widely used in Web3. A bug in it would be devastating for the many contracts that derive from it. But how do we know that it is correct? And how do we know contracts that derive from it do so correctly and do not introduce bugs?

In Part One of this series on formal verification, we explained how CertiK uses formal verification to mathematically prove the correctness of ERC-20 contracts that we audit. Let’s dive in and see what happens when we apply it to OpenZeppelin’s code.In Part One of this series on formal verification, we explained how CertiK uses formal verification to mathematically prove the correctness of ERC-20 contracts that we audit. Let’s dive in and see what happens when we apply it to OpenZeppelin’s code.

ERC-20 Standard Properties

At CertiK, we have written property templates that precisely describe the expected behaviors of ERC-20 token contracts. These templates are generic: our tools analyze the implementation details of each smart contract and adapt the templates accordingly. We have 38 property templates.

Let's look at some of the properties that we regularly verify on incoming ERC-20 token contracts. For the sake of readability, the formulas that follow are slightly simplified and omit some technical details that do not contribute to the overall understanding of the approach. CertiK's audit reports contain an appendix that reproduces the formulas that have been used during model checking. A list with all the formulas used in our ERC-20 verification approach is publicly available here.

The transferFrom() function in ERC-20 contracts requires special attention, as it needs to distinguish between the initiator of the transaction (the address is msg.sender), the accounts that spend and receive tokens, and because it needs to observe the limits imposed by the entries in _balances and _allowances.

Specifying Correct Allowance Updates

When transferFrom() succeeds, it must deduct the amount of tokens that have been transferred from the allowance that the sender has over the spender's account. However, many ERC-20 token contracts also allow the token owner to grant infinite allowance to another account. This is reflected by setting that account's allowance to the maximum value, i.e. to ((2^256)-1). Taking that exception into account, a correct allowance update can be specified by the following LTL formula 𝜑:

It states that when transferFrom() is invoked and terminates (without reverting) with a return value of true, we expect that the sender's allowance is either reduced by the amount of tokens in amt (the red subformula) or that the sender either is the owner of the transferred tokens or has unlimited allowance over the spender's tokens. In those cases, the allowance must remain unchanged (the blue subformula).

Specifying Dismissal of Transfers That Exceed the Allowance

Attempts to use transferFrom() to transfer an amount of tokens that exceeds one´s allowance should fail. This is formalized by 𝜓:

If the invocation of transferFrom() requests to transfer tokens from somebody other than their owner and if that transfer exceeds the sender's allowance, we expect the transaction to either revert, or to fail and signal its failure by returning false.

These are only two examples of the formalizations CertiK uses to capture the expected behaviors of ERC-20 token contracts. For more information about our properties and all technical details, refer to our property list.

OpenZeppelin's Reference Implementation for ERC-20 Contracts

The OpenZeppelin library provides reference implementations for many popular smart contracts. Its ERC-20 base contracts are popular and often used as building blocks for DeFi projects.

As many of the projects we audit contain contracts that derive from OpenZeppelin, we formally verified a set of 38 security properties on their ERC-20 reference implementation as of version 4.7.3. As can be seen the image below, all properties of the base contract are proven correct.

This result, however, tells us little about the security of actual ERC-20 token implementations, even when they derive from the OpenZeppelin contract! In actual blockchain projects, the reference implementation is modified by overriding its virtual functions and by introducing additional public APIs. What if someone makes a mistake?

The correctness of a base contract is generally not sufficient to ensure security in derived tokens! It is possible to introduce errors not only when overriding parts of the base implementation, but also by making changes to the contract’s state variables that were unforeseen in the base implementation.

OpenZeppelin implements their ERC-20 base contracts by making important state variables private. This ensures that contracts derived from them cannot simply destroy invariants that hold for those private variables. This encapsulation provides a certain level of protection from errors introduced within derived contracts.

Example: PancakeSwap's CAKE Token

PancakeSwap is one of the most popular decentralized exchanges. It is powered by the CAKE token, which implements the BEP20 standard (an extension of the ERC-20 standard). In general, CAKE tokens can be earned by staking and by providing liquidity to the exchange. Running our model checker on the CAKE token implementation successfully proves the basic ERC-20 behavior. The token implementation satisfies all of the properties that CertiK verifies on basic ERC-20 token contracts.

Conclusion

We’ve used formal verification to mathematically prove that OpenZeppelin’s reference ERC-20 implementation v4.8 meets basic ERC-20 properties. While it is not surprising that it does, this is good to know! We explained why just verifying OpenZeppelin’s implementation is not enough. You need to formally verify contracts that derive from it. Be sure to watch for our next blog post, where we discuss handling extensions to ERC-20 and other real-world challenges in verifying.

Read more: https://www.certik.com/resources/blog/7EELzmUpEOE7yhow8LpA3A-formally-verifying-openzeppelins-erc-20-implementation

Get the latest news here: Cointime channel — https://t.me/cointime_en

Comments

All Comments

Recommended for you

  • Nvidia Soars in After-Hours Trading

    On May 14, Nvidia experienced a rapid increase in after-hours trading on the US stock market, rising nearly 2% to $230. (Dongxin News Agency)

  • China and US Agree to Build Constructive Strategic Stability Relationship

    Beijing, May 14th – On the morning of May 14th, Chinese President Xi Jinping held talks with US President Donald Trump, who is on a state visit to China, at the Great Hall of the People in Beijing. President Xi Jinping stated: "President Trump and I agree to establish a 'constructive strategic stability relationship' as the new positioning for China-US relations." (CCTV News)

  • Key Updates for May 14 Afternoon

    7:00-12:00 Keywords: Aave, Metaplanet, DeFi Development, Cerebras Systems 1. Aave: Cross-chain between the rsETH mainnet and L2 has reopened; 2. Metaplanet has postponed its preferred stock listing plan due to structural issues in the Japanese market; 3. The U.S. government seeks to confiscate $1.07 million in assets before sentencing of former Celsius executives; 4. Aave proposes to integrate the Babylon protocol in version V4, launching a native BTC lending module; 5. Solana Treasury's DeFi Development report states that the value of each SOL share has increased by 108% year-on-year; 6. Bipartisan negotiations on the Clarity Act failed to reach an agreement overnight, with Democrats divided over ethical and BRCA provisions; 7. AI chip manufacturer Cerebras Systems has raised $5.55 billion in its IPO, making it the largest fundraising of the year to date.

  • Trump: US-China Relations Will Be the Best in History

    On May 14, according to China News Service, US President Trump stated that the relationship between the United States and China will be the best in history.

  • TSMC Projects Global Chip Market to Reach $1.5 Trillion by 2030

    On May 14, according to market news, TSMC stated in presentation materials released ahead of a technical seminar on Thursday that it expects the global semiconductor market to exceed $1.5 trillion by 2030, up from a previous forecast of $1 trillion. TSMC anticipates that artificial intelligence and high-performance computing will account for 55% of the $1.5 trillion market, followed by smartphones (20%) and automotive applications (10%). TSMC also mentioned that the company is accelerating its capacity expansion plans for 2025 and 2026, with plans to build nine new fabs and advanced packaging facilities by 2026. (Dongxin News Agency)

  • Xi Jinping: Make 2026 a Historic Year for China-U.S. Relations

    Beijing, May 14 — On the morning of May 14, Chinese President Xi Jinping held talks with U.S. President Donald Trump, who is on a state visit to China, at the Great Hall of the People in Beijing. Xi emphasized that the common interests between China and the United States outweigh their differences, that the success of each country is an opportunity for the other, and that stable China-U.S. relations are beneficial to the world. The two sides should be partners, not adversaries, achieving mutual success and common prosperity, and forging a new path for major countries to coexist correctly in the new era. "I look forward to exchanging views with President Trump on major issues concerning both countries and the world, jointly steering and navigating the great ship of China-U.S. relations, and making 2026 a historic and landmark year for China-U.S. relations to build on past achievements and usher in a new future," Xi said. (Xinhua News Agency)

  • US Spot Ethereum ETF Sees Net Outflow of $36.25 Million Yesterday

    On May 14, according to monitoring by Trader T, the US spot Ethereum ETF experienced a net outflow of $36.25 million yesterday.

  • US Spot Bitcoin ETF Sees Net Outflow of $630.38 Million Yesterday

    On May 14, according to monitoring by Trader T, the US spot Bitcoin ETF experienced a net outflow of $630.38 million yesterday.

  • Xi Jinping Welcomes Trump to China

    On May 14, Chinese President Xi Jinping held a ceremony at the East Plaza of the Great Hall of the People in Beijing to welcome U.S. President Donald Trump on his visit to China. President Trump had arrived at the welcome ceremony site by car. (CCTV News)

  • U.S. Senate Confirms Kevin Walsh as Federal Reserve Chair

    On May 14, the U.S. Senate confirmed Walsh as the Chair of the Federal Reserve with a vote of 54 to 45. The Senate had previously approved Walsh as a Federal Reserve Governor for a 14-year term on the 12th. Following the approval of his chairmanship on the 13th, Walsh will officially assume office after completing the necessary signing procedures at the White House, succeeding the current Chair Powell, whose term ends this Friday (May 15). However, Powell is expected to remain on as a Federal Reserve Governor. This vote marks one of the most significant partisan divides in history: only one Democrat—Senator John Fetterman of Pennsylvania—voted in favor alongside the Republican majority.