How to do something to some state in some contract

ETHCC 7 · 11 Jul 2024 · 36:56

zpedro, an engineer at Aztec Labs, argues that a blockchain without privacy risks becoming an Orwellian ledger, and shows how to write a private smart contract on Aztec. He runs the Aztec sandbox and writes a Noir contract for a simple vote where the tally is public but each vote is private, with a nullifier so nobody votes twice. The live demo hit an error at the deploy step, and he says the full tutorial is going into the docs. Q&A covers the context object, note encryption, quantum resistance, block explorers and Noir verifiers on the EVM.

Speakers

Topics

From ETHCC 7, hosted by Ethereum France.

Transcript

Hello. I am Ze Pedro. I'm DevRail engineer from ASTEC Labs. And I'm I'm kind of a meme guy, so yeah, expect some memes here. Uh so I'm gonna be teaching you how to do something to some state in some contract. That's basically the definition of Aztec, just someone doing some things to some contracts. Um yeah, I there's I'm I'm from some Aztec labs as well. Anyway, I know the secret for like your success and happiness.

Um like I am gonna give it for you for free because I like you, okay? And so this is gonna be very very uh important for everyone here, okay. So this clicker. Yeah. So how to be successful. There's just two steps actually. The first one is you trojan yourself, okay.

So you track every interaction between citizens by bringing all the real oral world data on chain to an easy this immutable undeniable ledger. I don't know if this is recognizable, but yeah, uh anyway. It's just a coincidence, okay? No one is trying to do this, right? Then the second step is you become a dictator, right? Like you can be happy, you can be successful, you can rule as you wish, you can like yeah, you just can make all your opponents just randomly have this urge to throw themselves out of windows and everything. So like this it's crazy, like uh yeah, just okay, yeah, but so okay, uh this is the problem, right?

You can actually not do whatever we are doing here, because this is the way to this kind of warwillian nightmare that apparently we're trying to escape. So we need something else, okay. So, how can we avoid this nightmare? First, we need to ensure this kind of basic human right, okay? Like actually so basic that there's a little like kind of I mean a small document that no one cares about called uh the Universal Declaration for Human Rights that says that you cannot be subjected to arbitrary interference with your privacy, family, home, and correspondence. Okay, so that kind of doesn't compute with putting everything on chain, right?

Okay. So why if why I mean I am even here, right? I mean, if I'm saying that you can do this, like why why why I'm in this in this conference, right? Isn't it the wall point to be private? Like, and if you told all the crypto bros and you tell like blockchain without privacy is actually a night an Orwellian might nightmare. They were like they were they are like, okay, yeah, some die, someday some guys will solve this. Yeah, that's true.

Obviously, I mean, I'm not saying that you shouldn't worry about other things, you should, but it also, I mean, this is something that we should collectively care about. There's scaling, there's there's a bunch of other things that other people are worried about. We at ASTEC Labs, we are worried about the privacy. So you cannot forget the privacy. That's something that you just cannot say, yeah, uh, this is secondary, it's not. Okay.

You can also offload privacy. You can just go and say, Yeah, I'm just gonna give my data to someone else, and someone will take care of my privacy. That's just not how it works. Like you actually need to do it yourself. So you can't just like you can't hire someone to slip it or like ship shit post for you. You you the only solution is if you do it yourself, right? Sorry, and yeah, this is a meme, right? Again, another one. If you hand it to someone else, even if you trust that person or institution as much as possible, they are not private anymore. Okay, so you have to own your data, and this is very hard to do, and we are trying to, and I'll show you how to do that.

Actually, I kind of fail to understand why can't we even onboard the next million by making everyone we do public, right? And so, like, just doesn't have doesn't work like that, you know. So, how can you keep your information private? So, I'm gonna make this agenda, try to make this like a like a respectable workshop and everything. So we were gonna set up a development environment, we're gonna write programmable privacy, and then we're gonna run it. Okay.

