Mitigating MEV with FHE - Blind Arbitrage on Ethereum

ETHCC 7 · 11 Jul 2024 · 20:01

Jonathan Passerat-Palmbach of Flashbots presents research on mitigating MEV with fully homomorphic encryption (FHE). He explains sandwich attacks and front-running, then why arbitrage can be useful if users are not front-run. He describes a prototype where a searcher computes a back-running arbitrage transaction blindly on an encrypted Uniswap trade. He reports that decoding and matching take about 30 seconds, amount calculation is very slow, and signing the transaction in FHE is a major bottleneck. He stresses this is early research, not a Flashbots product.

Speakers

Topics

From ETHCC 7, hosted by Ethereum France.

Transcript

All right, everyone, thank you for coming. I'm Jonathan, I'm a researcher at Flashbots. I work primarily on privacy preserving compute. And today I'm gonna tell you a bit about our explorations to mitigate uh AEV with uh fully homorphic encryption. So just a big disclosure: don't expect anything uh related to Flashbots product in this thing. This is like a research exploration, this is not going into product anytime soon, as you will understand. Okay, so just a bit of an intro on like what's AMEV, uh to bring everyone in context just in case. Um E V is like this kind of like horror story that we heard about.

You might have heard about the name. Uh it used to stand for minor extractable value, it's maximal uh extractable value now that we don't have miners anymore. But the idea is just to express the fact that any entity that controls ultimately the block production, so miner, uh valid data, sequencer, any any privileged actor in the in the protocol uh can change the order of transactions. They can insert their own transactions, they can sensor transactions and do all of that for a profit, right? That's kind of like the general idea.

And you've probably heard about this thing of like sandwich attacks, right? That's that's a thing that uh used to be uh pretty common and like scared a lot of people. Concept was you want to trade on a DEX like CUNYSwap, for example. Uh so you send your transaction, it goes to the mempool, and while it's pending uh in the mempool, a malicious actor, a bot, a searcher, call it however you want, uh, spots your transaction, uh, detects what you want to trade, and like places a higher priority transaction just before you uh in order to push the price up. So when you send a transaction on Uniswap, for example, you've got a bit of like slippage on like how much you're willing to pay, and as a result of the search as transaction, uh you pay a worse execution price, the worst price you're willing to pay.

And the same guy tries to sell right after you to pocket the difference. That's a sandwich attack, basically. Uh the thing is it's actually broader than that. Any kind of like bot that can spot uh the outcome of your transaction and try to benefit from it can be called like a generalized front-running bot. And we've known about these bots for uh a while. There's like a paper from 2019 by uh Phil Diane and colleagues, and a very nice write-up uh on the paradigm blog if you want to really like uh get the vibe of like what can be happening, uh could be happening in the in the public mempool. Um the thing is it's not just a problem for uh end users, it's actually a problem for Ethereum as a whole, because the ultimate incentive uh of reordering blocks is to potentially like reorder the chain as a whole, right?

So you can introduce like a consensus instabilities uh by having this incentive of like extracting as much MEV as possible. So it's really something that was very important to tackle, um, especially with like the merge and like the proof of stake, because like MEV uh became uh an even bigger portion of like block rewards uh as opposed to just transaction fees. Um and beyond just like threatening consensus, it's also a very strong centralization driver. Uh because right now, if you think about it, the main uh benefactors of like uh me v extractions are validators. So uh the more money you have, the more validators you can sell up.

And that means that if you can extract more MEV, you get more money and you can set up more validators. So it's a self-reinforcing uh system basically that that means that the same actors get more a bigger and bigger share of of the of the value that the network if you don't do anything. So it's pretty pretty scary as a whole. So solving MEV is important for user experience, but also for the stability of Ethereum as a whole. And that's kind of like what we do uh at um at Flashbots. Uh the MEV extraction process uh can be you know seen as like different um there's different like techniques, and and I'm gonna focus in this talk on one that's called uh arbitrage. And I just want to highlight one thing uh, which is for now we've mostly talked about um the negative side effects uh of MEV on user experience and and consensus.

But our um our way of thinking at FlashBots is that not all MEV is is negative, not all MEV is harmful. Sorry. Uh some types of MEV, like arbitrage, for example, can be seen as like a service to in a way rebalance the price across different markets and thus uh providing like a positive outcome for the for the network as a whole. So as long as we can guarantee that users do not get front-run in the process, uh we can basically try to steer MEV in a way that that is like beneficial for everyone and like reward the actors, uh searchers that that basically solve uh the arbitrage differences. Why can we do arbitrage on Uniswap?

So very, very quick introduction of how it works. Because Uniswap is an AMM, a summit in market maker, and each of the pools in Uniswap are ruled by a very simple constant product formula that determines the price, right? So this K equals X times Y means that there is always like a constant factor between the reserves of the two assets in a Uniswap pool. And what that what that means is that if you can see, if you can observe a pending transaction that is trying to make a trade on Uniswap, you can simulate the future impact of this transaction when it will be included on chain. So it means that before the transaction is even finalized, I can know what the future price will be.

