DAPPCON 2019: Workshop - Bring Data Privacy to Ethereum Apps w/ Lena Kleyner (Enigma)

DappCon 2019 · August 2019 · 36:39

At a DappCon 2019 workshop, Lena Kleyner of Enigma explains how to bring data privacy to Ethereum applications, covering techniques for computing over encrypted data without exposing it on-chain. She walks through Enigma's approach to combining smart contracts with off-chain privacy-preserving computation. The session gives developers practical tools for building privacy-conscious decentralized applications.

Speakers

Topics

From DappCon 2019, hosted by Dappcon Berlin.

Transcript

my name is Lena I'm a software engineer at enigma and enigma tries to make decentralized application to use private data I'm yeah I plan to just yeah this will be here for several seconds yeah okay so first of all if by any chance someone has done the preparation step before great I guess most of you haven't that's ok also the VM is quite big so I'm not sure that you'll if you start

now you'll be able to join us and during the workshop but it's completely fine this is this is optional you can just follow along and download the slides and do it afterwards if you if you want it's ok I can move on our cool ok so a small a small so what we're going to do today I'll do a introduction to enigmas solution and then we'll do a workshop in which we are going to learn how we can build a secret contract on a nygma and how we can interact with that please if

you have any questions don't hesitate raise your hand if this question are regarding things not working on your environment let's take it offline please okay so we're starting with enigma and why is that important for you to to know as you might know blockchain I private which is great and private sorry blockchain are public which means that all data is visible to everyone which is great for some application but there's some other application that everyone can think of that require private data as medical data or credit scores or let's

say an application like voting so enigma tries to solve this problem by enabling the the use of decentralized application who use private data meaning encrypted data we are working hard towards our first release which is going to be discovery this is will be a public a public permissions network economically incentivized and also compatible with the theorem by compatible with the

theorem I mean that in a sense you could outsource your private computation to enigma while doing the other functionality on etherium okay I think it will be most simple to understand our ecosystem through our stakeholders the we have developers who were the ones deployed secret contracts we have the users who use this the centralized application developed by the developers and we have the workers who are what the one that executes the the computation and they are getting paid for for the

execution ok so developers they're writing what we called secret contract meanings it's like smart contract run on on private data they deploy it on the Enigma network we have users who can interact with this secret contract which are the our execution units and send computation jobs which we called task and the task also sent to enigma Network we also use the cerium for us saving mater that methylate of the task

and we have the workers who are the ones that are executing those tasks workers are selected for the task on whaler lottery based on their staking okay so some more key turns secret contract and task we've already defined let's let's talk about state state is very much like storage on aetherium for stateful computation this is this is a we can save persistent data on enigma

Network another key term on a web assembly or in shortcut wisdom this is a standard for binary code originally developed for web application but also had in mind other environments like ours so we used web assembly in in a way that our code of the secret contract is compiled to web assembly is being it is what is being deployed on the network gas also a term that will be that that is used in our network again this is

very much familiar to to Assyrian developers or users each computation job has has a fee and the fee is being calculated by defining two parameters one is gas limit and the second one is gas price okay we'll move on to discussing about the development environment in what we what we're going to use in order to have a sense of enigma network okay so we have recently released a

development Network which is a sandbox network of enigma that you can run locally on your computer and it will give you it can make you familiarize with enigma solution it it gives you a way to develop a secret contract deploy them to the network interact with them and test them okay so from now on if you see this queue it means you can jump in I don't know if it's it's okay it's optional like I said at the beginning but just have that in mind okay so first

of all we're going to navigate to this path which is the the root path of one of the problem that we're going to solve and we first of all we need to start our local network by running discovery start you should see something like that it means all the relevant components of enigma are being in initialize and ready and they're running and ready from now on if for for the for the next phases we will use another job because we need the

work at the network running for to communicate with okay so we're now moving on to the fun part the workshop and the workshop is divided into two phases on the first phase we're going to develop our secret contract and then we're going to interact with it okay so secret contract or enigma are written in rust why last its memory safe it has a good performance it has a great package manager' and there's a fantastic

community it has been voted the most loved programming language three years in a row developer yeah okay so before we diving into the code let's try to introduce the problem that we're going to solve the problem is a malaria problem we have two guys Alice and Bob and they are millionaires and like any other millionaire they're obsessed with both their wealth but also with their privacy so they're very interesting who who is who is the richer between them

but they want to find that out without revealing their their exact wealth so that's what this is the problem that we're going to solve we're going to develop a secret contract implementing this problem and we're going to and what would it will be its interface we'll be adding a millionaire data Bob will add his data then Alice will add her data encrypted of course private scene only to the person who make them this request and then we're going to call compute

Rich's to find out who's the richest which can be done by in either one of Alice or Bob or by a third party and of course the computer richest will only give the address or the name of the mineral without revealing the the wealth okay so this is the mineral problem is that is the root directory which implements these problems and now we go - deep deep dive into the code implementing that okay so we recommend to start the implementation of each

