New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Starknet Malaysia Guest Speaker (Vyro - A Vyper to Cairo Transpiler)

Ethereum MalaysiaMon, Oct 9, 2023, 12:00 AM

We have heard of Warp by the Nethermind team but recently we have Vyro which transpile Vyper to Cairo! Hooray to all Vyper fans! We are pleased to have Gary, currently the sole author of Vyro, to come and share with us on his journey on building Vyro. If you are a fan of Vyper, or wants to learn how a transpiler works, or is generally interested in the blossoming Starknet ecosystem, this is one that you wouldn't want to miss Check out the repo here https://github.com/tserg/vyro

Transcript

that's all right so now I've started recording so hi everyone uh Welcome to our very first time Malaysia uh guest speaker talk and I'm chitran and uh I'm one of the organizers for starting Malaysia committee and today we are very proud and uh to uh to to invite over Gary and Gary is the uh creator of viral so what's viral you may ask so we have we have heard of this thing called what what is by the dandelion team it converts the Solitude code to Cairo code so you can write stuff in 30 and you can and it will transplant your code to Cairo so that helps to onboard like devs to Cairo and we have now the Viper version and it's created by Gary and I will leave the uh the further introduction to viral to Gary but yeah um take us away Gary thanks Chichen so hi everybody I'm Gary and today I'll be speaking about viral which is a project that I've been working on uh first off I want to thank XI Chen and Wen Kang for organizing this and inviting me to share about my project and um before that I'll just give a quick introduction about myself at the end of last year I was looking to get into web 3 Dev after a few years of dabbling around um yeah correct that's the GitHub link yeah so uh Back to December last year I was looking to get into web 3 Dev and I was applying for internships and first I got to spend the first half of this year with never mind working on the warp transpiler which as shuchen mentioned converts your solidity code into Cairo code and that's where I got to learn a lot about transpowers and how the entire process works uh which was really pretty cool stuff that they are doing I don't know if you have seen it but they can now transport the entire Unison contract onto stock net uh that was the latest update that I saw and at the same time at around December last year as well I started contributing to the fiber project so Viper it's a alternative to solidity and in this presentation I'll be introducing a bit more about Viper actually uh because our assume that most of the people here would be familiar with Cairo since this is a stock net uh equal system sharing session so yeah let's jump in so first what is viral it is a command line interface that converts your Viper Smart contracts into Cairo code that can be deployed onto starknet and as mentioned the inspiration for this is from the walk team at nevermind so I'm going to give a very quick intro to Viper uh just by comparing it to solidity and uh if most of you are familiar with solidity I will characterize it as a language that is very flexible it allows you to do many things without limiting your ability to for example go into assembly and you could have a ball Loop you could have a for Loop and Viper on the other hand is very opinionated about what you can or cannot do any intentionally limits what the developer can do or so that uh it is safer or rather it is easier to ensure safety in the code itself when compiling it so I really like this meme about a follow in solidity where you have Corner space of doing it and it has different implications on gas costs so if you would want to do a follow in Viper it would be really similar to the python syntax which is uh this snipper over here so personally I like the Viper syntax a lot more in solidity it's just a lot easier to read and to learn uh personally although the resources are a lot less Limited in comparison and I think another defining feature of Viper is that there is no contract inheritance uh like what you have in solidity and in this in this sense it's really quite similar to what we have in Cairo now where it Imports are they are still quite limited or you kind of inheritance although you are able to import certain functions or constants now from another file so this is just a quick uh FYI on who used or are still using Viper we have unisoft V1 which is in Viper Yen V1 as well and in Yen V3 they have a new governance module that is uh implemented in Viper um we also have little uh but it's used a lot less than solidity in a code base we have the if do deposit contract the first version it was in Viper but then it got scrapped and was re-implemented in solidity and lastly of course we have co-finance where a majority of their smart contracts are reasonable last but not least if any of you were catching Defcon and were watching the opening ceremony you might have noticed that fidelic himself shared a code snippet that is in Viper and you may not know this but Viper originally Grew From Another project called serpent which vitalik himself created yeah but that's a whole lot of history that I myself uh are not fully familiar with but if you check out the comments you will see so there's a question in the chat box uh does that mean that a Viper function call users or creates less bytecode [Music] I would say that in general Viper is more efficient and therefore it does create less bytecode and the reason is this is the case is because the things that you can do with Fiverr are more limited than in solidity and because of this limitation there it is easier to reason about the operations that need to occur to uh execute dot intended operation so in most cases Viper is actually more efficient than uh solidity yeah so but I mean of course you have to I'm comparing against pure solidity not someone who drops into assembly or you plus in solidity yeah because once you do that then if you're a good Optimizer I'm pretty sure you can be on par or even beat fighter easily and now I'll share about the compilation process for languages in general um so on the left what usually happens in a compilation process is we take the readable code that we are writing we convert it into a tree a tree data structure that's called an abstract syntax tree and then this is converted into a intermediate representation and then this is eventually transformed into bytecode or assembly this is the general compilation process and if we look at evm languages this is how they compare so we have solidity and rust they both compile to a intermediate IR that's called U which then gets converted into evm bytecode and then on the right we have viper which has its own AST and that gets converted into its own IR which is called lll or lisp like language and then this is converted to evm by code and then lastly we have half which is a newer language and very low level so based on what I understand they drop from the EST to bytecode directly and the reason they can do so is because half is already really low level so having an IR kind of defeats the purpose and with this in mind uh let's see how we can work with the Viper language to convert it into Cairo so first off we are primarily working with the Viper AST that's generated by the fiber compiler itself and viral takes this AST and it adds on a few more information that is specific to Cairo and the way to do so is by changing the structure of the ast3 either by modifying existing information or adding new nodes to handle chiral specific things so one example of a chiro-specific instruction would be uh let me just take a quick look yeah so reading and writing to storage in Cairo is very different from uh evm uh you it requires a specific operation so the storage variable name dot read or storage variable dot right so that that was something that was there was a class of EST notes that were created just to handle uh this special syntax in Cairo and another example it's the Cairo assert which actually functions as a assignment as well so that was something that we had to take note of in order to properly convert Viper code into Cairo code and after this transformation of the AST is done then the last step is really simple we are printing the AST that has been modified and outputting the Cairo equivalent uh for each node and right now in the Cairo compilation process it drops directly from AST into assembly but I think in 1.0 there will be a new IR that will be introduced that's called Sierra but the the point of this diagram is really to show everyone what the transformation process is and viral actually only touches the Viper AST and outputs it to Cairo code and then this Cairo code goes through its own compilation process with the Cairo compiler and uh viral itself does not do any do anything for this portion so let's uh run through a very simple toy contract on the left I have uh Viper contract that is an external view function and it does uh two operations first you take the input and you modulo it by two and you add it to the other we go back right uh the first step is to convert it into the Viper ASD so let's see what it looks like um I've extracted the important AST notes so that it is easier for this session uh in the full Viper EST you'll see a few more notes that are not present here so in case anyone is going to look at it after this session and let me just give a quick run through of how this contract corresponds to this asd3 so first you have the entire contract which is containing a module node and then we have a function depth which stands for function definition and refers to this function at Mount in 20 in 128. so if you have multiple functions then there would be multiple function Dev nodes under the module node and right now under this function that we have there is only one statement which is a written statement so here we can see that return is a child node of the function Dev node and then within written it has one value which is a b naught or binary operation and within a binary operation itself it has two operands one is the variable a or the argument a and then the other argument is another uh be not known which then operates on another two operands uh sorry there's a typo here this should be B and basically what this means is this part is B modulo 2. and then this section we are adding a to the result of B modulo 2. and then we are returning it this result for this function and this function constitutes the entire contract okay right then let's move on to how the viral uh transpiler that then operates on this Viper AST we break up the written value and we move it into a assignment so what we are saying is we assign the value of the return statement to a new temporary variable that we'll create let's just call it temp two and temp2 is basically a plus b modulo 2.

