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 Sha3_256 #425

Merged
merged 3 commits into from
Jun 30, 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 docs/crypto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Below is how you express cryptographic primitives in PyTeal:
Operator Cost Description
==================================== ========= ==================================================================================================================
:code:`Sha256(e)` `35` `SHA-256` hash function, produces 32 bytes
:code:`Sha3_256(e)` `130` `SHA3-256` hash function, produces 32 bytes
:code:`Keccak256(e)` `130` `Keccak-256` hash funciton, produces 32 bytes
:code:`Sha512_256(e)` `45` `SHA-512/256` hash function, produces 32 bytes
:code:`Ed25519Verify(d, s, p)` `1900`\* `1` if :code:`s` is the signature of :code:`d` signed by the private key corresponding to the public key :code:`p`, else `0`
Expand Down
1 change: 1 addition & 0 deletions pyteal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ __all__ = [
"SetBit",
"SetByte",
"Sha256",
"Sha3_256",
"Sha512_256",
"ShiftLeft",
"ShiftRight",
Expand Down
2 changes: 2 additions & 0 deletions pyteal/ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
BitLen,
Sha256,
Sha512_256,
Sha3_256,
Keccak256,
Not,
BitwiseNot,
Expand Down Expand Up @@ -197,6 +198,7 @@
"BitLen",
"Sha256",
"Sha512_256",
"Sha3_256",
"Keccak256",
"Not",
"BitwiseNot",
Expand Down
5 changes: 5 additions & 0 deletions pyteal/ast/unaryexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def Sha512_256(arg: Expr) -> UnaryExpr:
return UnaryExpr(Op.sha512_256, TealType.bytes, TealType.bytes, arg)


def Sha3_256(arg: Expr) -> UnaryExpr:
"""Get the SHA3-256 hash of a byte string."""
return UnaryExpr(Op.sha3_256, TealType.bytes, TealType.bytes, arg)


def Keccak256(arg: Expr) -> UnaryExpr:
"""Get the KECCAK-256 hash of a byte string."""
return UnaryExpr(Op.keccak256, TealType.bytes, TealType.bytes, arg)
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 @@ -7,6 +7,7 @@
teal4Options = pt.CompileOptions(version=4)
teal5Options = pt.CompileOptions(version=5)
teal6Options = pt.CompileOptions(version=6)
teal7Options = pt.CompileOptions(version=7)


def test_btoi():
Expand Down Expand Up @@ -146,6 +147,27 @@ def test_sha512_256_invalid():
pt.Sha512_256(pt.Int(1))


def test_sha3_256():
arg = pt.Arg(0)
expr = pt.Sha3_256(arg)
assert expr.type_of() == pt.TealType.bytes

expected = pt.TealSimpleBlock(
[pt.TealOp(arg, pt.Op.arg, 0), pt.TealOp(expr, pt.Op.sha3_256)]
)

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

assert actual == expected


def test_sha3_256_invalid():
with pytest.raises(pt.TealTypeError):
pt.Sha3_256(pt.Int(1))


def test_keccak256():
arg = pt.Arg(0)
expr = pt.Keccak256(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 @@ -183,6 +183,7 @@ def min_version(self) -> int:
replace3 = OpType("replace3", Mode.Signature | Mode.Application, 7)
base64_decode = OpType("base64_decode", Mode.Application | Mode.Signature, 7)
json_ref = OpType("json_ref", Mode.Signature | Mode.Application, 7)
sha3_256 = OpType("sha3_256", Mode.Signature | Mode.Application, 7)
vrf_verify = OpType("vrf_verify", Mode.Signature | Mode.Application, 7)
block = OpType("block", Mode.Signature | Mode.Application, 7)
# fmt: on
Expand Down