KL Mini Hack 2023: Cairo: A quick look at a provable programming language | February 15 | 8:00 PM
Ethereum Malaysia·Mon, Oct 9, 2023, 12:00 AM
Starknet's smart contract runs on a new VM called Cairo. WenKang will walk through what is Cairo, why do we need Cairo as well as some basics on Cairo. No need to be afraid as Cairo is not as difficult as you might have thought!
Transcript
thank you okay um yeah so hope everyone can hear me so uh yeah so welcome uh to another installment of the KL mini hack um today we'll be having a wincom uh which is a who is a software developer from nethermine and also a very active member of each kale uh presenting the topic Cairo a quick look at the approval programming language so uh without further Ado um let's get started hi uh nice to have you all here thanks for coming um thank you for sharing about me um so yeah here today we are here to look at cairoa Pro programming language so what what exactly is Cairo so um to add some contacts uh Cairo is occurring complete programming language for writing proverbal ZK stock programs it allows developers to very easily build efficient and scalable l2dx with l1's security with exponentially greater so what does this mean right it's a bunch of mumbo jumbo so um well essentially um Cairo is a um it's similar to solidity in the sense that it's a smart contract language in a sense used by startnet and start X as well uh for the development of a smart contract and the apps however uh instead of being a very domain specific language it's very much a general purpose Turing complete language meaning that it can do a lot more it's not constrained within what is possible um in only in the evm in a sense so it's used by stockx which Powers dydx decentralized uh exchange radio and also immutable some of them might be very um very familiar to you but you can look that up there are they are quite uh interesting use cases that start net and Cairo is being used for and it's also a smart Contracting issues by stocknet Okay so before we continue let's have a quick refresher some of you might have already seen this um yesterday by uh on the start Network so essentially this is what a subnet architecture is like underlying so as smooth surface no stock and it's a L2 um and how we can as we can see here a user account which is a smart contract will usually have a create transaction which will be sent to a sequencer and the sequencer itself is Cairo VM which can run a Cairo program which can will process in your transaction coming in and after processing it you will send the so to say execution Trace to approver we call it a sharp approver and this proverb wouldn't generate the proof uh and you know send this proof to a Smart contract on the on layer one basically back to ethereum and this would then be committed essentially to layer one and uh update the state essentially and the proof and the state difference will also be sent to a full moon so this is like a very general look on how a starting line architecture is like and in this case on what we'll be focusing on is mainly on uh so to say the Cairo OS and and the language that essentially this OS this operating system this computer this VM speaks this is a simpler diagram of it the color code essentially gets processed by approval which generates a proof and then sends to a verifier if I wouldn't commit all this to a legal one essentially Okay so now you might have the question of you know why do we need to approval language right what's there to prove about right a language should just be a language um uh we all are familiar I think most of you are familiar with typescript perhaps some python perhaps C plus plus whatever right there's a lot of language out there you know and why not just be like that why make it provable right why why don't we just have a language like Cairo for like like just be like solidity right like by like Viper and all that right there's a lot of different smart contract language but why do we want to use Cairo why why why why is provable a desirable trade in a language well it's essentially so that we get to retain the computational Integrity of an executional program in uh in the Cairo VM or like OnStar net so by computational integrity what does it mean well uh so someone called Lewis long time ago said that Integrity basically means doing the right thing even when no one's watching so computational Integrity essentially refers the same thing in that it means that uh even when no one is actively trying to look at it like look at the program uh you are actually Computing directing you are running the correct function you are running correct the correct routine and you're not doing anything else there's a lot of ways of ensuring computation Integrity just to give some example so as we can see here uh there's different type of ways of doing it reputation delegated accountability these Banks right we trust that banks are doing the right thing although we can't really see what they are doing the background knife replay Bitcoin which basically trusts that you are kind of doing writing by doing replace of your transactions uh trusted Extinction environments uh this usually refers to uh machine or a chit set that's a run that you trust to run things correctly fraught proofs which are a blockchain use case and I mean it's also a real life use case but uh here we refer to optimistic robots where something like optimism something like arbitrary their optimistic grow ups and they reutilize frog proofs to ensure the Integrity so the yeah so and finally cryptographic proofs so something like SK stocks or even Ziggy snacks so here um since we are talking about subnet which uses the ZK Azure knowledge stack proof system we talk about ZK stocks but there are also other blockchains that uses different kind of Pro system something like a snack DK snack these are very interesting topics that if you would love to look at feel free to look it up they are quite available as well maybe uh can we can add some links on that here in the chat as well later but yeah so these are multiple ways of ensuring computational integrity so essentially what this allows uh in the very long run is that it allows stop net which runs on Cairo to very easily scale up upscale like from what we have right now so um as most of us know as of now to know that a program is run correctly you would basically have to give that program to another person who wouldn't run it and then you can then verify that yeah it's run correctly um so as a live left frequency you can you can see that if I want to prove to another person that some program has run successfully and produce a hello world as an output I basically have to give the program to that person and that person would then run it on their own machine and if it gets hello right you know that it's correct right it might run a few more times just to be in case um but yeah that's the idea um with computational Integrity with something like a Cairo which can just generate a proof of that you essentially can achieve something like the right background where you can run a program right and generate a proof of that execution so you can execute a programming and write a program and after and you can generate like a trace of the program which can then be made into approve and now with this proof without actually showing what the entire program is or even you know walking through what's the what the entire program line by line you can just pass this proof over to another person and that person because of cryptography can just take this proof and pass it to a verifier which can verify if the proof is right or wrong um so I mean this two diagram looks quite similar but my ass you know no it looks the same but like it looks like just you know the first one with extra steps um well the skill the the part that makes it different is this imagine a decentralized network where you have to somehow prove that you have done some transaction and and this becomes very and then what we show here becomes very obvious so in this scenario if we rerun each transaction across all the compute or like everyone so everybody have to somehow verify this they don't have to run it they have to check it they have to check if it's the correct I'll put if no I I let's say I send someone about five if if the five if is sent if that change have been made and if it's been made then you know it's a valid uh transaction things like that so if we use something like a GK stock or in this I mean something uh in this kind of cryptography system what we can achieve is that now everyone involved in this uh involved in this network can essentially just verify the proof which is um a lot less uh Power hungry than them perhaps a programmatically because right now if you want to try to run something very computational heavy very complex on on the blockchain it will cost you a ton of gas you'll get charged for that because you know you have to go through every single step run the evm has to run it you have to check that this is correct that's correct and all of this basically cost computational power and in in the blockchain it basically costs you gas right um with something like a cryptographic proof like this is not even you essentially can just prove that yes I have done this computation right and without re-executing an entire thing everyone just only needs to verify that proof which most of the time would be a lot cheaper than um re-executing a program perhaps and this essentially allows stock net and uh software or xrx to have very low gas fee while being able to perform very complex operations right and that's what Cairo is made for in a sense Cairo is a language that's made to to allow you to write programs that are provable and with this you essentially can then um proof that you have done something and everyone can verify you if you've done something correctly without needing to re-execute again and again so yeah let's move on so essentially yeah this is what I've just described it's a diagram of how how it essentially is so your program would be possible approval which will generate the proof and then essentially this proves that you are moving like close that you are moving from the old state to a new state and this now will get verified by everyone every participant uh in the sorry a participant in the network and this is a lot a lot cheaper than just re-running everything again and again so right um a lot you you will say that you know yeah I know until this point yeah it's cool and all but I can only run in about chain well with a project called that's actually recently a project called Channel which is a Cairo to lbm uh compiler so this essentially allows you to run uh your Cairo program offline and the implication of this is that now you do not need to only use Cairo for like blockchain stuff you can essentially potentially use it for a lot of things it's not a only a blockchain constraint technology it's a very generic technology and given that it's a turing complete language and a generic uh General language like it's not limited to Smart contract anyways um what you can do with this I mean you can only imagine but um you can do a lot with what you have here okay sorry so just like go going a bit deeper into Shannon so Shen long is essentially uh VM as I mentioned and uh lvm is a language backend that allows you to compile to uh usually see code C based code is a c based back end so um yeah so with this essentially you can write Cairo code and you can now run on most of the machine if not all of the machine that we currently use because lvm is is essentially the back end that converts your converts your code into a machine code to put it very simply okay yeah I've said a lot I've talked about Cairo a lot so how does it look like you know but it might look like so now let me just show you a demo so allow me to just pull this over okay so for those that are familiar with Russ this will look very familiar to you this is a very simple uh program I would say so to run a career program um essentially you can just use Cairo run but here this is actually the repo I have cloned down their code code repo um it's actually powered by Russ underlining it as well but it's uh it's it's the back end it's written for us but this is essentially what it looks like so allow me to just like Okay so let's put something simple so just because so I think I mean it also needs a main function usually let me just try to return Network zero yeah uh sorry can you zoom in like one or twice yeah sorry about that I know from uh give me a second I have forgotten how to actually I think this does this work okay I think it's good no thanks okay thanks thanks for the reminder so let's just try to return a zero and let's try running this right now who knows maybe this camera yeah you know as we try to run oh can't return zero yeah because it's telling me that I'm not essentially uh telling you what to return but it's expecting like a essentially a void let's try running this again yeah it returns zero as shown here so uh some might see felt and be like well what what the hell is felt uh this is clearly an INT what you're talking about um so here I felt actually refers to field elements it's a very specific um I would say it's quite specific to Cairo in a sense so far refers to fuel elements uh it's essentially 252 bytes of integer in this sense um why do we use file well if there's specific properties that um which I wouldn't go very deep into that uh Cairo requires to function essentially and felt it's a very major It's Like A Primitive for Cairo um there's also other Primitives that comes later but essentially most of the Fields most of the data most of the things in Cairo is a felt so to say a field element that includes this yeah zero is a felt so second okay so oh sorry so right now let's try something a bit more complex right let's just do addition right why not one plus two I mean this is very simple stuff you know it works it's very hard to not work it's just one plus two yeah actually this and that so that's fine and all um let's try doing some logic I guess I mean most of you would already know things like variable declaration so yeah you can declare variable return variable this will work I'm not gonna I mean I'm gonna run it but just to show um there's also some very nice uh brush feature that's being added into this so let's say pattern matching right something that's quite nice to use as this allows you to do things like this striking for a bit let's try running this might break does it work it works yeah it's five zero so that's interesting five I think it's not really working that well the thing right now but it should run as expected it should return three oh no it's a matching non-zero value is not supported try this then return a if it's not zero try this returns five as expected so this is something that's pretty nice to have it's also enums in this language and all but let's just try something that I have here so it's it's okay okay nice so this is basically Fibonacci as most of you may know a Fibonacci basically is just something like this the sequence looks something like this and we can calculate in up to I mean let's try five which in this case would be one two three four five here right and one this one this is two which is 45. so let's try this this is a function also would look very similar a very uh familiar with everyone that uses Russ in the first place so yeah it returns five as I factored be nothing special here um I'm not going to demonstrate sending this to the shot prover to generate a proof um but you can potentially send this to a shopriver to generate proof for what you are going to do and you will basically generate a proof that yeah you have run this program in a certain way it's a lot more things like structs and all but yeah you see right you can see it's uh it looks something like this if you want to go into it there's also the has something like a shark it looks like this this is you can create your own structure this is my own type and you can essentially create something like this and this is very much runnable as well I mean I can try running this just adding a mean function and the result fifth zero ID 100 let's try running this drop yep it Returns the result value which is uh which is the 100th Fibonacci Sequence and it also returns me I believe this is the index yeah 100 yeah which is correct so yeah something like this there's a lot more to this um I'm just going to show very simple stuff here that's not really a workshop but do look forward for a workshop that's going to come later anytime in like the future yeah so yeah um that's like a very very short and simple demo um I'm going to show so this is not in a demo so I'm gonna show you how a erc20 contract looks like um in here so it essentially looks like this um you use something you use this annotation to mark that yeah this is contract and and everything else it's quite I would say if you're familiar with solidity this will look quite familiar to you one thing that might be a bit unfamiliar with is probably the function so as you can see it's declared like a function instead of like event transfer uh not sure if they're going to change the keyword later but yeah um and also you might look uh I mean this is all very familiar I think to most uh solidity users the Constructor well you know what Constructor is essentially all this is written in so as you can see you don't actually directly assign your fault in a construct you actually use something like a right uh function to write the value into your storage this is a bit different from like just directly assigning but yeah this is essentially what it does so there's also a third which does the checking transfer this emits a function this is essentially a event that you are emitting just directly put it in any way I mean the event so yeah most of this would probably look very very familiar to you um yeah approval function which basically just called proof helper which is here basically just checks and you know if it works I will just write the allowance set your allowance to a certain amount and emulate impact yeah this is how an erc20 contract in solidity will look like in a Cairo um in the chiral syntax Okay so one like as I said right most of you might already be familiar with solidity so what if you want to start with Cairo but you know it seems like quite a hassle to uh relearn the language I have so to start doing this right it might look cool might do cool things but you know how do I start well a very good um I would say something that's a good news to you uh might be well right it's a transpiler it's a transporter that allows you to transpile um so solidity code what you currently have to essentially uh Cairo code so of course A lot of times this is not perfect but it does help you start off your your language learning Journey a lot easier at least for me most of the chiral syntax are quite friendly and with uh right now what we have for Cairo One um it's it's very much getting better and but that's it right it helps to have a transpiler and this currently is actually already used by stockware which is a company responsible for stagnet as well to basically transpile the entire universe contract into Cairo with some modification so you can check out the the contract you can check out the code at github.com is essentially uh you need some completely written in Cairo this is also very uh I mean it's quite friendly to use as well so it basically looks like this right it look it compiles your contract and then it will transfer it transform it into here what we have here right these are all uh essentially transport from here you can see some artifacts that can get data underscore this but these are completely working and it's Deployable from the get-go I'm gonna also do a short demo on warp so let me just get that up okay okay so let's okay so one caveat right now um I'll talk about it later okay so let's try compiling some contract since we was we were talking about erc20 anyways so this is uh yeah C20 solidity contract um okay let's change this it's fine but as you can see now we have all the functions this and that it's a very simple contract so let's try to compile this so to install what it's basically very simple you just have to install it it's this command [Applause] install your log in and it should be available as a global executor so let me just transpile like erc20 yes software transpile it should be available at the output file here you get the ebi and also the chiral code so I'm going to explain why this color code looks slightly different from the one I've been showing just now later but essentially this has an older version of Cairo right now they are actually doing a migration from this older version of Cairo to a newer version of Cairo as you can see most of the things it's actually here the right functions here to write in storage the let me just look for like yeah this is the trans transfer function uh it's very sparse right now um total Supply accepts uh within and then you you know set total Supply get total Supply decimals as well returns a the taxable you have which is 18 is standard in the original contract here let me just go and take out the object here so balance off right it actually does the same thing um but as you can see they have specific um Syntax for this so this one basically checks it gets the account this account is essentially the input from here and you read this uh convert it to you in 256 because spell is actually two five two you have two un256 is uh not like it cannot fit 256 bytes into a felt so you actually have to expressly convert it into another type and yeah you read it and get you essentially get uh the balance that's the same thing uh mint let's look for mint balance two plus amount slightly longer here but it does the same thing these are all conversions and this reading um so it's basically checking doing the check if your amount is good enough if the address is correct if the amount is correct and then you would just do some assigning and write it to storage yeah this return one doesn't actually it's not so to say if you look at yeah it essentially returns true right in this case it actually treats at one as a one yeah it does the same thing yep so that's essentially how what works you can transfer a lot more things so you can look at some of the examples that actually gives you you can actually get this all from the GitHub repo from what but let me just take uh yeah this basically just checks for fallback function it's compilable lateral operation that's a much contractual tests but this is one of the things that essentially they use to test um if what is working as expected we can try to compile this as well use local variable but I'm sorry let's remove this this is unused well and it should be available down here as an output related so again Cairo this is a bit more complicated uh it actually does computation on I believe this I think the implementation of an array in a Cairo but let's just look at D then storage settings yep you can see it beats it reach the length and get an amount and it returns it it's it's a not it's quite readable actually but it's not that this is very distracting but yeah it's essentially quite readable this is also seen in the New Cairo version but it's just in different syntax slightly different so yeah that's what essentially so well uh as someone who started this so someone has started venturing using ecosystem you might start looking for projects and things to I would say explore there's actually a lot that's already there uh it's very quiet for those not in the start where starting Community but there's actually a lot a lot going on and once you go in you realize that people are doing a lot of amazing things so these are everything here so you can scan the QR for the documentation it's currently still in older version Cairo so regarding the Cairo version uh one thing is that that there's currently a very huge migration going on uh from Cairo 0.10 to Cairo One what I'm showing you is Carol one and it's actually a really functioning as I've shown just now um but a lot of what is on star net chain today is uh Cairo 0.10 but the next version of statnet which will come letting very soon uh so start net 0.
11 uh subnet one point uh sorry Cairo 1.0 bits will be supported from that version starting from that version so I would say this is like the a very good opportunity to start exploring this language uh it really does help I mean it really does allow you to like be be aware of a lot of things happening as well you're basically starting to use like a like a language that just came out right it's basically like that so yeah there's a lot going on so I'm just going to go through all the githubs here so language call this is by this is the Cairo language core it's written in Rust the back end is written in Russ but that that is currently heavy development like I I have been there's been a case where I actually I put the code in uh for myself to to start uh working on it I try to contribute to it and I think about half an hour after like there's like 40 plus comment already going into to their main branch so yeah it's in very active development which is which is a good sign um but yeah that's accepting a lot of contribution so go and take a look if you're interested in programming languages it's gonna be very fun for you um for tooling uh there's a lot of tooling like well this one actually protostar and here is actually a something similar to hard hat in a sense it's something to allow you to write uh chiral contracts this essentially allows you to very very easily create your contract deploy your contract using just protostar deploy um yeah that's that's something that's currently also currently ongoing a horse tracker is my netamine this is a smt checker um it's related to it's a bit technical this is related to um formal verification is which is like a security uh a way of basically auditing your contract that's currently being worked on the scope is essentially like Cargo in the sense that it's a package manager for the Cairo ecosystem as well so yeah um those are all the tooling happening there's also a lot of application going on there's a certain autistic which is just a multistic wallet written Cairo there's a which is a library for drawing graphs in Cairo so yeah it's things are coming by there's Kaka road which is a zkevm implementation completely written Cairo they play on subnet so yeah essentially that's a let's say evm inside Cairo VMS well so that's like that's quite fun if you think about it you can deploy contracts to this even this VM it will run so yeah that's fun like as I say it's a growing ecosystem there's a lot still in the works but that's also why it's very exciting to uh look into this uh right now to start looking at this to start playing with this uh most of the case use case right now is still focused on the uh the L2 subnet I mean the level two chain started itself but as I've shown like if I mean before it has a lot of potential as a brewable language it allows you to do a lot of things a lot of it gives you some flexibility in how you can use it other than just being a DSL right I think that's why it makes it so exciting to not at least me so yeah that's essentially it for me so yeah that's Cairo I hope you enjoy the talk uh well I think I can accepting any questions if um fuel filter and mute and asks if you don't type everything out um anyone has any questions oh yeah okay uh maybe I I'll have a question um Chen so uh personally um any uh so you mentioned quite a bit of tooling an application which one of these is like your favorite so far perhaps you can talk a bit about that um higher usage uh personally for toolingwise I'm very much a tooling guy I would say um I personally really like Co-op it's what uh when I first started using Cairo it's what helped me quite a lot in understanding how solidity translates to the Cairo language um and it's also interesting to me as someone I'm someone who is very interested in programming languages and how you know how languages are compiled how is it processed things like that so yeah for me what is very cool um I think one of the most impressive projects that I think which is on social media is kakara which is it isn't it's essentially like a you're creating a virtual machine inside another you're creating a virtual machine with this language right you can really write a virtual machine in it um so that's that's very impressive how what they've done right discuss Cyanogen in Labs but it's very impressive what they've done with Cairo what they have achieved with kakara foreign [Music] what are the main difference between Cairo 0.10 and Cairo one I believe one yeah okay so uh there's a few different so Cairo 0.10 is what currently is live right now it's it's the if you go look at a lot of Cairo documentation and a lot of credit code it's currently written in Cairo zero like that um it's quite a low level language in the sense that um you have to there's okay let me just show I think showing is easier code if you look at a Cairo uh this is Cairo 0.10 so what it essentially uh have is you have you can see all this like Cisco pointer Princeton pointer range checkpoint this uh things that Cairo the Caribbean uses to [Music] essentially prove that keep track of everything in this machine or in this OS and allow them to create proof and a certain operation so these are very much low level and for a lot of programmers or for all developers this is not very intuitive and it creates a lot of overhead for them so color 1.
0 takes away all these things and make the syntax a lot easier to understand gives you a lot of synthetic sugar to make it look a lot nicer if I just give you like a comparison of these two things like this you can see this is like a lot a lot more cleaner what is heavier like just return function you don't need to think about like Fibonacci like like nothing much like all the pointer okay there's a pointer things like that just look at this and you know it it allows you to do a lot of things and it's very modern uh syntax as well um yeah that's another question asking um if there are any other comparable languages to Cairo uh in like CK ruler kind of context yeah yeah um so far from my understanding uh okay so there's actually a few blow ups uh that have their own languages or I mean Grow Ups actually no actually most froze UPS uh either use uh solidity one of them have their own language I've been fighting does have their own language I'm not very sure how that's going but um from what I know about the language from what I know I'm not I don't know a lot but from I know it's not really a tearing complete language it's more of a DSL that's my understanding I might be wrong right don't quote me on this um so yeah um I think there's also zinc there they have something called zinc I'm not very sure about zip code yeah things like that and for a lot of uh the hero up they either have either just have solid compatibility or they actually I think most DK Rob I know does only have uh solidity compatibility but if it's non-zero up like let's say a zero knowledge base uh chain something like Mina for example or even uh I think as um those uh usually have their own interface to it um which are also most of the time DSL for me not at least which I know more about it's a DSL that allows you to write uh basically circuit in like that's what contract is essentially a type of circuit right um very but it's written in typescript which is a quite friendly as well but yeah I will see Cairo is quite unique in that it is a tearing complete language that can also be used for smart contract yeah Leo as well yeah alio yeah she's actually giving up a lot of change as well there's a lot of CK chains that each does different things they have their own language for it but I believe I mean you you know like yeah yeah any other questions um [Music] more questions I mean I have another question um if if you should 0.10 uh sorry I think you are muted [Music] yeah sorry I mean a question so um so if I'm coming into Cairo do I just jump into the newer 1.0 version there's uh more rust like um am I missing out if I don't learn or go through some of the older syllabus written in 0.10 okay good question a really good question zero okay so right now there's a lot actually there's a lot of similarities between 0.10 and 1.
0 that's the only thing that I've changed is that it's a lot friendlier to write so going through the old materials won't necessarily make you not be able to write the new in the new syntax but um in the community right now A lot of people are recommending learning the Cairo 1.0 syntax since it's it's actually quite I think it's I think it's the end of this month they are releasing already a compatible start net version that accepts uh 1.0 so I think starting to learn the 1.0 version would give you a better result than going through the entire thing about if you're going through the whole material it will still give you an Insight of how it might look like how it might feel like yeah I'm guessing it'll also give you some appreciation of what the current team has done as well yeah yes definitely yeah it's a lot better yeah okay um uh any more questions from the club uh feel free to unmute if you have do you have any questions foreign that might be it um so yeah I guess we can sort of wrap it up here um okay so before we end oh yeah should Kevin do you want to talk about the talk that we have tomorrow uh yeah I was true yes oh so yeah uh before and uh we do have another talk tomorrow um hold on just get the title it'll be on scaling ethereum with rollups and zero knowledge proofs so this will be presented by scroll um and you'll be tomorrow at 10 am so uh before you leave let me just quickly drop the link here uh if you haven't signed up yet so back on chat here so I'll drop the link um yeah so feel free to sign up so the the reason why is that NM is because of time differences uh but uh we will definitely be recording this as well and uploading it to the YouTube channel so um stay tuned everyone and I think that is it for today um thanks everyone for joining I'll just stop the recording
Automatic transcript — names and jargon may be misspelled.