and then the next step is to replace the return value with the new temporary variable that we have created so right now we would have attempt to as a written value here and then for the assigned node that we've created you would be attempt to let me see if I can annotate yeah so for this assignment we would have temp two equals a plus b modulo 2. and then again right now when we are running through this operation we will break up the science statement temp two equals a plus b module O2 into two step two separate statements so here we are shifting the second part of this uh a sign statement which has a nested operation into its own statement so we move this one level up and then we create another temporary variable called temp one and we assign the value to be B modulo 2. and then what would happen in the tree is we would replace this by temp one and this would complete the transpollution process so let me fix a typo here oops okay so there's a question in the chat foreign yeah so you're just in your correct we are trying to abstract away the operations into individual statements and the the reason that we need to do so is because we want to so firstly before Cairo 0.10 you couldn't chain operations so some operations like d modulo 2 they are not native Cairo and you need to wrap them in a function call so because of this it would be necessary to break up any nested binary operations into its own statement and it in terms of a transporter it makes sense to do so because you want to reduce the code paths as much as possible so in this case uh what I mean by that is if we have an operation in the written statement then our compiler will need to handle written statements or written notes in addition to assign nodes but if we abstract away all written values and we move them into a assigned node using a temporary variable then what this means for transpower is we only need one code path that handles assign nodes instead of both assign and return and in some sense this is what guides the entire transporter which is to funnel as much logic or code path as possible into as few nodes as you possibly could uh does that mean the node server will require larger Memory Bank in an exchange for handling less operations if I understood a question correctly the answer BS but in this case the node server refers to the compiler which is running on your local machine so sorry the transporter which is running on your local machine so that wouldn't really be your limitation in my opinion thank you let's move on and right now we have reached the final step which is to convert our viral AST into Cairo and I've broken it down into the specific sections uh or rather how each node corresponds to the code that's generated firstly we have the module node which contains information like the Imports and also the language modifier that is typically at the top of stock net programs um and then we have a function Dev node that prints the equivalent chiral code uh it takes the same function name and then provides the implicit arguments and right now the implicit arguments are built in in a sense that the transpiler is not checking which buildings are actually required but it is just giving you all the built-ins so maybe that's something that could be improved but I don't really prioritize that because typically we would need most or three buildings and then we have the arguments which are also under the function death note and in this case we have converted the type from int 128 into felt and as well as the return value uh the type is now felt and the slide modification is viral requires sorry Cairo requires return values to have a variable name before 0.10 so what the transporter does is it appends uh underscore i t to the function name and this is used as the return variable name so if you can see it this would break if it's a tuple value but I don't think viral right now handles top of values so there will be a feature to build for for the future but the other in my head the solution will be quite trivial we will just count the number of return values and then add a number to underscore IET and I think that would solve it yeah but let's leave it for the future uh let's go into the body of the function Dev node so in the transport ASD we have three nodes two assigned statements and one return statement and the Cairo code for these statements are pretty easy so let is hard coded because it's a pyro syntax and then we just print the name of the variable that is being assigned and then we print the value of that variable on the right side so just printing the name on the left and printing the value on the right and next we have the second statement which is let again the same thing uh just a variable name which is so both of these are temporary variables and which is why you see this syntax of viral underscore underscore a certain number and and then again the values on the right side and lastly we have the return statement which uh Returns the variable that was assigned in a previous node and just a side track module is replaced by a function call that is uh part of a library that is provided in viral the reason for this is I don't think modulo is of supported operand in Cairo which is why we have to create this wrapper function to properly transpile fiber code into Cairo so yeah that wraps up the quick and simple Viper example and what what is next for viral uh there are quite a few more features that could be supported and the first one is of course supporting all the types or as many of them as possible and the two most pressing ones would be static arrays and dynamic arrays we also want to support as many of fibers built-in functions as far as possible but again these two are subject to the fundamental differences between stomach and the evm for example stock net does not have a decimal type whereas Viper has so this probably could never be supported on stocknet and some of fibers building functions also rely on pre-compiles on ethereum and I think there are also hashing functions that would not be able that we would not be able to transfer to stocknet so yeah this is all on a best case best effort spaces uh and then the last two would be the ability to transform for Loops into Cairo and this will probably require some form of recursion and then uh the last one will be optimizing the code that is generated so if you transpire a more complex contract in Viper to Cairo you will notice that there are some statements that are redundant so let me give let me try to write an example so because the compiler it sorry the transpiler it reduces everything to the common case right like what I mentioned earlier we move everything into a sign statement and then we let a single code path handle all the logic so in some cases you could lead to something uh ridiculous like this okay this is very slow I'm going to type it so first we have a assignment let's say this is felt with a value one and then the next line we assign another temporary variable to the same value before that and then again we perform another assignment of the same variable um sorry actually this this is not that good an example let's use a written statement instead so if you look at this string of statements you will immediately notice that this is entirely redundant right because we are returning viral var2 but it is just a copy of viral var1 so why don't we just return viral var1 instead and this is what uh an optimization password do which is to eliminate this kind of statements and this is pretty common in compilers as well uh the usual compilers outside of blockchain so let's say you're I don't know C plus plus compiler Etc they all have passes in the llvm do this kind of things so yeah here everyone is to delete this and just return the viral file one yeah so this is the quick example of what an optimization password do okay let's delete this oh cool let's say I know my presentation and I've actually shared some resources for Viper if anyone is interested but again as I mentioned my assumption was most of the people here would be familiar with Cairo but yeah there are plenty of resources on Cairo actually and I would say they are even more available than Viper which is kind of sad for me but yeah it's a much bigger ecosystem so you shouldn't have any trouble uh finding chiral resources yeah so with that I will end my sharing you know pass the time back to qichung thank you very much Kerry for the amazing talk so right now we'll go into our q and aim session and to start things off no I would just ask Gary uh how did you uh what got you into ethereum how do you get to know about ethereum and the tree and you know what got you interested in it yeah yeah I think I had a pretty similar journey to any non-death I was sucked into the Ico boom in 2017.

