Skip to content
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

Wide Arithmetic Support (128bit uint) #236

Draft
wants to merge 21 commits into
base: feature/uint128
Choose a base branch
from
Draft

Conversation

ahangsu
Copy link
Contributor

@ahangsu ahangsu commented Mar 7, 2022

Summary

Provides a set of uint128 (essentially two uint64 values on stack) arithmetic support from 5 wide arithmetic opcodes:

  • addw
  • mulw
  • divw
  • divmodw
  • expw

Closes #101

Testing

Pending, currently only local testing, waiting on e2e semantics test.

@ahangsu ahangsu changed the title WIP: Wide Arithmetic Support (128bit uint) Wide Arithmetic Support (128bit uint) Mar 11, 2022

class WideUint128DivU64(WideUint128):
def __init__(self, lhs: WideUint128, arg: Expr):
WideUint128.__init__(self, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahangsu Request for clarification: In the following scenarios, I think programs generated via PyTeal do not produce the intended on-chain results. It's quite possible I have gaps in my understanding. Can you double check me?

  • Use WideUint128DivU64 for subsequent WideUint128 arithmetic (e.g. +).
    • Concern: The result is 1 uint64. Subsequent arithmetic operations assume a WideUint128 produces 2 outputs (2 uint64).
  • Produce WideUint128 with outputNum=2, and supply the value as argument to @Subroutine.
    • Concern: I'm concerned only 1 of 2 stack values will be passed into the subroutine. I didn't fully vet the concern before raising. Trying to gauge if you've considered it before I go deeper.

After addressing the above concerns, taking a step back:

  • Is the intent to provide a general purpose wide arithmetic API or to only provide the operations needed to use wide arithmetic operations for intermediate steps?
  • It feels to me like the PR attempts to provide a general purpose wide arithmetic API. Though it's unclear to me that the goal is a general purpose API.

Comment on lines 85 to 100
# stack: [..., A, C, D, B]
TealOp(expr, Op.uncover, 2),
# stack: [..., A, C, B, D]
TealOp(expr, Op.swap),
# stack: [..., A, C, highword(B + D), lowword(B + D)]
TealOp(expr, Op.addw),
# stack: [..., lowword(B + D), A, C, highword(B + D)]
TealOp(expr, Op.cover, 3),
# stack: [..., lowword(B + D), highword(B + D), A, C]
TealOp(expr, Op.cover, 2),
# stack: [..., lowword(B + D), highword(B + D), A + C]
TealOp(expr, Op.add),
# stack: [..., lowword(B + D), highword(B + D) + A + C]
TealOp(expr, Op.add),
# stack: [..., highword(B + D) + A + C, lowword(B + D)]
TealOp(expr, Op.swap),
Copy link
Contributor

@PabloLION PabloLION Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that line 88 and line 94 are not needed here.

@ahangsu ahangsu changed the base branch from master to feature/uint128 January 23, 2023 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for wide (128-bit) arithmetic
3 participants