Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit 328a9f0

Browse files
authored
Merge pull request #1 from appliedblockchain/sha3-256
Sha3 256
2 parents 70a076e + fff5189 commit 328a9f0

File tree

8 files changed

+44
-2
lines changed

8 files changed

+44
-2
lines changed

data/transactions/logic/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ For three-argument ops, `A` is the element two below the top, `B` is the penulti
109109
| Op | Description |
110110
| --- | --- |
111111
| `sha256` | SHA256 hash of value X, yields [32]byte |
112+
| `sha3_256`| SHA3_256 hash of value X, yields [32]byte |
112113
| `keccak256` | Keccak256 hash of value X, yields [32]byte |
113114
| `sha512_256` | SHA512_256 hash of value X, yields [32]byte |
114115
| `ed25519verify` | for (data A, signature B, pubkey C) verify the signature of ("ProgData" \|\| program_hash \|\| data) against the pubkey => {0 or 1} |

data/transactions/logic/TEAL_opcodes.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,15 @@ bitlen interprets arrays as big-endian integers, unlike setbit/getbit
11541154
- **Cost**: 10
11551155
- LogicSigVersion >= 4
11561156

1157+
## sha3_256
1158+
1159+
- Opcode: 0x96
1160+
- Pops: *... stack*, []byte
1161+
- Pushes: []byte
1162+
- SHA3_256 hash of value X, yields [32]byte
1163+
- **Cost**: 130
1164+
- LogicSigVersion >= 6
1165+
11571166
## b+
11581167

11591168
- Opcode: 0xa0

data/transactions/logic/assembler_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ itxna Logs 3
345345
const v6Nonsense = v5Nonsense + `
346346
itxn_next
347347
base64_decode URLEncoding
348+
sha3_256
348349
`
349350

