Sunscreen 🛠️ Building private (d)apps

ETHGlobal New York 2023 · 11 Sept 2023 · 23:24

Ravital Solomon of Sunscreen introduces fully homomorphic encryption and asks whether a blockchain can keep a secret. The workshop explains plaintexts and ciphertexts, private computation on encrypted inputs, and uses such as private DEX orders and voting. It then walks through Sunscreen's Rust FHE compiler, an encrypted AMM example, the Parasol private testnet with FHE precompiles, and hackathon bounty ideas.

Speakers

Topics

From ETHGlobal New York 2023, hosted by ETHGlobal.

Transcript

Hi, I'm Rabia Talal, the founder of Sunscreen, and today you're going to hear all about how to build private dApps with Sunscreen. So, specifically, we're going to be looking at what is fully homomorphic encryption, aka FHE, and how does it fit into Web3? We'll then talk a little bit about what Sunscreen actually does. We'll go into two of our main products that are available for this hackathon. We have our fully homomorphic encryption compiler that makes FHE easy for developers to use. We also have our private testnet Parasol available.

And then we'll talk about how exactly you can get started using our FHE compiler, our private testnet, and what prizes we have available for the hackathon. Before we dive into FHE and how it fits into the Web3 space, let's talk a little bit about what FHE is and how it works. So, first, let's introduce the idea of a plaintext and ciphertext. A plaintext is an unencrypted message or a message entirely in the clear, whereas a ciphertext is an encrypted message. You can think of fully homomorphic encryption as an extension of public key cryptography. So, in a public key encryption scheme, we have three major algorithms. We have keygen that allows us to generate both a public and private

key. We have encryption that allows us to encrypt messages to somebody using their public key. And then we have decryption, which allows us to decrypt messages we've received using our private key. The public key is supposed to be shared with other parties, that way they can send you encrypted messages, whereas the private key is something you really want to keep secret because it's going to allow you to decrypt messages you've received. What does the homomorphic part mean in fully homomorphic encryption. It really refers to two special properties of the encryption scheme. We have that encrypting A and encrypting B and adding these two cipher texts together is the same as having added A and B as plain text and then encrypting

that. We also have that encrypting A and encrypting B and multiplying those two cipher texts together is the same as having multiplied A and B as plain text and then encrypting that. Blockchains like Ethereum are inherently transparent, meaning that all data is public and computations are performed publicly. One of the main questions we want to ask ourselves in this presentation is can a blockchain keep a secret? And the answer is yes, especially with fully homomorphic encryption.

How does FHE fit into the Web3 space? Well, you can think of FHE as allowing for something called private computation. Here, you can take some encrypted inputs, you can run a computation on your encrypted inputs, and you can get some encrypted outputs. The magical thing about FHE is that anyone can perform this computation directly on the encrypted data. There's no need for this party to know what the underlying data is. Something important to keep in mind when working with FHE is that it's really focused on confidentiality, so hiding the inputs and outputs of a computation. FHE on its own does not provide anonymity. So, we don't have anything about hiding user identity

here. Next, let's look at an example of how you might use FHE to update encrypted account balances. Let's say Alice has an encrypted account balance and she's receiving some transaction with an encrypted amount. Her new account balance could be updated to be the sum of her previous encrypted account balance plus the encrypted transfer amount she's just received. Something important to note is that for most real-world use cases of FHE, especially in the Web3 space, we really need zero-knowledge proofs to work in conjunction with the technology.

This is mainly for ensuring that user-provided data satisfies certain application-dependent conditions. So, for example, in that private transaction case, we would really want to make sure that Bob, who is sending Alice some encrypted amount, has enough money in his account to make this transaction. For this hackathon, we're really going to be focused on FHE, so there won't be much about zero-knowledge proofs, but this is something important to keep in mind when you go from the hackathon into the real world.

Now that we've covered how FHE works, let's look at what exactly is possible with FHE and what are some interesting applications that are enabled with this technology. Well, there are some interesting applications in both the Web3 and the Web2 space. In terms of what FHE can enable in Web3, FHE can allow for private user orders in decentralized exchanges. It can also allow for private voting in decentralized autonomous organizations. In terms of what's possible in Web2, FHE allows for private machine learning. So, specifically, private training and private inference. It also allows for analytics with full user control.

