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

AVM: SHA3-256 hash function opcode #3544

Closed
wants to merge 16 commits into from

Conversation

Matt-Ryley
Copy link
Contributor

  • Implemented the SHA3-256 hash function required for interoperability of Algorand with the ICON blockchain.
  • Added the OPCode 0x96 for this functionality.
  • Updated documentation and unit tests to include the new SHA3-256 hashing function.

Added SHA3-256 hashing function as an OP code
Made required changes to pass the unit tests.
-- Changed testAccepts version from 1 to 6
-- Added sha3_256 to the Opgroups
From code review suggestion renamed opSHA3256 to opSHA3_256 in the eval.go and opcodes.go file
@CLAassistant
Copy link

CLAassistant commented Feb 1, 2022

CLA assistant check
All committers have signed the CLA.

@tsachiherman tsachiherman requested a review from jannotti February 1, 2022 12:55
@@ -294,6 +294,9 @@ var OpSpecs = []OpSpec{
{0x94, "exp", opExp, asmDefault, disDefault, twoInts, oneInt, 4, modeAny, opDefault},
{0x95, "expw", opExpw, asmDefault, disDefault, twoInts, twoInts, 4, modeAny, costly(10)},

//More Hash Functions
{0x96, "sha3_256", opSHA3_256, asmDefault, disDefault, oneBytes, oneBytes, 6, modeAny, costly(130)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you derive the cost?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure how to derive the cost. The cost of the SHA3-256 should be roughly the same as the Keccak256 as they are very similar algorithms, except the padding in the SHA3-256 adds up to 4 additional bits. Im open to any suggestions of how to derive this cost more accurately?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some benchmarking code in eval_test.go. But hold off for a couple days. We're in the middle of a merge that will change it slightly. But once that merge happens, you'll find
func BenchmarkBase64Decode(b *testing.B) that was used to compare base64 decode to the costs of various hashes. By adding sha3_256 in there, we should be able to get a feel for relative costs.

(The function may already be in master, if you want to look now.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the information! Which merge should I be looking out for?

Copy link
Contributor

@jannotti jannotti Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feature/contract-to-contract going into master #3397

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @Matt-Ryley, just FYI the aforementioned merge happened.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up @algoanne! Ill get on this as soon as possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just ran the sha3_256 through the suggested function and here is the output, comparing keccak256 and sha3_256
BenchmarkBase64Decode/keccak256_small-8 518492 2221 ns/op
BenchmarkBase64Decode/sha3_256_small-8 525247 1957 ns/op

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me they are in the same ball park as one another and thus should have the same cost, as the eqv for other hashes like
BenchmarkBase64Decode/sha256_small-8 1260788 1003 ns/op
BenchmarkBase64Decode/sha512_256_small-8 1695235 715.2 ns/op

Are very different

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it sounds like it should have the same cost as keccak. I think we can take this as soon as CI passes. I'll be making a pass over it once that happens as I have changed the opcode interface a bit. But I think your work is done! Thanks for the contribution.

@jannotti jannotti changed the title SHA3-256 added to hashing functions AVM: SHA3-256 hash function opcode Mar 10, 2022
@jannotti
Copy link
Contributor

Sorry, it looks like one thing you'll have to do to make CI happy is run make in the data/transactions/logic directory. That will regenerate a couple spec files from your docstrings. That's the codegen_verification step that's failing.

@codecov-commenter
Copy link

codecov-commenter commented Mar 11, 2022

Codecov Report

Merging #3544 (5d8d05d) into master (5a72986) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3544      +/-   ##
==========================================
+ Coverage   49.57%   49.60%   +0.02%     
==========================================
  Files         392      392              
  Lines       68590    68596       +6     
==========================================
+ Hits        34006    34024      +18     
+ Misses      30845    30836       -9     
+ Partials     3739     3736       -3     
Impacted Files Coverage Δ
data/transactions/logic/doc.go 56.00% <ø> (ø)
data/transactions/logic/opcodes.go 100.00% <ø> (ø)
data/transactions/logic/eval.go 86.07% <100.00%> (+0.03%) ⬆️
data/abi/abi_type.go 87.67% <0.00%> (-0.95%) ⬇️
network/requestTracker.go 70.25% <0.00%> (-0.44%) ⬇️
ledger/acctupdates.go 68.42% <0.00%> (+0.26%) ⬆️
network/wsPeer.go 69.16% <0.00%> (+0.83%) ⬆️
data/transactions/verify/txn.go 45.02% <0.00%> (+0.86%) ⬆️
ledger/blockqueue.go 85.05% <0.00%> (+2.87%) ⬆️
ledger/roundlru.go 96.22% <0.00%> (+5.66%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5a72986...5d8d05d. Read the comment docs.

@Matt-Ryley
Copy link
Contributor Author

@jannotti after upstream merge the arm64 test is now failing in one of the tests for the ledger. As I haven't modified any code in that directory I am confused to why this test fails. Do you know of any reason? My local machine is amd64 architecture and those tests run successfully in the CI pipeline and also on my local machine, so I can't really debug to find the cause of the error.

@jannotti
Copy link
Contributor

jannotti commented Mar 14, 2022 via email

@jannotti
Copy link
Contributor

Closed in favor of #3762

@jannotti jannotti closed this Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants