ZK Hack Istanbul: Deploying Zero-Knowledge Proofs with Aleo
24 Nov 2023 · 27:01
Recorded at ZK Hack Istanbul, a November 2023 hackathon for zero-knowledge proof developers, this talk focuses on deploying applications with Aleo, a blockchain built around zero-knowledge proofs and private smart contracts. It's part of Zero Knowledge's coverage of the event, held alongside partners including Polygon, O1Labs, and RISC Zero, and walks through what builders need to get a zk application live on Aleo's network.
Topics
Transcript
[Music] hey everybody uh thanks for coming I'm Craig I do developer relations here at Alo uh today we're going to be talking a little bit about the tools available uh that you can hack with this weekend a little bit about the bounties and a little bit about our Tech stack and architecture as well uh I'll go through some examples of our DSL Leo which is a pretty high level ZK circuit language um we'll deploy an app and I'll also show you our Alo JS SDK which you can ED to build full stack D apps that connect to circuits on chain I'm going to quickly run through
this presentation um we're not going to go through all of it but just going to talk a little bit about the the Tex stack here um so I think the important things uh are one I'm going to call out some of our unique infrastructure um so Alo is a layer one um so it's not a ZK evm which of course I'm sure most of you are familiar with um the main difference here is that we have we use proof of stake for consensus um and if you're interested in the consensus mechanism that's bullshark and narwhal uh it's a derivative of of that so similar to S we also have a decentralized PR Network so the way that this works
essentially is a User submitted transaction it gets sent to aover who wraps it into a zkp and then that proof is submitted to a validator for consensus um and then the most important thing which we be talking about for the most of our time today is Leo the programming language that you can use to implement your circuits and contracts um yeah so kind of like I mentioned before um you know if you're deploying a program you would submit it to a validator there's a program registry which looks something like you might have seen on ether scan right you can look up a program you can see all the code for the program and you can interact with it uh read mappings you can execute
transactions etc etc now if you're executing a transaction with a circuit uh like I said that looks like a caller makes a request to a prover node the Prov node calculates the result of that transaction and wraps it into a proof that proof as well as some metadata which might include any public state that needs to be changed will be submitted to a validator um and then that state change you can think of it uh you know kind of like your little circles from a state machine um you know is finalized in a state transition by the validator um these three things allow
you guys to easily write ZK apps using a highle language that looks like a rust or a python or you know a language that you're used to uh and ideally one of the main goals being that you don't need to get deep into the weeds of elliptic curve quotography to use the power of ZK circuits um little fun diagram about that um if you're so the way that we accomplish this is Leo compiles down into a intermediate reference presentation it looks a little bit like assembly um and then that's compiled down into actual circuits uh and I'll show a little bit more about that
later um so that's it for uh a little bit about our bounties just before we get started uh we are sponsoring a $5,000 total price pool it is a pool prize so it's split between the top five projects um each of you will earn $1,000 um we're there's a couple use cases that we're particularly interested in but I want to say that any idea that you have that's really cool uh is completely valid and and really you should work on you know whatever project is exciting to you um so some of those use cases are gaming identity and authentication zi zkl and of course voting and governance um cool so uh that's that's it with the
uh boring presentation part I want to jump into some uh some code and some examples uh and kind of show you what tools that you can use to actually work on these bounties um so first and foremost we're going to talk a little bit about Leo which is our ZK circuit language um you can check that out at github.com Alo Leo I'm going to jump into some examples and explain some of the concepts here okay so let's take a look at a couple programs here we're going to start with everyone's favorite example uh a token right if we want to implement a token in Leo what might that look like um so sorry about that so some interesting data structures that you
want to be aware of uh I guess we'll start with uh there's two main function types in Leo one is called a transition function and you can think of this as a private computation transition functions are executed by provs and they result in a proof that has to be validated there's also finalized functions which you see under here in finalize mint public the key thing to note here is that finalize are executed by the validators and it's generally used for onchain public activity finalized functions always overload a transition function so they have to have a respective transition um and the way that this you can think of
this you see this return Then syntax you can think of that as like an async 08 right first the transition has to happen and then we finalize it uh afterwards um so we you can see that we're using this mapping here mappings are essentially onchain storage you can't use a mapping in a transition function because transitions are private computations mappings can be viewed by any user on chain it's totally public uh you know if you know for example in this case if you know the address you can look up to mapping um who is familiar with utxo as opposed to the account
model okay okay so uh Alo execution model looks a lot closer to utxo than it does the ethereum account model um and the way that we handle that is with we call it the record model so every transaction has a input record and also resulted an output record um so there's not a central account per address that you know logs all the transactions and history of it um so yeah this is Leo uh you can see it's pretty high level right it looks a bit like rust or maybe JavaScript or or another language that you've used uh it has pretty modern syntax um you know we we have regular looking functions we have return statements uh we have Maps
uh as well as records and we also can write structs um so if we wanted to implement a token uh if and we wanted to do let's say the public use case we would we're not doing anything privately right so we're just offloading all of this information into the mapping we are receiving a public input and a public amount and we're doing it on in the finalized function and storing it in this public mapping that counts the amount of the token um in the private use case it's a little bit more interesting and that we notice you'll notice that we don't have a finalized function here at all and we would just mint the token into a private
record records are encrypted by the caller of the transaction by their private key and they can only be decrypted by that same key right um so if you were to Mint this token only the person who minted it would be able to see how many tokens you Minted as well as you know who owns the token so I'm I'm sure you can guess that if we wanted to implement a transfer public function it's going to look similar to The Mint except for we have to do some math to calculate balances and reduce from the sender and add to the receiver um similarly I guess this is a little
interesting right if we wanted to do a private transfer we actually have two records that are output one for the however many tokens are remaining for the owner so we take in one one input record and then we output a tuple of two token records which is that struct that we defined earlier um yeah so we do our little math we calculate the difference from the original balance and and we return it um this is super cool everybody loves tokens um the syntax is super high level um but I think this example is a little boring so let's get into something that uh is is like a little more exciting um
who is interested in governance anyone D's not super cool anymore um so let's uh let's look at how we can Implement a vote uh where all of the individuals addresses their identities are private um but we can keep the tally the yes and NOS Y and nays we can keep those totally public as well as the proposal is public as well right so private voting so we're going to Define some data structures here one is a struct uh and we can talk about I'm sure you you might be wondering what's the difference between a record and a struct records are a built-in type that's you that are used for private use cases they always
have an owner which would mean that they're encrypted by the owner's address whereas structs are just strs a way to organize your data and keep things nice and convenient and tidy so here we Define our proposal info uh we would hash our title into a field as well as the content um and then of course there's some original proposer and then in our Private Structure we map the info to this struct here and then somebody has to encrypt the proposal right um on chain we store these proposals publicly in our mapping and then we keep we have a private ticket to tally
votes we keep track of the IDS for the tickets publicly and count how many there are so that later we can determine if the vote passed or it did not uh and then also we have a mapping for agrees and disagrees so let's look at how we would actually Implement creating a new proposal right um so first we're going to create an ID by using a hash function to fit our string into a field and then we're going to create a private record for our data for the proposal and then finalize the ID by setting a mapping for the tickets with a current count of zero for that um and then if somebody wanted to
make a vote we would execute this new ticket function which takes in the ID of The Proposal as well as the voter whoever is voting address um we we create a new ticket and then we finalize the count of that ticket on chain um if you wanted to make and then once you have a ticket you can use that input record to vote right um and so if you wanted to make an agree you would execute the agree function we would increment the count of agrees and same for disagree um so this is like a super super high level um we've just implemented a very basic private proposal private voting system this
could be useful if you're working on a dow uh if you want to make bets on things um you know this use case could definitely be expanded so with with that um that's just a little overview of Leo does anybody have questions before I jump into anything else go ahead oh sure sure yeah so um we can do regular um you know regular if statements and whatnot uh we have all of that too right uh one you can do it this way you can do logical comparisons this way but also you can you can write if else just like any other language that you're used to um this is kind of an ugly example that's implementing Tic Tac
Toe uh so it's a lot of like hard-coded logic but yeah if you want to do um you know regular control statements like if and else as well as for Loops those those exist too sure sorry uh can I uh write another actually call another function within the F Clause um so as what you're what you're asking is if we were here in the program scope can we call another fun yes okay cool that's cool thank you yeah um cool so this is just a I just wanted to give you kind of an overview of the syntax so we're on the same page um and we can talk a little bit about
what tools are available so that was Leo uh that's the our DSL for writing circuits um I think one thing that's pretty cool that the team's been working on is our our web app SDK um so if you wanted to build a fullsack d app one you would have to write your contract and deploy that but you also need ways of easily interacting with your contract right um and I think the easiest way if you're hacking this weekend to get started is our who's familiar with create react app I'm sure most of you have have used something like that before right um you know kind of quickly spins you up a template to deploy and get started with an app so we have been working on our own version of this for create Alo app uh it's super easy to use and I'm going to do that with us right
now so it looks a little bit like this so you'd run this command name our project let call it ZK hack uh there's a couple different templates you can choose from vanilla JavaScript react or a nodejs one we're going to pick react today and also cool so now that we have that let's open that up in vs code sorry it's uh hard to type with one hand um cool so let's let's check out
what what did we just do uh what is what is this tool actually doing there's two main things to note here uh one is that we've created a very basic kind of Leo program structure um so we have our I mentioned the intermediate representation earlier right so if we look at this function that just adds two numbers we when we compile we also end up with an assembly like program um so we have a Leo program here and we also have a web app that interacts with this program um and so if we run mpm runev I think we need to do an
mpm install first so once this is done installing we'll be able to run a load of website that interacts with this contract um so so if you wanted you could deploy this you would write your code in here you would write your Leo program you would deploy it and then you can easily use the SDK to interact with it and build a full stock D app um and so you can learn a little bit more and look at some examples of how to use the SDK in this repo so that's github.com aloq SDK uh and so there's examples on how to create accounts how to execute programs um some architecture diagrams on that uh how to do things like
multi-threading or local program execution how to execute on the network uh some there's a ton of information about how to use the SD here highly recommend you check it out if you're looking at building a user interface for your circuits um some examples of how to transfer credits uh etc etc uh so one other thing that I if you're using Alo this weekend that's going to be super useful of course is the developer documentation uh so just want to give you a quick overview of what that looks like um there's a couple at this top bar here so this is developer. al.org um you'll probably be spending most of your time here if you're using Alo I imagine um so there's a couple important things here at the
top again all this SDK uh documentation that's in the GitHub is also here uh so you can check that out uh there's a ton of Leo dos super detailed um you can check out all the different data types that are available some information about program scope um you you were asking about different program control structures and for loops and whatnot um you can also see examples of how to do all of that um and then even more every operator that's available for example if you want to do uh random right we could we could check out how can we Generate random numbers in our uh in our Public public functions um yeah uh some more information about
transition and finalized functions mappings Etc uh definitely check out all of this on the docs uh if you're using Alo um so I guess one other thing that's important to know is how how's the protocol actually implemented right like what are the important repos that if you really wanted to De dive deep and see how things are working it's all open source and built in Rust so if you love rust you can check it out there's two main repos here one is called snark this is the node software um and you can see some information about requirements uh you can there's a script to run your own devet uh if you want to like do total local development and not interact with the network at all you can
run this script and you'll have a devnet set up in one line and you could deploy a program to your own devnet etc etc uh if you want to run approver validator client uh snark OS is is where you'll go this also a CLI for interacting and doing deployments execution Etc uh the Syntax for that looks a bit like this so we would uh Sarco as developer and is there some functionality like execute deploy uh Etc um and you can check that out I Believe by just saying starco as developer and probably
dasel right and and you can check out all the all the commands that are available there uh this is one way to interact with the network there's also a ao. tools which is an implementation essentially of this CLI but in a web uh in a web app um you can check this out uh there's lots of like useful little functions here that you might want to use um you can even deploy I believe if you go to the develop tab you can even like execute and deploy a program just here in the web app um pretty pretty exciting uh if you need you might have to get credits uh which is Alo credits are the native token of our layer one um you can reach out to me
or you can try to use the faucet which I believe has been having some issues uh so if you have a Us phone number you can go to faucet. leo.org and try this um also you can go on the Discord and there's a faucet channel in there uh and you just can type a command and there's a bot that will send you some credits to your wallet and it looks a little bit like this so you would type this command you get some credits uh you can also reach out to me in the ZK Haack Discord I will I'll be available I'll probably um they're stress testing the network right now so the faucet Pop might not be
working um but I'll happily send you some credits if you need to deploy or execute programs on chain uh cool so we have 10 more minutes I just want to make uh make sure that everybody's like on the same page I my main goal is kind of sharing you the tools that are available today giving you a brief overview of Leo uh I'll show you how to deploy a program real quick as well but just want to pull the audience see if anyone ask questions um great great question um so I don't believe that we have a like integration or unit test framework right now um I I know that there is a way in snar to write tests but this is really for the
compiler uh and we can I I can show you after I don't want to dive into it right now but come and find me and I'll show you how that's done any any other questions cool uh so cool let's deploy a program real quick uh it's super easy U so we'll start with this one uh which is the one we generated using Create Alo app um the program registry in Alo is programs are registered under their name and names must be unique uh so the first thing that I'll do here is is rename this so I'm just going to rename this everywhere it is
used and then I'm going to grab this deploy script here uh if you want to steal my uh developer wallet there's like 50 credits in there uh now is your time to screenshot um so we are this is just a deploy script and it's really just wrapping it's just a b script that wraps that uh CLI tool called snark that I mentioned earlier right and makes my life easy by having all of these these things here um if you want to see a tutorial on how to do this yourself you can go to the documentation and I think it's pretty easy to find it you look up the Alo deploy and execute demo you can see some
guides on how to use all these tools as well as a a full demo on how to do it right so this will take you from generating your wallet to getting credits to creating your first application all the way through to deploying your app down here which is what I'm about to do um so I'm going to grab this deploy script and I'm going to drop it into this other repo right here we want to go in here I'm going to create the script right um probably have to rename this folder
as well this this is fun I have one hand maybe I should like has anyone ever programmed like with a hand tied behind their back like we should uh we should try doing that as a hackathon idea like uh a bounty for whoever can write the best app over the weekend just using one hand uh cool so I just want to execute this script and I haven't tested this so it might not work uh but I'm I'm pretty sure T have used it before uh yeah so it's it's
that simple right you just use a CLI command and your app will get deployed on chain and if we wanted to I could edit the react app here and we could interact with it as well oh I see so I think the the API is down at the moment for deployment as I mentioned they're stress testing the network I'm so what I would do here is I'd spin up a devet and just deploy to that instead right um and I can deploy all my programs and test them locally with with the devet and you can find that um either by reaching out to me or if you go to snar there is a script that spins this up that our CTO Howard recently wrote I think it just spins up a bunch of uh a
bunch of t-o Windows and and puts nodes in each of them uh since you need you need at least four nodes right to have a a full network with consensus up cool uh that's that's all I had you know super unstructured just want to again like call out the different tools that are available uh if you want to see a bunch of examples of how different contracts and and different circuits are implemented I would either look at github.com aloh qworkshop or you can also go to the Leo playground which is pretty cool uh it's like a cool little web app that has a bunch of these examples implemented and you can also run them execute them Etc uh and there's some you know if you want to go back to
your algorithms class and a bubble sort uh you can do that um these examples are a little outdated I believe uh because we recently launched arrays so of course you would Implement that using using that um but yeah that's that's really all I had today um I hope you guys enjoyed it I hope you have fun with Alo and really want to call out this uh we have $5,000 prize pool this week um $1,000 for the top five prizes each uh split evenly cool uh yeah any any questions from the crowd um and I'll be available all weekend either via Discord or here in person uh to answer questions help
you guys out um if you have any struggles or any successes while using Leo and Alo and building your app I'll be here to help