pyteal icon indicating copy to clipboard operation
pyteal copied to clipboard

ABI: Extended math support - signed integers and fixed point numbers

Open jasonpaulos opened this issue 3 years ago • 7 comments

Summary

Add support for the following numeric types to PyTeal:

  • signed integers (using two's complement)
  • signed fixed point numbers
  • unsigned fixed point numbers

Scope

Ideally this would replicate most of the feature described in https://github.com/algorand/pyteal/issues/181 for these new types.

Note that signed integers and signed fixed point numbers are not ARC-4 ABI compliant, so we should not allow public ABI methods to accept arguments or have a return value that uses these types. But they could be added to the ABI in the future, and in any case they are still useful internally.

jasonpaulos avatar Feb 02 '22 17:02 jasonpaulos

Looks good!

CiottiGiorgio avatar Feb 02 '22 17:02 CiottiGiorgio

Leaving pointers to branches making progress towards the story's request:

  • https://github.com/barnjamin/pyteal-utils/blob/fp-class/pytealutils/math/fixed_point.py
  • https://github.com/CiottiGiorgio/pyteal/tree/feature/int

michaeldiamant avatar May 09 '22 13:05 michaeldiamant

In this fork, we can see an initial implementation for signed integers. It is leveraging Uint implementation with some minor issues. Namely:

  • When setting a signed integer starting from a generic Expr, the result must be checked for valid range. This ends up checking for < 2^bit_len in Uint implementation and for < 2^(bit_len - 1) . This is obviously correct but redundant.

So far most ABI types can make use of native instructions to provide correct results and that makes their usage in contracts trivial (.get() to extract and then use). This will not be the case for both signed integers and fixed point numbers. There should be a way to have "methods" or operator overloading for ABI types such that we can make their usage transparent to the particular implementation of an abstract type.

P.S.: The ideal name would be Int and IntTypeSpec but I didn't want abi.int.Int to clash with ast.int.Int. I find SInt not to be the obvious name for this. Name suggestions are much welcomed.

CiottiGiorgio avatar May 26 '22 09:05 CiottiGiorgio

Since Int's in pyteal and teal are really uint's by default, it seems reasonable to signal the opposite with a name such as Sint or even the more verbose SignedInt

tzaffi avatar May 26 '22 15:05 tzaffi

Added some implementation for fixed point numbers. Not much to show but feedback is appreciated.

CiottiGiorgio avatar Jun 03 '22 11:06 CiottiGiorgio

Added some implementation for fixed point numbers. Not much to show but feedback is appreciated.

Thanks for this work.

How would you like to receive feedback? If you open a pull request, even in draft mode, this will facilitate having a discussion and it is easier to point to specific code in the PR-mode. Other forms of discussion could work as well if you have something specific in mind.

tzaffi avatar Jun 05 '22 18:06 tzaffi

Good point. I have opened this draft PR. Thanks!

CiottiGiorgio avatar Jun 06 '22 11:06 CiottiGiorgio