Towards Secure Program Partitioning for Smart Contracts With LLM's In-Context Learning

Ye Liu, Yuqing Niu, Chengyan Ma, Ruidong Han, Wei Ma, Yi Li, Debin Gao, David Lo

TL;DR

PARTITIONGPT is a novel framework that uses Large Language Models (LLMs) and in-context learning to automatically partition smart contracts into "privileged" and "normal" components. By isolating sensitive logic into secure execution environments, it effectively thwarts data manipulation attacks while reducing the trusted codebase size by 30% compared to traditional methods.

What Problem Does This Paper Address?

Smart contracts on blockchains like Ethereum are inherently transparent—every variable and state change is visible to the public. This transparency is a double-edged sword: while it ensures auditability, it also enables manipulation attacks. Attackers can observe sensitive internal states (like price bins or liquidity reserves) to orchestrate complex exploits such as price manipulation, front-running, or predictable randomness.

Existing defenses fall into two camps, both with flaws:

  1. Hardware-based (TEEs): Running an entire contract inside a Trusted Execution Environment (like Intel SGX) is secure but incredibly "heavy," leading to high gas costs and infrastructure dependency.
  2. Manual Partitioning: Developers can manually split code into sensitive and non-sensitive parts, but this is labor-intensive, error-prone, and often results in "coarse-grained" partitions (splitting entire functions rather than specific lines of code).

The authors seek to automate fine-grained program partitioning—splitting a contract at the statement level—to minimize the amount of code that needs to run in a secure environment without breaking the contract's original logic.

Methods

The authors introduce PARTITIONGPT, which combines traditional static analysis with the reasoning power of LLMs. The workflow follows seven key steps:

  1. Locate: Uses taint analysis on Program Dependence Graphs (PDGs) to identify which functions and specific statements interact with user-annotated "sensitive" variables.
  2. Slice: Generates two program slices: one for privileged (sensitive) logic and one for normal logic.
  3. Iterative Loop (The Core):
    • Generate: Uses an LLM (specifically GPT-4o mini) with in-context learning. By providing a few "seed" examples of successful partitions, the LLM learns how to refactor the Solidity code into a privileged sub-function and a public callback structure.
  4. Revise: If the generated code has syntax errors, the system feeds compiler errors back to the LLM for a "grammar fix."
    • Validate: A detector checks if any sensitive data "leaks" back into the public partition.
  5. Weighted Selection: Since LLMs can produce multiple variations, a fitness function ranks them based on edit distance (to keep code readable) and the size of the privileged partition (to minimize overhead).
  6. Formal Equivalence Checking: This is the "trust but verify" step. The authors built a custom prover using symbolic execution and the Z3 solver to ensure the partitioned contract behaves exactly like the original.

Datasets

The study utilized several benchmarks for evaluation:

  • Confidential Smart Contract Benchmark: 18 real-world contracts (from platforms like fhEVM and COTI) containing 99 sensitive functions. These were manually annotated to create a ground truth for "sensitive" variables.
  • Real-World Attack Gallery: 9 high-profile manipulation attacks (including the Jimbo protocol exploit) that resulted in a combined loss of over $25 million.
  • LLM Training Data: A newly created dataset of 1,267 program partitions used to tune the ranking coefficients for the fitness function.

Key Insights and Ideas

  • LLMs as Refactorers: Program partitioning is technically an NP-hard problem (specifically the multi-terminal cut problem). The paper demonstrates that LLMs are remarkably adept at handling the "messy" parts of code refactoring that traditional algorithms struggle with, provided they are guided by few-shot examples.
  • Fine-Grained > Coarse-Grained: By partitioning at the statement level rather than the function level, PARTITIONGPT reduced the Trusted Computing Base (TCB) by roughly 30%. Smaller TCBs mean lower gas costs and a smaller attack surface.
  • The Necessity of Formal Verification: In one example (Listing 2), the LLM successfully partitioned the code but introduced a subtle semantic bug in a flag assignment. This highlights that while LLMs are great at generating solutions, formal provers are essential to guarantee security in financial contexts.
  • Model Sensitivity: GPT-4o mini significantly outperformed open-source models like Llama-3.1 and Qwen-2.5, suggesting that the "reasoning" required for secure refactoring is currently a differentiator among top-tier models.

Key Results

  • High Accuracy: PARTITIONGPT successfully generated compilable, secure, and formally verified partitions for 78% of the sensitive functions tested.
  • Attack Mitigation: The framework effectively prevented 8 out of 9 real-world manipulation attacks. It only failed on one case (BelugaDex) because the root cause was a flaw in withdrawal logic rather than data transparency.
  • Moderate Overhead: When deployed in a TEE-based environment, the partitioned contracts saw a gas increase of 61% to 103%. While this sounds high, it is significantly lower than running the entire contract in a TEE and is a small price to pay for preventing multi-million dollar exploits.

Limitations and Future Work

  • Manual Annotation: The system still requires developers to manually tag which variables are "sensitive." Future work aims to automate this identification process.
  • State Explosion: The formal equivalence checker relies on symbolic execution, which can time out on extremely complex contracts with deep nested loops (e.g., the AuctionInstance case in the paper).
  • Declassification: The current model doesn't handle "declassification"—scenarios where sensitive data is intentionally made public after certain conditions are met.
  • Complex External Calls: The prover currently struggles with contracts that make complex external calls to other un-partitioned contracts.

Summary updated

BibTeX
@article{liu2026towards,
  author = {Ye Liu and Yuqing Niu and Chengyan Ma and Ruidong Han and Wei Ma and Yi Li and Debin Gao and David Lo},
  title = {{Towards Secure Program Partitioning for Smart Contracts With LLM's In-Context Learning}},
  journal = {IEEE Trans. Software Eng.},
  year = {2026},
  doi = {10.1109/tse.2026.3668858},
}
Figure