-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add arithmetic host functions for i128 and u128 #697
Comments
We didn't do this in our first pass with {iu}128 because at the moment at least all developers are using Rust and {iu}128 are widely available in the guest, and when we profiled arithmetic on the host vs. guest, it seemed like it was actually faster to stay on the guest in these cases -- a u128 add is less work than making a host function call. But it's kinda "on the line" and I can see the point that some users might prefer to have the host do them (and this might apply even more firmly when we add {iu}256) so .. this seems reasonable to pursue, if perhaps not the highest current priority. |
Thanks for the quick reply and for considering this request. From my perspective it is like this: I am implementing it with AssemblyScript. For example, I want to add two i128 values: What do I have to do for this?
If I could do it in the host in comparison, it would be just one host function call ( So, in my opinion, it depends a lot on the use case. Therefore, being able to choose depending on the use case would be very valuable in my opinion. |
The "custom logic" is built into rust -- i128 is a standard type -- so you need not do anything for 5. While you're right that for a single arithmetic operation in isolation the cost of transfering to guest and transferring back is larger than the savings. But what I'm saying is that if you do much more than a single operation -- if you do say a couple compares or a few adds, multiplies or subtracts -- it's cheaper to do guest-side and bracket the guest operations with loads and stores to the host. I'm not 100% opposed to adding functions here (there's space to do it) but .. we can certainly survive without for a first version, and I think the only cases where it's a win are so small (single-op cases) that in a real contract you won't notice either way. Unless you have dozens or hundreds of contract functions that all do exactly one arithmetic op. |
If I understood it correctly, the reason for this request is that you are trying build a WASM contract with AssemblyScript, or build the AssemblyScript SDK, and there is no native support for i128 in AS. Given relative narrow use case of AS (and the broader use case of supporting other SDK types) we will move this to post launch in order to better focus on the Rust and soroban-sdk use cases. |
@christian-rogobete can you use a 3rd party library like as-bignum to do arithmetics guest side? |
Hello @jayz22 and @tomerweller, As for the 3rd party libraries it is of course possible to use them. The as-bignum is currently the only one I have found. It works at least partially but not completely in the context of Soroban (e.g. you get - Status(VmError(Instantiation))- errors when calling some operations). Furthermore, many operations are not implemented, especially in connection with i128. Moreover, there has been no further development of the library for a longer time. I integrated some operations into the as-soroban-sdk (mostly ported from as-bignum) that currently work well in the context of Soroban. For example: add, sub, mul, div, muldiv, sqrt, pow, eq, lt, gt, or, xor, and, shl, shr and others. They make it easier for developers to handle u128 and positive int128. Since soroban preview 9, the high words of i128 are now i64 (were u64 before). Thus the as-soroban-sdk can also be extended in this respect. Developers can alternatively implement operations themselves if they need to use them intensively. However, I think it would be very good to provide host functions for this, since these are certainly more reliable and in my opinion sufficient for most cases. Self-implemented operations could lead to various problems such as overflows or they are not implemented performant enough, so that in my opinion it would be better to use reliable host functions. |
If we're building a system that is intended to support a variety of languages building for WASM, then I think we should probably lean towards adding host functions. And given that Rust itself doesn't have {iu}256 types, and yet we provide those types, it feels even more like we should do this for both types for consistency. This will probably be important for folks who are building contracts that interact with ETH and the ETH ABI. |
One of the troubles with the arithmetics host functions is there are too many of them. For i128 alone there are a few dozen unique methods (deduping That's the reason for @christian-rogobete I'm curious from your perspective, what would be the minimum required set (and what would be nice-to-have set) of functions for AS support? Also it would be nice to know what causes the |
Does this result in a higher vm instantiation cost for all contracts, or just contracts that import (use) those specific host functions? I expect the latter. If it's the latter that doesn't seem like a problem. |
From what I can tell the linker needs to go through all the host functions and define them for the wasmi module. |
@jayz22, from my perspective, the ones that were offered earlier for Furthermore I think that
Another one would be As for the
Such special imports need support from the host environment. See AS special imports. But this can also be circumvented by implementing a special However, there are still problems, especially regarding working with strings, which I can't understand. e.g.
I have documented this before in this issue. Hope this helps. |
Thanks @christian-rogobete, this is definitely helpful! |
What problem does your feature solve?
Developers who do not use the Rust SDK must implement arithmetic functions like add, sub, mul, <, > etc. for i128 and u128 themselves. This is difficult, error prone, and unnecessary since the data ends up back in the host anyway. It would be great if these functions are offered as host functions.
What would you like to see?
Arithmetic functions as they were offered before for bigint.
What alternatives are there?
The text was updated successfully, but these errors were encountered: