ZK Hack Istanbul: Exploring RISC Zero: A hands-on Workshop
24 Nov 2023 · 25:29
This hands-on workshop from ZK Hack Istanbul, a November 2023 zero-knowledge hackathon, walks participants through RISC Zero's zkVM tooling for building and proving general-purpose programs. It ran alongside partner and sponsor teams including Polygon, Aleo, O(1) Labs, and Scroll as part of a weekend of zero-knowledge talks, workshops, and hacking sessions for builders new to the ecosystem.
Topics
Transcript
[Music] my name is Daisy Adams I'm a Devo engineer at risk zero and today I'm just going to be walking through a very simple we call it our Hello World tutorial on how to get started um writing in the zkm so just a quick introduction to risk zero um our main product we are a general purpose of zkm um it's rust first and it's open source um we're not tied to any blockchains we're not building any l2s it's just a very general purpose zkm and our second product is bonsai a distributed proving service that runs
the zkm remotely so if you don't want to run the zkm on your computer and you don't want to use you know your own infrastructure then we can provide that for you and run the proofs and generate them um for you so throughout the workshop I'm probably going to mention some terminology that you may not be familiar with because it's just words that we use to describe some of our um pieces of the tech but um don't worry about that I'll explain it as we go through so first things first um I hope you guys have uh rust installed um I'm just going to navigate over to the cargo risk zero um Library and get that installed so if you guys
don't have that go ahead and walk um through that with me so here you'll find um three main commands that you need to run you need to install the bin install let's go ahead andall I should already have these but I'm just going to walk through it step way we're all doing it together and then we're actually going to install uh cargo risk zero and yeah that was pretty fast for me um since I already have it let's go ahead and check the version so if you just recently installed it you should be on version um 9.1 which was actually released like
yesterday and then lastly let's actually install the cargo risk zero tool chain and this part might take a little bit of time um but hopefully not too long okay yeah then I'll probably just continue on with the rest of the talk um I'm just going to make a new folder for my new project I'll call this factors um because today we're going to be proving that we know the factors of a certain number um let's go ahead and CD into that um okay that's still installing so the command to uh install or to make a
new project is Cargo risk zero new and then you pass in the project name so for my project I'm just going to call it factors um and then it's going to prompt you for a guest name so this is important because this is what you're going to call your image IDs and your Elf binaries um again just for this project I'm going to call it factors and now it should be generating a whole template with your guest code your host code and everything that you need to run the zkm and this part might take a while as well because it's generating all of that template
code so maybe while it's still populating all of the template code are there any questions so far about risk zero about the zvm or bonsai yes yeah just to get your your question on the video it's always better thanks uh so what constraints are there when writing the verifiable r code or what can we do what can we not do and use yeah so with our G uh zkm it's very
general purpose and we we can prove basically any arbitary risk code right now that's what we're supporting we want to expand to other programming languages um any program language that compiles down to risk 5 binary code um we should be able to you know support that um but right now we're just very focused on Rust crates um making sure that we're as interoperable with rust um and our zkm so there aren't any like limitations that I can think of right now um but you may face maybe at some point if you import a rust crate and that's not compatible yet that might be an issue but it's very unlikely because we are
compatible with I believe up to like 77% of the top 1,000 rust crates I believe we keep track of how many rust crates how many rust libraries were you know able to support and stuff and it's it's quite a lot and we test for that every night as well gosh I was not expecting this to take this long yes so sorry can you wait for the thanks what AR uh arworks what's sir uh arworks uh the library artworks just BAS is the crypto Library
uh very powerful one with a uh harsh commitment and all this stuff yes I believe we are compatible with that library and I believe we do have some templates um that do utilize that artworks as well yeah so whenever you start off with your new template you're going to be prompted to write in your inputs um and for this case since we're multiplying two numbers my two inputs Were A and B and attach to them I attached two um functions the read function and that's just telling the host to read this input as soon as it gets passed um as soon as the Valor gets passed into the host and then the computation that's actually going to be
proven is the multiplication of A and B and the product gets committed to the journal the journal being a public output in order for you to identify that this was the code that you ran um so some terminology here that I mentioned our guest and host um what are they what's the difference for that I prepared a very short slide the guest is the guest code that's running inside of the zkm so all of that computation the product of A and B that is the only part that will actually get proven the host is the system that the zkm runs on so for example my zkm is running on my Mac so my Mac is the host um the way that I
like to think about this interaction or relationship guest host is whenever you launch a VMware instance or a Docker container the docker is the guest um the guest instance on your host machine and your host you're passing in parameters to Docker so you're telling Docker I want this number of CPUs I want this amount of memory those are parameters that I'm passing to my guest and that's exactly what we're doing here in our host code so in the host I'm passing my actual values and these are private um these are not actually being committed to the zvm the only thing that the zvm will actually reveal is the product like we mentioned in the guest
code so here I just separated out my code between the prover and the verifier theover is um of course proving that or it's running the computation 15 time 17 um and to make things a little bit more um organized I separated out the receipt um from theover because ideally theover is not doing the verifying um so here I stored my receipt um in uh or is it in a binary file um and I should have it somewhere here um and uh yeah so yeah that's another terminology that I mentioned receipt um
and journal so the journal is the part that is public that would be the 2 55 or the actual outputs of the product and the receipt is actually the Stark um you know what can you say uh you know the the stamp that says this is the execution that was ran in the guest program and here is the proof um and we just you know we use this terminology to make it easier to understand Stark proofs and you know all this cryptography knowledge so yeah so I walked through the prover um here again I am taking out the output from the journal and I'm
revealing it in the print line the verifier is just taking the binary uh deserializing it and then it's checking if it actually matches with the image ID and if it does the proof should be valid if not it's not valid yeah and I guess I can uh just run this code since it's already here first I need to save the file go ahead
and run oh my gosh oh strange okay so I'm running into some weird issue cuz like I'm like installing it again and then like I'm running this and I'm suspecting okay it's actually done now I think I'm just going to exit out of this I don't know if you guys would want me to walk over this again um but we would do it live together if if that's if you guys are
interested in that or okay okay awesome okay so now we have the factors template let's go ahead and open that up so this is what you should have in your entire template you have the host code you have your methods which has the guest code it should be inside of guest source and Main this is exactly what you're provided with um again it's just a template so let's actually write in our um inputs that we want to prove so again I'm taking two numbers and they both they'll both be like unsigned um
variables and then we'll read them and we'll copy that two and I don't need this line anymore and let's go ahead and take the product of both of these multiple B and let's expect an integer overall okay oh and then I need to actually commit the product to the journal and again this is public outputs this is the only thing that will be revealed which is the product of A and B
But A and B will not be revealed okay so now I have my guest code this is the only thing that will get proven let's go over to the um host code yes well in this case the only thing that ZK can prove is computation so A and B are not computation and a and b are as you'll see in the get in the host code um those two will get passed but I'm not committing the actual value A and B I'm committing the product of A and B so those A and B do remain private except the product um so here anything that you
commit to the journal will be public and any that's not as private okay so let's go ahead and go to the host code and now it's the host kind of like what I was saying from the beginning the host is passing in parameters to the guest so I'm going to pass in the value um a uh I think it was like 17 or something 17 15 I'm just going to comment this line out because we don't need that and here this command is actually
setting up your environment so if we read here the comments the executor environment describes the configurations for the zkm including the program inputs um so here they give us a little like a beginner um line we're not going to write input we're going to write a and then we're going to unwrap that and then we're going to write B because that's the second value and then I'm missing one okay I don't need this one build and then we obtain the approver we have the receipt that
already includes the elf binary that my template set up and then the output is being retrieved from the journal which we committed earlier and let's write a line here saying I know the factors of A and B and I can prove it and then we're going to actually pass in the output and down here you'll see that it actually verifies already but in the final example I took apart the verifier and the proof because ideally the the approver is not the one doing the the verification as
well let's go ahead and run this meths huh this is
weird what's that yes H it's saying it failed to build the custom command and I'm guessing it's cuz I was installing the tool chain but I
never let it install properly so now it's not finding it yes live coding right yes you go back to the code and I came in five minutes late so apologies if I missed it where you specify the factors elf like where
okay is that that's the program you're proving right like the binary yes so that's the binary I'm giving it a name so that my program can identify it yeah so yeah that's that's the method but and that was that was the guest right the thing that was so how how how does this find out about that like are you passing the path to it like what how does this host find the guest program to prove oh so the guest is already here um it just works in sync so it it it has a very comp the zkm has a very complicated build process um so it looks for these like key words to find the guest program
um I guess the question is it is it loading at all like compiling everything into the same like process and then just proving some code or is it like the guest is compiled the host is compiled and then the host loads the guest it's the first one yeah so it's all together so if you were to compile the guest it would give you some sort of an error because the host it's expecting some host parameters you can't just have a guest I mean I yeah yeah so the output is like yes the the zkv M post and the guest program that it's going to prove that's that's the like output of compiling it yeah yeah so the output would be compiling guest and host together got it yeah thanks for
clarifying uh seeing this live session uh it's really curious to see the jobs running after you you compiled the template can you maybe please type jobs uh I'm interested to see that [Music] the what do you mean the like the these no in the terminal after the build was failed oh jobs just jobs you you mean you want to see the error or no I want to see like the jobs running in the background after you compiled the template oh um not sure like you want to see like my processes on my computer or no I like in
my in my mind I just wanted to see why the build fail because other jobs didn't finish and that's why oh I see I see um yeah yeah anyway uh one question about calization like does it support any kind of calization like is it only surday can you use borch or any other serialization as far as I know it's only surday um it's actually already embedded when in your execution environment so here uh no so here when I commit Like A and B I don't need to like add any you know translations to that because that's already embedded into the right function okay no that was actually a new
addition that we added recently also am or the circuit inputs uh like saying but like can you specify them to be private or public because you need some public inputs to verify that on chain and I think that's very very important for adap so you're asking like can I make some inputs private other public um yes in in a way that you would only expose the public ones um Again by committing to the journal so if you don't expose that information to the journal then it's just not going to be
public so that that's that's how you can you know keep some private and others public by using that commit function so you can uh commit the environment also not only the product which is the output but also the inputs because some circus don't use outputs just have some inputs and you check like Integrity of something after calculation yeah I mean if you want to commit A and B you can do that as well like there's yeah okay I see thank you mhm so this is all on your machine that
maybe it's not part of the demo but I was curious what it looks like in the cloud version like do you sort of upload this compiled thing or do you just upload the guest or like how do you how do you do that yeah so we do have the Bonsai proving service um and that is you can think of it as like a cloud or it's a remote proving essentially um and there's various methods that you can invoke Bonsai so you can use um the template or you can there's this new way where you can actually just pass in Bonsai environment variables and the cargo risk zero tool will actually pick up that you're using Bonsai um so you don't necessarily need to be onchain in
order to invoke Bonsai you can just use it for any large computation that you want um so let me see if I yeah this is the quick start guide so it walks you through the entire zkv process which is what I was planning to do and then at the end um you can then choose to do remote proving instead of actually running the KVM on your machine mhm um and let's see yeah I can see that let you put your API and the URLs there in the remote proving part yeah so here yeah so here are the four methods so you can use the cargo risk zal tool which right now is uh failing for me um and
then there's the templates or you can call the SDK or you can even use rrest API to call Bonsai so there's multiple ways um but you do need to have a bonsai API key in order to use it and that um you would just sign up with a very simple form and then we would provide you with the API key yeah super helpful thank you y yeah so I have about four minutes left and I just want to apologize for this thing not working um so I'll just go over quickly um o over what bounties we're offering um we have three different categories our first one is just build a general app it doesn't matter what it is um you can either use this zbm or Bonsai and we'll consider up
to five projects um we're giving away 7,000 um so if five top projects submit then we'll probably split the 7,000 for the five projects and if only a couple of them submit then we'll split those couple within the 7,000 and then the second category is the best ZK co-processor idea using The Foundry template um and that is on on the website um it's a very simple template of course you initiate it and then you build um for this you would have to build something on chain or something that connects to the ethereum blockchain um and for that we're offering $2,000 and then the last category is $11,000 for the best zkl project um and we do have a zkl template that you can utilize
for it um but that's that's all I had thank you