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 extract expressions #118

Merged
merged 3 commits into from
Sep 23, 2021
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
27 changes: 24 additions & 3 deletions docs/byte_expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,33 @@ example:
Substring Extraction
--------------------

Byte slices can be extracted from other byte slices using the :any:`Substring` expression. This
expression can extract part of a byte slicing given start and end indices. For example:
Byte slices can be extracted from other byte slices using the :any:`Substring` and :any:`Extract`
expressions. These expressions are extremely similar, except one specifies a substring by start and
end indexes, while the other uses a start index and length. Use whichever makes sense for your
application.

Substring
~~~~~~~~~

The :any:`Substring` expression can extract part of a byte slice given start and end indices. For
example:

.. code-block:: python

Substring(Bytes("algorand"), Int(2), Int(8)) # will produce "gorand"

Extract
~~~~~~~

.. note::
:code:`Extract` is only available in TEAL version 5 or higher.

The :any:`Extract` expression can extract part of a byte slice given the start index and length. For
example:

.. code-block:: python

Substring(Bytes("algorand"), Int(0), Int(4)) # will produce "algo"
Extract(Bytes("algorand"), Int(2), Int(6)) # will produce "gorand"

Manipulating Individual Bits and Bytes
--------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion pyteal/ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@
BytesLe,
BytesGt,
BytesGe,
ExtractUint16,
ExtractUint32,
ExtractUint64,
)

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

# more ops
from .naryexpr import NaryExpr, And, Or, Concat
Expand Down Expand Up @@ -184,6 +187,7 @@
"GetByte",
"Ed25519Verify",
"Substring",
"Extract",
"SetBit",
"SetByte",
"NaryExpr",
Expand Down Expand Up @@ -224,6 +228,9 @@
"BytesGe",
"BytesNot",
"BytesZero",
"ExtractUint16",
"ExtractUint32",
"ExtractUint64",
"Log",
"While",
"For",
Expand Down
76 changes: 76 additions & 0 deletions pyteal/ast/binaryexpr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union, Tuple, cast, TYPE_CHECKING

from ..types import TealType, require_type
from ..errors import verifyTealVersion
from ..ir import TealOp, Op, TealBlock
from .expr import Expr

Expand Down Expand Up @@ -34,6 +35,12 @@ def __init__(
self.argRight = argRight

def __teal__(self, options: "CompileOptions"):
verifyTealVersion(
self.op.min_version,
options.version,
"TEAL version too low to use op {}".format(self.op),
)

return TealBlock.FromOp(
options, TealOp(self, self.op), self.argLeft, self.argRight
)
Expand Down Expand Up @@ -517,3 +524,72 @@ def BytesGe(left: Expr, right: Expr) -> BinaryExpr:
right: Must evaluate to bytes.
"""
return BinaryExpr(Op.b_ge, TealType.bytes, TealType.uint64, left, right)


def ExtractUint16(string: Expr, offset: Expr) -> BinaryExpr:
"""Extract 2 bytes (16 bits) and convert them to an integer.

The bytes starting at :code:`offset` up to but not including :code:`offset + 2` will be
interpreted as a big-endian unsigned integer.

If :code:`offset + 2` exceeds :code:`Len(string)`, the program fails.

Requires TEAL version 5 or higher.

Args:
string: The bytestring to extract from. Must evaluate to bytes.
offset: The offset in the bytestring to start extracing. Must evaluate to uint64.
"""
return BinaryExpr(
Op.extract_uint16,
(TealType.bytes, TealType.uint64),
TealType.uint64,
string,
offset,
)


def ExtractUint32(string: Expr, offset: Expr) -> BinaryExpr:
"""Extract 4 bytes (32 bits) and convert them to an integer.

The bytes starting at :code:`offset` up to but not including :code:`offset + 4` will be
interpreted as a big-endian unsigned integer.

If :code:`offset + 4` exceeds :code:`Len(string)`, the program fails.

Requires TEAL version 5 or higher.

Args:
string: The bytestring to extract from. Must evaluate to bytes.
offset: The offset in the bytestring to start extracing. Must evaluate to uint64.
"""
return BinaryExpr(
Op.extract_uint32,
(TealType.bytes, TealType.uint64),
TealType.uint64,
string,
offset,
)


def ExtractUint64(string: Expr, offset: Expr) -> BinaryExpr:
"""Extract 8 bytes (64 bits) and convert them to an integer.

The bytes starting at :code:`offset` up to but not including :code:`offset + 8` will be
interpreted as a big-endian unsigned integer.

If :code:`offset + 8` exceeds :code:`Len(string)`, the program fails.

Requires TEAL version 5 or higher.

Args:
string: The bytestring to extract from. Must evaluate to bytes.
offset: The offset in the bytestring to start extracing. Must evaluate to uint64.
"""
return BinaryExpr(
Op.extract_uint64,
(TealType.bytes, TealType.uint64),
TealType.uint64,
string,
offset,
)
Loading