Higher Precision With Larger Numbers, EVM | Oscar Serna Rosero - Thrackle
Ethereum Denver·Thu, Oct 9, 2025, 12:00 AM
a new library offering up to uint1024 support that includes novel mathematical methods for achieving high-precision calculations with very large numbers and low gas consumption. We will discuss interesting Number Theory concepts, identities, and other cool techniques that we used to unlock efficient high-precision mathematical operations in the EVM. 🚀 Get Ready for ETHDenver 2025! 🚀 We're already hard at work preparing for next year's biggest Web3 event! Keep your eyes peeled for more info on ETHDenver 2025—it’s going to be epic! 🌟
Transcript
[Music] today with you guys my name is Oscar Cerna I'm a lead developer at trapse which is part of the Forte ecosystem of Partners and today we're going to be talking about higher Precision with larger numbers in the evm and I'm going to start this talk by spelling the T $2.2 billion were stolen from def5 protocols last year alone and a good chunk of these uh hacks were due to uh Precision issues in the math for example the onx protocol of September of last year got hacked through a rounding issue of a very small number that uh was magnified by a flash loan and that's how the hacker got to steal 3.8 million you can find many many examples like this like the raft protocol that even went through many audits uh with top-of-the-line Auditors like 12 bits and hat finance and they still got hacked so going through audits is not a guarantee that your that your math is safe um that is just to say that the Precision in the evm is Paramount but don't worry we have been working really hard to make the evm a safer place for math dependent applications and that's why we're going to be be talk going to be talking today all right so who are we um I mentioned earlier that I'm part of thr labs and you can think of thr as a company focus on helping people to to build successful blockchain economies we're working closely with forte in this regard and as such we have been developing amm among other products and that's why we uh we have been so involved with math however what we have found is that the mathematical tools are out diso at our disposal are not enough to guarantee good precision and that's why we have been developing our own tools which is what we want to share with you guys today all right and as I mentioned we are part of the Forte ecosystem of Partners and uh what we do is we serve developers across the entire web three sphere from those working with real World assets and stable coins to defi platforms and gaming Studios ranging from independent developers to Industry giants like Zinga and Jam City all right now let's get straight to it this is just one uh one example of the math that we do as you guys can see uh it's a complex equation this is just C of n it's just one example however a lot of things can go wrong with the Precision in this equation there is a lot of things involved this is pretty much much um a hacker's dream come true right I I'm not going to get into details here because I don't have much time but just know that there's a lot of things that can go wrong here all right so this is just a refresher most of you guys already know this but what are the main challenges when uh dealing with math in the evm as you guys know the evm is a integer math Beast so there is no concept of uh fractional numbers and Sol pretty much is the same way right there is no concept of floating points or fix Point math in solidity we do have a fix Point type but it is not implemented yet so we pretty much don't have it yet right and that is pretty much where all of these rounding issues come from the fact that we don't have a way to store or Express fractional numbers in a safe way that's what hackers uh take advantage of to steal your money and another uh constraint of the evm is that it is a 256bit architecture which is um and the math in the evm is modular which is really cool and allows you to do a bunch of stuff but if your numbers start to grow larger than 256 bits then you have a problem however as you guys can imagine already some projects have been working on this and this is just an example of the state-of-the-art of the evm as it is today um we have a bunch of fixed points math libraries uh most of them work with 18 decimals some of them work with 27 decimals so L is just one of them and the fix Point math is the most popular way of doing math with uh fractional numbers in the evm we also have floating Point libraries like the abdk which implements a very popular standard in computer for floating Point numbers by the i e however floating Point numbers are not very popular in the sphere because because um most people think they are just uh imprecise or not safe for the for the blockchain and we also have uh libraries like the UN 512 which pretty much allows you to do math with uh numbers larger than 256 bits but smaller than 512 bits so pretty much what I'm trying to say here is that we have tried to overcome these limitations of the blockchain uh by developing these kind of uh libraries however uh what we found is that if you want to do crazy math like this um then these tools are probably not enough or it's going to be very hard for you to to have good Precision with them so that is exactly why we have been building our own Solutions and we want to uh give them to you today guys we have built two solutions we have built the UN 1024 and the float 128 so I'm just going to start with the UN 1024 and it is not worthy uh worth noting that this is 100% an integer math solution and the concept of this library is really simple just like the UN 512 Library this Library allows you to do math with numbers as as large as 1024 bits um we actually built on top of the UN 512 library because it uses very very cool math uh number Theory Concepts like Chinese remainder hense LMA Mongomery reduction which we extended we also implemented our own algorithms that we developed and what is very cool about this library is that it's very powerful when you couple it with uh fix Point math so why is it so powerful because this way you can store as many decimals as you want and your Precision can be very very very high even if you're doing math like this so all of the sudden um with the UN 1024 you can do a very very straightforward implementation of pretty much any equation that you can imagine all right um so let's go straight to how to use this Library um this Library we we decided to build three types as you guys can see uh these types are pretty much like the the sizes of the numbers that we can manage so you have the UN 512 the UN 768 and the UN 102 4 and we also are showing how to use them in this case we have the subtraction uh case you can use the struct version or you can use the unpacked version the reason that we have both version is because with the struct um you can this is this is the more usable way of of using the the library however it's also the more gas expensive way so we're not limiting you guys to do like one or the other you can just uh do what is convenient for you to do all right and real quick uh let's go to the pros and cons of this un 1024 Library um so as I mentioned earlier the pros are that it is perfect to couple with fix Point math libraries um it also has exact Precision in all mathematical operations and it allows you to do uh math with numbers that are four times larger than what Sol allows you to do out of the box however there are some downsides as you guys can imagine already um one of the downsides is the gas consumption the truth is that the larger the number the more gas it costs to do operations with it as is expected it is we have made it very efficient but it is still something to consider right another con that we have is uh with this library is that uh different types make it a little bit combersome to use in some cases and I'm going to expl I'm going to explain a little bit better this um so because so because you uh need some type in the inputs and you return you might return other types when in the output you're going to find yourself writing equations in a way that it's going to be like if you if you implement an equation that is like a multip by B plus C over D most likely you're going to end up doing something like a multip by B save the result add plus C save the result divide by D and the reason is a different type so if you multiply a un 512 by another un 512 your result will be a un 1024 and that's pretty much the reason that you have to do things like that all right so those were like some of the reasons why we also decided to explore other options and we decided to explore the floating Point solution and we created actually a a library for uh to manage floating points so a a lot of people like I say earlier are scared of the floating points in the blockchain however what we have found is that they have very very interesting properties that might be perfect for most applications out there so I'm going to go ahead and explain but before that just uh just some just a refresher of some uh concept of floating Point numbers we have uh a floating Point number pretty much is composed of two pieces one is the mantisa and the other one is the multiplier and here you have an example Le of one single number that is represented in two different ways in the floating Point format uh the mantisa in the first case is a minus1 124 and the other one is -1 1240 0 and pretty much the multiplier tells you where the the radic point is going to be located in the number uh the the multiplier is also composed of two different pieces one is the base and the other one is the exponent in this case both are base 10 and the exponents are minus 6 in the first case minus 8 in the other one okay so now that we all remember what the different pieces of a flooring Point number is I'm going to explain real quick uh what our library how our library was built and our library is called float 128 and it is base 10 the mantisa has 38 digits and this is this was thought to uh store as much information to have as much Precision at the same time being as much gas efficient as possible uh our our types are signed so you can do sign math out of the out of the uh the box and our exponent range goes from- 16,000 to positive 16,000 so you can do uh you have a huge range of numbers right here what we decided to do was to uh Implement two different types and I'm going to uh show this with the next slide if if you guys look at the image at the top left of the slide you're going to see two different types one is packed float which is a un 10 uh 256 under the hood and you have also the float which is a struct composed of two integer uh pieces one is the mantisa and the other one is the exponent the reason we decided to do this is pretty simple is because one is uh it's optimized for storage the packed float is optimized for storage since it's storing information in the blockchain and reading from it is one of the most expensive things to do we decided to create the pack float uh however with this type you have to encode and decode uh for every operation and that's why we also have the float which is the one that you don't have to encode and decode however if you want to store information in the blockchain it is going to be more expensive all right and here we're examplify uh the addition case and also how to pretty much convert from a fix point to uh floating point it is very simple you simply wrap your your number as an INT you use it to packed float and the minus 18 pretty much is saying that this is an 18 decimal number which is the most popular uh fix Point math that you can find out there and the way you add is very simple you just do the number do add parentheses the other number all right now what are the uh pros and cons of this uh Library so real quick it is very easy to use if you compare this with the UN 1024 it is actually easier and the reason is that there is no this there is no size or concept of size with this floting point numbers because the exponent pretty much adjust itself and so they all fit in the in the same type versus in the UN 1024 you have all of these types right another uh Pro about the floating Point number and this is like floating Point numbers in general is that they store they can store the information in a very efficient way again because of the exponent and a very cool property about the FL Point numbers and is that they are very precise for small numbers and I'm going to explain this a little bit better if you guys remember when I start the this talk I mentioned the onx protocol was hacked because a very small number uh had a rounding issue which was then magnified and that's how the hack uh the hack happened well it turns out that that happened because most likely they were doing fix Point math and with fix point you have the disadvantage that when you have a very small number you're pretty much already using most of your decimals to just store zeros right and so that pretty much any uh any truncation of the of the decimals is going to be magnified when you multiply with the floating Point numbers this doesn't happen because what you do is you always store all of your of your number with 38 digits in our case so you always have 38 decimals and you just adjust the exponents so most likely if the onx protocol were working with 4 point numbers the hack would have probably been uh avoidable right however the flip side is also true though with very large numbers fixo math is superior to floating Point numbers because uh you don't you cannot store all the information you need in a floating Point number what you do is just you ask like in our case you turn up to the 38th digit another very interesting property is that it's very gas efficient with large numbers and it is also in comparison with the UN 1024 um I mentioned with the UN 1024 library that the larger the size the more gas it needs to do operations this is not the case with the floting point numbers because again there is no concept of size you simply are dealing with the mantisa and the exponent and the exponent pretty much says how big or how small the number is so they all cost the same now the flip side is also true right if if you're dealing with normal numbers normal size numbers or or small numbers you can say that the floating Point library is actually more expensive because with very small numbers or normal numbers you can just use the math that solidity gives you which is very cheap all right so we have been talking already about some pros and some cons already now let's let's explore the downsides of this Library uh one of the downsides is that being a floating Point number it is not exact as integer math I put here fix Point math because under the hood fixo math is just integer math so that that is true uh however we have found is that the imprecision is pretty much insignificant also uh numbers with more than 38 significant digits we lose Precision that is a that is a case uh that is that is mostly related to our implement of the floting point numbers and the reason is I mentioned earlier if you have more than 38 digits you pretty much truncate it and you lose that inform extra information however again that information is considered insignificant for precision and uh the last point is something that I already explained it is not very efficient in gas terms for uh for for small numbers because you can do operation with small with for with small numbers we solidity uh very very cheap all right and I'm just going to mention this uh is an edge case I'm not going to go into details here but just know that if you're dealing with math subtraction is very very dangerous for your Precision the reason is that if you have a very huge number and if they're very very similar all of the most significant digits are going to cancel each other and it's going to leave you with the least significant digits which are prone to have the least Precision so uh actually fix Point math is superior to floting point math uh in this case however there are tricks that you can do in your equations to to make it safe also with floting Point numbers all right and as promised um we are we are making public our libraries we want people to start using them we're actively working on them as we speak and you guys are welcome to submit PRS to uh the libraries we are still maintaining them and what uh what's coming with these libraries right so with the UN 1024 we we still want to do more optimization in our algorithms and in the float 128 we still adding some functionality and we're also thinking about increasing the amount of digits that the mantisa can hold and going from 38 to 71 digits with to make it very very very precise like this is going to be like very very precise however it comes with the cost of more gas consumption so we're thinking of how to do it very precise but at the same time efficient all right and that's it from me guys uh I just want to remind you that the CTO of the company if you guys are interested on why we're using so much crazy math right well we're building amm as I explained earlier and tomorrow the CTO of the company Neil is going to be uh giving a talk about updating amn to boost St liquidity in a stable manner at Captain ethereum stage at 5:00 p.m thank you very much w
Automatic transcript — names and jargon may be misspelled.