I Know Who Clones Your Code: Interpretable Smart Contract Similarity Detection

Zhenguang Liu, Lixun Ma, Zhongzheng Mu, Chengkun Wei, Xiaojun Xu, Yingying Jiao, Kui Ren · DOI

TL;DR

This paper introduces SMARTDETECTOR, a novel approach for detecting similar smart contract functions with high accuracy and interpretability. By breaking down complex Abstract Syntax Trees (ASTs) into smaller "statement trees" and employing a sophisticated hyperparameter optimization technique, SMARTDETECTOR significantly outperforms existing methods, offering a clear view into why functions are deemed similar.

What Problem Does This Paper Address?

The widespread reuse of open-source code in smart contract development, while boosting efficiency, amplifies the propagation of bugs and vulnerabilities. Detecting similar smart contract functions is crucial for security, but existing methods face challenges. Traditional AST-based approaches struggle with the intricate structure of code, while deep learning methods often lack interpretability. Furthermore, the performance of these methods heavily relies on optimal hyperparameter settings, which are difficult to find in the vast search space. This paper aims to bridge this gap by providing an accurate, interpretable, and efficient solution for smart contract function similarity detection.

Methods

SMARTDETECTOR employs a multi-stage approach:

  1. AST Splitting into Statement Trees: The core innovation is decomposing the Abstract Syntax Tree (AST) of a smart contract function into smaller, semantically meaningful units called "statement trees." This granular approach simplifies comparison and allows for statement-level analysis. Six types of statement trees are defined (Variable Definition, Assignment Operation, Conditional Block, Control Loop, Function Call, and Other Operation).
  2. Feature Extraction: For each statement tree, seven types of category-level features are extracted. These features capture different aspects of the code, such as data types, operators, and code constructs, with weights assigned to each category based on their importance.
  3. Hyperparameter Optimization: To address the challenge of finding optimal classifier hyperparameters, SMARTDETECTOR uses a novel two-stage process:
  4. A hyperparameter evaluation neural network is trained to predict cross-entropy loss.
  5. A cosine-wise diffusion sampling process is employed to efficiently search for optimal hyperparameters around promising seeds, mathematically derived to sample in a cosine manner. This process is applied to optimize a LightGBM classifier, termed Smart-lightGBM.
  6. Similarity Computation: The Smart-lightGBM classifier computes similarity scores between pairs of statement trees from two functions. These scores are aggregated to determine the overall function-level similarity. A dual-threshold design is used to handle cases where one function is partially contained within another.

Datasets

The paper evaluates SMARTDETECTOR on four benchmark datasets:

  • FC-pairs: 328,268 smart contract function pairs from 115,800 Ethereum smart contracts.
  • ST-pairs: 1,799,972 statement tree pairs from 231,618 Ethereum smart contracts.
  • BL-pairs: 1,740 smart contract function pairs from high-access sources.
  • CP-pairs: 8,365 function pairs from Binance Smart Chain and Polygon smart contracts, demonstrating cross-platform generalizability.

The FC-pairs and ST-pairs datasets were collected from open-sourced Ethereum contracts and involved expert manual labeling.

Key Insights and Ideas

  • Statement Trees for Granularity: Decomposing ASTs into statement trees is crucial for overcoming the complexity of full ASTs and enabling fine-grained, interpretable similarity analysis.
  • Feature Categorization Matters: Not all AST elements are equally important for similarity detection. Categorizing nodes and assigning weights to these categories (e.g., data type and arithmetic operator features being highly weighted) significantly improves performance.
  • Diffusion Sampling for Hyperparameters: The cosine-wise diffusion sampling process offers a more efficient and effective way to optimize hyperparameters compared to traditional grid search or Bayesian optimization, especially in high-dimensional spaces.
  • Interpretability at Statement Level: By linking statement trees back to specific lines of code, SMARTDETECTOR provides a clear explanation for its similarity predictions, which is vital for developers and security auditors.
  • Traditional Methods Struggle with Smart Contracts: Graph-based and even some tree-based methods designed for traditional languages perform poorly on smart contracts, highlighting the need for specialized approaches that understand Solidity's nuances.

Key Results

  • Superior Accuracy: SMARTDETECTOR achieves an impressive average F1-score of 95.88% across datasets, with a significant average improvement of 14.01% over state-of-the-art methods. On the FC-pairs dataset, it achieved 93.81% precision, 91.80% recall, and 92.79% F1-score.
  • High Efficiency: SMARTDETECTOR demonstrates competitive efficiency, with a training time of 842 seconds and a testing time of 29 seconds on the FC-pairs dataset, significantly faster than methods like SmartEmbed. Its average detection speed per function pair is 0.2 ms.
  • Interpretability: The method provides detailed reports that pinpoint similar code lines, enabling developers to understand why functions are considered similar and to identify potential vulnerabilities.
  • Feature Importance: The study revealed that "data type node feature" and "arithmetic operator node feature" are among the most influential features for detecting similar code.

Limitations and Future Work

  • Robustness Against Adversarial Code: The current datasets may not fully capture advanced adversarial transformations that preserve semantics. Future work will focus on creating specialized evaluation sets to improve robustness.
  • Handling Inline Assembly: Inline assembly blocks in Solidity present challenges due to their opaque nature and limited AST support. While SMARTDETECTOR assigns lower weights to these nodes, bridging the semantic gap between inline assembly and high-level Solidity remains an open area.
  • Leveraging Similarity for Vulnerability Identification: While SMARTDETECTOR identifies similar code, it doesn't directly find vulnerabilities. Future work could integrate vulnerability mining with similarity detection to aid in discovering new vulnerabilities.
  • Scalability for Real-World Deployment: Direct similarity comparison can be computationally intensive for large-scale real-world deployments. Addressing scalability and validating practical deployment strategies are important future directions.

Summary updated

BibTeX
@article{liu2025i,
  author = {Zhenguang Liu and Lixun Ma and Zhongzheng Mu and Chengkun Wei and Xiaojun Xu and Yingying Jiao and Kui Ren},
  title = {{I Know Who Clones Your Code: Interpretable Smart Contract Similarity Detection}},
  journal = {IEEE Trans. Dependable Secur. Comput.},
  year = {2025},
  doi = {10.1109/tdsc.2025.3598985},
}

Loading related content...

Figure