ZK HACK - Aleo Workshop Part 3 - Leo circuit compiler workshop
1 Dec 2021 · 39:59
Errol Drummond of Aleo leads the third and final session of this ZK HACK workshop series, a hands-on walkthrough of the Leo circuit compiler used to write zero-knowledge circuits on Aleo. The workshop is part of a seven-week program from Zero Knowledge and ZKValidator pairing tutorials on zero-knowledge tools with a puzzle-solving competition.
Speakers
Topics
Transcript
lastly i want to do a little workshop where we're going to play around with leo and build something so we're going to do this in the playground hopefully so there's a few people from the alia team in the chat hopefully one of them can chuck the link in there this is play.leohyphenlang.org and oops this is what it should look like i'll just give people a minute to open that thank you fabiano are there any questions i should
try and answer in this brief interim nice thank you pranav we may as well get going here 65. okay so that was the link that that's been shared if you do have a second screen it'll be really really useful to use now but you can have the playground on one screen and this presentation on the other we're gonna build a game of hangman and i do assume some programming experience so if you don't have any programming
experience hopefully you can look around and understand some of what's going on um but you know this is for people who know some just a quick general philosophy of what i'm going to be trying to do a lot of the language intros i see they're very rote and straightforward you know like here's a here's a u8 and this is how you use it and here's a character and a string and this is how you use them and like if you really want to learn that's fine you can use it but it's not as fun and i think there's better ways to do this so instead i want to take a different approach which is like we have a general aim we want to build hangman and we're going to try and translate
hangman into code in more of a fun way i i've wrote written all the code but i've broken it down for you and i've inserted some questions so everything will be done for you but i'll have some small questions that i'll try and see if you can play around with an answer hopefully you all know the game of hangman from being a child pretty straightforward somebody um picks a word and the other person has to try and guess letters of that word and they have certain number of guesses and if they don't guess in time well you lose the game this little guy here who's hiding his eyes is almost being hanged and that will be game over
but you can see the whole state of the game here um you know i don't want to give away too much stuff this is one of my questions later but what data is here that represents the game most of this is just front-end stuff to make it look nice the actual game of hangman is fairly compact uh right so hangman and the chain how is our application going to use the l1 well the game is discreet it's not continuous you know this picture it's a very specific time of the game we've guessed these letters and these assets have been exposed and
this is how many lives you have left so we can put that into a state and send it to the chain in the beginning somebody will pick a word and we we're going to want them to commit to the word so they can't change it later so in our context we'll have them hash their word and send the hash to the chain and then we can update the state of the game along the way uh and you know why hangman why this game there's no reason why hangman necessarily has to happen on chain it's just more of a fun thing to build with it but poker and other things would make much more sense
but of course they're much more complicated in code and this is more limited and we can do in a short period of time okay a quick exploration of how it works this is our playground if you can see on my screen in the top left we have all the code this is the main file and this is where all the main stuff happens all the gray means it's a common and doesn't affect the code and you'll always have a main function now when you run your program it's the main function that gets run every time you run a leo program you're running the main function the main function has these two inputs in this example and the inputs are in the bottom left
this this window here contains a and b and there are inputs the main also outputs something u32 and we just initiate that in the inputs file doesn't matter what value you give it but data type is correct then in the top right we can press run and we will run the program we'll run the main function and then we'll have all this stuff happen in the bottom right so build bill is making the circuit you remember the constraints we saw earlier it's defining what all the constraints are right it's just defining the constraints then obviously look we've got the number of constraints here as well 4 328
and this is also over here so you with a previous count so that you can try and improve your program smaller is better we have set up which is adding your values to the constraints if you have x multiplied by x minus one equals zero the setup is adding a value to x thirdly we're going to prove once you've done any constraints we're going to chuck it into the proof system and create our proof then we'll just verify the proof to make sure it's valid and look it's valid seven second seven millisecond verification very very quick [Music] okay so let's move forward
one thing i want to ask you for for your own sanity is don't read what you don't need now the hangman game that we wrote for you has over 200 lines of code and if you try and read every single part you're just going to confuse yourself and waste our brain space that you don't need for example this thing here is called a circuit it's like a struct in rust it's got two things it's got something called x which is a field element you know it's not a u8 or or anything else uh not a character not a string it is a got something called y which is also a field element so it just has two things within it and then we have an implementation on it
we've got this ec in the gate where it says point negation and probably elixir occur in negation then if you ask yourself like what is the code doing here what it doesn't matter like you know you're just putting in a point and getting minus that point this detail isn't relevant right now so don't waste your brain with the space because there's going to be a lot of lines of code similarly for point addition well bc add probably elliptic curve add well what about all these lines of code it doesn't doesn't matter just this function adds points together for you you know this is this box here that's half of the code that's in this circuit and you're not gaining anything by
reading it so if your insanity try not to read too much i'm gonna ask you to go to question one now if we go to the leo playground in the top left you have examples if we go down to question one click continue uh here's the code that i just showed you in in the presentation is a point you see negate ec add and then i've got question here for you one one one i'm asking if you can instantiate a point p and then negate it and i've initiated some of the code for you and i'm going to say you can have three minutes and see if you can work out how to do this
if you have additional time you can instantiate a second point and add the two together and we're just playing around initially with some of the some of the things and like you know we we want to make a point because in the beginning of the game the person has to commit to their word when they commit they have to hash their word and what they get out is a point so we're just defining the point here okay so just another minute or two to try and finish that so
i can slowly start helping you uh also where i type the questions i try to add a bit of uh stuff that may be helpful when trying to solve it like if you want to stand here at a point you want to have this structure i mean i can just help you with this here so p is going to be a point [Music] and i can define x can be one field and y can also be one field well i don't need to type field here okay and then so i've defined the point p it contains one field and one you see i didn't in x i wrote one field but in why i just wrote one i don't have to say one
field because the compiler can work out for us that this should be a field minus b i'll get my point p e c underscore the gate there we go now if i run this it passes where it wouldn't have done before and sorry i forgot to explain this line this console log what this is doing you may have guessed it's going to be printing out something here you see we print out this the coordinates are one and this long thing and if you're surprised at the size of this this is because this is equal to minus one you remember in part two where we said that all all variables are some number between zero and p minus one well
this is p minus one which is the same as minus one [Music] okay and here's a bit more of the code so you define a point uh we can negate it by doing p dot ec negate and then if you had time for the second question you can define another point and add them together or you can add p to itself and again we're making the the point because we need to hash to a point and that's the part of the game it's doing for us uh next up we're going to have the hangman game so a circuit we introduced right it's just kind of a struct it contains a few bits of information my question to you is
what variables what things should we put in the in the hangman circuit that will represent the state of the game i've got a picture here of the game for you and we can see the entire state of the game within this photo so i'm asking you what things do you think we could add to the hangman circuit to represent the state of the game because once we have this then we can send it to chain and when things are on chain the kind of turn has happened i've given you a little bit of a start instead of the first thing which is the commitment you know we want to make sure the person is not changing their word so the commitment is a point
oh sorry and this is going to be question two in examples yes that's actually me um quick quick uh question can you actually turn off your video because i think then we get a full screen on the recording and then we'll just be able to see your screen a little bit better just your video yeah perfect thanks thank you for pointing that out uh so a couple minutes to do that again if you open example two once you start scrolling down you'll see a lot more code here we have the whole peterson hash but again i don't want you to worry about it i've defined it for you the only thing you need to know is it's it's something that does the hashing for you you know here's the structure called bison hash make a new one you can hash
stuff but primarily you want to be looking on line 72 and figuring out some of the other details and again i give you some hints in the description about what you could use you know a character an array of characters so for example we could have revealed just another minute to play around there oh okay this shouldn't be a semicolon it
should just be a comma [Music] uh okay so one way you could have filled this in this is you know there's a few creative things you can design choices you have this is what i did so we have the commitment which is the point we're going to have revealed you see in the picture we have r e t a and t and where those letters are in a word this is what i have for revealed you may have been wondering like how many how how large this array be and again this is one of the quirks of proof systems that you're going to need to need to know the size of this array 20 it can't be variable it always has to be the same
you may be thinking like if you look at this game well the length of this word is always the same which is true it doesn't change throughout the game but we're making a generic handman program and different times you play this game if this word is 10 letters another time you might want 11 or 9 and we can't have that variability in a circuit at least not directly so an easy thing we can do is we can say well if all letters oh sorry if all words we're expecting people to try and guess from are at most 20 letters we can just set this up a bound if we set the upper bound
we want to add another thing which is going to be the length of the word here this word has length 10 and so the length of the word ideally is something you're going to put in the hangman struct as well but right now we can't add it because the word length has to be a constant you know it's not mutable everything here is mutable and right now we don't have the functionality to add a constant to a circuit but we will do in a future release of leo right now we're just going to deal with that separately another piece is use guesses in the image you can see we've guessed a tv etc etc the number of guesses left
in the image you can see how many items have we added to the gallows and how close is this little guy to dying and you losing the game and then do you have victory or not but like the the victory it's not necessary if revealed is equal to the word or if revealed hashes to the point then you'll know you have victory so there's some design choices you have here uh in our game we only gonna allow lowercase english letters so we want a way to check that letters are valid and i'm asking you if you can fill in this new code question 333 and question three on the playground so you can have a little look at the
function for example question three [Music] scroll down we get to line 75 with question 333 we see we're in a function called valid car takes in a letter outputs a ball well we want it to be true if the the letter is valid i.e if it's in this array so what could we do here i'm going to give you a few minutes to see if you can fill that in and feel free to look around at some of
the other bits of code if you feel like it but try and keep in mind the general aim we have here we're trying to make hangman work and we need to check if a guess or if a letter is valid right so here's an array with all the valid letters well maybe we could look through the array and do some sort of comparison i'm kind of having you guess the syntax that uh yeah i find that it comes easier that way than than just having it be given in a bit of a boring way so we could do something like four 26 letters in the english alphabet if
c which is our letter is equal to double equal sign for the syntax valid cos square bracket i then we want to do is valid car equals true now if i click run so this this is the kind of thing you you would have want to have written this for loop with an if inside it and we set his valid car to true if uh the guest letter or the letter is inside the array so now when i clicked run i had this come up and we see here that the results are true and false so if we scroll down to the main file
we're checking if c is a valid letter and also if underscore is a valid letter well c is valid so we get true that's good but underscore is not valid so we get false which is exactly what we wanted now if i silence this code like we had in the beginning in click run we get false and false but this letter c is valid so you know it doesn't work and you manage to make it work okay so we managed to decide if a letter is valid or not the next part would be uh to check
if the the word is valid you know if someone's trying to do the word that has a hyphen or a star in it then we don't want that to be a possibility we're also not allowing capital letters just for the sake of making it simple and how do you check that the word is correct you just made a function that checks if a letter is correct so how do you now check for a word and again i give you some of some hints in the description to help you right so if if the word is invalid what we do to make that clear is we change the commitment to the point zero zero if you ever see the state of a hangman
game where the commitment is zero zero then you'll know that this is you know invalid the person has done something invalid and we're not going to allow this game to continue or all that move to be valid just a couple minutes here again this is gonna be question four scroll down
working around here well what can we do for i and zero to word length but i'm going to want to check if it's ever not right so you can look at the inputs to new game here you've got something called word well that's the word that we're guessing uh you've got the word length and we're going to output the new uh the new game which is going to be a hangman struct so if word position i doesn't equal uh what are we doing here no if we defined
is valid car so we've got hangman is valid of word position i not equal to true is equal to false then we're going to set commitment equal to this point here so this should run maybe i made a mistake somewhere is that a car what are we doing oh it's just a valid car not his
valid car and we get it to run firstly i just want to point out here in this for loop which i wrote for you we do 4 i and 0 dot dot word length now i mentioned earlier that word length was a constant you can only do a for loop up to a set size this is the reason why word length has to be constant if it was a variable then this for loop is the size of it's going to depend on the input to the circuit and this is a problem for proof systems it's something that's not allowed so you can only make a for loop with a known
length which means it has to be a constant now i filled in this code for you and i clicked run and we see that we get this commitment here and this is because we're setting up a game with a valid word the word is aliens and i filled the rest with underscores because they're not needed and i set the word length to six but if i set the word length to seven then the word technically is aelion's underscore and aliens underscore shouldn't be a valid word which means we should see a change in what's printed here because this commitment is now normal but we said the indicator of an invalid word would be that the commitment should be point zero zero so now if i run it again let's see
okay look we get our zero zero which is good our code works okay well then for giving that a go question five second last question so we're a fair way into uh the the code now we've made a point because we're going to hash to a point and the hash ensures that the person is using the right word we've got a valid character function to make sure something's valid we've got a function new game to set up a game you know we just take in the word and check it's valid and initiate it and then the last function we're going
to need is a guest letter function my only question to you here is what should the input to this function be and what should also be the output you know if this is the state of the game in this image here what things do you have to take in in order to update the state of the game and what will the output be so guess letter okay well you know we made the hangman
circuit so maybe we'll be taking in a reference to the circuit of hangman that we made probably going to need the word otherwise we can't do a comparison gonna want the word length and well this is the guest letter function so probably we're going to want the the letter being guest letter what do we output well the word's not going to change the word length is not going to change and the letter doesn't really change it doesn't really mean anything so
all we need to output is again another self and this is what i was hoping you to to fill in in terms of uh what what we could do but again there's some creativity you could do here you could design the game slightly differently to take in different parts of it and move aspects around but once we fill out this function we'll have a way to update the state of the game now my last question to you is if we're in the the guest letter function now and let's assume somebody has guessed the letter you know the letter e it's a valid letter can i pass you um just in the uh this
question came up so why do we set the point to zero zero in um you know after checking that the word length is less than ten twenty so yeah uh so the reason here why would be setting the point uh the hash to oh did i write hash this should be commitment uh the reason why we set this to zero zero is we're checking if the word is valid let's start the context this is the new game function we're setting up a word a game of hangman which means that the person has to have a valid word you know they can't do star star underscore hyphen that's not a word we don't want to allow anything like that specifically
we're only allowing lowercase letters now in this for loop we go through uh the length of the word so probably we're looking at each letter and when we look at each letter we check if it's a valid character if there's a an invalid character if this evaluates to false you know they used an underscore or they used a hash sorry a star or anything else we want to know and a very simple way to do this it's just like have an indicator and the indicator is if the hash is zero zero something invalid happened yeah so
if the word is invalid the hash is set to zero similarly if the word length is more than twenty well we set the maximum of twenty so this shouldn't be allowed this will also make uh zero zero hash so this is just a way for us to prevent our dishonest players from acting dishonestly moving forward once we set our game up and we have the guest letter function if a guess is valid you know they're guessing the letter e what do you have to update in the state of the game i want to see if you can fill in let's last about question six and give you because five minutes it's a bit more involved [Music] all right scroll down if you go down to
line is just the right example uh yeah line 161 you'll see question six so the player's chosen word uh sorry a letter and what variable should we update above you can just see we have the revealed letters here just called reveal we have the use guesses and we have the number of guesses left what kind of things can we update in this if condition so again i'm slightly throwing you in the deep end here throwing all these these functions at you but i just want you to remember what we're trying to
achieve we've got a game of hangman and you know we can represent the state of the game in hangman in a in quite a limited way and there's only two moves really in the game the first move is initiate the game choose a word and commit to it and the only other move is guess a letter that's all we're trying to achieve and if you do get a lowercase english alphabet letter what do we have to update in the state of the game well
maybe you will want to know where this letter is in the word so do this i and 0 word length well if what are we calling here letter is equal to word position but maybe we want to add this to the revealed this thing here position i should equal letter i'm probably going to want to add this to the used guesses as well [Music] position 10 minus
and it's the right position in this array [Music] and if you're gonna do guesses left minus equals one reduce the count okay so i have tried to fill in this code for you the way i've put it in here again there's some creativity you could do here i do a for loop with an if condition to see if the guess letter is in the word you're trying to guess if it ever is in the word we reveal that thing that letter in that position of
the revealed struct similarly we need to update the use guesses array and we need to take one life away you could for example also update the the victory ball um i just did this below separately for no particular reason um but you can ideally you want to do this in as few constraints as is possible okay and you know basically now we have all all the functionality of the game i think it will make sense here we i can show you that this this works now if we get examples hangman full okay so if i scroll down uh before we go
to examples um maybe a qualification question for um just understanding how we're encoding the game into this uh this program from david um so why are we passing in the word as an argument to guess letter presuming that we actually don't know the word itself right a good question so we have to think about who's going to be running this code if you want to reveal where that letter is in the word the only person who can do this is the person who knows the word you know when you play hangman if you and i play together and i've chosen the word when you make your guess you can't reveal it
in the word you have to communicate it to me and then i have to update it so the context here is that you'll send your guest letter to me i will make a proof that i updated the game correctly that if you sent the letter e that i've actually revealed where e is in the word that i committed to so the the person running these functions has to be the person who knows the word the role of the other player is just to communicate their guess the role of the proof is to ensure that the players act honestly okay so we are currently in the hangman example in the playground i scroll down
to the main function i want to point out this thing here called create game and this is a ball so if create game is true then we're we're setting up the game we're running at the new function and if create game is false then we're guessing a letter and in the in the main sorry in the inputs file here you'll see that it's false so this means we're guessing a letter what's the word well it's the word here alions got these commitments actually this these commitments aren't quite right i'm going to update this so i'm going from false to true so we set up the game i'm now going to run the code and
currently we don't have uh it's not we've not finished the the thing that's going to make and use records for you easily so right now i'm just printing everything out here so now i've set up the game and we have these commitment points uh you know the reveal thing is empty and so as use guesses we have 10 guesses left i can now copy this across ctrl c replace the things here i'm going to set this true to false so that we'll be guessing a letter and i'm going to guess the letter e now if i run this let's say we get out this stuff here aliens and we'll see like the revealed
struct here we now have the letter e revealed in this position so we've updated the game we've got the used guesses we've added the letter e to it and the number of guesses left has gone down by one to nine and we don't have uh victory yet i can copy this across again copy across now if i don't change my current guess this e what happens when we run the code will it update you'll see that we haven't added the things that use guesses and we haven't lost a life and this is because in the code we're checking that the guest letter hasn't already been used so if i
change this e to an a hopefully this will work i press run and this is a valid guess so we update the state of the game we now have two things in the use guesses and we have these two revealed things and this program was made of proof of the new state of the game and this would be the thing i would send to the chain and then you as the person trying to guess the word you're going to see the stage of the game and errol yeah sorry about that i think the mic is coming in a little scratchy okay this has happened a few times i'm like no not available
interesting uh i wonder if i can fix this it just started so it wasn't it wasn't the whole time okay is that better now yep okay cool i think so yeah uh thank you uh i mean we're almost done so i was just showing you that this this does indeed work and then trying to go over what we were trying to achieve again i set up the state of the game and we can package this and send it off to the chain now if you're guessing letters you'll see it on chain you know i'm going to give you a possibility of viewing it and you'll see the new updated state and that also i acted in good faith and we can continue
playing yeah so again like there's no real reason why you have to do hangman on chain it's just a fun thing to build and helps demonstrate some of the ways that we interact with the chain and it's easier to generalize from there if you're interested in building yourself we have a big grants program you can try to make another game somebody's trying to build werewolf and someone's thinking about building battleships but you could make poker you can make any other game that you think could be fun or relevant then there's also any of the ecosystem ideas the the private nft platform anything down related verifiable credentials adding to that that kind of stuff but you're just free to build anything you want we're really interested in
hearing your ideas and working together with you other ways to get involved uh we launched incentivized test net i believe today so if you want to mine and and help please join in there we host a weekly zk salon where we spend an hour discussing zk related things whether it's cryptography or something else happening in the space or just uh you know anything else and we try to make these interactive and questions from the audience come along to those we're also planning some hackathons and lastly we're trying to make some zika explainer videos i'm trying to make a video to visually represent what's going on in a rank one constraint system there's some amazing
material out there already but nothing yet in video format that we're aware of and this is something we're working on thank you very much for coming i hope you learned a few things and i hope you have more questions to share with us and we look forward to building with you