And as a searcher trying to make arbitrage, it means that I can observe the price on a centralized exchange, for example, and I can see, oh, there is actually a price discrepancy between binance and the price that is coded on Uniswap. So if I can lock that price in, uh I can insert a back running transaction to buy at a lower price and try to sell at the higher price on Binance and pocket the difference. That's kind of like the idea of arbitrage. And in on this slide, I label it as like non-atomic arbitrage because there's like two legs in this process. Not everything happens in a single transaction on chain.

You first need to buy something on the on the decks, and then you need to sell on the centralized exchange. So there's like kind of like an inherent risk in doing so, because like you're not sure that the second link can go through. But the thing is, to minimize the risk on the on-chain part, there's uh something important to understand. As we know, you can you can figure out like the future price. So we need to understand for how long does this price will hold uh that the one that you've just simulated on the pending transaction. And there's actually two uh spots in the two locations in the block where this thing holds. Uh the first one is like right after the user transaction, so that's what we call a back run, because there's no price discrepancy between two transactions on Ethereum, there's nothing happening, right?

It's a it's a discrete uh time series, so there's there's nothing going on in between. So if you can insert your transaction as a searcher right after the user's transaction, you get guarantees that the price you've simulated will be the same. And the other one is if you can simulate the price uh once a block is finalized, once the block's on chain, then the top transaction on the next block will also have the same price because nothing will happen in between this end of block and the new and the new transaction. So that's like two locations in block that searchers compete uh to basically insert the transaction into. Uh the issue with like this situation is that from what I've told you, there is absolutely no guarantee we can give to users that they won't be front-run, right?

So that's that's kind of like not cool. Uh so all the work um that flashbots and others have been doing is to kind of try to enforce uh social contracts between uh searchers to let them extract MEV and rebalance the prices uh under the condition that they want uh front-run users. But it's just holding because there's like this trusted middlemen like flashbots for now uh that in a way interface between the users and uh and the searches. Uh so it works, but it's not very decentralized.

Um we still have like these validators caching in all uh most, if not all the MEV in the process, and user don't really benefit from MEV that is at the end of the day generated by like their own trades. So this should intuitively like you'd want them to kind of like get a kickback uh out of it. Uh and we think that uh we can go we can go beyond that, we can do something better uh by utilizing something that is getting known uh in the space as like programmable privacy. And you might have like listened to a few talks like describing it in different ways. Uh I'd like to think of it as like you know, anything that allows you to compute on data or joint data that you sh that you're not able to see uh in clear text.

And uh in this talk, uh we will do it with a specific type uh of programmable privacy that is called fully homorphic encryption. Uh so the objectives of what we're trying to do is to show that we can enable uh enable the back runs, uh, kind of like prevent front runs and still allow searchers to uh compute uh the optimal price that they would like to do and that they can do it today on these clear text transactions, but now they should be able to do it in a blind way on encrypted transactions. And the way we're doing it uh is by using this concept of like fully homorphic encryption that uh is a different form uh of encryption that allows you to encrypt uh data in such a way that the data is protected by a bit of noise, uh, you still have a secret part, but the the way the ciphertexts are structured uh allow to allow you to perform uh arithmetic operations or or boolean operations on this ciphertext, right?

The the important part is that it's not um there's no silver bullet, it's not magic. Uh this noise that we add on the data is growing uh with uh the operations we are uh we're performing on on the on the ciphertext. And as a result, uh we have uh usually like an overhead uh when when computing uh with FHE because once uh once in a while we need to remove the noise, and that's called bootstrapping. It's like the in a way, like the bottleneck of this FHE technique. I won't go into details, but uh you can you can talk to me afterwards if you want to better understand.

It's also very nice uh bootstrapping, because we'll see that we can use it to uh to compute more advanced functions uh that we would like to perform, like encrypted comparisons, for example, that will be needed in what we're doing. Um the settings of what we're trying to do is an overall simplification of the real-world uh settings of of what's going on in the in the searching game. We can't swing a single user transaction, uh, so on the left. The user encrypts that transaction, and they will make it available uh essentially like to the searcher. And we see that there's this notion of FHE circuit, which is just means like a an FHE program if you want.

Uh and this circuit can be run on the search as infrastructure because all of this transaction is remains encrypted, right? So the searcher cannot see it, but it can execute something on it. The searcher uh can also inject uh its own parameters in the in the in the protocol. And the reason why we need to do that is that we can derive the optimal price, but the searcher might still want to target specific assets, for example. So the searcher should be able to say, oh, I only want to target a specific pool on Uniswap. But also, I've calculated that the arbitrage will be profitable only if the amount that I need to put in in my own trade in the background transaction doesn't go beyond that that value.