And so maybe this could look difficult. It's not actually, it's just like you run a bunch of commands like this, and that that's it. Um yeah, but yeah, in enough memes. Um let's just hack something. Just remember, like if you have any any problem, just keep trying. Okay, don't read documentation because I mean you're gonna you're gonna save four hours, you know, just like yeah, five hours debugging is always save you five minutes of reading documentation. So uh don't remember, don't forget that. You can scan this if you are interested in the documentation.

Um yeah. I'll just stop when people stop having phones in the air. Um so actual the actual agenda now. Um we're gonna set up a development environment. We are gonna we're generating some useful TypeScript bindings, so you can leave now if you I mean write TypeScript. Um so we're gonna write our deployer, we're gonna write a smart contract, and we're gonna some do some testing because it's just like the way should things things should work, right? So we're gonna run the sandbox, that's the first step.

That's uh that's a kind of a development environment. Um we call it the sandbox. I like it, right? Um, which is just you can just run this and be done with it. I'm gonna show you. And then we're gonna compile and generate this TypeScript binding so you can actually interact with the blockchain and run command deploy, etc., which is also just this. I'll show you in a bit. And then you're gonna have the good stuff that you're probably gonna like, which is native private account of structure. Like, wow, woo!

This is really cool. Like, can you imagine? Account abstraction is already amazing, but if you can keep it private, can you imagine having like a like an account that's owned by multiple people, and you can see which are those those people that are amazing for DAOs and everything, right? Anyway, we're gonna write a contract with this. Um the contract is gonna be like this. So we're gonna have a private execution environment, which is like you can you can think of like a wallet, but really really cool. It has a prover inside.

We're not gonna be using the prover, I'm just putting it here so you know it exists. And so it comes with a wallet, actual black keys and everything. Um we're gonna deploy an account, right? Because it's uh this account abstraction, uh, which is also the admin, just for I mean, to to make it simple, not gonna make have time to make a very difficult contract. Uh, then our voting contract. And then yeah, this is basically how it's gonna have just uh simple storage with a tally, which is like the count of all the votes, which is gonna be public, right? So let's make it simple.

Obviously, you can make this private as well, and we have examples for that, but this is just a simple, simple, very simple smart contract. So all the tile is gonna be public, and admin is also gonna be public, it's gonna be you. Uh so no problem. Um yeah, but the fun, but uh actually, your vote is gonna be private so that's already something right not bad like uh no one will know that you cast your vote and no one will know who you cast your vote to but obviously they could just listen to the tally and just see it rising and kind of make some uh yeah compute something with it but anyway um so the tali is gonna be public and the function will also be public and I'm doing is so you can also see how to basically call public functions from private functions which is a really cool process the other way around is not not that simple anyway so yeah it's kind of worthless right it's like as as as a product itself but that's why I'm not presenting a pro a product I'm just showing you some simple example and then yeah just like test and see it actually not dark magic it actually works even though there's no prover but yeah I mean the prover is there it's just uh it would be too slow so um uh just not gonna have that and here's some links before we start hacking this is the Aztec Discord the Aztec documentation I think I put this one already this is my Twitter X and my telegram if you wanna keep in touch with me um yeah you can like zoom in to scan any QR code if you want to or just find me at uh something like uh z Pedro doth or I think this is something this is my telegram but my ex is uh z Pedro not gonna make it because I'm not okay so cool um onwards with the actual coding thank you so I don't know can okay yeah thank you it's just so I can use both hands for coding.

Yeah so as I said you can I'm I don't think there's time to actually go and like have wait for you to follow along so anyway this is a tutorial so it's gonna be fine. But what you can do right now is just run this bash command. So actually I'm gonna I like to leave dangerously I'm just so I'm gonna go like export version master so I can use the master and it's gonna help me with some other things. So yeah this is the command to install the sandbox so you run this and say yes you just accept whatever you you have. It will like pull this version master I hope it's not actually pulling because yeah I was oh okay yeah someone pushed uh something anyway I'm just gonna cancel this so I should have all the images I want um and now I'm ready to actually so I have here some notes so I don't forget um I can run my sandbox so I gonna I can go and run Aztec sandbox and it will run the sandbox um so basically, if you read these logs, you can see more or less what's kinda what's happening.

It's running an NVIL node because it because Aztec is an L2, so it's running an envelop tool, an enable node, and then it's running deploying some contracts, some testing contracts that are uh like account contracts, uh Schnard account contracts, you see, added contract uh Schnarr account. Um so we can use them um immediately, we don't have to deploy these contracts. Um yeah, now it says like it's listening to port 8080, so we were good to go. And then we have this cool npx script.

Um npx Aztec app in it. Again, I like to leave dangerous dangerously, so I can just pass just version master. Um I actually I don't think it will even work if you go without the master because um the code it's cloning is actually not uh in any release. I merged it yesterday. So just wait a bit, it's cloning and probably the internet's kind of slow. Um let me just see.

Okay, here is it. You can also like with this NPX Aztec app, you can also like clone actual um actu what what we call boxes, which are actual boilerplates with front-end code and stuff that can run on the browser and everything. So it's it's really cool tool. Um and yes, like let's just see what's in the what's in here. Like we have a package.json. Um just to be easier, it comes already with the three of the most used um dependencies you can you you will need. And there's also nargo.suml that also comes with the um Aztec package.

So Aztec NR. So we are gonna write noir, but we're gonna use this Aztec dependency, which is a library. Um so it's you can think of kind of a framework on top of NAR. And then we have our actual contract main.more. Okay, so it doesn't have anything, right? Um but we can actually go and try to compile it. So if you install the sandbox with that command like the bash whatever, this one, uh you can actually um you already have like Aztec Nargo, so you can go Aztec Nargo compile and it will basically like happily compile this into your uh Aztec contract. What we want now is to have some really cool TypeScript interface.

Basically we want a TypeScript. I don't know if you're comfortable or uh know this type chain. It's kind of that thing, so it will generate some nice methods you can call and they will call the contract. So uh you can do that with Aztec builder, go gen, and then the output and like well the input is in target. So this will generate that nice artifact. You can see it has the deploy method, and also all the methods will be here, but right now there's just a constructor, so there's not much. Anyway, let's start with um with a new file.

Uh just gonna be like index.s and we're gonna import the pxc client. So you can go create PXE client from uh Aztec. Aztec.js. I know why it's not auto-completing because I didn't run yarn, and that's why. Uh in any case, let's keep going. I'm gonna import the create account. Again, this is a testing. Oh, cool.

Uh thanks, GitHub cobiled. Um yeah, just this is again one of these testing contracts that it deployed in the beginning. That's much easier. We can just use it right now for this simple example. And then oh cool. Yeah, smart, smart, smart AI. Um so yeah, it already knows like I want to deploy this uh main contract. So yeah, you're like uh how can like can you can you please deploy? Yeah, I will just uh just say let's just like create this PXE. So um this uh PXE is kind of a wallet, but right now it's running locally.

It would be really cool if someone wanted it, wanted it so bad on a on a wallet that they actually put it in a wallet, and it's eventually gonna happen. So I'm just gonna create the PXC client and I'm gonna pass um my my node. Um just saying, yeah, please wallet uh connect with this node, so that's not different from oh the that's not different from any other wallet. And I'm gonna get also uh wallet, like an account, wallet, whatever, and just gonna create this account and pass the PXE. I think I need to await this.

So this wallet is now connecting with my own server that serves my own keys and nothing here leaves your machine. Which is pretty amazing. Like you can actually deploy a contract and like on a plane without internet connection and make transactions, mean tokens. Uh it's really really powerful. Anyway, I should also have an address, even like talking with the network, I can know what my address is. This is not difficult. It's not different from how it works on Ethereum. Uh I just go main contract, I can deploy this.

I need to pass the wallet I'm using to deploy, which is gonna be that one. And just send and deploy it, and that's it. Like I can actually console log this and say address. Okay, so as for the contract, I can go here and say this is my constructor. So my tally, I have my admin, which is an Aztec address. Uh and I'm gonna uh store this. So I'm gonna storage admin write uh my admin. So you're gonna ask like what's this storage?

No problem, gonna write it. Aztec storage, and it's a struct. So struct storage. It has the admin, which is gonna be a public mutable. I'm gonna explain what this is. So yeah, it's a it's a public value that can change, right? It's the name says it. And then the tally, which is gonna be a map. So a map from a field. A field is kind of can think of it as a kind of a big integer, right? Savio, please don't kill me. And and then you have uh it's also gonna be uh as I said, it's gonna be public, like the task is gonna be public, so it's also like a count of um.

Yeah, that's cool. Like, yeah, and you're like, okay, yeah, now I wanna I wanna cast votes, no problem. I'm gonna do that. I think I s I think the constructor actually needs to be public here. Um yeah, anyway. Let's just make this Aztec private. So it's not like it's not difficult. You just go cast it, it's just a just a contract, it's just a function, it just runs on your on your machine. So it's not even difficult. You go like a candidate, you just choose the candidate, which is gonna be a number because it's just it's a simple example. Um and you say, sorry, I'm passing things so I have the time.

Um I'm just gonna like get the header. Um get header, and I'm also gonna get the message sender. This is um npk hash, let's call it header, wait, header get npk hash. So this is the um the nullifier, basically the the yeah, nullifier private key. And then there I need a secret because everything is encrypted to me.

So I go context request. This is uh a nullifier for this specific app, which I'm gonna pass the npk up. The npk hash, so it's like a global hash. And and then finally a nullifier, which is basically I'm just gonna use Aztec, uh like noir nullifier, no problem. Just hash Pedersen hash, and then I'm gonna pass the message sender, because like if I vote once, like I can vote twice, then that means I I I have voted and no one knows and and push the nullifier so I go context and push nullifier and yeah at this point we are done with voting actually now we want to basically make this public call we're gonna say I'm gonna add this to it to the tally so you call the same contract and you say add context this is this address add totally this is gonna be a function that we're gonna make add totally public add to tally candidate and just in queue this because this will run after so if you read the docs you will understand why all the public functions run after the private functions so now we need this one quick and easy this should be quick you go and so add uh sorry add totally also a candidate also field you're gonna say oh okay uh fill you're gonna say like current tally storage tally almost there at candidate you're gonna read this value and actually I'm gonna already put plus one here and you just write to it say add candidate right uh the new tally uh sorry the current tally or it should be new anyway and that's it like we should be done with our contract so you can see that's like 30 lines we just need to import the dependencies and see uh use the Aztec it's gonna be I think prelude and then all the stuff that we need to import from the Aztec library which is gonna be Aztec address the public mutable the map and I think that's it.

Cool like let's see if this compiles let's just go Aztec now go compile I'll be surprised okay uh yeah just some missing CMI columns which is quite common I guess where where am I missing it? Oh candidate write yeah here it's actually a dot and type I'm probably missing something here context message sender let me see my notes oh yeah it needs a fill yeah of course I forgot the two fill cool we see okay cool compiled now now you need this um this um builder like this uh code jar like you go and and again we run this so we can have this nice interface that you can connect with you can cast vote compute add to tally everything so you can go here on index.cs and just go um write the code to to actually make these calls. And like you already understand that he's already complaining because he doesn't have an admin, right?

So like you know, the constructor was expecting an admin. So I go wallet and like get getAddress, or yeah, and it will stop complaining. And I can actually make the votes. I can go like just get here the methods, and I can go like methods. Um sorry, not at the tally, it should be actually internal, but I can cast a vote. I'm choosing my candidates, gonna be one and a send, uh wait for it to be uh to be done, and like cool. This I actually it uh does it return, oh it doesn't return anything.

