Learn to write super-duper gas optimized contracts with Huff [workshop] by Clément Lakhal (@clemlak)

ETHDubai 2023 · 15 Mar 2023 · 51:51

Clément Lakhal introduces Huff, a low-level assembly language for the EVM. The workshop uses its features to write gas-optimized smart contracts.

Speakers

Topics

From ETHDubai 2023, hosted by DeFi Dubai.

Transcript

hello everybody hi okay hi everybody I'm very happy to be here today to talk about half so my name is Clement and I'm a smart contract developer so the first question I'm going to ask you today is uh what do you know about half have you already heard about it or just something completely new for you okay looks like we we have a lot of beginners so it's going to be cool all right so the question that you should be asking yourself right now is what the hell is

half so basically according to their documentation health is a low level Assembly Language for the evm so this might be a bit confusing a bit obscure so what does it what does this means exactly so basically when it comes to programming languages for the evm for the ethereum virtual machine you have different choices and the most common ones are either solidity or Viper so basically these languages are considered as high level because they are going to take

um they're going to do a lot of work for you when you're coding with solidity or Viper you don't have to think about managing the memory or managing the storage they are going to do everything for you all of these teachers things but we do have other languages that are lower level so for example we have inline assembly which is basically the facts of including Assembly Language within solidity smart contracts we have Yule which is pure assembly that can be used as a standalone to write smart contracts and then you have more fancy staff more fun stuff for example have with get to

that in a bit and mnemonic bytecode so let me very quickly show you some examples so I'm sure everybody here should be already familiar with solidity code so very easy to understand inspired by JavaScript or c uh we do have also Viper which has a pythonic syntax also very high level and very easy to read but then we get into some more complicated complicated but also Advanced languages that are offering you more features for example with inline

assembly you can manipulate the storage directly or um the the memory and you can do that within your solidity code then we have what I've been talking about it's not very readable but that's okay we have Joel which is basically pure Assembly Language that you can use and that you can compile in the end using the solidity compiler and then we have what we are for here today they have language so yeah it looks a bit complicated like this but don't worry we will get to that

just after and you will see that it's quite easy to to understand and just for for fun I'm just showing you so this is mnemonic byte code so we are using directly instructions here and values so very unreadable um and this is just bytecode same thing as the previous one actually but all the operations are actually in hexadecimal values so now you're probably asking yourself why the hell would I ever want to use half because it looks complicated it doesn't look easy to read um well to answer this question I'm just

going to give you small some small history about how half was created so Health was created in 2018 by the Aztec team basically they were working on the new small contracts that was Computing elliptic curves very complicated algorithm something that was used previously in zedcash and they wanted to implement that on the evm but neither solidity or assembly was able to give good results in terms of gas so they just they thought about creating a new language and here's how the original have was created

so looks very obscure very esoteric and actually yeah it's quite esoteric because Aztec said about half so half was designed with a complete disregard for semantics safety or basic consistency any similarities to production code or probably incidental and have should on no account be used by anybody but this was before because what happened recently is that last year a group of very very smart and talented Developers decided to reboot half so what they did that they took the typescript compiler

and this decided to make a brand new compiler written in Rust they added new features they've created documentation they decided to fix a specific Syntax for the language and so yeah like the new version of have was created um and so here's a an example of what we can do today very simple contracts uh we'll get to that just after but yeah I have recently got tractions especially since the yeah the beginning of the year because all the optimizers on Twitter were talking about

it and also recently I think two weeks ago or last week they started accepting donations so yeah they have many things planned I have the roadmap here so basically yeah now that they have fundings um they are going to work on new language features uh new grammar uh many things something quite interesting also the evm object format support um and yeah like agathon workshops very cool stuff and also our EPL so yeah we'll be able to have fun with with her so now I gave you some small details

about the history and how it was created and what is it today so let's get into it and I'm just going to give you a small introduction so what can we do with her so basically I have is a smart contract um programming language so the goal is obviously to interact with other contracts and to be interacted with so in half you can declare interfaces for example for functions for events and for errors so it works the same as more or less the same as solidity would do and what this will be used for later

that you can generate a signature of your function for example and you can use that in the code later and also this you can generate an API in solidity that you can use for example for testing or touch things one very good feature of have um basically is Jump labels so I'm just going to give you an example if you tried by any chances to write pure byte code by hand at some point the way it works is that when you want to jump somewhere to your code you will say Okay I want to jump to the instruction number

