Sunscreen 🛠️ Building private (d)apps
ETHGlobal·Mon, Oct 9, 2023, 12:00 AM
Join Ravital Solomon and their team as they build on sunscreen! Hack with some of the most skilled web3 developers, designers and product builders from all around the globe for a weekend-long adventure to advance the Ethereum ecosystem, ETHGlobal New York 2023. ----------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com
Transcript
Hi, I'm Robatal, the founder of Sunscreen, and today you'll hear all about how to create private DAPs with Sunscreen. So, specifically, we'll look at what fully homomorphic encryption, also known as FHE, is and how it fits into the web 3 space. Then we'll talk a little bit about what Sunscreen actually does. We will be looking at our two main products available for this hackathon. We have our fully homomorphic encryption compiler that makes FHE easy for developers to use.
We also have our private umbrella testnet, and then we'll talk about exactly how you can get started using our FHE compiler, our private testnet, and what prizes we're offering for the hackathon. Before we delve into FHE and how it fits into the web 3 space, let's talk a little about what FHE is and how it works. So, first let's introduce the concept of plaintext and ciphertext ciphertext . Plaintext is an unencrypted message or a message that is completely in the clear, while ciphertext is an encrypted message. Fully homomorphic encryption can be viewed as an extension of public-key cryptography.
So, there are three main algorithms in a public key encryption scheme. There is a keygen that allows us to generate both public and private keys. There is encryption that allows us to encrypt messages sent to someone using their public key. And there is decryption, which allows us to decrypt the messages we receive using our private key. The public key must be provided to other parties.
This way parties. This way , they can send you encrypted messages. The private key is what you really need to keep secret, as it will allow you to decrypt the messages you receive. What does the homomorphic part mean in fully homomorphic encryption? It actually refers to two special properties of the encryption scheme.
Encrypting A and encrypting B, and then adding these two ciphertexts together–is the same as adding A and B as plaintext and then encrypting it. We also have that encrypting A and encrypting B, and then multiplying these two ciphertexts together, is the same as multiplying A and B by the plaintext and then encrypting it. Blockchains like Ethereum are inherently transparent, meaning all data is public all data is public and computations are performed publicly are performed publicly . One of the main questions we want to ask ourselves in this presentation is whether blockchain can maintain secrecy? And the answer is secrecy?
And the answer is yes, especially with fully homomorphic encryption. How does FHE fit into the web 3 space? Well, you can think of FHE as an opportunity for something called private computing. Here you can take some encrypted input data, you can perform calculations on it, and you can get some encrypted output. The magic thing about FH is that anyone can perform this calculation directly on encrypted data encrypted data .
This party does not need to know what the underlying data is. It's important to remember that when working with FHE, it's really focused on privacy. So hiding the input and output of the calculation. FHE does not provide anonymity by itself. Therefore, there is nothing here about hiding the user's identity.
Next, let's look at an example of how FHE can be used to update encrypted account balances. Suppose Alice has an encrypted account balance and she receives a certain transaction with an encrypted amount. Her new account balance can be updated to the amount of her previous encrypted account balance plus the amount of the encrypted transfer she just received. It is important to note that for most real-world use cases of FHE, especially in the web 3 realm, we really need zero- disclosure evidence to work with this technology. This is mainly to ensure ensure that the data provided by the user meets certain conditions that depend on the application.
For example, in the case of this private transaction, we really want to make sure that Bob, who is sending Alice a certain encrypted amount, has enough money in his account to complete this transaction. In this hackathon, we will really focus on FHE. So, there won't be much about zero- disclosure evidence , but it's important to remember when you move from the hackathon to the real world. Now that we've looked at how FHE works, let's look at what exactly is possible with FHE and what interesting applications can be implemented using this technology. Well, there are some interesting applications in both the web 3 and web 2 spaces.
As for the capabilities of FHE in web 3, FHE allows users to execute private orders on decentralized exchanges. It can also allow private voting in decentralized autonomous organizations. Regarding web 2 capabilities, FHE enables private machine learning, including private training and private inference. It also allows for analytics with full user control. Now that we've got you interested in FHE and its potential in both the web 2 and web 3 spaces, let's look at what FHE is from a developer's perspective.
There are a few important things to remember when you start working with FHE. working with FHE. First, FHE introduces both time and space overhead. Therefore, when you work with FHE, you can expect calculations to take longer when encrypted compared to performing them in the clear. Additionally, when you encrypt data using the FHE scheme, it is now significantly larger than it was in plaintext.
Additionally, you will find that certain calculations are much more difficult to perform using FHE than when you were not working with any encryption technology at all. What exactly does it mean to use FHE if you are currently a developer? Unfortunately, it turns out that FHE is actually quite complicated to use. You can really think of FHE as being designed by experts for other experts. When you work with most FH libraries, you really need to think about how to choose the circuit parameters to ensure optimal performance.
You also need to be concerned about ensuring a certain level of security when choosing these schema parameters. You need to worry quite a bit about the key and ciphertext sizes , as there can be quite a bit of bloat if you 're not careful with your choice of scheme parameters. And finally, you need to worry quite a bit about computational depth. For example, how many consecutive multiplications do you want to perform on the encrypted data encrypted data ? This may affect what scheme parameters you actually choose.
Given all these difficulties in using FHE, how can developers leverage the capabilities of FHE to create private applications? That's why Sunscreen was created. At Sunscreen, we build privacy tools for developers. Our goal as a company is goal as a company is to make advanced privacy technologies, such as fully homomorphic encryption, easy for developers to use. We are really working at the intersection of fully homomorphic encryption and zero- disclosure proofs.
But for this hackathon, we will focus on FHE. Two products will be available for this hackathon . We will have our FHE compiler, which makes working with FH easier for developers. We will also have our private testnet available, which can be thought of as an EVM, plus some limited pre-compilations of FHE. We have some pretty interesting things in the works.
In particular, we have our zero- disclosure proof compiler, and we are also working on implementing a threshold FH scheme that will support private shared state. Unfortunately, neither our ZKP compiler nor our threshold FHE implementation will be available for this hackathon, but stay tuned for updates. You can find this at another hackathon. Thank you, Ravitl. My name is Rick, and today I'm going to show you how to use sunscreen sunscreen , which we'll use to get your off-chain bounties.
So, what is sunscreen? Sunscreen Sunscreen is a library for performing computations using zero- disclosure proofs and full encryption. But today we will only look at the FHE part. It consists of three parts. There is a language for expressing FHE programs, a compiler for converting them into schemes, and a runtime environment for running these schemes, generating keys, and encrypting data.
You can view the documentation at docs.ssonscreen.te or pause the video and go to the QR code on the right. Let's dive right into creating an FHE program creating an FHE program . Here we have an FHE .
Here we have an FHE program called " program called " simple multiplication" that returns the product of A and B. Notice that this is a standard Rust function, except that it has an FHE it has an FHE program macro that converts it into a form converts it into a form that our compiler can use. There are limitations to what you can do in the FHE program, and a full list is available in the documentation I linked to earlier. A striking example is comparison. You cannot perform comparisons in FHE programs.
Therefore, please do not attempt any tasks that require them. You will notice that A and B have a signed cipher type T , as does the return value. The cipher T means that the input value is encrypted, and T means that it is not encrypted. Sunscreen allows you to perform calculations on a combination of encrypted and unencrypted data unencrypted data . Additionally, FHE programs can return a single value by returning this value, or you can use a tuple to return more than one.
Next, let's compile our simple multiplication example. First we create a compiler create a compiler , then we pass a simple multiplication function, and then we call compile. It's that simple. During compilation, the compiler will choose FHE scheme parameters that maximize performance. And finally, the compiler will return the result of the program.
You can use this program later when you need to actually run the program. And also you can get the schema parameters that the FH compiler chose. Next, we will create an execution environment. The runtime environment allows you to encrypt and decrypt data, generate keys, and run programs. To do this, you call the new constructor and pass in the parameters that were previously obtained as a result of compiling the program.
This returns a runtime environment runtime environment that you can use later. Now let's put it all together. We are going to use our runtime runtime to first generate the public and private keys. Then we're going to encrypt 15 and 5, using this public key, into the values ​​A and B. And then we're going to run our simple multiplication program, which we get from our program.
We pass the values ​​of A and B in the public key to the run function, and it returns a vector of results. This vector contains the return values ​​from your FH program. But if you remember, our simple multiplication example only had one, which we decrypt and assign to C using the private key. And you'll notice that finally we assert that 75 is equal to 15*5, and the program runs successfully. Next, let's create a more complex example.
We are going to create an AMM with a quote. And I use quotes because it doesn't work in a chain and the balances don't actually update. But it allows us to see how we can express the constant product formula using sunscreen sunscreen . So, Alice is going to encrypt some new crypto tokens that she wants to exchange for Ethereum. Send this value to the validator, which will then run the constant product formula homomorphically.
Return the amount of Ethereum that Alice should receive encrypted under Alice's key. Alice can then decrypt this value. Next, let's look at the code supporting our AMM example. First, we need to write an FHE program that takes several new crypto tokens that we want to trade and returns the number of Ethereum tokens they should receive according to the constant product rule . We hardcoded the number of Ethereum and new crypto Ethereum and new crypto tokens in the liquidity pool and placed the FHE program macro in our swap new function.
This converts it into a form that our compiler can use. Next, let's look at the minor or validator. The first thing they will do is run setup, which will compile the FHE program and get the parameters needed to run it, which we can pass to the FH runtime environment we created, and we will save this runtime environment for later so we can run the FH program. The launch contract feature does just that. It accepts a certain number of new encryption tokens for exchange, encrypted under a certain public key, and calculates the amount of Ethereum that person should receive, encrypted under the same public key.
Next, let's look at our client. We named our client Alice. The first thing Alice will do is run setup. During configuration, it needs to take the AMM-related parameters and generate a runtime environment runtime environment . It uses this runtime to generate its public and private keys.
And you can imagine a real-world situation where Alice publishes this public key on the chain and stores her private key somewhere in her wallet somewhere in her wallet . Alice can then create transactions by taking the unencrypted amount and encrypting it under her public key. Ultimately, Alice can decrypt the received Ethereum amount using her private key, which is done here. Now let's tie it all together by looking at the basic method. First, we run a minor setup, which gets the schema parameters that Alice can use to create her runtime environment and generate her public and private keys.
Alice can then create a transaction. So, she wants to exchange 20 new crypto to exchange 20 new crypto tokens. She can then pass this encrypted transaction amount and her public key to a miner, who can run the contract and return to her the encrypted amount of Ethereum she will receive. Then Alice can decrypt this balance and we can see it. So, let's run this program.
And for 20 new crypto tokens, Alice should receive 1.96 Ethereum. Next, let's talk about some of Sunscreen's extraordinary rewards. We are going to offer a cash prize for the best use of the FHE Sunscreen compiler. Some of the reward ideas include private, inferential learning and data analytics.
For private learning, you can imagine a neural network that operates homomorphically on encrypted inputs, producing an encrypted output. For data analysis, you can imagine an application where you want to calculate certain statistics about user data without compromising their privacy. To see the full list of hackathon ideas, go to hackathon.ssonscreen.te or pause the video and use the QR use the QR code on the right.
And now I'll move on to Sam, who will talk about the server's on-chain bounties . Thanks, Rick. Hello everyone. I'm Sam Tay, and I'm here to tell you about our private parasol testnet. Fortunately, parasol is easy to explain if you are familiar with EVM.
Our nodes are essentially standard EVM nodes. They were simply supplemented with additional precompilations to activate FHE. In other words, you can do everything you can do on Ethereum, plus the additional capabilities of FHE. Now, if you've ever had to call previous compilations manually, you know that it's quite complicated and usually requires some complex assembly. So, we wrote a reliability contract FHE.
sol that you can import to have all these FHE features at your disposal your disposal . You can view this contract by scanning this QR scanning this QR code. It mostly contains functions similar to those listed here to perform arithmetic operations, such as addition, multiplication, and subtraction, on various types and combinations of encrypted and plain text values. In this example at the bottom of the slide, we update the encrypted balance with the incoming encrypted transaction amount. This operation requires a public key, and the cyperex arguments must also be encrypted with the same public key provided here.
So, in our network, each user has their own personal key pair, which we will call the user key. But if you remember the limitation we mentioned earlier, this private computation is performed on ciphertext ciphertext encrypted with a single public key. Therefore, if you have any shared private property, it will be an obstacle. One way to get around this limitation is limitation is to use precompilations of our network keys. Again, they are exported from fagg.
sol. In this scenario, all relevant ciphertexts for computation are encrypted under the network's public key. Of course, at some point you will actually need to decipher the result of some calculation. The procedure is to re-encrypt network ciphertexts into ciphertexts under your key pair so that you can decrypt them locally using your own private key. Note that this re-encryption operation is performed by precompilation, but contract authors should ensure proper access controls so that people cannot re-encrypt data they should not have access to.
Here is a diagram showing the general flow for an application that uses a network key architecture. The two parties who 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 they both encrypt their values ​​under the network's public key. These ciphertexts are shown here in turquoise 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 are both encrypted under the same public key. This results in a new ciphertext C, which is still encrypted under the network key. Then each party requests the re-encryption of the network ciphertext C into ciphertext encrypted with the user's key pair. We have indicated this by using orange for the ciphertext encrypted with Alice's key and green for the ciphertext encrypted with Bob's key. Finally, they can decrypt using their respective private keys.
Finally, I should note that you may not necessarily need to use a public network key depending on your application. You will notice that you can specify can specify any public key you want when performing FG when performing FG operations. And you may want to have certain cyberattacks that are encrypted with only the user's key pair, depending on your trust model. But if you have shared private state between multiple parties, you will find that the computation becomes much more difficult to implement without a network key. Here is a very simple example demonstrating the use of our FH.
soul contract. This contract uses a network key. It simply stores an encrypted number here, in line 7, which can be either set or incremented. You can see in the constructor that this is set to the encrypted value zero, and the set number is similar. Increment is a little more interesting.
Here we add the plaintext value of one to the current encrypted number and get the new ciphertext. You can also see that we provide the network's public key as an argument. Finally, there is the reencrypt number method. It takes the user's public key and outputs a new ciphertext that encrypts the same number. But this ciphertext can be decrypted using the user's private key.
Another thing to note is that in solidity we represent our ciphertexts and public keys as bite arrays. This bytes type bite arrays. This bytes type is simply a dynamically modified bite array in solidity. You can see this contract in our monor repository template. This is the easiest way to start hacking on parasol.
This screenshot is specifically made for our Rust template for our Rust template , but we actually have a few different options for people who want to use other programming languages. If you go to this QR code on the right this QR code on the right , you will see a list of available language templates and links to the corresponding pages on GitHub. And when you go to GitHub, you'll see this green button called " button called " use this template". Just click there to create a repository create a repository starting from this template. So, to briefly explain this directory structure, we have a contract directory, which 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 [ __ ] contract as a dependency, so you can get started right away. What's really cool about this is that the bindings directory contains a Rust container with bindings that correspond to Solidity contracts. So all the methods you define in your Solidity contract now exist in Rust with the correct type signatures and everything. This is really great.
If you look at the app directory, you will see a CLI application that uses these generated bindings. And this CLI bindings. And this CLI application also demonstrates the use of our Sunscreen web3 library, which greatly simplifies the use of Sunscreen FH types on Parasol. So, this is a really good starting point for people to just start with the template and modify it to suit their needs. The readme file should contain all the information you need to get started.
We have exclusive prizes to be awarded for the best use of Parasol, separate from the FH compiler prize. While the hackathon has an open schedule, we have a few reward ideas to help you get started. One of them get started. One of them is AMM with private user orders. Having private orders has real value.
Not only can you keep your balances private, but you can also prevent certain attacks such as front running. We previously saw one small snippet of a potential encrypted AMM implementation, but this reward is specifically for implementing that as a contract on the Parasol testnet. So this will be a little more complicated than the code example we saw earlier we saw earlier . Another interesting application idea is the implementation of private voting. This idea is becoming increasingly popular, not only in web 3.
If you are not familiar with voting schemes in general, it is highly desirable to have private voting private voting . Without it, voters are subject to bribery, coercion, and all sorts of social pressure, which can lead to poor results. Precompiling network keys will likely be useful for both of these projects. Again , here is a link to our hackathon guide. You can scan the QR code to get there.
These rewards are discussed in much more detail there. You will also find more information about FHE and best practices. So be sure to check this out. If you have n't heard of FHE before this video, I hope we've conveyed to you how groundbreaking this technology is, especially in web 3. We've seen how it can completely eliminate front- eliminate front- running in D5, and also allows for a much broader class of applications that require private shared state.
We are very excited about this technology, and we hope you are too. That's all for this video. We'll be at the hackathon at one of the cocktail tables cocktail tables , so come by to chat or ask us ask us any questions. We will also have a Discord channel dedicated to members, so you can find us there as well. We're curious to see what you come up with.
Happy hacking.
Automatic transcript — names and jargon may be misspelled.