But yeah, what you could do now it's uh maybe create like a view function here, which costs no gas, and you can actually go like Aztec public and then uh Aztec view, which is a again a view function and go get tally. Um cool. Yeah, it seems like GitHub copilot already knows how to work with um Aztec contract. So why don't you?

Ah, I'm kidding. Uh anyway, so you can you can you can go here and just go, let's get the tally one. Like uh yeah, before I do something, I wanna show you that totally zero, right? So I can go like uh await like methods, get tally, etc. I can actually oh get tally for uh for uh the one, like the the candidate I want. I'm gonna simulate this because it's uh a view function, so no need to get anything. It's gonna go console tally one. And if I run this, I'm gonna run it with n n NPX TSX.

So if you like, if you don't like TS like me, you're gonna like to use TSX because it doesn't require anything. And uh there's some some problem. Anyway, I don't think I have the time to debug this. There's some problem around deploy account. Uh yeah, anyway, I I'll maybe get complete address. Maybe maybe it needs a complete address.

Please, please. Okay, no. Yeah, you you know what I mean. Uh you can trust me, bro, that uh that it it would if if the code was correct, it would actually be deployed. Um yeah, I mean you could you could basically go with this and then try to vote twice and it will just break. And you can after voting, you can try to get it again, and you'll see that it will increase by one. And that's pretty much it. Like, is it isn't it simple? Like it's 14 lines of code, right? Really cool.

Yeah, trust trust me, it would work, yeah. Yeah. No, seriously, there's a private counting code uh in your box. The tutorial, is that pretty much it? Yeah, that's pretty much it, yeah. Yeah. This is this is gonna be into the the Aztec docs soon. Actually, the wall contract, the whole tutorial is there, uh, it's actually more complete than this one. And I'm gonna be putting this one, which is a little bit simpler. Um, I'm just waiting for some tests to pass. And it's gonna be there. Yeah. Cool.

So sorry. I have five five minutes. No. Okay, cool. Yeah. I think that's five minutes for questions. That's more useful than trying to make this work. Do you have any questions? Cool. Oh, yeah, the microphone. Uh quite simple. I was just wondering where the context comes from. What does it have? What can I do with it? Yeah, good question.

The context is actually a two-fold object, so you can have it's actually an object that has different properties, whether it's public or private or private. So, like you, for example, you can get the block hash if you're running it in a public function, and also you can get like the nullifiers if you're trying to get it on public on public side. Uh and basically you can go um so Aztec has uh noir has this uh very handy extension that's for some reason it's not working in my computer now. I think I don't know why, but yeah, it will give you uh it it has autocomplete, so it will give you all everything you can call, but also in the docs you also have a reference where we have everything that you can that is in the public and and private context, but yeah, it's just everything that's on your machine that's available on private context, everything that it's not available and you can't get it because it's on the chain, it's it's also in the context when you call it by any public function.

So yeah, it's really cool. You can think of it like as a message, like on on ethereum you get the message.sender. Yeah, it's kind of a kind of that, yeah. Um sorry, uh basic question not Aztag. Um is the on-chain data uh just simply hashed or is it also encrypted um so if I have a transaction that has a sender and the receiver how does the receiver get the secret? Uh so they can let's say I I'm doing a token transfer how does the receiver get the secret so they can spend the the value they got yeah good good question uh it works in a UTXO model so basically when I when I encrypt something for you I use your public key your incoming public key and so I I encrypt that to you so you you can you are the only one that can decrypt it.