22 so you will put 22 and then jump and you have to be sure that at the position 22 in your bytecode there is a jump destination but if at some point you decided to change the bytecode then you will have to manually change all the destinations and to change all the the places where the jumps are happening so this is very tedious and obviously in half we have something quite simple for that basically what we can do is that we just say okay here I want to jump to success and it's just going to say okay so success success is here I want to jump so I'm going just going to go here

and execute this operations so this is a very important feature of the of the language then there are some more standard features such as constants for example so you can Define one constant variable that you can simply reuse in the code later interesting aspects also about have that you can Define macros so it works a bit the same way as like the the jump basically at some points you can decide to jump to a specific macro

so you can Define it this way so hashtag Define macro you can give it a name and say if you wanted to receive stack variables or return any stack variables you just put your code inside of it then just going to execute this so at any point in your code you can just call your macro and for example from here and then it will jump to the macro and so the way it works is that when your contract is compiled every time you're calling a macro the entirety so the full macro will be copied inside of your code so for example if you want to have a very small byte code in the end then it's more

interesting to use functions because the way of functions or working in that functions will be copied at the end of your byte code that you compile bytecode and then every time you're calling this function we are going to jump to the to the function execute it and jump back where we work so have also comes with a lot of very handy features built-in functions actually so for example let's say you want to get the signature of a function then you can just use this so you put 200 scores and long signature and then either you put

like the actual signature here the actual function sorry or you can use also the D file that we saw earlier so let's say you define the mean function then you can just put Min here and this will generate the function signature so this is used many times for example when your receiving a call in your smart contract you want to know which function you are supposed to call so you're going to use the functional signature for that it's the same thing for events when you want to emit an event and here so these functions are very useful because before we were copy pasting the function definition in a tool and getting the

hash ourselves and copy pasting back from the website to the code editor so it wasn't very handy so yeah this is just an example with the error like we can Define errors custom errors and then we can just use their signature here so when you are using the error function then just going to to get the signature videos

with basically it shows the way to informed churches by children so I think so after showing you about the features on this language the right placement really in their relationship at least I imagine if for health and the first one is a professional purpose so this is

very loading myself people foreign foreign

100 so basically you should mentioned the students don't make contact just like their supporters you can save your life home and very nice as I think so this is for the technician what you actually calling uh so that's kind of things no yesterday

for example if you think of things interesting to have contracts because a positive team requires specialist Library so for example let's make that I want to

do the Early Education is definitely academics at least not practical operations coming into the library stand below employment technology the fact that you have to do ideas not just in Christophers foreign

[Music] of so this map just creating another 32 uh so if you want like the the University of Pennsylvania so I don't know because also another information I'm very many

husbands versus the idea was to Intimates for today or later standards but basically if you have creators like a relationship changes as part of the track to have so they take more to make like a yes but it's very nice to somebody specific for example there is one contract starts with Library uh

because yeah a lot of things but uh foreign still but it's available right now is the

thought we have and it's like one train here we will have fun tonight and then we can just disappointment to the security problem so it's very unicient they immediate it is easily um yeah so but just wait a minute so basically 18 years it's fancy

language because the the features that all the most needs it's a schedule using to your writing well I don't resources it's very interesting but whatever yeah we can try to write some guys

so okay I'm just going to I'm just going to Gmail is directly have a project together business ES so I'm just going to show you this

okay so we specific people yeah this is a direct employees and it's like a religious uh projects so just like just particular entire ly I've already told myself at least on yours for your practice so it's like two moments basically this is the installation of our Foundry

and this is [Music] resources that they do foreign [Music]

foreign says w

inner and this is universe so as I said when your company from church like this you can't be listening to either confused the signature of a function as if you have contract vegetables like so

here we have different kind of constants variables in your health because we are in a little something to do you know according foreign so what do we have about what we're

going to do with different reasons and we have our variable what we're going to do that they are going to write down anything happen those important business so we are just going to enjoy yeah yeah you can just use platform okay and by popular people eat them the radicals only once [Music] like function so by myself two times

so so okay so I know exactly but the way it works so I'm going to hold the there's no chance but it's going to happen because I'm going to choose to directly means and I'm going to take their first four lines of this

hash it's called this position what is good how about with them especially watches [Music] that they really have the actual development that's important to say yes I'm staying okay we won't and we want to do this after the first place

