-
Notifications
You must be signed in to change notification settings - Fork 125
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
System/standard library for bignumbers #189
Comments
I'm not sure the The name The naming conventions are not very consistent everywhere, but I believe there are 3 options:
|
I like the pedantic option. |
I think we should add all 256-bit methods to the host interface: We could argue that some of them are fast enough if implemented in Wasm, but the secondary goal here is to reduce code duplication and it helps in that. Any comments? |
Personally I feel like it would complicate the design for the bignum library to only contain the arithmetic methods that are not fast enough in wasm. Reducing code duplication is definitely another big benefit of having a "complete" bignum lib. As long as we can minimize host function overhead, this is great for reducing contract bloat. |
Potentially we could make use of the references proposal for Wasm. Let assume the following example is for fixed 256-bit long bignums:
This assumes that opaque references (what |
Let's agree on this first basic API, which isn't using a bignum stack:
|
How about the first argument is the output. so output position is consistent for different numbers of arguments, for example |
I thought about having output as the first operand, but wasn't sure of the merit. Some POSIX functions do that, but find some of them confusing. What would be the benefit we gain? Note, I expect this to change during the upcoming months. We also need to make a decision whether to replace (or add) a stack based version. |
Convention. Gnu Multiple Precision and lots of crypto pass output first. But it doesn't matter.
Not sure which algorithms benefit from the stack based version, other than pathological benchmarks. But it may be simple enough to implement -- |
I agree with @axic here. |
This actually go against many other conventions, e.g. Go and other OOP designs (to some distinct): https://golang.org/pkg/math/big/, RISC assembly, C calling convention. Also |
@chfast can you clarify which convention you referred to or which one you prefer? |
I prefer the outputs to be the first arguments. |
A couple more discussion points:
|
Makes sense to return a carry. It may also make sense to have an extra argument for carry.
It should. Modular multiplication is a bottleneck for elliptic curve crypto.
@chfast wisely said that anything bigger than |
We kind of agreed to have another set of methods with carry. |
Proposing this slightly modified API:
Note: these changes only make sense if the overhead of additional parameters and return values is negligible. |
Carry for |
@chfast Thanks for the input. Removed carry for |
EVM supports 256-bit operations "natively". While that precision is not needed in a lot of cases it does come as very useful for certain operations, such as handling token balances, in Ethereum.
With wasm one would need to include a bignumber library to do that, potentially in a lot of contracts. This suggests a good opportunity to define a standard bignumber library, which can be used by contracts, but it is not mandatory to be used.
Due to the design of "host" functions in wasm (having a separation of namespaces) it is possible to design this in a forward compatible manner, but implement it right now in the clients, such as Hera.
Lets define a new namespace
stdbn
(jk,bignum
orstdbignum
) with the followingmul256(a: i32ptr, b: i32ptr, out: i32ptr)
mulmod256(a: i32ptr, b: i32ptr, mod: i32ptr, out: i32ptr)
All of the arguments are 32-bit pointers to a memory location containing a 256-bit little endian number. The pointers can overlap or be the same.
In the future this could also be implemented as a (system) library: #17.
The main benefit of a "system library" is the predefined address it lives at and the client's ability to make a decision whether it uses a wasm blob or implements it natively.
The text was updated successfully, but these errors were encountered: