Skip to content

Commit 6bebe92

Browse files
Add BytesSqrt (#163)
* Add BytesSqrt * Update pyteal/ast/unaryexpr_test.py Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com> Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com>
1 parent 7b73148 commit 6bebe92

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

pyteal/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ __all__ = [
134134
"BytesGt",
135135
"BytesGe",
136136
"BytesNot",
137+
"BytesSqrt",
137138
"BytesZero",
138139
"ExtractUint16",
139140
"ExtractUint32",

pyteal/ast/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
Balance,
4545
MinBalance,
4646
BytesNot,
47+
BytesSqrt,
4748
BytesZero,
4849
Log,
4950
)
@@ -239,6 +240,7 @@
239240
"BytesGt",
240241
"BytesGe",
241242
"BytesNot",
243+
"BytesSqrt",
242244
"BytesZero",
243245
"ExtractUint16",
244246
"ExtractUint32",

pyteal/ast/unaryexpr.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ def BytesNot(arg: Expr) -> UnaryExpr:
153153
return UnaryExpr(Op.b_not, TealType.bytes, TealType.bytes, arg)
154154

155155

156+
def BytesSqrt(arg: Expr) -> UnaryExpr:
157+
"""Get the bytes square root of bytes.
158+
159+
This will return the largest integer X such that X^2 <= arg.
160+
161+
Requires TEAL version 6 or higher.
162+
"""
163+
return UnaryExpr(Op.bsqrt, TealType.bytes, TealType.bytes, arg)
164+
165+
156166
def BytesZero(arg: Expr) -> UnaryExpr:
157167
"""Get a byte-array of a specified length, containing all zero bytes.
158168

pyteal/ast/unaryexpr_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
teal3Options = CompileOptions(version=3)
1010
teal4Options = CompileOptions(version=4)
1111
teal5Options = CompileOptions(version=5)
12+
teal6Options = CompileOptions(version=6)
1213

1314

1415
def test_btoi():
@@ -357,6 +358,27 @@ def test_b_not_invalid():
357358
BytesNot(Int(2))
358359

359360

361+
def test_bsqrt():
362+
arg = Bytes("base16", "0xFEDCBA9876543210")
363+
expr = BytesSqrt(arg)
364+
assert expr.type_of() == TealType.bytes
365+
366+
expected = TealSimpleBlock(
367+
[TealOp(arg, Op.byte, "0xFEDCBA9876543210"), TealOp(expr, Op.bsqrt)]
368+
)
369+
370+
actual, _ = expr.__teal__(teal6Options)
371+
actual.addIncoming()
372+
actual = TealBlock.NormalizeBlocks(actual)
373+
374+
assert actual == expected
375+
376+
377+
def test_bsqrt_invalid():
378+
with pytest.raises(TealTypeError):
379+
BytesSqrt(Int(2 ** 64 - 1))
380+
381+
360382
def test_b_zero():
361383
arg = Int(8)
362384
expr = BytesZero(arg)

pyteal/ir/ops.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def min_version(self) -> int:
170170
gtxnas = OpType("gtxnas", Mode.Signature | Mode.Application, 5)
171171
gtxnsas = OpType("gtxnsas", Mode.Signature | Mode.Application, 5)
172172
args = OpType("args", Mode.Signature, 5)
173+
bsqrt = OpType("bsqrt", Mode.Signature | Mode.Application, 6)
173174
itxn_next = OpType("itxn_next", Mode.Application, 6)
174175
gitxn = OpType("gitxn", Mode.Application, 6)
175176
gitxna = OpType("gitxna", Mode.Application, 6)

0 commit comments

Comments
 (0)