Now that we've gotten you excited about FHE and its potential in both the Web2 and Web3 space, let's look at what FHE is like from a developer's point of view. There are a few important things to keep in mind when you start working with FHE. The first is that FHE introduces both a time and space overhead. So, when you're working with FHE, you can expect that computations are going to take longer when they're encrypted versus doing them in the clear. Additionally, when you encrypt data with an FHE scheme, the data is now quite a bit larger than it was when it was in the clear. Additionally, you'll find that certain computations are quite a bit more difficult to do with FHE than they were when you weren't

working with any encryption technology at all. What exactly is FHE like to use if you're a developer right now? Unfortunately, it turns out that FHE is actually quite difficult to use. You can really think about FHE is developed by experts for other experts. When you're working with most FHE libraries, you really have to think about how do you choose scheme parameters to ensure optimal performance. You also have to worry about ensuring that a certain level of security is met when you choose these scheme parameters. You need to worry quite a bit about key and ciphertext sizes because there can be a fairly large blow-up if you're not careful choosing scheme parameters. And finally, you have to worry quite a bit

about computational depth. For example, how many sequential multiplications do you want to do on your encrypted data? That might inform what scheme parameters you actually choose. With all of these challenges in using FHE, how then can developers harness the power of FHE to build private applications? Well, that's exactly why Sunscreen was started. At Sunscreen, we build privacy tools for developers. Our goal as a company is to make advanced privacy technology like fully homomorphic encryption easy for developers to use.

We really work at the intersection of both fully homomorphic encryption and zero-knowledge proofs. But for this hackathon, we're going to be focused on FHE. There will be two products available for this hackathon. We'll have our FHE compiler that makes FHE easier for developers to work with. We'll also have our private testnet available, which you can think of as EVM plus some limited FHE precompiles. We have some pretty exciting things in the works. Specifically, we have our zero-knowledge proof compiler, and we're also working on an implementation of a threshold FHE scheme that would support private shared state. Unfortunately, neither our ZKP compiler nor our threshold FHE

implementation will be available for this hackathon, but stay tuned. You might find this at another hackathon. Thank you, Rivolta. My name is Rick, and I'm going to be walking you through how to use Sunscreen today, which we'll be using to approach our off-chain bounties. So, what is Sunscreen? Sunscreen is a library for performing computation using zero-knowledge proofs and fully homomorphic encryption, but we're only going to be looking at the FHE portion today. It consists of three parts. There's a language for expressing FHE programs, a compiler for turning them into circuits, and a runtime for running said circuits, and generating keys and encrypting data.

You can view the docs at docs.sunscreen.tech or pause your video and visit the QR code on the right. Let's dive right in and create an FHE program. Here we have an FHE program called simple multiply that returns the product of A and B. Note that it's a standard Rust function except that it has an FHE program macro on it that puts it in a form that our compiler can use. There are limitations as to what you can do in an FHE program, and the full list is available in the documentation I've previously linked. A notable example is comparisons. You can't perform comparisons in FHE programs, so please don't attempt any bounties that require them.

You'll note that A and B have the type Cipher of Signed as does the return value. Cipher of T means that the input is the value is encrypted, and T means that it's not. Sunscreen allows you to perform computations on a mix of encrypted and unencrypted data. Additionally, FHE programs can return one value by returning said value, or you can reuse a tuple to return more than one. Next, let's compile our simple multiply example. First, we create a compiler and then pass the simple multiply function, and then we call compile. It's that simple.

During compilation, the compiler will choose FHE scheme parameters that maximize performance, and then finally, the compiler will return the application that results. You can use this application later when you need to actually run the program, and you can also get the scheme parameters that the FHE compiler chose. Next, we'll create a runtime. A runtime allows you to encrypt and decrypt data, perform key generation, and run programs. To do this, you call the new constructor, and you pass the parameters that previously resulted from the application compilation, and this returns a runtime that you can later use.

And now, let's put it all together. We're going to use our runtime to first generate a public and private key. We're then going to encrypt 15 and 5 using said public key into the values A and B, and then we're going to run our simple multiply program that we get out of our app. We pass the values A and B and the public key to the run function, and this returns a results vector. This vector contains the return values from your FHE program, but if you recall our simple multiply example only had one, which we decrypt and assign to C using the private key.

