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

Add opcode support for divw #192

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyteal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ __all__ = [
"Div",
"Mod",
"Exp",
"Divw",
"BitwiseAnd",
"BitwiseOr",
"BitwiseXor",
Expand Down
3 changes: 2 additions & 1 deletion pyteal/ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
)

# ternary ops
from .ternaryexpr import Ed25519Verify, SetBit, SetByte
from .ternaryexpr import Divw, Ed25519Verify, SetBit, SetByte
from .substring import Substring, Extract, Suffix

# more ops
Expand Down Expand Up @@ -186,6 +186,7 @@
"Div",
"Mod",
"Exp",
"Divw",
"BitwiseAnd",
"BitwiseOr",
"BitwiseXor",
Expand Down
22 changes: 22 additions & 0 deletions pyteal/ast/ternaryexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,25 @@ def SetByte(value: Expr, index: Expr, newByteValue: Expr) -> TernaryExpr:
index,
newByteValue,
)


def Divw(hi: Expr, lo: Expr, y: Expr) -> TernaryExpr:
"""
Performs wide division by interpreting `hi` and `lo` as a uint128 value.

Requires TEAL version 6 or higher.

Args:
hi: Quotient's high 64 bits. Must evaluate to uint64.
lo: Quotient's low 64 bits. Must evaluate to uint64.
y: Divisor. Must evaluate to uint64.

"""
return TernaryExpr(
Op.divw,
(TealType.uint64, TealType.uint64, TealType.uint64),
TealType.uint64,
hi,
lo,
y,
)
33 changes: 33 additions & 0 deletions pyteal/ast/ternaryexpr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
teal3Options = CompileOptions(version=3)
teal4Options = CompileOptions(version=4)
teal5Options = CompileOptions(version=5)
teal6Options = CompileOptions(version=6)


def test_ed25519verify():
Expand Down Expand Up @@ -138,3 +139,35 @@ def test_set_byte_invalid():

with pytest.raises(TealTypeError):
SetByte(Bytes("base16", "0xFF"), Int(0), Bytes("one"))


def test_divw():
args = [Int(0), Int(90), Int(30)]
expr = Divw(args[0], args[1], args[2])
assert expr.type_of() == TealType.uint64

expected = TealSimpleBlock(
[
TealOp(args[0], Op.int, args[0].value),
TealOp(args[1], Op.int, args[1].value),
TealOp(args[2], Op.int, args[2].value),
TealOp(expr, Op.divw),
]
)

actual, _ = expr.__teal__(teal6Options)
actual.addIncoming()
actual = TealBlock.NormalizeBlocks(actual)

assert actual == expected


def test_divw_invalid():
with pytest.raises(TealTypeError):
Divw(Bytes("10"), Int(0), Int(1))

with pytest.raises(TealTypeError):
Divw(Int(10), Bytes("0"), Int(1))

with pytest.raises(TealTypeError):
Divw(Int(10), Int(0), Bytes("1"))