Now you ask me do I need to listen to all the transactions in the world in the network to see which ones are for me. Yes. There's uh there's there's actually a tagging mechanism that will make this simpler but yeah that's like how Zcash works for example yeah maybe if I should I worry about uh post quantum safe uh encryptions you should yeah okay so it's not yeah I guess it's not quant quantum resistant encryption at the moment no uh snarks as far as I understand are are not quantum resistant but I mean this is something that we are thinking about uh I I think everyone in ZK world is thinking about that but yeah they are not by nature like Starks are um quantum resistant so yeah you shouldn't worry about that anyway yeah I guess a little bit similar question, but would it be possible for us to have some shared private state and not like I would encrypt it under your public key, but I would all like MPC sort of idea?

Yes, yes, definitely. Um because especially because of a contra obstruction. So you could have like a multi-c and just have stuff encrypted to that to that multi-c. So actually there's there's some if you see the Aztec examples in the Aztec repo, you're gonna see like a crowdfunding example that will give uh some idea of that. Uh so there's it's pretty powerful, yeah. You could do something like that. Um more questions. I think one quick question.

Do you think UR will be expanded to support standard EVM rather than the Nasdaq DVM? Oh it is it is already like noir is its own language. Uh and so then you write noir and then you it you can export a solidity verifier that's uh verifies that noir program. So at this point it's actually the only verifier we have, which is uh on the on the EVM, but there are people like working on making this verifier for for example StarCon it. So so yes, like it doesn't run on any blockchain, it's just a ZK language.

You can actually use it for like encrypting stuff in banks, like or yeah, it's really powerful. And there are tools we generate the verifiers on that standard. Yeah, it's generated locally and you can deploy it on a standard DVM. And yes, there are tools. We do yeah, it's it's it's it spends like about 300k gas, so it's not cheap, but uh I mean it does something that's kind of almost black magic, so yeah. Uh it's pretty cool. So notice the um the Amazon Mat in the the code there.

Um obviously that's like Rust. Is it does that mean it has a borrow checker like Rust? Sorry? See the Amazon Mat context, for example. Um it's similar to the Rust, yeah. Yeah, um it has a borrow checker like Rust? Yeah, I think memory works in a in a slightly different way because there's no actual memory. Like you just start in uh yeah, it's just start in a different way. But yeah, but like it in practical terms it works just as well. I mean it's a type system is it's checking stuff like that in the same sort of way that Rust put it up. Yeah, yeah.

Yeah. The idea is just to keep to make it as similar to Rust as as possible in the ZK environment. Just a quick one. Uh what's the state of the network? And do you guys have an explorer to see an example of uh some transactions? Not yet. We we are we are deploying uh so test net has been achieved internally, has to say. So yeah, we're we have some we have we've been experimenting with kind of like distributed networks like internally. And that those are gonna be out for you to try like soon.

And uh and yeah, I guess from that point on there are already people thinking about block explorers, and we we incentivize you to uh to go and try because you can I mean you can run the network locally, which means that you can already start write writing uh a block explorer and you're highly incentivized to do so. Yeah. Given that there are private data, how do you do a block explorer? I mean, if it that's a good one. Yeah. How how can you what data can you see from a chain that reveals nothing, right? I don't also don't know.

But uh I mean, yeah, there's there's there's a lot of public data, right? Like uh Aztec is Aztec is fully public, right? Uh it's a fully public transparent network. Um like everything runs in public. It's just like the data is encrypted if you want to, like programmatically encrypted. So there is public data like this Tally, for example. Uh and so there's a lot of public data floating around. You just can't see the stuff that is programmed to not be seen, which is uh cool. So yes, you can you can definitely have like um, for example, token standards that reveal some of the information in certain conditions, and for for for that you can definitely have um block explorers. But that's I guess that's a bit of a creativity example.

You can just use Etherscan and do the same, it won't work as you as you just mentioned, yeah. And also given that this is private, any should we be scared about tornado cache like stuff happening? I mean uh this is just the code, right? Uh so yeah. Yeah, but this is just a code send someone to jail, so depends. Uh I guess that's that's something we could speak we can speak later, but yes, uh potentially, yeah. Cool.

Thank you, thank you for coming.

Related talks