350351
var nonsense = map[uint64]string{
@@ -362,7 +363,7 @@ var compiled = map[uint64]string{
362363
3: "032008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f4478222105531421055427042106552105082106564c4d4b02210538212106391c0081e80780046a6f686e",
363364
4: "042004010200b7a60c26040242420c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d047465737400320032013202320380021234292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e01022581f8acd19181cf959a1281f8acd19181cf951a81f8acd19181cf1581f8acd191810f082209240a220b230c240d250e230f23102311231223132314181b1c28171615400003290349483403350222231d4a484848482a50512a632223524100034200004322602261222b634848222862482864286548482228236628226724286828692422700048482471004848361c0037001a0031183119311b311d311e311f312024221e312131223123312431253126312731283129312a312b312c312d312e312f44782522531422542b2355220823564c4d4b0222382123391c0081e80780046a6f686e2281d00f24231f880003420001892223902291922394239593a0a1a2a3a4a5a6a7a8a9aaabacadae23af3a00003b003c003d8164",
364365
5: "052004010002b7a60c26050242420c68656c6c6f20776f726c6421070123456789abcd208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d047465737400320032013202320380021234292929292b0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e01022581f8acd19181cf959a1281f8acd19181cf951a81f8acd19181cf1581f8acd191810f082209240a220b230c240d250e230f23102311231223132314181b1c28171615400003290349483403350222231d4a484848482b50512a632223524100034200004322602261222704634848222862482864286548482228246628226723286828692322700048482371004848361c0037001a0031183119311b311d311e311f312023221e312131223123312431253126312731283129312a312b312c312d312e312f447825225314225427042455220824564c4d4b0222382124391c0081e80780046a6f686e2281d00f23241f880003420001892224902291922494249593a0a1a2a3a4a5a6a7a8a9aaabacadae24af3a00003b003c003d816472064e014f012a57000823810858235b235a2359b03139330039b1b200b322c01a23c1001a2323c21a23c3233e233f8120af06002a494905002a49490700b53a03",
365-
6: "062004010002b7a60c26050242420c68656c6c6f20776f726c6421070123456789abcd208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d047465737400320032013202320380021234292929292b0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e01022581f8acd19181cf959a1281f8acd19181cf951a81f8acd19181cf1581f8acd191810f082209240a220b230c240d250e230f23102311231223132314181b1c28171615400003290349483403350222231d4a484848482b50512a632223524100034200004322602261222704634848222862482864286548482228246628226723286828692322700048482371004848361c0037001a0031183119311b311d311e311f312023221e312131223123312431253126312731283129312a312b312c312d312e312f447825225314225427042455220824564c4d4b0222382124391c0081e80780046a6f686e2281d00f23241f880003420001892224902291922494249593a0a1a2a3a4a5a6a7a8a9aaabacadae24af3a00003b003c003d816472064e014f012a57000823810858235b235a2359b03139330039b1b200b322c01a23c1001a2323c21a23c3233e233f8120af06002a494905002a49490700b53a03b65c00",
366+
6: "062004010002b7a60c26050242420c68656c6c6f20776f726c6421070123456789abcd208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d047465737400320032013202320380021234292929292b0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e01022581f8acd19181cf959a1281f8acd19181cf951a81f8acd19181cf1581f8acd191810f082209240a220b230c240d250e230f23102311231223132314181b1c28171615400003290349483403350222231d4a484848482b50512a632223524100034200004322602261222704634848222862482864286548482228246628226723286828692322700048482371004848361c0037001a0031183119311b311d311e311f312023221e312131223123312431253126312731283129312a312b312c312d312e312f447825225314225427042455220824564c4d4b0222382124391c0081e80780046a6f686e2281d00f23241f880003420001892224902291922494249593a0a1a2a3a4a5a6a7a8a9aaabacadae24af3a00003b003c003d816472064e014f012a57000823810858235b235a2359b03139330039b1b200b322c01a23c1001a2323c21a23c3233e233f8120af06002a494905002a49490700b53a03b65c0096",
366367
}
367368

368369
func pseudoOp(opcode string) bool {

data/transactions/logic/doc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var opDocByName = map[string]string{
2828
"sha256": "SHA256 hash of value X, yields [32]byte",
2929
"keccak256": "Keccak256 hash of value X, yields [32]byte",
3030
"sha512_256": "SHA512_256 hash of value X, yields [32]byte",
31+
"sha3_256": "SHA3_256 hash of value X, yields [32]byte",
3132
"ed25519verify": "for (data A, signature B, pubkey C) verify the signature of (\"ProgData\" || program_hash || data) against the pubkey => {0 or 1}",
3233
"ecdsa_verify": "for (data A, signature B, C and pubkey D, E) verify the signature of the data against the pubkey => {0 or 1}",
3334
"ecdsa_pk_decompress": "decompress pubkey A into components X, Y => [*... stack*, X, Y]",
@@ -298,7 +299,7 @@ func OpDocExtra(opName string) string {
298299
// here is the order args opcodes are presented, so place related
299300
// opcodes consecutively, even if their opcode values are not.
300301
var OpGroups = map[string][]string{
301-
"Arithmetic": {"sha256", "keccak256", "sha512_256", "ed25519verify", "ecdsa_verify", "ecdsa_pk_recover", "ecdsa_pk_decompress", "+", "-", "/", "*", "<", ">", "<=", ">=", "&&", "||", "shl", "shr", "sqrt", "bitlen", "exp", "==", "!=", "!", "len", "itob", "btoi", "%", "|", "&", "^", "~", "mulw", "addw", "divmodw", "expw", "getbit", "setbit", "getbyte", "setbyte", "concat"},
302+
"Arithmetic": {"sha256", "keccak256", "sha512_256", "sha3_256", "ed25519verify", "ecdsa_verify", "ecdsa_pk_recover", "ecdsa_pk_decompress", "+", "-", "/", "*", "<", ">", "<=", ">=", "&&", "||", "shl", "shr", "sqrt", "bitlen", "exp", "==", "!=", "!", "len", "itob", "btoi", "%", "|", "&", "^", "~", "mulw", "addw", "divmodw", "expw", "getbit", "setbit", "getbyte", "setbyte", "concat"},
302303
"Byte Array Manipulation": {"substring", "substring3", "extract", "extract3", "extract_uint16", "extract_uint32", "extract_uint64", "base64_decode"},
303304
"Byte Array Arithmetic": {"b+", "b-", "b/", "b*", "b<", "b>", "b<=", "b>=", "b==", "b!=", "b%"},
304305
"Byte Array Logic": {"b|", "b&", "b^", "b~"},

data/transactions/logic/eval.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,16 @@ func opSHA256(cx *EvalContext) {
901901
cx.stack[last].Bytes = hash[:]
902902
}
903903

904+
// The NIST SHA3-256 is implemented for compatability with ICON
905+
func opSHA3_256(cx *EvalContext) {
906+
last := len(cx.stack) - 1
907+
hasher := sha3.New256()
908+
hasher.Write(cx.stack[last].Bytes)
909+
hv := make([]byte, 0, hasher.Size())
910+
hv = hasher.Sum(hv)
911+
cx.stack[last].Bytes = hv
912+
}
913+
904914
// The Keccak256 variant of SHA-3 is implemented for compatibility with Ethereum
905915
func opKeccak256(cx *EvalContext) {
906916
last := len(cx.stack) - 1

data/transactions/logic/evalCrypto_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ byte 0xc195eca25a6f4c82bfba0287082ddb0d602ae9230f9cf1f1a40b68f8e2c41567
5353
testAccepts(t, progText, 1)
5454
}
5555

56+
func TestSHA3_256(t *testing.T) {
57+
partitiontest.PartitionTest(t)
58+
59+
t.Parallel()
60+
/*
61+
pip install hashlib
62+
import hashlib
63+
hashlib.sha3_256(b"fnord").hexdigest()
64+
*/
65+
progText := `byte 0x666E6F7264
66+
sha3_256
67+
byte 0xd757297405c5c89f7ceca368ee76c2f1893ee24f654e60032e65fb53b01aae10
68+
==`
69+
testAccepts(t, progText, 6)
70+
}
71+
5672
func TestSHA512_256(t *testing.T) {
5773
partitiontest.PartitionTest(t)
5874

data/transactions/logic/evalStateful_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ bytec 0
7878
sha256
7979
keccak256
8080
sha512_256
81+
sha3_256
8182
len
8283
intc_0
8384
+

data/transactions/logic/opcodes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ var OpSpecs = []OpSpec{
294294
{0x94, "exp", opExp, asmDefault, disDefault, twoInts, oneInt, 4, modeAny, opDefault},
295295
{0x95, "expw", opExpw, asmDefault, disDefault, twoInts, twoInts, 4, modeAny, costly(10)},
296296

297+
//More Hash Functions
298+
{0x96, "sha3_256", opSHA3_256, asmDefault, disDefault, oneBytes, oneBytes, 6, modeAny, costly(130)},
299+
297300
// Byteslice math.
298301
{0xa0, "b+", opBytesPlus, asmDefault, disDefault, twoBytes, oneBytes, 4, modeAny, costly(10)},
299302
{0xa1, "b-", opBytesMinus, asmDefault, disDefault, twoBytes, oneBytes, 4, modeAny, costly(10)},

0 commit comments

Comments
 (0)