Chiral Analysis of Smart Contracts: Detecting Vulnerabilities from Relational Inconsistencies Across Business Paths
This paper introduces "chiral analysis," a relational static analysis model implemented in the ChiralDetector tool that detects smart contract vulnerabilities by identifying inconsistencies between semantically paired business paths, effectively uncovering complex logic bugs that traditional single-function analyzers miss.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
The Detective's Dilemma: When Code Lies by Omission
Imagine you are a detective trying to solve a mystery in a bustling city. Usually, you look for a smoking gun: a broken window, a muddy footprint, or a suspicious note. In the world of computer code, specifically the "smart contracts" that run on blockchains, traditional security tools act like this detective. They scan the code line-by-line, looking for obvious mistakes like a missing lock on a door or a math error in a calculation. These tools are great at finding "local" bugs—the kind that happen in a single room.
But what if the crime isn't in the room at all? What if the mystery is that the front door is locked, but the back door is wide open, and the two doors were supposed to be part of the same security system? In computer science, this is called a "relational" problem. It's not about one broken piece; it's about two pieces that should match but don't. This paper explores a new way to catch these sneaky bugs by comparing pairs of code paths, treating them like mirror images that must reflect the same truth. If one path says "stop" and the other says "go," the system is broken, even if both "stop" and "go" look perfectly fine on their own.
The Paper: Chiral Analysis and the Mirror Test
This paper introduces a clever new method called Chiral Analysis. The word "chiral" comes from chemistry, describing objects that are mirror images of each other but cannot be perfectly stacked on top of one another (like your left and right hands). In the world of smart contracts, the authors propose that many business operations come in pairs: a "single" trade and a "batch" trade, a "buy" and a "sell," or a "preview" of a price and the "execution" of that price. These pairs are the chiral twins of the code.
The core idea is simple yet powerful: treat these pairs as if they are checking each other's homework. If a user buys an item, the code should charge them a fee. If that same user sells the item later, the code should handle the money in a way that makes sense with the original buy. If the "buy" path charges a fee in dollars but the "sell" path accidentally refunds in a different currency, or if the "batch" version forgets to refund money that the "single" version returns, a bug exists. The paper argues that these bugs are invisible to standard scanners because each individual line of code looks correct. The error only appears when you hold the two paths up to the mirror and see they don't match.
To find these invisible errors, the authors built a prototype tool called ChiralDetector. Think of this tool as a super-smart intern who has a specific job description. First, it reads the entire codebase and maps out every possible "business path" (like tracing every route a customer can take through a store). Then, it uses a "static ranker"—a set of simple rules—to guess which paths might be chiral twins. For example, it might look for a function named buy and another named sell that touch the same bank account.
Once it has a list of potential twins, it calls in the heavy hitter: a Large Language Model (LLM), which is a type of AI that understands human language and code. The AI doesn't just look for errors; it acts like a logician. It asks, "If these two paths are supposed to be mirrors, what rules should they follow?" It checks seven specific dimensions:
- Guards: Did both paths check the same passwords or permissions?
- Actors: Did the same person pay and receive money in both?
- State: Did both paths update the database in the same way?
- Value: Did they handle fees and refunds consistently?
- Order: Did they do things in the same sequence?
- Failure: If things go wrong, did both paths crash or recover the same way?
- External: Did they trust the same outside sources?
If the AI finds a mismatch, it doesn't just scream "Bug!" immediately. It passes the finding to a "strict validator." This validator is a skeptical editor who tries to prove the AI wrong. It asks, "Is this really a bug, or is it just a design choice?" Finally, the tool groups similar findings together so that instead of reporting 100 tiny errors, it reports the one big root cause.
The Results: Finding the Hidden Glitches
The authors tested this system on a real-world project called the Phi protocol. The results suggest that this approach works, though it's still a work in progress.
Here is what happened in their experiment:
- The tool started by looking at 3,217 pairs of code paths.
- After filtering out the ones that clearly weren't related, it kept 1,643 pairs to investigate deeply.
- The AI detector found 201 potential issues (a mix of "suspicious" and "confirmed" candidates).
- After grouping similar reports and removing duplicates, this shrank to 101 groups.
- A strict validator cut this down further to 44 confirmed positives.
- Finally, after a human expert reviewed the root causes, the team identified 13 unique, effective issues.
These 13 issues were the kind of things standard tools missed. For example:
- The "Proof" Mix-up: A system allowed a proof of ownership to be reused for a different item because the "buy" and "claim" paths didn't bind the proof to the specific item correctly.
- The Fee Confusion: One part of the system calculated fees in "basis points" (a unit of percentage) while another part treated the same number as "wei" (a tiny unit of currency), leading to massive financial errors.
- The Refund Trap: When a user overpaid, the "single" trade path refunded the money to the user, but the "batch" trade path accidentally sent the refund to an intermediate contract, leaving the user empty-handed.
The authors suggest that this method is particularly good at catching "business logic" bugs—errors in how the system thinks about money and rules—rather than simple typos. They note that the process isn't perfect; it generated a lot of "noise" (false alarms) that had to be cleaned up, and it relies on the AI to be smart enough to spot the relationship. However, the fact that it found 13 distinct, high-impact issues that other tools missed suggests that looking at code through the lens of "chiral pairs" is a promising new direction.
The paper concludes that while this isn't a magic wand that solves every security problem, it offers a structured way to find the bugs that hide in the spaces between lines of code. By treating code paths as mirror images, we can finally see the cracks that appear when the reflection doesn't match the object.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.