and shortly after that I started working as a lawyer for a while and at my Law Firm I was heavily involved in financial regulatory so Financial regulations so I had to do a lot with clients that were in crypto or the first wave of crypto and if you have if you follow regulations you will notice that regulations are incredibly slow to react to technology and rightfully so because legislation should end front run developments you cannot force you but it's going to happen but as someone who got increasingly interested with the tech it became very frustrating so that's when I started to do and learn stuff on my own to eventually get to the technical side or the dev side and yeah the past three to four years is basically a slow journey into where I am today so I finally took a plunge last December uh did some internships with never mind Etc and then now I'm working full-time in crypto and my day job is building a new uh synthetics protocol on starnet yeah that's uh my journey over the past five years that sounds very exciting I I imagine a you know it's a very big career change from you to from a lawyer to a developer yeah definitely definitely okay so uh everyone feel free to put a question in the chat as usual so uh right now so a question from the chart is uh how's the road map like for viral well there isn't really a roadmap so if you are interested in contributing you can just reach out to me on Twitter or Telegram and then we can talk about it uh so this is a site project for me and I'm also I consider myself a young programmer so I'm also learning a lot of things on the way so don't be don't feel intimidated or anything because yeah we all start out that way nice so one question um that I personally have is uh so with the um you know the upcoming roadmap for ethereum the idea of ewasm uh what's your thought on you wasn't as you know it's meant to be so to say the thing that would enable people to write in the language that they want and have it work on evm by transpiling it to ewasm so what's your thought on it and how do you think it will maybe you know relate to viral as a project uh honestly I'm not familiar with you awesome but I do know that this uh Endeavor has been going on for quite a few years maybe on and off so I don't think even if it pulls through that it will replace the uh the position of solidity and Viper because if you look at if I want to find something on solidity any resources there are so many of them now and it's really difficult to overcome this first Common advantage and yeah so even for other languages or smart contract based languages I think it's really difficult to uh gain traction but the other cable is I don't know much about he wasn't so so we can't really answer your question well but yeah it does make sense that a lot I mean since we are so used to using solid DN and viral it's very unlikely that a lot of people that are really familiar with it will um you know just suddenly give up on it yeah I do agree on that as well so uh how does the champale work for you in 256 so um how does you know when when transpiling how do you deal with the un256 type especially for Cairo since for Cairo it's a library you have to import so how do you convert that from viral to sort of from Viper to Cairo yeah that's a good question and uh the the technical answer is whenever there's a uint 256 we wrap it in a function call because in in Cairo the way you define or you're in 256 is this almost like a function call basically you do I'm just going to type somewhere here in Cairo for you in 256. he would do something like this low equals I don't know one high equals zero Maybe and if we think about it from the Viper ASD point of view this is actually a function call with a optional argument or quote low and another chord High and what this means is when we transport it we in Viper this would be something like X U into 56 equals one yeah so this would be the equivalent code that you would see that is transport and un256 they introduce a lot of friction actually because when you perform any arithmetic operations or add subtract division or multiplication you need a custom library to handle all this sorry not a custom Library I think stop net has built-in functions but what viral does is it uses a built-in Library sorry not a building Library I created a new library to handle during 256 operations but it's in substance very similar to what the what team did which is to handle uh during 256 operations with their own chiral functions and I think that's uh the most elegant way of doing it you cannot replicate evm semantics directly in uh Cairo simply because of the difference in the base data type which is felt and yes up to 251 bits or 252 bits I think 251 bits whereas in evm the base data type is 256 bits yeah got it so some questions for the comment section um so what's your top three learning tips for a non-death ones to start coding yeah there's a it's something I used to ask myself actually when I'm picking up something new and I actually only have one tip which is to work on your own projects or work on or contribute to another project uh I think that's the most um impactful thing that you can do for your learning which is why I keep trying to build stuff on my own because it really forces you to use what you've learned in a way that is practical when I started learning coding I was doing all this causes online but once the course ended I didn't know what to do and eventually those knowledge that I had became Rusty so if you can find something that you're interested to work on on the site they'll be really helpful to your learning and just keep finding small things that you can add on to improve Etc yeah I think that would really help a lot thanks for the amazing advice um so um are you only using viral when working on your synthetic project on stock net or you still need to understand other languages too right so for my day job I'm using Cairo mainly for the smart contracts and then the the development framework we are using just python or pure python without any external packages for now so yeah I don't use viral at all for my day job also because there are no Viper Smart contracts involved because but even if there was uh I wouldn't use a transporter for production project and uh the reason is transpowers are good for learning but the code is not very readable and if you are working on a project with multiple people you want to build a code for maintainability so the others people other people can understand and new joiners can understand so in that sense I don't think a transporter is fit for most production use especially if your project gets building uh but it is very useful for understanding the semantics if you are coming from a different language or if you just want to get something up quick and fast yeah that's my review on or my view on at least Bob and viral so to uh extend on the previous question like your answer on transpiler a bit so uh something like typescript it's essentially a transporter from to JavaScript and it's it's one of the I would say or more production really transfer do you think that what or Viper would one day have such a use when it goes stable or with it you know in your opinion would it always just be a very good entry point I think it's a bit different because typescript it introduces its own syntax so in that sense it's actually closer to a compiler than a transporter so for example I think there are so in comparison to JavaScript I think typescript enforces you to define the type of each variable right and it actually does runtime checks sorry not runtime checks compile time checks on these definitions so if you declare something of this type and it's wrong if I'm not wrong typescript virtual and error when you try to compile it so in a sense typescript is actually more of a an entirely new language that is built off JavaScript and it's closer to a compiler so it's quite different from a transpiler actually got it so one question that I also have is that does what handle implicit argument like viral if so what will be what is their strategy as compared to variable I think warp is more intelligent about the implicits actually but I I can't confirm that because I did manage to look into that while I was working on it yeah but I don't think there is a cost to just including the buildings because if you don't use them the pointer is it Remains the Same yeah I might be wrong that I don't know Justin has a question so was there an interest to make viral more production ready yes I'm not really sure what the question means by coding viral on a lower level language because the limitation is not on the machine running the transporter so yeah it's not a limitation of speed or memory so how about like any interest on making viral production really or do you think it's I mean I think you answered this already but yeah yeah I think probably not unless there's some organization that's willing to take on this effort because you will need it to be formally verified and everything or your course production really in crypto means a really high standard and I think what themselves are undergoing formal verification or maybe it's completed um I can't remember yeah but I as a side project I can never give that level of guarantee for sure yeah you for the answer yeah open source project it's it's not easy to maintain them especially if you are alone or if you don't have a team a lot of the current open source project right now does have like quite a big team if you look into like more production really ones like Babel or I mean what else yeah actually a lot of free mode as well is usually maintained by an organization be it non-profit or you know big companies like Google so you know everyone who is interested in the project or like to help you know even not even not you know to production ever already but you know make this project something that's um even more amazing that it's in the early it's yeah so yeah yeah do check out Viper if you check out my yeah guys do check out if you are interested in contribute to Yahoo contact us so uh one question that I have is so right now um Cairo is constantly having a change of syntax especially with you know Cairo 1.0 coming up yeah as well so do you have any strategy that you employ in order to add it quickly to this very rapid change um not really actually so there there are some blockers the first one is the viral users it framework for its testing so whenever there's a upgrade in Cairo and stocknet then we would need to wait for it framework to update their plugins before we can perform that update to the new version and in terms of the chiral syntax itself you can't really prepare for it because uh they could introduce breaking changes especially with 1.0 coming out and yeah you would just have to adapt the transporter when that happens so there are some things that are very easy so when we move from Cairo 0.9 to 0.

