SolType: Refinement Types for Solidity

Bryan Tan, Benjamin Mariano, Shuvendu K. Lahiri, Isil Dillig, Yu FengarXiv 2021

Abstract

Smart contracts deployed on blockchains like Ethereum manage significant financial assets, making arithmetic overflows a critical security risk. While developers currently rely on runtime checks (e.g., SafeMath) to prevent these vulnerabilities, such checks incur substantial "gas" costs. This paper presents SolType, a refinement type system designed to statically prove arithmetic safety in Solidity. By introducing relational-algebra refinements and a novel type inference mechanism based on soft constraints, SolType can automatically eliminate over 86% of redundant runtime checks while maintaining high precision and performance.

1. Introduction

In the ecosystem of decentralized finance (DeFi), smart contracts represent value through integer-based accounting. A single integer overflow or underflow can allow an attacker to manipulate balances, leading to catastrophic financial losses, as seen in the DAO and Parity Wallet exploits. To mitigate this, the standard practice is to replace native arithmetic operators with library calls that perform runtime checks. However, on the Ethereum Virtual Machine (EVM), every computation costs gas. Redundant checks—those performed on operations that are mathematically impossible to overflow—unnecessarily inflate transaction fees.

Existing static analysis tools often struggle with the complex data structures common in Solidity, such as nested mappings. This paper introduces SolType, a system that leverages refinement types to provide a formal, static guarantee of arithmetic safety, allowing developers to safely remove redundant runtime checks without compromising security.

2. System Design and Methodology

The authors implemented SolType in a tool called Solid. The system's architecture is built around three core innovations designed to handle the unique challenges of smart contract development.

2.1 Relational-Algebra Refinements

Refinement types pair base types with logical predicates (e.g., an integer v where v < 2^256). While standard refinements work for scalar values, they fail to capture the invariants of smart contracts that manage collections. SolType introduces relational-algebra operators—Sum, Flatten, and Fld (projection)—to express relationships between scalar variables and aggregate data. For instance, SolType can formally represent the invariant that the totalSupply of a token must equal the sum of all individual balances stored within a nested mapping.

2.2 MiniSol Intermediate Representation

To bridge the gap between high-level Solidity and formal verification, the authors developed MiniSol. This IR models the transactional nature of the blockchain using fetch and commit statements. This design acknowledges that global invariants (like the sum of balances) may be temporarily violated during the execution of a function. By using fetch to load state and commit to finalize it, SolType only enforces invariants at the boundaries of public functions, mirroring how smart contracts actually operate.

2.3 MaxCHC Type Inference

Manual type annotation is a significant burden for developers. Solid automates this process using Constrained Horn Clause (CHC) solvers. A common pitfall in static analysis is that a single bug in a program can cause the entire inference process to fail. To solve this, the authors treat safety checks as soft constraints (MaxCHC). This allows the tool to satisfy as many constraints as possible; if one operation is truly unsafe, the tool can still infer invariants for the rest of the contract and specifically flag the problematic operation.

3. Experimental Evaluation

The effectiveness of Solid was evaluated against 120 real-world smart contracts divided into two categories:

  1. VeriSmart Benchmarks: 60 contracts known to contain CVE-reported vulnerabilities.
  2. Etherscan Benchmarks: 60 randomly sampled modern contracts, 81.3% of which utilized complex nested mappings.

Solid was compared against VeriSmart, the previous state-of-the-art verifier for Solidity arithmetic.

4. Key Results

The evaluation demonstrated that SolType significantly outperforms existing methods in both efficiency and precision:

  • Check Elimination: In fully automated mode, Solid proved that 86.3% of SafeMath checks were redundant. In a semi-automated mode where developers provided top-level invariants, this rose to 90%.
  • Precision: Solid achieved a false positive rate of 20.4%, nearly half that of VeriSmart (39.3%). This reduction is largely attributed to SolType’s ability to reason about aggregate sums in mappings.
  • Performance: Solid is highly optimized for developer workflows, averaging 41 seconds per contract—a 10x speedup over VeriSmart’s average of 451 seconds.

5. Discussion and Limitations

SolType's success stems from its ability to link scalar integers to the aggregate properties of hash maps, a requirement for verifying ERC20-style contracts. However, the system has limitations. The current logic does not support quantified formulas (e.g., "for all X..."), which may be necessary for certain complex proofs. Additionally, the underlying CHC solver can occasionally time out on highly complex queries, leading to "unknown" results that contribute to the false positive rate.

6. Conclusion

SolType demonstrates that refinement types are a powerful tool for smart contract security. By combining relational algebra with a robust inference engine, Solid provides a path toward gas-efficient, provably safe arithmetic. Future work may extend this framework to verify other critical properties, such as reentrancy protection or access control logic.

Summary updated

BibTeX
@misc{tan2021soltype,
  author = {Bryan Tan and Benjamin Mariano and Shuvendu K. Lahiri and Isil Dillig and Yu Feng},
  title = {{SolType: Refinement Types for Solidity}},
  year = {2021},
  eprint = {2110.00677},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2110.00677},
}
Figure