FHE on Ethereum
ETHCC 7 · 11 Jul 2024 · 37:07
Clément Danjou and Petar Ivanov present fully homomorphic encryption (FHE) on Ethereum, building on their earlier FHEVM work. They explain encrypted types in Solidity, access control for ciphertexts, and a threshold key management service for decryption and re-encryption. They describe an FHEVM coprocessor that does the FHE computation off-chain while Ethereum only handles small ciphertext handles, with zero-knowledge proofs for encrypted inputs. A Sepolia demo mints and transfers an encrypted ERC-20. Q&A covers DeFi integration, compliance and whether FHE could run inside the EVM itself.
Speakers
Topics
From ETHCC 7, hosted by Ethereum France.
Transcript
Okay, you can hear me? So we can uh start. So today we will present with uh Peter uh FHE on uh SROM. So this is like a um presentation we do uh following the previous one we did uh last year uh regarding FHE VM. So this is like uh our progress on FHE VM because now we we have uh FHE VM basically on SROM and we will present to you uh how it's working and what we we can do now uh directly uh on uh SRM L1. So one issue uh on the blockchain is uh that everything is public.
So this is great because this is beneficial if you want uh to uh to to have something verifiable and uh trustworthy. But when you want to deal with uh some uh private information like uh you can imagine uh health data, personal uh information, well this uh openness will become uh uh an issue. Okay. Uh FHE uh is uh is a fully homomorphic encryption. So this is like a cryptography uh technology that allows uh anyone to operate on encrypted data and uh without the needing to decrypt it. It means that if you uh develop an application or a cloud service, you don't need to decrypt the data at any point, and you can maintain the encryption during the whole process.
It means that from transmission to storage to uh processing, you don't need to decrypt and you don't even need to change the user experience. FHE VM, uh so this is like uh the an evolution of the EVM we we developed. It allows you to have a end-to-end encryption, so from inputs and storage, and also it includes the possibility to have some composability on your encrypted data. It means that if you, for example, want to use a ciphertext stored on contract A and ciphertext stored on contract B and store the result of this operation on contract C, you can do it.
So the world chain will be able to operate with many contracts. So this allows a lot of uh use cases. So if we think about like, for example, blind auction, uh blind auction, if you do it without uh FHE, you would have like uh all uh users bid uh an encrypted bid, okay. And in the end, if you want to determine who is the winner, you would have to uh decrypt all bits. In FHE, you don't have to decrypt all bits. So you can reveal only the winner without decrypting any bits, even the winning bits.
So since FHVM is based on EVM, uh everything is in Solidity, so we have like a new types uh in the EVM. So this is like new uh native type, I would say, like encrypted Boolean, encrypted UInt up to 256 bit. You also have uh like every operation you can think uh of like we have uh obviously addition, subtraction, and multiplication, but you also have the possibility to do comparison between uh encrypted data, so like uh equal, not equal or lower or greater than. And uh we also have the possibility to have like a randomness on the on-chain.
So you can imagine that you can generate encrypted numbers using a PRNG, so pseudo-ronom number generator. So it allows like new use cases, which was not possible without an oracle before. So you can obviously think about games, but you can think about like many other use cases where you need some randomness. And as I said uh earlier, like everything is on EVM, so everything is in Solidity, which is great because if you like use Solidity today using your compiler, you can use exactly the same tools to develop on FHEVM. And this is exactly what we will see right now, like just a quick quick view on uh how you write the code with uh FHVM.
So this is like for example a contract. So we just like look at the transfer function. So if you think of like a RC20 uh transfer function, you would have like an addition, subtraction, and probably you will check that the user has enough funds before doing the transfer, which makes sense. So this is like how you would write it uh in uh FHE VM with encrypted types. So we will dig a little bit on that. So first, when you think about the add and sub, uh this is uh uh under the wood, it's like calling uh library called TFHERS, which is a like the core library handling t TFHE uh encrypted data. So everything you see in Solidity will be like interpreted as a TFHS uh function.
But for a developer, you don't have to uh take care of that. But it's important because if you think about like uh this library, we have some benchmark on that, and today we are like approximately on CPU or between one and two TPS. On GPU we are like around five, but with uh like some optimization, we expect to have like 30 on GPU uh on TPS, and when we have uh ASDIC in some years, um or some months maybe, but we'll have like a ASDIC around like 1000 TPS. So it will be like a very huge.
The other part in the transfer function we've seen uh before, this is like the allo. So when you have encrypted data on chain, so UAP, you can process it, you can do a computation, but at some point you would expect someone to decrypt it and get the value. This is where the allo function uh operates. It's uh the idea is you can imagine like an access control is contract on-chain. So you can see it as a key value store. Where basically we say, okay, this ciphertext belongs to this user. So it allows like specific user to uh decrypt uh any data, and when you want to store uh this information, saying that this uh like for the second line uh the two uh so the uh the user owning the balance can decrypt it, you need to store it on chain, and this is exactly what is doing the TFHE uhlo.
The next part will be uh done by uh Peter. This is about like the decryption and uh re-encryption, and we'll dig more about how we handle that uh with the key management system. Hello. Um so with FHE obviously you can do computation in succession multiple times, but for this to be useful, you have to show the data at some point, and that's why we have decryption and re-encryption. Uh and uh the way we implement that is by introducing uh threshold key management service, uh or KMS for short. And this is uh a blockchain running an MPC protocol uh to be able to do decryption and re-encryption and also key generation.
We use a blockchain because we want an immutable look at what has happened, what decryptions or encryptions have been requested, and also we can use it for payments. But the secret key obviously we cannot give it to any single party. And that's why we we split it to multiple shares and give it to individual potentially independent parties. And we run an MPC between them to be able to do the decryptions and re-encryptions.
And since the FHEVM runs on the blockchain, and the KMS itself is a blockchain, we we need them to be able to communicate, and that's why we introduce the gateway. I'll talk about the gateway in the next slides. And I think you should uh check it out. So let's start with decryption. The main idea for decryption is to make a value public. So you basically take an encrypted value on chain and review it, putting it back on chain as a plain text value. And we it works asynchronously in the sense that from a developer's perspective, they will be calling gateway.request decryption, passing in the ciphertext.
To be more precise, passing a handle to a ciphertext, a very small value that refers to that ciphertext talk again, I'll talk about it a bit later, and also a callback function. And this callback function will be called with the plain text value eventually in uh in uh in some future block. Uh it in some sense it works like an oracle. That's how decryption works. Uh, and to to have a more detailed look, what would happen is when request decryption is called in uh step one. Uh there will be an event for this decryption.
Uh the gateway is an off-chain component that will be listening for such events. When such an event is uh found, the gateway will fetch a proof that uh this ciphertext can be decrypted from the ACL that Clemann was mentioning. This is in practice, this is just a Merkle proof from the storage of the ACL contract. Uh and the gateway at that point, at step three wants to fetch the actual ciphertext to be decrypted from a data availability component. Uh, this is where we store ciphertext because they're relatively big compared to plain text data, it's also off-chain, and it's a key value store from a ciphertext handle to a ciphertext. So just a normal key value store, it's it's also public.
And at that point, the gateway has all the data to request decryption from the KMS, namely the ciphertext and the proof. The KMS verifies this proof, uh, and if it if it verifies, then it does the decryption via MPC, signs the value, the plain text value, and returns it to the gateway, and then the gateway calls the callback function in a normal transaction. So if the blockchain is Ethereum, this is just a normal Ethereum transaction with the plain text value. Re-encryption is a bit different.
It's different because we it doesn't reveal the value publicly. We is instead we want to show the value to a particular user. A good example is let's say ERC20, encrypted ERC20 tokens. If I if I want to see my balance, I don't want it to make it being public. That's why I'll do a re-encryption. And essentially re-encryption will encrypt it under my public key. So I'll have a secret and a public key pair. And re-encryption will re-encrypt it to this public key such that only I can decrypt it. And the important point here is that the value is never decrypted.
Not a single party can see the decrypted value. It's a to like magically transform from being encrypted under FHE to my own public key. And we have a JavaScript library that's uh that's very easy, it makes it very easy for developers to do that. Uh and this is totally off-chain. There's no transaction involved. And uh let's see how it works in the flow. So the user would request the re-encryption with their public key, also sign their public key. Again, step two, the gateway will fetch the same proof from the SEL contract.
Also step three fetch the ciphertext, and then again send the data to the KMS, but now it just also includes uh not just the ciphertext but also the public key. And the KMS again will do MPC, run the MPC protocol to do the re-encryption, sign the result, the re-encrypted value, pass it back to the gateway, and the user can now decrypt uh with their own secret key. And only the user will see the the plain text value. And uh let's talk how how to run this on Ethereum. Uh we introduced the FHEVM coprocessor.
Uh and what it is, it's an off-chain component that's also a full node. And the main idea here is that we offload FHE computation from the blockchain to the co-processor. Smart contracts are deployed as per normal on the blockchain on the O1. The bytecode lives there, all the code is there, all this all the tooling remains the same, the compiler, all the all the software stack remains the same. And what happens is that when an FHE operation is requested on-chain, the FHEVM coprocessor, which is a full node, will eventually see that because it's a full node executing all the blocks. And in addition, it will also do the actual FHE computation.
And I was mentioning that already, but uh ciphertexts are a bit bigger, and that's why we also store them off-chain in a data availability component. Uh there are a lot of benefits to doing it this way. The main one obviously is that you can run this on almost any chain without changing the actual blockchain. So Ethereum is uh is an option to implement on. Uh and uh makes it very easy to develop because you don't have to change any networks, you don't have to change your infrastructure, you just you just deploy your contracts on Ethereum. Uh from a developer's perspective, you don't need to deploy anything else, you just deploy a contract and you can use FHE directly on Ethereum.
And uh because everything is kept on Ethereum, you don't need to breach or uh anything like that. Uh you can also mix plain text and ciphertext uh contracts uh because again everything remains on the on the same chain. You can also compose between new encrypted contracts and existing um, for example, your C20 contracts, you can mix and match with uh let's say um USDC, USDT or whatever you want, and um implementation wise, this allows us to do parallel FHE execution. I'll show it a bit later, and uh basically scale much much better than what we had with the native implementation of FHEVM where we require the blockchain itself to be to be doing the FHE computation.
And uh the main the main idea to implement such uh such a coprocessor is what we call symbolic execution. And uh the the way it works is that we don't do FHE computation on chain, we just do symbolic execution on very small handles, and these handles are just uh 32 byte values that refer to a ciphertext. And say you want to do A plus B uh homomorphically, what will happen on the L1 is that we'll just take these small values and produce a new result, say C equals A plus B will just produce C, but at that point there won't be any FHE computation. Uh and this is like a commitment to the fact that A plus B equals C on-chain, and you can use even use C in subsequent computations without having done the FHE computation itself.
And then since the coprocessor is itself a full note, it will be listening to uh executing all the blocks also, and uh once it finds such uh such uh operation, FHE operation, it will actually do it. Fetch the inputs from the DA, do the computation and insert the results back into the DA. And uh this works because we usually you don't need to decrypt or encrypt immediately after you've done the computation. You would be doing a lot of FHE computation and then eventually do a decryption or encryption. And this means that the execution can happen much later.
It doesn't have to be synchronous with the execution on the on the blockchain itself. And I I mentioned handles multiple times, but uh just to be a bit uh just to give you a bit more information, it's just a UN256 value or 32-byte value that uniquely refers to a ciphertext in the data availability layer. It can it can point to any type of a ciphertext, could be 8-bit ciphertext or uh 64 bits, doesn't it doesn't matter, it's still 32 bytes. Very easy to operate on them on chain, very cheaply.
And you can think of them as uh futures in the sense that when you produce a handle uh from a computation, this is just a future to a value that is supposed to be computed by the co-processor in the future eventually. Uh we talked about uh how results are handled, but we didn't talk about what if you want to input encrypted data on the chain. Um for example, if you do encrypted tier C20 transfers, you don't want to the amount to be public. You want it to be encrypted, uh and that's when you why you need inputs.
Uh the input is just is basically the ciphertext that the user encrypted, but it also comes with a zero knowledge proof of knowledge. And we need this uh zero knowledge proof because uh we we want first of all to make sure that uh the user knows the actual plain text. We don't want other users to be able to just take this ciphertext and use it from uh from another user. Uh also verifies that encryption has been done correctly. Uh and also it ties this particular ciphertext input to a smart contract. So you cannot just take uh uh ciphertext from a smart contract A, use it in another smart contract B.
And this zero knowledge proof is verified by the coprocessor. Uh it's inserted by the user before being using transactions uh in a network call in step one. Um the coprocessor will verify, and then if it if it verifies, insert the FHE ciphertext in the DA layer, and um also sign the sign this input. And then in step three, the user will be using this handle to the ciphertext and the signature uh in a normal transaction to to Ethereum. Uh, and then you can use it in the in the same way as any other uh ciphertext.
And the signature is needed because we don't want uh the user coming up with some random handles, and then the coprocessor will be won't be able to uh to actually fetch such uh ciphertext. The signature is checked on chain. This ensures that uh we always always uh produce uh results and uh computation wouldn't stall on the coprocessor. Uh we can we can secure co-processors in different ways. Uh simple way is to use a centralized one uh where we trust the operator.
Uh, maybe it's a reputable company that's running it, and the users will be fine with that. There's also an option of decentralizing it, running multiple instances of the coprocessor doing the same thing and then reaching consensus, and this way it's not just one node doing the computation. We can also use fraud proofs. Everything that the coprocessor is doing is public. All the data in the data availability area is public, so anyone can be verifying what it's doing, what ciphertext is producing. And if it uh if someone finds a problem, they can uh submit a fraud proof on chain, and uh maybe the processor is penalized for that.
We can also use a third-party service to uh to verify uh the coprocessor. Uh and finally uh since symbolic execution is very cheap, it's on very small handles. Like a malicious user can just uh deploy contract that's doing a whole bunch of FHE computation almost not for free, but like uh for a very cheap price on on the L1. But the corresponding FHE computation would be very expensive to do on the coprocessor. And that's why we want the coprocessor to be paid for that for the services, and we also want to uh limit the amount of FHU operations uh done in in a period of time.
We have different ways to do it. We can uh I'm just saying we can do it in many ways. Uh we don't have to stick to one single way, pay in fiat money, we can use the native token, we can use ERC20 tokens, we can decide whether the developer or the application pays it or the user pays it, it's just up to the application to to decide how to do it. Uh and yes, that's uh that's it, and now we'll show how it actually works. Uh okay.
Uh so um we have like a demo like this, it will be uh easier to to see uh how you can write a contract directly on a SROM using uh FHE. Um so we have like an encrypted ERC20 uh contract. So um the first thing to notice is uh how we write uh balances. I think it's yeah, maybe I will uh and uh so as you can see like balances are not like uh uint but uh encrypted un64. So this is one first thing, and we will uh take a look at uh two functions.
The first one is uh the mint function. So what you see is like not encrypted uh minted amount. So this is like a un64 uh not encrypted, but you can notice that uh already with the min function we do some TFHE operation. So how it's working, we have this uh the balance of the contract uh owner, so we add uh to this balance the minted uh amount. Okay. So uh this is like a TFHE add.
So since we are on ACROM, we won't do any FHE operation. As Peter explained, it will be uh just uh like uh changing uh computing a handle on-chain, but this function, the TFHE add, will be catched by the coprocessor, and the FHE operation will be done uh off-chain. But we'll see that uh later with the demo. So this is like a TFHE add, so this is pretty simple. Like we just do uh an addition between the owner balance and the mean amount. And since we generate there a new handle, so meaning we are generating a new ciphertext because we are computing something with TFHE add, we need to uh allow uh this contract and also the owner of the balance to uh to have the possibility to decrypt or re-encrypt the balance at any point. This is where the TFHE operates.
So this is what we explained uh during the slide. So you have to do it uh twice. The first one is to say, okay, the contract can uh use this ciphertext to uh process, decrypt, re-encrypt, and also the owner. Then we have the total supply, so this is like a classic uh solidity because uh non like total supply and mint element are not uh encrypted, and then we emit uh uh an event. So this is like the mint function, and we can then take a look at the transfer function. So this transfer function just there.
So um this transfer function is uh is using like so this is uh the the first one is the transfer uh value with a select. So what is a select? Um when you deal with uh FHE, you can't uh do branching uh in the in your function. So it means that you need to uh operate with uh what we call like a multiplexer. So this is what is a select. Um so how it's working, it's like uh like a ternary operator. So we have uh is transferable, it's an encrypted Boolean.
So yes or no, but encrypted. If is transferable is true, uh the value assigned to transfer value will be amount if is transferable is false the uh assigned value will be uh zero like an encryption of zero as you can see we have the as the U in 64 which is a trivial encryption of zero. So the transfer value will be D amount or zero depending if the user has enough fund. So is transferable is the encrypted Boolean like a received by this transfer function but this is done like previously on the transfer function above where we have like a can transfer and like you can see here L E is a lower or equal. So this is where we check that the user has enough fund before doing the the transfer.
So this is like the first line of our transfer function. So now we know that this value is zero if the user has not enough fund or the exact amount to to be transferred. So based on that we can start uh through the transfer. So what is the transfer is just adding funds to the receiver and removing funds from the the sender. So the new balance for the receiver will be like an addition of is balance with the transfer value so zero or the amount. And then as we we did before we need to allow this new ciphertext to the owner of the this balance and also the contract and it will be exactly the same for the new balance from where we do the same for the sender.
So we will remove the amount to the sender's balance and give access to this ciphertext to the the senders directly. So like when we read it, I think it's uh understandable because this is like very similar to what we we use in Solidity when we deal with uh unencrypted data. And then what uh it's happening when we want to uh do it in reality with uh DAP. So we will like deploy um a contract, so we are basically deploying exactly the contract we we've seen, and then we can like start to play with it and see uh what's happening and uh like so.
First to the first thing we are connected on Sepolia, okay. No magic trick, okay. We are on a S3 on Sepolia. And um so yeah, I published uh my account, uh my contract, sorry. Uh so first thing to do, uh I want to mint uh a few tokens. Let's say uh I mint one thousand tokens. So again, this is like Sepolia transaction. So 1000 is not encrypted, like like we we just see it in the contract. This is like a plain text uh value.
But like first thing we should see it's uh the total supply would be uh one thousand, but also my balance needs to be one thousand, and this is exactly what we see. The total supply became one thousand, but my balance also became one thousand, but it has been uh decrypted. If you look at the at the right, you see that uh we retrieve the handle of the of my balance, and my balance was EEF7FF. And this is like a Uint 256, and this is what uh Peter mentioned.
This is the handle of the ciphertext. So on chain, my balance is exactly this value. So this is a U in 256. So based on this handle, I can ask the coprocessor to say, okay, I have the right to decrypt it, like more precisely to have a re-encryption of this uh ciphertext. Can you give me a re-encryption under my public key of this value? Like this, I can display it in the DAP. And this is exactly what's happening. Like I just received the re-encryption of this value, and now I can uh display it uh in my DAP, but on chain, you can't see the real value, like everything is uh remains encrypted on chain. So the first thing uh the second thing we want to do is like uh like a transfer.
So the transfer was uh oops, not changing network, just account. Switch back. So I will transfer, let's say 200. So for this uh operation, I need to encrypt an input. So as explained before, um I encrypt uh 200 and this value will be sent uh off-chain to the coprocessor. The coprocessor will store the ciphertext and will return like uh the actual handle and signature of the co-processor, and I will send to to uh Sepolia only the handle and the signature, not the actual ciphertext.
So I'm waiting for the transaction, and normally uh my balance will be uh just uh 800. And if we switch to uh the second user, we would have like uh the new balance. So okay, so the transaction has been done. And if I refresh my balance, I see that I have 800 now. So the my balance has been uh updated correctly, and you see the on my handle of my balance also uh has been changed with uh with a new handle, and uh we can check that if I uh switch to uh the second uh account, I have like 200. And um that's it.
Um the last thing to to see is obviously to take a look at the transaction, like this. You can see uh what's happening. So this is a mint transaction. So as you can see, there is nothing encrypted uh as uh we explained. If we look at logs, I think we have the plain text value, so this is 1000. So this is the min transaction, so very similar to a classic mint on a normal USC20, unencrypted one. And if I take a look at the transfer function, we should see uh some encrypted data, not encrypted data, but the handle, so a U in 256 represented the ciphertext, not log, sorry, in EC, yeah. And the input data is there, so you will have the signature and the un 256 in uh this uh input.
And uh this is like exactly what has been uh uh sent to the co-processor, and then the co-processor uh sent me uh this handle and this signature to uh to be used as uh as an input. I think that's all. We have like a four minutes uh for questions if uh anyone will uh have questions. Thank you. Uh yeah. Uh what you showed us is a fungible token that you call ERC20, but the events don't match RC20 and the function signature don't match RC20. And I'm expecting there is no balance function to get your balance, obviously.
So my question is how do you expect that to uh be integrated into DeFi? Like, I mean, I'm pretty sure it would not be able to work with Uniswap or any of the traditional DeFi that relies on the ERC20 uh uh interface. So are you expecting a new class of DeFi to be built on top of that? Uh I think we have like uh two possibilities on that. Uh the first one is clearly to say okay, we we create uh uh swap with uh encrypted ARC20 token, because this is like the like you said, like not exactly the same signature. This is like something very interesting to implement uh because like you could like imagine uh like keep uh everything encrypted even if you swap your your your tokens.
Another uh possibility is to uh have a wrapper around your ERC20. So you keep like the classic ERC20, but you just have a contract which uh like uh is uh on top of uh RC20, and you can use it uh like to have also using the classic swap. But at some point in this case, you would have to decrypt the value before doing the swap and then re-encrypt the value. So this is like possible, but it's less uh like you you leak some information during the process. So the best solution would be to have a a way to say okay, we do uh uh like the the AMM or uh or things like that directly in uh in a decryption decrypted value directly.
But we are like already work on this uh type of thing, and uh we have like uh like already like many developers like building stuff with uh encrypted uh values more with your answer, it it starts looking like a mixer of some sort. Uh are you worried that uh it may have legal implication? Um it's not like a mix of a mixer, you would like hide the the uh address like the wallet uh sending and receiving. This is not the case, you just uh like uh hiding uh eventually the amount.
And also like since everything is in solidity, you can be compliant with uh like any legal uh government or anything uh very easily, like because you can for example when we have this ACL, you can say that this wallet, which like uh is from like uh like a this government can decrypt any amount, any balance of anyone. You can write it in the contract. So you can also like imagine like a decentralized ID on chain where you store the country of every wallet encrypted, so no one like has this personal information in clear text on chain, but you can use this country to say okay, wallet from uh France can be decrypted decrypted by French government, for example. So this is like more like a contract implementation than technology issue.
I have a question. Is it possible using a solidity library uh to compare ciphertext to plain text right now? Yeah, sure. Yeah, okay. Uh the next question is this might be a naive question, but would it be possible to implement uh fully homomorphic encryption completely within the EVM? Like have it all like in Solidity or like some like you know, pre-compiled bytecode or something where you could do all this fully homomorphic encryption, but inside the EVM. Is that possible? Uh so without uh modifying modifying it. Um I think it's not maybe like maybe uh you could have something, but it would be too gas expensive, like if you want to integrate like TFHE or I like I've never seen that.
Uh we know that we people like uh use can use GFHE in Wasm. So uh it's possible, but it's very slow and uh very costly if you want to run it. If you're asking about the EVM and not Ethereum itself, that's what we actually did initially. We did it with the FHEVM native. Uh requiring a such requiring every validator to run a modified EVM using pre-compiles to be able to run the computer. That's what we started with. Uh but we feel like this approach is better uh because it can you don't need to run a set a separate network to do to do that. But uh yeah, we we we still have that uh running with uh some of our partners using pre-compiles.
Related talks
- Private smart contracts using homomorphic encryption
- Unlocking Privacy on Chain: Advancements in Hardware for FHE
- Privacy as Infrastructure for Web3
- 'Beating the Flash Boys with Fully Homomorphic Encryption' - Ravital Solomon
- Cryptography for Encrypted Mempools with Dan Boneh | a16z crypto Research Series
- ZK & The Future of Privacy
- Kohaku : privacy for everyone, by default
- Hiding Network Metadata: Introduction to Mixnets
- The Ethereum Privacy Stack: Private Reads, Networking & the Hidden Leak