So the searcher needs also to express his own constraints to make sure that the background at the end of the day remains profitable for the arbitrage to be to be closed. And as we can see, there is still like a trusted actor in our setting, uh the builder, which is the one really assembling the blocks and sending them to the validators. So we assume that we can trust the builder. It's absolutely not uh you know realistic in the in practice, but for now, let's assume that this guy uh will be able to decrypt the results of this calculation. So this result will be what we call a bundle.

So you can think about it as like a partial block containing at least uh the user transaction and the back running transaction that we've generated uh from this encrypted calculation, right? Um, so the AFHE calculation we're making uh is basically done uh using a specific scheme uh that's called TFHE. Uh and in particular, we're using Zama's declination of uh the Rust implementation of TFHE called TFH E RS. Uh so you can you can speak to them.

I think they've got booth, so if you want to hear more about the details of TFH ERS. The algorithm is split into two main parts. Um the first one consists in decoding the transaction. So if you if you ingest an Ethereum transaction, it comes encoded as uh RLP, right? Uh and normally you need to pass this RLP to look into the fields that you're interested in to find the pools, the functions that the user is calling, the gas price, all these kind of things. So now we need to do that in the encrypted realm.

And it boils down to doing a bit of like bitwise operations, the bit shift. And it turns out that these operations are pretty uh FHE and TFHE friendly, especially if you need to do bit shifts by powers of two, they're pretty much free. So that's cool. It's something that doesn't add too much overhead. We're happy with that. At the same time, what we want to do is to find the way to express the user's condition. But in FHE, we can't do branching. We can't have like if-else conditions and like you know, change the the outcome of the program as such. So we need to find a way to encode the fact that the whole calculation can fail if it doesn't respect the searcher's conditions.

And we do that uh by applying each criteria of the searcher along the way, uh, comparing it with the fields of the transaction. And if one of these criteria is not respected, then uh at the end of the day, we compare, we do an encrypted comparison with this Boolean, and we generate either the real transaction or a complete completely null transaction full of zeros. So that's our way of like expressing that the calculation failed, and that's not gonna be a valid transaction. Um and these comparisons are also pretty efficient uh using TFHE, so we are cool with that. When it becomes uh complicated is when we need to do some more advanced arithmetic.

And in particular, you have to trust me with like this variably equation at the bottom. But that's how uh based on like how Uniswap calculates prices. If you are a searcher willing to pay uh X, for example, and considering the fees and the encoding precision and things, if you calculate this thing, you will get the amount of tokens Y that you would obtain from your background transaction. So that's the amount that you would then need to compare with like the centralized exchange to see how much profit you're making out of this background. And in particular, the two operations that are pretty hard to compute in FAT here are these divisions, so all the red things.

So that's challenging, even though it's not as bad, because like the denominator here is not encrypted, so it's like kind of like okay ish. But the bad thing is uh we have a square root uh at the beginning. And the way we can do square roots in FAT, one way we can do square roots is uh by approximating them using an algorithm called uh the Newton approximation, and it boils down to making a series of divisions. So all these ugly divisions we didn't want to make, we actually happen to make even more than that. Uh so not good. Uh in practice, um, here's how it looks in terms of like runtime. Uh you realize very quickly one thing, the overhead in terms of like bandwidth and what you need to communicate is not too bad.

Uh the ciphertext in FHE are of course larger than Ethereum transaction, but since Ethereum transactions are not too big, uh this remains pretty much uh contained in a way. Uh and we see also that the the decoding phase, so everything that is prefixed by RLP uh and the matching phase are actually pretty fast. Uh it's about 30 seconds, so it's of course like much slower than what you would do in clear text, but it's still something we can work with. The amount calculation, on the other hand, is dead slow. Uh the only thing to be uh to rejoice about is that uh it benefits pretty significantly from uh vector instructions on CPUs and thus should benefit as well from GPU acceleration, Fiji acceleration, any kind of like hardware acceleration you can throw at it. So that's cool.

Uh we got something to work with here. Right. So let's summarize uh these points. Uh if you think about it, what we've been talking about is a prototype that could potentially work its way into an FHT encrypted mempool. Uh so we need, of course, there's a lot of work to do in this direction, but that's showing that this programmable privacy approach can at least allow you to express these things, and now the job is to make it run faster to accommodate like the the searcher uh conditions. The computational overhead is a problem, uh, but you might have noticed something that is missing from our backgrounding transaction, something we didn't talk about at all, and that would come into play if we were to deploy that in practice.

For the back running transaction to be a real Ethereum transaction, the searcher needs to sign it. So we could, for example, inject a private key, a private ECDSA key in the circuit and try to sign reproducing, sorry, the signing algorithm in FHE. It turns out that it's dead slow. So that's a huge bottleneck and a huge pain point. And if we can find uh a workaround to that, I think we can we can make a lot of progress. So if you want to work on these kind of problems, uh, well, I'm very happy to talk further about it. And uh, we're actually hiring to uh work on this type of problems as well.

So if you are a researcher interested in programmable privacy, please come and talk to me. Thank you very much.

Related talks