And you'll note that finally, we assert that 75 equals 15 * 5, and the program passes. Next, let's build a more complex example. We're going to build a quote AMM, and I use quotes because this isn't running on chain, and no balances are actually being updated. But what this does do is allow you to see how you can express the constant product formula using Sunscreen. So, Alice is going to encrypt a number of new cipher tokens she wishes to change for Ethereum, send this value to a validator who will then run the constant product formula homomorphically, return an amount of Ethereum that Alice should receive encrypted under Alice's key, and Alice can then decrypt this value.

Next, let's look at the code backing our AMM example. First, we need to write an FHE program that takes a number of new cipher tokens that one wishes to trade and returns the number of Ethereum tokens they should receive under the constant product rule. We have hardcoded the number of Ethereum and new cipher tokens in the liquidity pool, and we put the FHE program macro on our swap new function. This puts it into a form that our compiler can use. Next, let's look at the miner or the validator. The first thing they will do is run setup, which will compile the FHE program and gets the parameters required to run it, which it we can pass to an FHE runtime that we create,

and we stash this runtime for later so that we can run the FHE program. The run contract function does just that. It takes a number of new cipher tokens to trade encrypted under a particular public key, and computes the number of Ethereum that individual should receive encrypted under the same public key. Next, let's look at our client. We have named our client Alice. The first thing Alice will do is run setup. During setup, she needs to take the parameters associated with the AMM and generate a runtime. She uses this runtime to create her public and private key, and you can imagine that in a real situation, Alice would publish this public key to the chain and keep her

private key in her wallet somewhere. Alice can then create transactions by taking an unencrypted amount and encrypting them under her public key. She finally Alice can decrypt amount of Ethereum she's received using her private key which is done here. Now, let's tie it all together by looking at the main method. First, we run minor setup, which gets the scheme parameters that Alice can use to create her runtime and generate her public and private key. Alice can then create a transaction, so she wishes to trade 20 new Cypher tokens.

Then, she can pass this encrypted transaction amount and her public key to the minor who can run the contract and return to her an encrypted amount of Ethereum she should receive. Alice can then decrypt this balance and we can see it. So, let's run this program. And 420 new Cypher tokens, Alice should receive 1.96 Ethereum. Next, let's talk about some of Sunscreen's off-chain bounties. We're going to be offering a cash prize for the best use of Sunscreen's FHE compiler. A few of the bounty ideas include private inference and training and data analytics.

For private inference and training, you can imagine having a neural network that runs homomorphically over encrypted inputs, producing an encrypted output. For data analytics, you can imagine having an application whereby you want to compute some statistics over user data without compromising user privacy. For a full list of hackathon ideas, go to hackathon.sunscreen.tech or you pause the video and use the QR code on the right. And now I'm going to throw over to Sam, who's going to talk about some of our on-chain bounties. Thanks Rick.

Hi, everyone. I'm Sam Tai, and I'm here to tell you about our private testnet Parasol. Fortunately, Parasol is easy to explain if you're familiar with the EVM. Our nodes are basically stock EVM nodes. They've just been augmented with additional precompiles to enable FHE. In other words, you can do everything you can do on Ethereum plus additional FHE capabilities. Now, if you've ever had to call manually, you know it is quite a pain and typically requires some gnarly assembly.

So, we've written an you can import to have all these FHE functions at your disposal. You can view that contract by scanning this QR code. Mostly, it contains functions like the ones listed here for performing arithmetic like add, multiply, and subtract on various types and mixes of encrypted and plain text values. In this example at the bottom of the slide, we're updating an encrypted balance with an incoming encrypted transaction amount. The public key is required for this operation, and the ciphertext arguments should also be encrypted under the same public key provided here.

So, on our network, each user has their own personal key pair, which we'll call the user key. But, if you recall the limitation we mentioned earlier, a given private computation is done on ciphertexts encrypted under one one public key. So, if you have some shared private state, this is going to be a hurdle. One way around this limitation is to use our network key precompiles. Again, these are exported from FHE.sol. In this scenario, all the relevant ciphertexts for a computation get encrypted under the network public key.

Of course, at some point, you're going to actually need to decrypt the result of some computation. The procedure for this is to re-encrypt network ciphertexts into the ciphertexts under your key pair, so that you can decrypt locally with your own private key. Note that this re-encryption operation is performed by a precompile, but the contract authors need to enforce proper access control, so that people can't re-encrypt data that they shouldn't have access to. Here's a diagram showing the general flow for an application that makes use of the network key architecture.