secret contact by adding this annotation the no STD annotation rust has a standard a library which provides lots of useful information ality but because we are running we're using the wisdom the web assembly sandbox not all functionality is being accessible to us so so even like we if you use some of using some of this functionality like

threads it can in compile it will compile but we might have a runtime exception that's our commendation to remove the use of the standard library but don't worry there's useful functionality like strings and vector which we'll be bringing into scope of the contract by adding by importing a Nigam of specific libraries so we're bringing into scope one of the Enigma libraries it's called Eng wasum we imported and then and the second line we're bringing it to scope everything

within so what it has is the subset of the standard library functionality as described but also enigma specific services like reading and writing to and from the state what state the persistent state the stories that we've discussed before which is also being kept encrypted the next thing that we're bringing into scope is another enigma library called Eng wasn't derived and we bringing we're bringing into scope only one annotation from it called pub interface and this is required because

this is enable us to expose public function to be called from outside and we'll see the use of it shortly the last thing that we need to bring into scope is server which is a create or package in Raths responsible on civilization in visualization and we'll see the use of it shortly ok here we're defining a constant string millionaires and we'll see the use of it very shortly ok now we're going to define a struct a struct

that will represent a millionaire and like we saw before a million I will have to two parameters one is the address the ID of the milliner the the other thing is it's a network please note that above the definition of distract we use a notation which what it does it auto code for theorization and visualization and it is required for us for reading and writing from States which we'll see in a second I want to you all you have pay attention to the types of the parameters

this is H 2 5 6 in you 2 5 6 there's the list of supported types that can be defined in the secret contract but for the for publicly exposed types and these two types are part of this list and the fullest can be found at the appendix of this presentation ok now we're moving to definition of public facing function so we're defining here a trait which is very similar to abstract interface for the ones of you who are familiar with

Java so we're defining here to function the name of the the name of the trait is not something that is fixed we used your contract interface you can use whatever you want so we're defining here to fact to function again like we saw in the previously on the example aadmi Lehrer to add the information of the new millionaire and compute riches to find out who is the richest from the added from the current minerals added I would like us to pay attention for something important at this point um if we have a constructor if we need if our secret

contract requires the constructor for doing some initialization then the definition of the constructor should be added here in our case there is nothing that we need to initialize thus we leave it empty but the default constructor will be generated automatically for us and the use of this contract so we will see shortly okay so now we're starting with the definition of our contract again here we define a struct name contract this is also not a fixed name you can use any name that you like in in

rust in order to implement trade you have to define a struct which implements the traits on on on in it so here's this is the so this is the structure trick we create here we are going to implement a private function to be used by our other functions and the function is get millionaires and what it returns it returns a vector of million and the milliner is distracted we've defined them before and here we are please note that we using a service brought to us in the in the ng

was a package it's called it's it's reads that and basically it what it does it's reads from the contract state the persistent state the state is a key value store the key has to be a string that's why we're using the current string we've defined at the beginning as you might remember and this con string is mapped to a vector of milliner's what we are doing next we're reading from this state which returns us what we called an option in rust and then we're doing a match using match it's it's a match expression very similar to switch

case from other languages and we can have two values or we have some value in this case of vector and then we will turn the vector or we have none meaning no Miller has added so far to the state so we're initializing a new empty vector and this is what we return ok we're moving to the to the second part of implementing our function that we've defined in our trade so one of the first function is add Millionaire which received two parameters of the new of the newly added millionaire and

basically what we're going to do is to read from state to append a new millionaire to the end of the vector and to write to state and this is what we see here you see the use of the private function that we've defined before they get malaria it returns a vector of the milliner then we push a new millionaire struct which is created and with these two parameters given and then we use another service the corresponding is to read state which is right state and again what right states require is a key and a value and this is the syntax

that we've defined to write into state the key has to be string the value again the newly updated vector please know that you can write into state many such pairs separated by comma but in this case we only have one ok now we're moving the the previous function has gone up now we're moving to the second function which is compute witches and it returns the address or the name of the richest millionaire what we are doing here is also again calling our price private function get mirrors to read from the state the currently the current vector of our millionaires and we

iterate upon this when defining a filter next by the net worth so hearing also we are saying again using the match expression if we have received the million we return its address only its address and but if we receive nothing meaning the list is empty we're returning an empty address to indicate there is no milliner okay so we finished our last code this is our first secret

code written in rust and now the next step will be to compile it we can run discovery compile and what will be created is a dot Watson file which will include the bytecode in webassembly ok the next step will be to deploy this contract into the Enigma network ones of you're familiar with truffle this is very we use discovery me great command to deploy our contract to enigma the the environment comes already with two files

one is an initial immigration file but the second one called to deploy contract and it comes already if you open that probably after this workshop you can find implementation of the deploy secret contract function so the function is already implemented there you can look at the implementation we're not we're not going to go over it we're going to see how we can use it in order to deploy a secret contract to the Enigma network the deploy secret contract receives a configuration object and ok and here we are seeing the use of this function in