10 we introduce Cairo introduced curly brackets and there were some changes in the syntax like you don't need to use the keyword and to terminate uh if condition or error message so this kind of synthetic exchangers are very easy because we are just changing the module that prints chiral code from the modified AST and a lot a lot of this printing is actually hard-coded like the curly brackets are hard-coded right it says where you put where you place them in a code uh and analogy is the LED keyword in every assignment that's hard coded so this kind of syntax changes are easy to handle but then you have more difficult uh changes that for example introduces a new keyword so for example 0.10 allows you to chain conditions together using end so that wasn't possible before and yeah it's something that will require more work to support this kind of stuff hey thank you yeah the Cairo changing syntax the actually from what I know the 0.9 to 0.10 change causes a lot of things to break and I think last a few weeks before this I tried on a bit on board and the syntax is actually still on 0.9 as well not sure if they updated it so yeah like syntax changes are always a pain to deal with I think for a lot of people I guess yeah it's unavoidable because we are early on stock true it's a good thing right we are early by the same time you know it's exciting but changes are a lot more radical yeah yeah definitely so uh Justin asked what's the biggest challenge facing viral right now there are actually multiple challenges so the first one is so few people know Viper that I don't know if anyone has tried using viral uh just to transport anything other than me I also try yeah it's it's working oh that's cool yeah so but definitely not as many people know about Viper as compared to solidity so that's one challenge uh the second one is um just the effort that may be needed to maintain it and also to support the features that we want to support primarily because yeah this is just a side project that myself and maybe a few others contribute to in a free time so yeah I would say those two are the biggest challenges I can think of but thankfully one thing that I should point out is the Viper compiler is the Viper language and the Viper compiler is a lot simpler than solidity so because of this it is easier to work on viral if you have a good understanding of vipers code base but of course then again it goes back to the same question right which is not enough people know about Viper itself uh but if anyone is interested just try taking a look at solidity compiler or Rebel and fibers compiler report yeah it should be I I personally find that the Viper one is a lot easier to navigate and to understand let's go back to solidity I should probably also check it out yeah um any more questions from the audience there's no more questions than I think I'll pass it back to chicha yay thanks wankang for the nice Q initiation and Augustine Theory I personally think that this is a very definitely talk so thank you again I it's very very nice very nice I think all I've always enjoyed and yeah um glad to see you everyone is here we do have a couple of uh uh sorry I got heck of my time we do have a couple of workshops coming up soon the closest one is happening this Saturday actually uh in somewhere in Petaling China it's going to be in real life we have a CK we actually kickstarting our ZK study group so for those who are interested in joining us this Saturday uh in real life a feel free to dress DM me uh personally or wherever you can find me you can feel it near me and I will add you I'll give you give you more info on that and next week if kale group will have like a Defcon watch party it's gonna be on Wednesday at 8 pm to 10 p.

m it's going to be at AWS office in Mid Valley so you might you may also want to check that out uh and then and all all events are free and yeah we we are still planning on the next uh in real life Star net Malaysia Meetup so if you have any idea uh feel free to share and that that is going to probably be next month uh considering that we are we have we have a region pack minor already this for this month so that's all from me and I want to like thank Gary again and and yeah uh that's that's all we have and I hope to see you guys in the next one cool thanks everyone for coming yeah and thanks again bye-bye so I'll start recording but I'll be still be here for for any questions and other stuff

Automatic transcript — names and jargon may be misspelled.