so we're just going to remove Fortress Adventures then this is survival track hit me out of control track it just looks like you know eyeliner tattooses if you don't have anything that so we can talk about it and then what you are going to do this we said okay so um

but if you want to spell it so to stop it and you have to know what what is the location is the variables here I still appreciation and this will give us the population so yeah average where do they want to stop this island before I decided it's called our article

to disturb them you [Music] know maybe we can't wait I think she's really very excited synonyms because that we want to be able to feel just go do it once it's actually what is foreign [Music]

uh here we're just love you for the Bible and today everyone is supposed to happen that's here we have the right advisors according to the stories similarities so we'll get just continues at the versus even existence and the first one in the world series anniversary store these guys do at their positions please

okay you want to deteriorate so thank you just a few images so we created much smartphones why would you just stay at the value and we are going to police but our it's not a culture it's just two cultures so that's what we're going to do we're just going to try

it so it will inherently this members this is a backflow level so it is going to happen we are Republicans foreign [Music] [Music]

this essential nature but also for breakfast so what we're going to tradition Ally that's what we are going to remove their lives that's interesting so imagine that you have you know like your work on that value it seems like smiling we have this so we are just going to move everything loser values LT is like it it's like uh the

bedroom or cheers and it's quite very simple here actually the signature of State value then it yourself or whatever easiest foreign and in this case either

the hard way because we talk to internships so this is like the kind of lost basic experience but what you can do this successful but I think it's actually very interesting all those musicians released on the exactly ages and you know I had to make different places for the Charities so yeah it's going to be important because do you have any questions so it's basically the way towards the

tests to be able to return information so yeah this is really important information any other questions okay I do have another example in physics teachers want to change the sounds so this is something about this morning is way safer yeah I think it's it's quite

obvious doesn't matter because it shows you like about communication are getting values and in Victoria Celsius the goal of this modified is is very basically you can listen to our leaders you can add them in terms of value so is this interesting as long as successful as ever we are just like yeah defining our functions so it's important countries and this options [Music] are different things

we have our conscious examples so in this case I was thinking about what will happen by the chairman and then 32 nights for the University and every episode [Music] so I believe and what is a good population here that is closer

uh just the hour about water very soon Supply Company for next to the medicinal so we said okay I was the the Kolkata but I know all developers call lies because I went to the functions naturally and it totally leads to that because I know this is all decision so here gives you have to keep our staff we will

have that here in the first one features and then we have this example of our district so what will happen if you want to do with that because once you do like [Music] that so just to uh and what is going to happen I just need you to make one that's already available and so we have five okay I have five what do you want you to think so let's updates in the memory and the position zero it was analysis

so his name is below five and then yes this is better than what is so this whole journey is quite soon here inside of our main battle is asking exactly this is like as before qualifications so we get a beautiful fantastic we pushed at this

unfortunately we check it's really a lot of physical changes in Italian so yeah another type of example of what we can do quote quite similar I guess you can just imagine it's for example but basically so the thing is that is when we were using their last time you talked about three

hours yesterday oh this is where this website is please convenience and useful because you know exactly here what do we have a nice person we need to use that and what is going to be I mean so for example uh so this is so for example in the previous News Network shows you the ads conversation with them so yeah are you interested in it's like

academically put them together and they were not only our thoughts and if you can also lots of produced interest for example like sell help developers or uses it's basically functions module yeah we have new questions foreign a little bit

so then if you want to alert us um like what is our quotes about doing things happening so sometimes you know when you have three words you have like they want you to help them exactly items because again 2016 teachers um [Music] but foreign uh you know

an Instagram they do the season just had them ever made exactly the storage individual voluminous that elevated so it was like they're they're even like that very nice positions so yeah I didn't think this is used to basically too it's going to learn more about half we can help with everything else um visit the tutorial that I'm going to start with the the laptop that's enough that's actually really nice

they're project table yeah to practice and you have an example this is says I definitely so again this is our do you have any special wishes that makes sense but basically let me

know your business have the overall athletic University issues and so it's literature is making the children that's all different form that you have this if you're happy to have if you are very social security if you get overwriting it like to take and obviously you might get just during your

test so if I have this very much very much yeah this um list of the resources because you have a couple of examples of static um I think it's quite there is something that it's quite a completely rise

it takes a lot of time to go in there it's very risky and I never heard about the politics here audiences Health contracts so I heard some people were like into 3D s that I'm touching and I don't think that it's unrated maybe things used for readers like parts foreign [Music]

music foreign thank you