order to deploy our million a problem contract so what what this function requires from its parameters the file name of course the finally which includes the bytecode of the compiled contract which is mineral problem dot wasum a function here we see the constructor that we've discussed again very similar to cerium once a contract is deployed to the network its constructor is being called a constructor can be called only once on the deployment again we have no arguments for our constructor that's how arguments is empty and we are defined here gas limited and gas price also

we're defining be here in any theorem account from because this is also computation test that needs to be paid for but we'll touch that in more death shortly okay after we've defined this configuration object we simply using the deploy secret contract function with this furniture okay this the code is already there and we can go and deploy run it and deploy our contract by running discovery me great okay great so now we're moving to the second part after we've deployed the

secret contract now we'll see how we can interact with it and for interacting with the sequence contract will be using enigma Jas library which is a client-side side library for interacting with secret contract it enables you to send tasks encrypt ask inputs something that perhaps wasn't mentioned too much too much but all everything all the inputs are encrypted and the outputs are encrypted so encrypting the user the task inputs submitting tasks of the Enigma network and getting that computation result or this end status

okay a partial API which will see the use of it is compute task which receives the following parameters get tasks result function and get task status will see the use of it in a second okay so as we've described already we're going to do the following add Bob's information then add Alice information meaning calling a task with the encrypted data on Bob calling a task with encrypted data on Ellis and then calling a task compute to riches and getting the result so here we

are I want to to you to look at three lines from the end enigma is an instance of the enigma JS library you have to trust me it was initialized outside of this slide and we're going to call one of its method tasks with the following parameter which are defined above we have the task function which is the ad million right which is these two parameters and we have the task argument as follows again they all be encrypted when when sent to the network I want to mention a small

thing you might see that the types are a bit different from the type that we've seen in rust it's their cool equivalent types in solidity and again you know the pendings you could see a mapping of all the rust supported types to their equivalent solidity types okay now we're doing a nave after we've ran computer's command and send the task to the network now we want to discover how what was well that whether the task was finished so we're doing the nave

polling to get the task status and once if we see it finished we can end please note if the ad Millionaire function doesn't return any output so we can finish that for this function this is practically the same code with different methods now we're doing the same ad piano with Alice okay and now we can call the computer richest again this is very similar we call the compute ask with the following parameters the tough function is different it doesn't have any aggravation arguments but this is

the same API again we are we need to poll the network to understand when has the task was computed and finished and only after the task has finished we can query the network for the result so we're going to use another API function which is get tasks result and this is an encrypted result so we need to decrypt it and now all the codes in the last slides are basically taken with

small modification from the test under the in the route path under the test directory you can add your own test for testing your secret contract in you have already in your environment a test for the millionaire problem and the code that we seen is from there with slight with small adjustments so by running discovery tests the tester that is there will run and you can see the result but basically the purpose of it is for you to use that when you are trying to test your develop contract what else we have

ready in the environment a client folder with the starting point for building a front-end the app so you can use it as well already in the in your environment okay so what we've done today we've developed a secret contract and we've interacted with one questions yes so it's a very good question this is

something that really wasn't covered on this presentation what is happening is that once the user wants to send the task to the to the Enigma Network first it queries the the network for the worker okay and after it has the worker information it do keep a key exchange in or deferment in order to both to them to have a symmetric key and this is how it's done basically yes a very good question

the state is recruited in other keys okay what is happening is that we have what we call an epoch or like we can think of it as a time interval in in the in this time interval there's a mapping if we can use this world for workers for different secret contracts meaning at the certain epoch only a certain worker can work on this secret contract so before working on this secret contract he has to have the keys for decrypting the state for this secret contract so this is a different encryption you more

questions no the output is also decrypted encrypted sorry the inputs are encrypted the outputs are encrypted and this persistent state is encrypted everything is encrypted here the only thing that this public is the code the byte code of the of the secret contract exactly because you you're the user is the one

doing the key exchange at the beginning of the task with the worker yes okay yes so yeah so something that we haven't touched upon but we have an assyrian bridge meaning that we can call etherium contracts after the execution or on of tasks on the enigma network thank you for bringing this up yes taxes

it's a great question you're raising another thing that there wasn't cover on this but what the negro network is the worker like all this works with using trusted execution environment okay so all the code and all the computation are done are done via the in clave which is an isolated environment within the workers machine so let's say you want to be a worker first of all you have to stake some value but but you have to have a trusted execution environment and and what is run there is is isolated so more good questions

ah thank you yeah so if you want to follow this workshop or to learn more you can do it afterwards unfortunately it was hard to do it during the workshop itself any more questions yeah at this phase we are compatible with the Syrian but we might

consider exactly exactly it basically and basically we're agnostic but our first release is very combined with the theorem