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 BytesSqrt #163

Merged
merged 3 commits into from
Jan 19, 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 @@ -134,6 +134,7 @@ __all__ = [
"BytesGt",
"BytesGe",
"BytesNot",
"BytesSqrt",
"BytesZero",
"ExtractUint16",
"ExtractUint32",
Expand Down
2 changes: 2 additions & 0 deletions pyteal/ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
Balance,
MinBalance,
BytesNot,
BytesSqrt,
BytesZero,
Log,
)
Expand Down Expand Up @@ -239,6 +240,7 @@
"BytesGt",
"BytesGe",
"BytesNot",
"BytesSqrt",
"BytesZero",
"ExtractUint16",
"ExtractUint32",
Expand Down
10 changes: 10 additions & 0 deletions pyteal/ast/unaryexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ def BytesNot(arg: Expr) -> UnaryExpr:
return UnaryExpr(Op.b_not, TealType.bytes, TealType.bytes, arg)


def BytesSqrt(arg: Expr) -> UnaryExpr:
"""Get the bytes square root of bytes.

This will return the largest integer X such that X^2 <= arg.

Requires TEAL version 6 or higher.
"""
return UnaryExpr(Op.bsqrt, TealType.bytes, TealType.bytes, arg)


def BytesZero(arg: Expr) -> UnaryExpr:
"""Get a byte-array of a specified length, containing all zero bytes.

Expand Down
22 changes: 22 additions & 0 deletions pyteal/ast/unaryexpr_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_btoi():
Expand Down Expand Up @@ -357,6 +358,27 @@ def test_b_not_invalid():
BytesNot(Int(2))


def test_bsqrt():
arg = Bytes("base16", "0xFEDCBA9876543210")
expr = BytesSqrt(arg)
assert expr.type_of() == TealType.bytes

expected = TealSimpleBlock(
[TealOp(arg, Op.byte, "0xFEDCBA9876543210"), TealOp(expr, Op.bsqrt)]
)

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

assert actual == expected


def test_bsqrt_invalid():
with pytest.raises(TealTypeError):
BytesSqrt(Int(2 ** 64 - 1))


def test_b_zero():
arg = Int(8)
expr = BytesZero(arg)
Expand Down
1 change: 1 addition & 0 deletions pyteal/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def min_version(self) -> int:
gtxnas = OpType("gtxnas", Mode.Signature | Mode.Application, 5)
gtxnsas = OpType("gtxnsas", Mode.Signature | Mode.Application, 5)
args = OpType("args", Mode.Signature, 5)
bsqrt = OpType("bsqrt", Mode.Signature | Mode.Application, 6)
itxn_next = OpType("itxn_next", Mode.Application, 6)
gitxn = OpType("gitxn", Mode.Application, 6)
gitxna = OpType("gitxna", Mode.Application, 6)
Expand Down