# Control AI for Smart Contracts: Can we stop backdoors in Solidity code generated by AI?

By [Shanzson](https://paragraph.com/@shanzson) · 2025-04-08

---

Introduction
------------

Generative AI is being increasingly used for all kinds of tasks as it is improving and mastering every action that a human can do from art to music, from video generation to writing. One key area where AI is excelling at is writing code, especially the latest models released by Anthropic’s Claude Sonnet 3.7 and its Claude Code, or the OpenAI’s GPT-4o.

As this AI understands and gets better at coding, it is undoubtedly replacing programmers and developers (at least being used in code-assist if not fully replaced). [Anthropic’s Economic Index](https://www.anthropic.com/news/the-anthropic-economic-index) introduced captures how and where its AI model is being used in terms of occupations. It can be seen from the report that about 37.2% of the conversations being Computer & Mathematics related and 16.8% of it being used for developing & maintaining software applications & websites.

The Problem
-----------

Smart contracts and blockchains are perhaps the most critical area of software where code is so critical that one cannot risk having bugs or backdoors in them. As any bug or backdoor can lead to an exploitable vulnerability resulting in millions of dollars of worth of loss of funds in crypto. Thus, if AI is used to generate code for smart contracts, it can become very risky if it goes unaudited or has hidden backdoors in it.

So we need a mechanism or protocol that can keep a check on the code generated by latest AI models, which are termed are Untrusted model here (see [this](https://arxiv.org/pdf/2312.06942)). The latest AI model such as GPT-4o in our research is assumed to be untrusted because even though it is very powerful, it can (intentionally or unintentionally) generate much sneakier malicious smart contract code that can go unnoticed.

We test and design protocol with a setup involving an Untrusted model such as GPT-4o and a Trusted Model such as GPT-3.5-turbo, building on the [previous research](https://github.com/UKGovernmentBEIS/control-arena) , but with a dataset that involves solidity smart contract code. Additionally, an older model is assumed to be more trusted than the latest released model as it is assumed to be more battle tested and less powerful.

If we are able to control even the most malicious Untrusted model via a protocol that involves either untrusted models, or trusted models or both, then we can be able to catch backdoors generated by an Untrusted model and fix them much before they turn into an exploitable vulnerability and potential loss of funds.

Methodologies
-------------

Let us first understand the methodology used for controlling an Untrusted AI model by [Ryan et al](https://arxiv.org/pdf/2312.06942). Their methodology involves creating 2 teams- One red team and Blue team. Almost in all the scenarios, the red team is tasked in using the Untrusted model of AI to solve coding problems from CodeForces while generating sneaky Malicious code. And the blue team is tasked with uncovering this hidden malicious vulnerability. If the red team gets away with adding a sneaky bug undetected (but passing the test cases), then it would mean that the red team has won. But if the blue team catches the red team while adding the sneaky bug, then it means that the blue team wins.

Here the goal is to find out those methodologies and settings(protocol in general) in which the win chances of blue team increase significantly given a powerful untrusted model to the red team.

Some of the main methodologies used by Ryan et al’s team are as follows:

_1)_ _Trusted monitoring_\- In trusted monitoring, the red team uses an Untrusted AI model such as GPT-4 and the Blue team uses a Trusted model such as GPT-3.5. The task of the Trusted model is to add a sneaky backdoor to the code before submitting it to the blue team and the blue team uses the Trusted model to monitor and check for the backdoor and hence the name trusted monitoring.

The blue team uses a suspiciousness rating to rate whether the code given by the red team is suspicious or not. This is done by asking the trusted model to rate whether the code looks suspicious to it. And it audits only the top 2% most suspicious code.

_2) Untrusted monitoring_\- This is a more intriguing scenario when things can turn out in more interesting ways. The setup is similar to the trusted setup above, with the only difference that, the blue team too has to use the Untrusted model that the red team uses. This can be gamed pretty fast by the red team if they train the untrusted model to lie and say that the code does not look suspicious, which would lead to the red team winning in 100% of the time.

But this can again be countered by the blue team by using strategies such as manually constructing a set of 30 problems and solutions and then asking the the untrusted model whether the code looks suspicious or not, and then shutting down the Untrusted model if the false negative rate is too high. This can significantly increase the win chances of the blue team.

_3) Defer to trusted monitoring_\- This method is the same as trusted monitoring. But the only difference is that if the Untrusted model’s output is too suspicious above a threshold, the blue team discards it and instead replaces the code with the code generated by the Trusted model.

### Metrics

There are 2 very important metrics that are used to evaluate the outcome and analyze the results. These 2 metrics, as defined in the paper, are Usefuleness and Safety.

To measure usefulness, the proportion of submitted code which is correct (i.e., passes all test cases) is measured. This metric reflects how well the control protocol solves the main task (e.i., programming problems), assuming the model is behaving honestly. Thus this is evaluated during an **honest evaluation** (no attacks) scenario of the trusted setup.

> usefulness = number of problems passed / total number of problems evaluated

Now to measure safety, the probability that the protocol submits backdoored code at least once during the sequence of problems is measured.

> safety = number of runs without any backdoor submissions / total number of runs

This metric reflects the **risk of a successful attack**: whether any backdoored output slips through undetected.

Solidity Trusted setup
----------------------

In our setup, we created a dataset of solidity problems and solutions by referring the Dapp World coding platform.

We decided to use the Trusted monitoring for our research, with GPT-4o as our Untrusted model and GPT-3.5-turbo as a Trusted model. The rest of the settings were similar to that of the APPs dataset setup that Ryan at al. had used in his paper.

The complete paper on our research can be found [here](https://docs.google.com/document/d/1qBAWruh7XF7WuDXvKPQDL0ty5L7GFbQaaXKPRQAg5qI/edit?usp=sharing).

![Figure 1 – The Task Score for Solidity dataset](https://storage.googleapis.com/papyrus_images/0b8208adcadd4eed2527c26a08ea3fb921c2e6c5ddfbe7c354fb1e59b21ee613.png)

Figure 1 – The Task Score for Solidity dataset

In the baseline honest mode of trusted monitoring, the Untrusted model still fails on passing all the test cases for all the problems as it fails in 4 out of 6 problems given to it. This means that the untrusted model is not sneaky enough or intelligent enough to introduce a bug or vulnerability in the code without failing the test cases(it should be noted that the untrusted model does not know of the test cases beforehand).

Based on Fig. 1, the Usefulness of this Trusted monitoring setup for solidity code will be as follows:

Usefulness = useful\_tasks / total\_tasks = 2 / 6 = 0.333 or 33.33%

And safety based on Fig. 2 is as follows:

Safety = safe\_tasks / total\_tasks = 4 / 6 ≈ 0.667 or 66.67%

Note that these usefulness and safety metrics will be more useful when they are calculated on large datasets. For the scope of this study, we had worked on a small dataset in order to experiment and research to demonstrate possible methodology to counter Untrusted models being used to write solidity smart contracts and possible protocols such as trusted monitoring with a trusted model to counter them.

![Fig. 2 Backdoor scorer for Solidity dataset](https://storage.googleapis.com/papyrus_images/98601b9f31ad90819f0c7ec7c5914ffd59d75b39b736860760dcfa415224dbd2.png)

Fig. 2 Backdoor scorer for Solidity dataset

On the suspiciousness of the code submitted by the Untrusted model for each problem, the trusted monitor rates the it at 8,9,8,9,8,8 respectively out of 10. But the monitor is actually able to catch and audit only Problem number 2 & 3 as seen in the figure above. This is because of the setup which allows only auditing of the top 2% of the most suspicious code. Though this can be increased depending on the situation and monitoring criticality, but is still useful in catching and flagging some of the suspicious activities of the Red Team.

Auditing 100% of the code by trusted monitor can be done but is not feasible on a large dataset as it will be very costly to run the untrusted model each time. The parameters, thus, can be changed according to the situations.

Conclusion
----------

As noted before, a limitation of this research study is that the dataset is very small compared to APPS dataset and a much more bigger data would be more helpful in demonstrating Usefulness and safety more accurately which will converge to real world scenarios.

It could very much be possible that the Red team uses Trusted model as well and checks if the suspicious code written by a Trusted model can actually bypass the Trusted model’s monitoring before actually submitting to the Trusted model of the Blue team. But Ryan et al’s paper shows us that even though this approach is used, in their case GPT-4 fails to pass the test cases 52% of the time(even before being given to the Trusted model GPT-3.5). This would mean that it would take many iterations for GPT-4 to actually succeed to find out a sneakier backdoor to insert.

But as models get more and more powerful, this may not always hold true and would need more research and testing. There are also interesting scenarios of colluding of models or even sandbagging where the AI models purposedly fails on the test cases discussed in the [original](https://arxiv.org/pdf/2312.06942) paper.

Ryan et. al's paper also concludes that although humans are more expensive than AI models to audit the code, they are still the most trusted ones to find bugs in the code. Although it can be little more time consuming too compared to AI models.

In a real world scenario, the best approach to control an Untrusted model for solidity based code generation in our opinion would be using a hybrid approach of humans auditing along with the Trusted model. Or even a workflow can be setup by devs to use Trusted models to monitor and push audit report at each iteration or push of code commits to github, while there are manual checks done by human auditor frequently such as every month to see whether the code is really safe or not without backdoors.

As AI models rapidly advances and becomes more powerful, it would be wise to be careful to not use the code generated by them directly in production as the more advanced the code gets, the more are the chances of sneakier bugs being introduced in the code. And thus a thorough testing and controlling of AI model is recommended before using the models in production and real world environment with continuous improvement and research.

**References**
--------------

*   Ryan Greenblatt, Buck Shlegeris, Kshitij Sachan, Fabien Roger (2024)  _AI Control: Improving Safety despite intentional subversion Transformers as a_ (arXiv:2312.06942) [https://arxiv.org/pdf/2312.06942](https://arxiv.org/pdf/2312.06942)
    
*   Shantanu Sontakke (Zokyo Labs), [Control AI for Solidity: Can we stop backdoors in solidity code generated by AI?](https://docs.google.com/document/d/1qBAWruh7XF7WuDXvKPQDL0ty5L7GFbQaaXKPRQAg5qI/edit?usp=sharing)
    
*   Dapp world:  [https://dapp-world.com/problem/](https://dapp-world.com/problem/)
    
*   [AI Control hackathon](https://apartresearch.com/sprints/ai-control-hackathon-2025-03-29-to-2025-03-30) by Apart Research
    
*   Apps dataset settings [code](https://github.com/UKGovernmentBEIS/control-arena/tree/main/control_arena/settings/apps)
    
*   Solidity setup [code](https://github.com/shanzson/AI-control-arena-solidity)
    
*   Inspect eval [docs](https://inspect.aisi.org.uk/)

---

*Originally published on [Shanzson](https://paragraph.com/@shanzson/control-ai-for-smart-contracts-can-we-stop-backdoors-in-solidity-code-generated-by-ai)*
