DSS 2026 — From EVM bytecode to Solidity and back to bytecode Gerard Persoon Source video: https://www.youtube.com/watch?v=wzQggpjXCr0 Automatic captions — not human-reviewed. Complete spoken-audio coverage is unverified. Source language tag: en (English). Source export SHA256: c4135419cdaaa8261696ac6466636c2ab52ec3605ab83d2b5dd6e81589fbd8c9 [0:01] Yes, welcome everyone. I'm going to talk [0:04] about something that I've been creating [0:06] the last half year. [0:08] And that's [0:10] a way to get [0:12] from EVM bytecode to Solidity. And when [0:15] you compile the Solidity again, you get [0:18] bytecode of course, but exactly the same [0:20] bytecode. So you're 100% sure that you [0:23] have a good representation and you can [0:25] also verify that source. [0:29] A little bit about me. I've been doing [0:32] Solidity security reviews for the last [0:34] couple of years, starting at Code Arena [0:37] and currently at ConsenSys and Spearbit. [0:40] And if you want to contact me, here's my [0:42] Twitter handle which is at gpersone. [0:48] Okay, why do I [0:51] spend time [0:53] on the Solidity decompiler? [0:56] Well, there are several nice use cases [0:58] for a decompiler. [1:00] I'll show a couple of use cases later [1:03] on. And when I started, at least there [1:05] was no really good solution to decompile [1:08] your contracts. [1:10] But [1:11] now there are better solutions also. [1:14] And [1:17] creating a decompiler that can fully [1:19] round trip, that's a nice target to [1:21] learn about LLMs. Because an LLM can [1:24] then [1:26] yeah, measure that it has the right [1:29] correct answer by round tripping. So [1:31] that's a perfect use case for an LLM. [1:34] And it's also not too easy for an LLM. [1:37] At least when I started, it was [1:39] relatively difficult for an LLM to [1:42] create a decompiler. But they've [1:44] improved a lot, so now it's probably a [1:46] lot easier, but at least then it was [1:48] difficult. [1:51] And also the LLMs currently are actually [1:53] so good that they can already decompile [1:56] a contract. So if you just put in uh [1:58] bytecode in um frontier LLM and say [2:02] decompile it and check that it round [2:04] trips. It does that already. [2:07] Um so you can say why are you going to [2:09] create a decompiler anyway? It's not [2:11] worth the trouble anymore. [2:13] Um but if you use an LLM it takes a long [2:15] time, [2:16] uh uses a lot of tokens and times [2:18] sometimes you get a refusal. So it's [2:21] still there are still use cases [2:23] uh for a decompiler. [2:27] So I uh collected a couple of use cases [2:29] what be would be useful or [2:32] what would be the use for a decompiler? [2:35] Of course what you can do is verify the [2:38] contracts [2:39] that are not verified yet and put all [2:42] those sources in Sourcify. [2:44] And then once you have that [2:47] um all all the security tools or [2:49] whatever can be used [2:51] on those contracts. [2:53] And you can of course use it to analyze [2:57] uh hacks and whatever. [2:59] Um and do all kinds of things like that. [3:03] Um you can also if this works correctly [3:06] you can even do live [3:09] um transaction checks so in your wallet [3:12] or maybe even on an L2 that before you [3:15] do a transaction or before the [3:17] transaction is [3:18] admitted on the chain that you can do a [3:20] check that there is no hacks or whatever [3:23] going on. [3:24] Um and then if you would have decompiled [3:27] contracts that would be a lot easier [3:29] than doing that right now. [3:32] And you can of course create diagrams of [3:36] how the transactions work or how the [3:38] code work. So you can do all kinds of [3:40] nice things. [3:42] And one small detail but still [3:44] interesting I think you can find [3:46] compiler bugs. So I back tested that [3:49] with a couple of contracts that I [3:51] decompiled and [3:52] um compared the the solidity that I [3:56] decompiled to the one that was on [3:58] Sourcify. And if there are a there there [4:02] is something wrong. So it was able to [4:04] find a couple of old bugs in the [4:07] Solidity compiler that have already been [4:09] solved, but it was nice to see that it [4:12] is able to find those [4:14] bugs. [4:17] So what I did of of for this [4:19] presentation, I've taken a [4:21] random contract just to show some things [4:23] and this one unverified contract [4:27] which has a relatively high ETH balance, [4:29] so that's interesting to take a look at. [4:32] And if you look at [4:34] on Etherscan and you look at there's no [4:37] source there of course, but there's a [4:39] decompilation option there. And if you [4:42] look at that, [4:43] it says decompilation didn't finish. So [4:45] that's an example that's the [4:48] the the decompiler over there isn't [4:52] good enough actually. [4:55] So I've been five coding the decompiler. [4:57] I've been using an LLM and just set it [5:00] set to it [5:02] just create a decompiler. [5:05] And this is a sort of high level what [5:07] it's [5:08] how it's created. So on top is the [5:11] decompiler with a lot of faces in it. [5:15] And [5:17] below that I also [5:19] sort of asked the decompiler to create [5:21] code [5:22] for when it cannot decompile the [5:24] contract yet that it give a lot of [5:27] information to improve the decompiler so [5:29] that it can improve itself with the help [5:33] of an LLM so it can decompile the next [5:35] contract and then continue to improve [5:37] itself until finally it can decompile [5:40] all contracts. [5:43] And what's [5:46] relevant to know if you decompile [5:47] contracts, of course you you cannot [5:49] perfectly normally decompile all the the [5:52] the variable names and the internal [5:54] function names and all that kind of [5:56] stuff. So it's maybe not super readable, [6:00] but [6:01] then I have another phase right behind [6:04] it. If you just throw in the decompiled [6:07] code into a random LLM, then it can make [6:10] it a perfectly readable contract. It [6:13] creates very nice variable names. It [6:16] even adds [6:17] comments to it. [6:18] So then you get a very nice contract [6:21] that you can easily read. [6:26] So here are all the phases that the [6:28] decompiler is doing. [6:30] And I actually just five coded it. So I [6:33] don't actually really know what [6:36] it does, but it sounds very interesting [6:39] what it's doing. [6:43] Uh what of the things of course that's [6:44] difficult [6:46] when you're decompiling is that there [6:47] are a lot of Solidity versions. [6:50] So whatever like [6:52] all of those. So the decompiler first [6:54] has to figure out which decompiler [6:57] Solidity version you are using. So the [7:00] later Solidity versions have that [7:02] encoded into the bytecode. So that's [7:04] easy. And the older ones [7:07] it can infer that from at looking at [7:09] which [7:11] yeah commands are used or which which [7:14] instructions are being used. So it's [7:16] able to infer all of that. [7:21] One of the more difficult [7:24] and interesting things is how do you get [7:26] the the function name back from the the [7:29] public functions? And that's where [7:32] Sourcify comes in. So that's a super [7:34] handy tool to use. So you take the [7:38] selector that you [7:39] want to [7:41] reverse engineer. You put it in Sourcify [7:44] and most of the time you get back the [7:46] the the good function name. [7:49] Of course first you have to infer the [7:51] parameters because they're part [7:54] of the hash which is put in the [7:55] selector. So you have to make sure that [7:58] you have the right parameters. Otherwise [8:00] you get a wrong match. [8:02] But that's possible to to get from the [8:04] bytecode. [8:08] But if you if it's not on Sourcify yet, [8:12] then you can brute force it. And this [8:14] can can actually be done in in two ways. [8:17] You can try to find [8:19] sort of the real name. So you use a [8:21] dictionary and you try all kinds of [8:23] words until you finally get a match. [8:26] And that works quite often. [8:29] But if that doesn't work, you can also [8:33] brute force like a random name, so with [8:36] a sequence number behind it. [8:38] And it's important that you have [8:40] a name that [8:42] results in the exact selector. Otherwise [8:45] you can never get a [8:48] round tripping contracts. Then you would [8:50] have a slightly different bytecode. [8:53] So it's not perfect, but it is still [8:55] round tripping. [9:00] Okay, so this is an example of what a [9:02] decompiler creates. So the left side is [9:04] what it creates. So it infers the [9:08] the right function name execute in this [9:10] case. [9:11] But for example, the the variable names [9:14] that are just arg zero, arg one, etc. [9:17] So if you have the left side, it's it's [9:20] kind of difficult to read. [9:23] So and then if you put this in an LLM, [9:25] then it creates that outfit. And there [9:28] you can see that [9:30] all the internal names and the the [9:32] argument names, whatever, they're [9:34] readable and they actually make sense. [9:36] So that's what an LLM can do. [9:43] Now what I also did normally when I do [9:45] security reviews, I always create a [9:47] diagram of the code. So that helps me to [9:49] understand the code and and easily [9:53] yeah, verify it. And now LLMs are good [9:55] enough to do that automatically. So I [9:58] took the decompiled code and [10:00] put it in an LLM to create a diagram. [10:04] And then it automatically created this [10:06] diagram. [10:07] And this format is the format that I [10:10] used myself. So I'm used to this format, [10:12] but it could actually create any format [10:15] that you want. [10:17] So you could do [10:18] for example build this into a wallet [10:21] and then if you want to do a [10:22] transaction, it could automatically [10:24] create [10:26] a diagram like this and you could [10:28] interpret it [10:30] if you're technical of course. [10:36] Now the interesting thing is [10:39] how fast is a decompiler compared to [10:41] just doing it with an LLM because the [10:43] LLM can do it also. [10:47] If you take a large contract, so the the [10:49] bottom layer, then an LLM takes about [10:52] half an hour to reconstruct it. [10:57] And more important, it would cost about [10:59] $8 on LLM costs. [11:03] So that's not a trivial amount. [11:06] And if you do it with a decompiler, the [11:08] decompiler itself of course it's that's [11:11] just Python, so that's free. [11:14] But if you do the post process part [11:16] where you make it readable, you still [11:18] need an LLM, but then compared to the [11:21] other one, it's a lot less. So about 50 [11:24] cents worth of [11:26] API calls. [11:28] So that's a lot cheaper. And also you [11:30] don't really need a front-end LLM, front [11:34] frontier LLM. So although the the the [11:37] local models can also easily do this. [11:40] So [11:41] that's actually the main advantage of [11:44] the decompiler. [11:46] So you can do that. [11:51] Now of course when I was doing this, I [11:52] had all kinds of interesting experiences [11:56] with LLMs. [11:58] One of the thing is reward hacking. So I [12:01] asked it make something that can round [12:03] trip and it said I'm ready, it's [12:06] finished. [12:07] And then it created this code. [12:09] And if you compile this again, you get [12:11] exactly the same bytecode out of it. [12:14] So for an LLM, was perfectly happy with [12:17] it. [12:18] And then I had to explain to the LLM, [12:20] yeah, this is reward hacking. I don't [12:22] want this. Oh, oh, you don't want it. No [12:24] problem. [12:25] So it understands that I didn't want [12:27] reward hacking. [12:30] But lots of other things went wrong. In [12:32] the beginning it made [12:33] it used a lot of regular expressions for [12:36] the decompilation and then they nested [12:38] like 10 levels of [12:41] regular expressions and finally it got [12:43] so complicated that the LLM itself could [12:46] not understand it anymore and could not [12:48] make any progress on it anymore. [12:51] So then I [12:53] started like all over again. So I'm on [12:56] the fifth generation now of my [12:58] decompiler, so I had to do that a couple [13:00] of times. Another time it created so [13:02] many tests, like more than 10,000 tests [13:05] and it took like three days to run all [13:08] the tests. So it sort of never ended [13:12] yeah, with the tests. [13:16] So where am I now? [13:19] So it's working quite good. I put in [13:22] about 800 contracts to to decompile and [13:25] it has now decompiled more or less [13:27] random contracts and it has decompiled [13:29] 500 contracts about now [13:32] like perfect bytecode. [13:34] And if I just keep it running, it just [13:37] gradually decompiles more. So I don't [13:40] have to do anything [13:43] for it, just let it run and it's [13:45] continuously improving and and [13:47] decompiling more [13:50] bytecode. [13:52] And but I also also noticed that the [13:54] newer the LLM models are, the the better [13:56] they are. [13:58] So at this moment writing a decompiler [14:00] is kind of straightforward, so anyone of [14:02] you could also [14:04] take a front tier LLM and just ask [14:07] create a decompiler. [14:11] Okay, but now I have this decompiler and [14:14] now my question is yeah, basic to all of [14:16] you help me think about what I should [14:18] do. [14:19] What should I do with the compiler? So [14:22] if I just make it open source so [14:24] everybody could use it or if I just [14:26] decompile all contracts so maybe then I [14:30] would enable a hack so I would decompile [14:32] a contract that maybe has like a million [14:35] dollar worth of value in it and there's [14:37] a vulnerability in it and if I decompile [14:39] it somebody can hack it and then [14:41] the million dollars are gone [14:43] and then it's not so difficult to trace [14:45] back to me because they can watch this [14:47] presentation [14:49] and then they maybe can [14:51] make me responsible for it. [14:53] So I don't want that. [14:56] So yeah my my open question to me and [14:59] please come to me after the talk for [15:01] what would be a good approach to use [15:04] this. [15:05] So what I [15:06] want to do anyway is continue improving [15:08] the decompiler [15:10] and starts to verify sources on Sourcify [15:13] initially with contracts that are [15:16] yeah not important. [15:19] So yeah this is my presentation. Thank [15:21] you very much and we are out of time.