The two parties that want to compute with each other are Alice and Bob. Alice has a private value of 10. Bob has a private value of 20. And both of them encrypt their values under the network public key. Those ciphertexts are shown in teal here in the middle labeled A and B. Then on the Parasol network, validators will compute the homomorphic addition of these two ciphertexts, which they can do because they're both encrypted under the same public key. This results in a new ciphertext C, which is still encrypted under the network key pair.

Each party then requests a re-encryption of the network ciphertext C into a ciphertext encrypted under their user key pair. We've indicated this by using an orange color for ciphertexts encrypted under Alice's key and green for ciphertexts encrypted under Bob's key. Finally, they can decrypt with their respective private keys. Lastly, I should mention that you don't necessarily have to use the network public key, depending on your application. You'll notice that you can supply whichever public key you wish when performing FHE operations.

And you may want to have certain ciphertexts that are only encrypted under user key pair, depending on your trust model. But if you have private shared state amongst multiple parties, you'll see that competition becomes much more challenging to implement without the network key. Here's a very simple example demonstrating usage of our FHE.sol contract. This contract makes use of the network key. It just stores an encrypted number here on line seven that can be both set and incremented.

You can see in the constructor this is set to an encrypted value of zero and set number is similar. Increment is a bit more interesting. Here we add the plain text value one to the current encrypted number and get back a new ciphertext. You can also see that we're supplying the network public key as an argument here. Finally, there's the re-encrypt number method. This takes a user's public key and outputs a new ciphertext encrypting the same number, but this ciphertext can be decrypted by the user's private key. One other thing to mention is that in Solidity, we represent our ciphertexts and public keys as byte arrays.

This bytes type is just a dynamically sized byte array in Solidity. You can see this contract in our mono repo template. This is the easiest way to get started hacking on Parasol. This screenshot is specifically of our Rust template, but we actually have a few different options for people who want to use other programming languages. If you added this QR code on the right, you'll see a list of available language templates and links to the appropriate pages on GitHub. And when you go to GitHub, you'll see the screen button titled Use this template. Just click there to create a repository starting from this template.

So, to briefly explain this directory structure, we've got a contracts directory that is a Foundry project. If you're not familiar, Foundry is a framework for writing Solidity contracts. The Foundry project already comes with Sunscreen's FHE contract as a dependency, so you can hit the ground running. What's really cool about this though is that the bindings directory contains a Rust crate with bindings that match to the Solidity contracts. So, all of the methods you define on your Solidity contract now exist in Rust with the correct type signatures and everything. It's really nice.

If you look at the app directory, you'll see a CLI application that makes use of those generated bindings. And that CLI application also demonstrates usage of our Sunscreen web3 library that makes it really easy to use Sunscreen FHE types on Parasol. So, this is a really good starting point for people to just start from the template and modify it to your needs. The readme should contain everything you to know to get started. We do have exclusive prizes available that will be awarded to the best use of Parasol.

And this is separate from the FHE compiler prize. While the hackathon is open-ended, we do have some bounty ideas to help get you started. One is an AMM with private user orders. There's real value in having private orders, not only in that you get to keep your balances private, but you can also prevent certain attacks like front running. Earlier, we saw one small piece of a potential encrypted AMM implementation, but this bounty is specifically about the lending this as a contract on the Parasol testnet.

So, it'll be a bit more challenging than the code example we saw earlier. Another interesting application idea is to implement private voting. This idea has become increasingly popular, and not just in Web3. If you're not familiar with the voting schemes in general, it is highly desirable to have private vote casting. Without it, voters are subject to bribes and coercion and all sorts of social pressures that can lead to poor outcomes. The network key precompilers will likely come in handy for both of these projects.

Again, here's a link to our hackathon guide. You can scan the QR code to get there. These bounties are discussed in much better detail there, and you'll also find more information about FHE and best practices, so definitely check it out. If you haven't FHE before this video, I hope we've conveyed to you just how groundbreaking this technology is, particularly in Web3. We've seen how it can completely eliminate front running in DeFi, and it also just enables a much broader class of applications that require private shared state.

We're really excited about this technology, and we hope you are, too. That's all for this video. We'll be back up on that one of the cocktail tables, so drop by to chat or ask us any questions. We'll also have a Discord channel dedicated to participants, so you can find us there, too. We're excited to see what you come up with. Happy hacking.